Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

malloc already hands you more than you ask for in a lot of cases, check out malloc_usable_size.


sort by: page size:

Things like malloc_usable_size() exist but you're not supposed to use the overage it tells you about so they're not very useful APIs.

It would be nice if there was a modern malloc implementation (with good multi-threaded performance) that optimized for size.

That would require implicit heap allocation, which is a lot to ask for in a language where malloc is a library function.

In such an environment, malloc() will do that.

Or, here's a crazy idea: how about we actually allocate the memory when you call malloc(), and if there isn't any, give you an error instead? Programs could check the return code and decide what to do when they run out of memory themselves. Crazy, I know.

I think that putting a hard limit on alloca allocation size, somewhere around 1-10 Kb, could help prevent this type of exploits. Also, it is possible to write a function that would choose between alloca() and malloc()/free() depending on the size.

What if I want to write a malloc that requires the size to be stored separately? (i.e. one that needs to be paired with free(ptr, length) rather than just free(ptr) for good performance.) Wouldn't that provide more flexibility in the challenge and be more useful (e.g. for C++'s std::allocator)?

oh man that is the kind of bad idea that I love. I'm definitely going to try implementing malloc that way and report on the results =D

Too bad malloc gets called significantly more often than free.

Oh come on. How hard is it to log how much memory is malloc'd.

(edit: I don't mean to be offensive here, I just don't see why people find such simple concepts hard. A simple wrapper over malloc would do :P)


malloc and free could also be hardcoded.

For many types of programs it's really, really important to have control over allocation behaviour which malloc doesn't provide (for instance a per-frame bump allocator, an allocator which reuses memory blocks but still works within a pre-allocated memory chunk, a specialized allocator for GPU memory blocks, etc...).

Then don't make it too big and fall back to malloc(3) above a certain threshold?

Malloc is O(n²) so it totally depend on how able you are to not do gradual allocation.

malloc isn't magic, just mmap some memory to dole out.

Are you aware that malloc() is slow and doing all these tiny allocations is probably the biggest bottleneck in your program?

I think we do this in practice. The issue is that malloc doesn' t ask the OS for memory, it calls the system allocator which in turn asks the OS for memory. But once you've freed something, that allocator can give you that memory back without talking to the kernel

(and also reducing the number of malloc/free calls)

I thought we were talking about a situation with no heap! If you have that luxury, heck yeah just call malloc.
next

Legal | privacy