Skip to content

Instantly share code, notes, and snippets.

@pjako
Created December 11, 2020 11:41
Show Gist options
  • Save pjako/672fc9a3a6b3ea153ee28221a45aabac to your computer and use it in GitHub Desktop.
Save pjako/672fc9a3a6b3ea153ee28221a45aabac to your computer and use it in GitHub Desktop.
Handles with generation
#define HANDLE_SIZE 32
#define HANDLE_GEN_BITS 8
static const uint32_t handleIndexMask32 = (1 << (32 - HANDLE_GEN_BITS)) - 1;
static const uint32_t handleGenMask32 = ((1 << HANDLE_GEN_BITS) - 1);
static const uint32_t handleGenShift32 = (32 - HANDLE_GEN_BITS);
#define _handleIndex32(HANDLE) ((ui_u32)((HANDLE) & handleIndexMask32))
#define handleGeneration32(HANDLE) ((ui_u8)(((HANDLE) >> handleGenShift32) & handleGenMask32))
#define handleSameGeneration32(HANDLE, GENERATION) (((uint8_t)(((HANDLE) >> handleGenShift32) & handleGenShift32)) == (GENERATION % 255))
#define handleMake32(GENERATION, INDEX) ((ui_u32) ((((ui_u32)(GENERATION) & handleGenMask32) << handleGenShift32) | ((ui_u32)(INDEX) & handleIndexMask32)))
// index starts from 1 so if the handles is zero it can be counted as invalid
#define handleIndex32(WHANDLE) (_handleIndex32(WHANDLE.id) - 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment