void* NRT_MemAlign(void **ptr, size_t size, unsigned align) { AlignHeader *base; size_t intptr; unsigned offset; unsigned remainder; /* Allocate extra space for padding and book keeping */ size_t total_size = size + 2 * align + sizeof(AlignHeader); base = (AlignHeader*) NRT_Allocate(total_size); /* The AlignHeader goes first, so skip sizeof(AlignHeader) */ intptr = (size_t) (base + 1); /* See if we are aligned */ remainder = intptr % align; if (remainder == 0){ /* Yes */ offset = 0; } else { /* No, move forward `offset` bytes */ offset = align - remainder; } /* Store the aligned pointer to the output parameter */ *ptr = (void*) (intptr + offset); /* Remember the total allocated size */ base->total_size = total_size; /* Return the pointer for deallocation with NRT_Free() */ return base; }
MemInfo* NRT_MemInfo_alloc_safe(size_t size) { void *data = NRT_Allocate(size); /* Only fill up a couple cachelines with debug markers, to minimize overhead. */ memset(data, 0xCB, MIN(size, 256)); NRT_Debug(nrt_debug_print("NRT_MemInfo_alloc_safe %p %llu\n", data, size)); return NRT_MemInfo_new(data, size, nrt_internal_dtor_safe, (void*)size); }
MemInfo* NRT_MemInfo_alloc(size_t size) { void *data = NRT_Allocate(size); NRT_Debug(nrt_debug_print("NRT_MemInfo_alloc %p\n", data)); return NRT_MemInfo_new(data, size, nrt_internal_dtor, NULL); }
MemInfo* NRT_MemInfo_alloc_safe(size_t size) { void *data = NRT_Allocate(size); memset(data, 0xCB, size); NRT_Debug(nrt_debug_print("NRT_MemInfo_alloc_safe %p %llu\n", data, size)); return NRT_MemInfo_new(data, size, nrt_internal_dtor, (void*)size); }