Skip to content

Commit

Permalink
Add Fibre local storage (Fls*) functions for armcc (#55)
Browse files Browse the repository at this point in the history
* Add Fibre local storage (Fls*) functions for armcc

* Update dll/kernel32.cpp

Co-authored-by: Simon Lindholm <simon.lindholm10@gmail.com>

* Update dll/kernel32.cpp

Co-authored-by: Simon Lindholm <simon.lindholm10@gmail.com>

* Update dll/kernel32.cpp

Co-authored-by: Simon Lindholm <simon.lindholm10@gmail.com>

* Update dll/kernel32.cpp

Co-authored-by: Simon Lindholm <simon.lindholm10@gmail.com>

* one more copypasta

---------

Co-authored-by: Simon Lindholm <simon.lindholm10@gmail.com>
  • Loading branch information
mkst and simonlindholm authored Oct 17, 2023
1 parent a829d0d commit 30ef434
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions dll/kernel32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1993,6 +1993,63 @@ namespace kernel32 {
*Target = Value;
return initial;
}

// These are effectively a copy/paste of the Tls* functions
enum { MAX_FLS_VALUES = 100 };
static bool flsValuesUsed[MAX_FLS_VALUES] = { false };
static void *flsValues[MAX_FLS_VALUES];
int WIN_FUNC FlsAlloc(void *lpCallback) {
DEBUG_LOG("FlsAlloc (lpCallback: %x)\n", lpCallback);
// If the function succeeds, the return value is an FLS index initialized to zero.
for (size_t i = 0; i < MAX_FLS_VALUES; i++) {
if (flsValuesUsed[i] == false) {
flsValuesUsed[i] = true;
flsValues[i] = 0;
DEBUG_LOG("...returning %d\n", i);
return i;
}
}
DEBUG_LOG("...returning nothing\n");
wibo::lastError = 1;
return 0xFFFFFFFF; // FLS_OUT_OF_INDEXES
}

unsigned int WIN_FUNC FlsFree(unsigned int dwFlsIndex) {
DEBUG_LOG("FlsFree(%u)\n", dwFlsIndex);
if (dwFlsIndex >= 0 && dwFlsIndex < MAX_FLS_VALUES && flsValuesUsed[dwFlsIndex]) {
flsValuesUsed[dwFlsIndex] = false;
return 1;
} else {
wibo::lastError = 1;
return 0;
}
}

void *WIN_FUNC FlsGetValue(unsigned int dwFlsIndex) {
// DEBUG_LOG("FlsGetValue(%u)", dwFlsIndex);
void *result = nullptr;
if (dwFlsIndex >= 0 && dwFlsIndex < MAX_FLS_VALUES && flsValuesUsed[dwFlsIndex]) {
result = flsValues[dwFlsIndex];
// See https://learn.microsoft.com/en-us/windows/win32/api/fibersapi/nf-fibersapi-flsgetvalue
wibo::lastError = ERROR_SUCCESS;
} else {
wibo::lastError = 1;
}
// DEBUG_LOG(" -> %p\n", result);
return result;
}

unsigned int WIN_FUNC FlsSetValue(unsigned int dwFlsIndex, void *lpFlsData) {
// DEBUG_LOG("FlsSetValue(%u, %p)\n", dwFlsIndex, lpFlsData);
if (dwFlsIndex >= 0 && dwFlsIndex < MAX_FLS_VALUES && flsValuesUsed[dwFlsIndex]) {
flsValues[dwFlsIndex] = lpFlsData;
return 1;
} else {
wibo::lastError = 1;
return 0;
}
}

}

static void *resolveByName(const char *name) {
Expand Down Expand Up @@ -2181,6 +2238,12 @@ static void *resolveByName(const char *name) {
if (strcmp(name, "InterlockedDecrement") == 0) return (void *) kernel32::InterlockedDecrement;
if (strcmp(name, "InterlockedExchange") == 0) return (void *) kernel32::InterlockedExchange;

// fibersapi.h
if (strcmp(name, "FlsAlloc") == 0) return (void *) kernel32::FlsAlloc;
if (strcmp(name, "FlsFree") == 0) return (void *) kernel32::FlsFree;
if (strcmp(name, "FlsSetValue") == 0) return (void *) kernel32::FlsSetValue;
if (strcmp(name, "FlsGetValue") == 0) return (void *) kernel32::FlsGetValue;

return 0;
}

Expand Down

0 comments on commit 30ef434

Please sign in to comment.