Created
February 10, 2017 20:26
-
-
Save NSProgrammer/8c2ce755d15777e62079788a7d788394 to your computer and use it in GitHub Desktop.
Determine the size of the CFRuntimeBase at runtime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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