Re: Windows API: GetUserName: Retrieve the name of the user associated with the current thread.

2023-08-20 Thread BoQsc via Digitalmars-d-learn

 Update: GetUserName by print to stdout

Main function has been updated with `string[] args` and a new 
feature:

```
if (args.length > 1 && args[1] == "print") {
write(username);
}
```


 Usage:
`WindowsGetUserName.exe print`

 Demonstration


![img1](https://i.imgur.com/dWPlo8L.png)

 WindowsGetUserName.d
```d
import core.sys.windows.windows;
import std.conv;
import std.stdio;
import std.range;

pragma(lib, "advapi32.lib");

/**
 * Retrieves the currently logged-in user's name in a safe manner.
 *
 * This function first determines the required buffer size, 
allocates memory for the username,

 * and then retrieves the username.
 *
 * Returns:
 *   - The username as a string if successful.
 *   - An empty string if an error occurs.
 */
string getSafeUsername() @system {
wchar[] userName;
DWORD userNameSize = 0;

// First, try GetUserNameW (Unicode version)
if (!GetUserNameW(null, )) {
int error = GetLastError();
if (error != ERROR_INSUFFICIENT_BUFFER) {
// Failed for a reason other than an insufficient 
buffer

return "";
}
}

// Allocate memory for userName
scope(exit) userName.length = 0; // Ensure memory is released 
if an exception occurs

userName.length = userNameSize;

// Retrieve the user name by calling GetUserNameW
if (GetUserNameW(userName.ptr, )) {
// Successfully retrieved the user name, convert it to a 
string

return to!string(userName);
}

// If GetUserNameW fails, try GetUserNameA (ANSI version)
char[] userNameA;
userNameSize = 0;

if (!GetUserNameA(null, )) {
int errorA = GetLastError();
if (errorA != ERROR_INSUFFICIENT_BUFFER) {
// Failed for a reason other than an insufficient 
buffer

return "";
}
}

// Allocate memory for userNameA
scope(exit) userNameA.length = 0; // Ensure memory is 
released if an exception occurs

userNameA.length = userNameSize;

// Retrieve the user name by calling GetUserNameA
if (GetUserNameA(userNameA.ptr, )) {
// Successfully retrieved the user name using ANSI 
version, convert it to a string

return to!string(userNameA);
}

// Both GetUserNameW and GetUserNameA failed, return an empty 
string

return "";
}

/**
 * The entry point of the application.
 */
void main(string[] args) {
string username = getSafeUsername();
if (args.length > 1 && args[1] == "print") {
write(username);
} else if (!username.empty){
writeln("Logged-in user name: ", username);

} else {
writeln("Failed to retrieve the user name.");
}
}

```


Re: Spec for the ‘locality’ parameter to the LDC and GDC builtin magic functions for accessing special CPU prefetch instructions

2023-08-20 Thread Iain Buclaw via Digitalmars-d-learn

On Saturday, 19 August 2023 at 19:23:38 UTC, Cecil Ward wrote:


I’m trying to write a cross-platform function that gives access 
to the CPU’s prefetch instructions such as x86 
prefetch0/1/2/prefetchnta and AAarch64 too. I’ve found that the 
GDC and LDC compilers provide builtin magic functions for this, 
and are what I need. I am trying to put together a 
plain-English detailed spec for the respective builtin magic 
functions.


My questions:

Q1) I need to compare the spec for the GCC and LDC builtin 
magic functions’ "locality" parameter. Can anyone tell me if 
GDC and LDC have kept mutual compatibility here?




I'd have thought GCC and LLVM have mutual compatibility thanks to 
a common target API in Intel's `_mm_prefetch()` function (and in 
fact, the magic locality numbers match `_MM_HINT_*` constants).


```
#define _MM_HINT_T0 1
#define _MM_HINT_T1 2
#define _MM_HINT_T2 3
#define _MM_HINT_NTA 0
```

Q2) Could someone help me turn the GCC and LDC specs into 
english regarding the locality parameter ? - see (2) and (4) 
below.



https://gcc.gnu.org/projects/prefetch.html




Q3) Does the locality parameter determine which _level_ of the 
data cache hierarchy data is fetched into? Or is it always 
fetched into L1 data cache and the outer ones, and this 
parameter affects caches’ _future behaviour_?




It really depends on the CPU, and what features it has.

x86 SSE intrinsics are described in the x86 instruction manual, 
along with the meaning of T[012], and NTA.


https://www.felixcloutier.com/x86/prefetchh



Q3) Will these magic builtins work on AAarch64?



It'll work on all targets that define a prefetch insn, or it'll 
be a no-op.  Similarly one or both read-write or locality 
arguments might be ignored too.