24 lines
801 B
C
24 lines
801 B
C
// Kernel monotonic identifier allocator. Public identities stay 32-bit while the internal cursor detects exhaustion without wrapping.
|
|
|
|
#include <ntlite/atomic.h>
|
|
#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) {
|
|
int64_t next = NtlAtomicLoad64(&allocator->Next);
|
|
if ((uint64_t)next > UINT32_MAX)
|
|
return NTLSTATUS_ID_EXHAUSTED;
|
|
|
|
int64_t desired = next + 1;
|
|
if (NtlAtomicCompareExchange64(&allocator->Next, desired, next) == next) {
|
|
*idOut = (uint32_t)next;
|
|
return NTLSTATUS_SUCCESS;
|
|
}
|
|
}
|
|
}
|