> so alloc() doesn’t just need to hand back a pointer. it needs to hand back a pointer that’s correctly aligned for whatever type the caller is about to store there. Malloc doesn't know the required alignment (because has no idea what the type is, everything is cast through void ). So all malloc implementations have a minimum alignment guarantee. Typically 16 bytes these days on x86, as that means even 128bit SSE val…
Why malloc always does more than I asked for?
11–20 of 52 posts
Re: Why malloc always does more than I asked for?
#12 if (a->cursor + size > a->limit) return NULL; // out of memory
I'd rewrite it like this: if (size > a->limit - a->cursor) return NULL; // out of memoryRe: Why malloc always does more than I asked for?
#13> so alloc() doesn’t just need to hand back a pointer. it needs to hand back a pointer that’s correctly aligned for whatever type the caller is about to store there. Malloc doesn't know the required alignment (because has no idea what the type is, everything is cast through void ). So all malloc implementations have a minimum alignment guarantee. Typically 16 bytes these days on x86, as that means even 128bit SSE val…
There is no reason an allocation needs to contain any inline metadata. And even if it does, the allocator could choose to make it unalined, and pay the cost of an unalined access on de-allocation.
But most C code out there assumes malloc will always return something that is at least aligned to sizeof(void *), it's very rare to see aligned_alloc. So how is your alloc allocation going to know when it can get away with a smaller alignment?
Even if you are on a cpu that doesn't fault on unaligned memory access, any malloc implementation that doesn't align by default will have serious disadvantages in any benchmarks. IMO, There is no good reason to use an unaligned backpointer.
Re: Why malloc always does more than I asked for?
#14 [ Header ][ ...variable padding... ][ Back Pointer ][ User Memory ]
^ always exactly sizeof(void*)
bytes before User Memory,
no matter how much padding
came before it
and then it says we don't need to align the back pointer and we end up with [ Header ][ Back Pointer ][ Padding ][ User Memory ]
without a clear explanation of how we now get to the back pointer if it's behind the variable alignment.Re: Why malloc always does more than I asked for?
#15I'm a little confused. We start with [ Header ][ ...variable padding... ][ Back Pointer ][ User Memory ] ^ always exactly sizeof(void*) bytes before User Memory, no matter how much padding came before it and then it says we don't need to align the back pointer and we end up with [ Header ][ Back Pointer ][ Padding ][ User Memory ] without a clear explanation of how we now get to the back pointer if it's behind the va…
Re: Why malloc always does more than I asked for?
#16Your bump allocator suffers from integer overflows turned into buffer overflows when the requested allocation is big enough: if (a->cursor + size > a->limit) return NULL; // out of memory I'd rewrite it like this: if (size > a->limit - a->cursor) return NULL; // out of memory
if (size > a->limit - a->cursor) return NULL;Re: Why malloc always does more than I asked for?
#17Why bother with dynamic padding and a back pointer? That wastes at least 8 bytes. You might as well always align to 8 bytes and make your header a multiple of 8.
Re: Why malloc always does more than I asked for?
#18Earlier quoted context omitted.
There is no reason an allocation needs to contain any inline metadata. And even if it does, the allocator could choose to make it unalined, and pay the cost of an unalined access on de-allocation.
True. But most C code out there assumes malloc will always return something that is at least aligned to sizeof(void *), it's very rare to see aligned_alloc. So how is your alloc allocation going to know when it can get away with a smaller alignment? Even if you are on a cpu that doesn't fault on unaligned memory access, any malloc implementation that doesn't align by default will have serious disadvantages in any ben…
Re: Why malloc always does more than I asked for?
#19Your bump allocator suffers from integer overflows turned into buffer overflows when the requested allocation is big enough: if (a->cursor + size > a->limit) return NULL; // out of memory I'd rewrite it like this: if (size > a->limit - a->cursor) return NULL; // out of memory
Your test is backwards. I would write it like this: if (size > a->limit - a->cursor) return NULL;
Re: Why malloc always does more than I asked for?
#20I've read some allocator walkthroughs before but I thought that this line stood out: "to get individual free() working, the allocator needs to remember something about every allocation it handed out. and that’s the moment metadata stops being optional." That's just a very nice distillation of an important concept.
Oh? How do you think munmap does it? If you can convince the caller to keep track of that metadata themselves you obviously don’t need to. That can be important. Something I noticed is that _very often_ the code that is calling malloc(n) is keeping track of n somehow for its own reasons (bounds checking, grow/gap pointers, etc) so merging the value halves stack churn and it’s an easy win.
likely it'd a be perf. hit in most cases. They'd have to copy to the tail end (likely) of the allocated area. Or the start and offset the pointer, they'd need to know the size of the metadata and account for that, including aligning it.Hence, the tail feels 'nicer'
It's possible to manually use mmap and forgo malloc entirely, rolling your own arena manager.