Skip to content

Instantly share code, notes, and snippets.

@michaeleisel
Created October 25, 2018 20:47
Show Gist options
  • Save michaeleisel/40940de446d25e82e46b55724b996596 to your computer and use it in GitHub Desktop.
Save michaeleisel/40940de446d25e82e46b55724b996596 to your computer and use it in GitHub Desktop.
#import "FFOJemallocAllocator.h"
#import "jemalloc.h"
static CFAllocatorRef sJemallocAllocator;
void *FFOContextMalloc(CFIndex allocSize, CFOptionFlags hint, void *info) {
return je_malloc(allocSize);
}
void * FFOContextRealloc(void *ptr, CFIndex newsize, CFOptionFlags hint, void *info) {
return je_realloc(ptr, newsize);
}
void FFOContextDealloc(void *ptr, void *info) {
je_free(ptr);
}
CFIndex FFOContextPreferredSize(CFIndex size, CFOptionFlags hint, void *info) {
// Round up to the next multiple of 16 to copy what jemalloc does it allocates, since it's 16-byte aligned
NSInteger remainder = size & 0xFF; // remainder = size % 16
if (remainder == 0) {
return size;
}
return size + 16 - remainder;
}
static CFAllocatorContext sJemallocContext = {
.version = 0,
.info = NULL,
.retain = NULL,
.release = NULL,
.copyDescription = NULL,
.allocate = FFOContextMalloc,
.reallocate = FFOContextRealloc,
.deallocate = FFOContextDealloc,
.preferredSize = FFOContextPreferredSize,
};
__used CFAllocatorRef FFOJemallocAllocator() {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sJemallocAllocator = CFAllocatorCreate(kCFAllocatorDefault, &sJemallocContext);
});
return sJemallocAllocator;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment