Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@NSProgrammer
Created February 10, 2017 20:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NSProgrammer/8c2ce755d15777e62079788a7d788394 to your computer and use it in GitHub Desktop.
Save NSProgrammer/8c2ce755d15777e62079788a7d788394 to your computer and use it in GitHub Desktop.
Determine the size of the CFRuntimeBase at runtime
static size_t _CFRuntimeBaseGetSize()
{
static size_t sSize = 0;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// CFUUID has a very nice feature in that it's structure
// is always the CFRuntimeBase struct (which we don't have access to)
// followed by a UUID in bytes.
// By simply traversing the CFUUID structs byte layout until we find
// the matching UUID bytes, we can determine the canonical size
// of the CFRuntimeBase at runtime!
// This is crucial since CFRuntimeBase is not guaranteed to stay
// the same size for any given OS release, and runtime inspection is
// necessary.
CFUUIDRef uuidRef = CFUUIDCreate(NULL);
CFUUIDBytes bytes = CFUUIDGetUUIDBytes(uuidRef);
Byte *uuidBytePointerStart = (Byte *)uuidRef;
Byte *uuidBytePointerMax = uuidBytePointerStart + (sizeof(bytes) * 2);
for (Byte *uuidBytePointer = uuidBytePointerStart; uuidBytePointer < uuidBytePointerMax; uuidBytePointer++) {
if (memcmp(&bytes, uuidBytePointer, sizeof(bytes)) == 0) {
sSize = (size_t)(uuidBytePointer - uuidBytePointerStart);
break;
}
}
});
if (!sSize) {
NSLog(@"Could not determine the CFRuntimeBase struct size!");
}
return sSize;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment