23 lines
814 B
C
23 lines
814 B
C
// Kernel monotonic identifier allocator. Public identities stay 32-bit while the internal cursor detects exhaustion without wrapping.
|
|
|
|
#include <intrin.h>
|
|
#include <kernel/ke_id.h>
|
|
|
|
// Allocates one 32-bit identifier and permanently advances the allocator.
|
|
KSTATUS KeAllocateId32(KE_ID_ALLOCATOR* allocator, uint32_t* idOut) {
|
|
if (!allocator || !idOut)
|
|
return NTLSTATUS_INVALID_PARAMETER;
|
|
|
|
while (1) {
|
|
long long next = _InterlockedCompareExchange64(&allocator->Next, 0, 0);
|
|
if ((unsigned long long)next > 0xFFFFFFFFULL)
|
|
return NTLSTATUS_ID_EXHAUSTED;
|
|
|
|
long long desired = next + 1;
|
|
if (_InterlockedCompareExchange64(&allocator->Next, desired, next) == next) {
|
|
*idOut = (uint32_t)next;
|
|
return NTLSTATUS_SUCCESS;
|
|
}
|
|
}
|
|
}
|