Live data from Hacker News

Tinyalloc: replacement for malloc/free in unmanaged, linear memory situations

github.com

31–40 of 60 posts

Re: Tinyalloc: replacement for malloc/free in unmanaged, linear memory situations

#31
post #12
post #5

Its a nifty tool, but I can't help feeling a little disappointed that we're still having to deal directly with issues such as memory allocation in 2019.

You don't necessarily. Don't forget that theres memory managed languages that compile to c. They still use malloc under the hood.

Thats a good point.

Re: Tinyalloc: replacement for malloc/free in unmanaged, linear memory situations

#33
post #5

Its a nifty tool, but I can't help feeling a little disappointed that we're still having to deal directly with issues such as memory allocation in 2019.

Well 'we' deal with it so that you don't have to. What do you suggest? Somewhere memory needs to be allocated, this stuff isn't magic.

Re: Tinyalloc: replacement for malloc/free in unmanaged, linear memory situations

#35
As some comments point out that this code might not be production ready, can anyone recommend other solutions that are able to manage a given block of memory? My understanding of libraries like jemalloc is that they are replacing malloc (using system calls brk and mmap [0]) but I can't hand it an already allocated block of memory to manage, right?

Something like

    memory_manager m(some_buffer);
    allocation a = m.allocate(512);
would be nice. :-)

[0] https://medium.com/iskakaushik/eli5-jemalloc-e9bd412abd70

Re: Tinyalloc: replacement for malloc/free in unmanaged, linear memory situations

#36
post #15

Earlier quoted context omitted.

There are other ways to get chunks of memory separate from the stack - static uninitialized byte arrays.

Of course, but then we are back to memory pools.

What is the difference between allocating memory right when the program starts with malloc and using the same size in static unitialized byte arrays?

There are differences of course, but that memory can be used in the same way, and so memory pools are orthogonal to how the memory is aquired.

Re: Tinyalloc: replacement for malloc/free in unmanaged, linear memory situations

#37

Earlier quoted context omitted.

There are other ways to get chunks of memory separate from the stack - static uninitialized byte arrays.

Yeah, but it's nicer to define that kind of stuff in your linker script IMO, where sizes of large memory regions can be thought of holistically.

The parent post was talking about using malloc once at the start of the program, are you talking about defining that with your linker script?

Also if the goal is to get one chunk of memory with a single call, why use malloc at all? Why not use the direct memory mapping command?

Re: Tinyalloc: replacement for malloc/free in unmanaged, linear memory situations

#38
post #15

Earlier quoted context omitted.

Of course, but then we are back to memory pools.

What is the difference between allocating memory right when the program starts with malloc and using the same size in static unitialized byte arrays? There are differences of course, but that memory can be used in the same way, and so memory pools are orthogonal to how the memory is aquired.

If you use modules with opaque pointers, the types are incomplete and therefore their size is not known outside of the module. You can either make the type visible (against the idea of opaque pointers), modify the module to allocate a fixed set of instances (somewhat against the open/closed principle), or you let the module use malloc to allocate memory for new instances.

Re: Tinyalloc: replacement for malloc/free in unmanaged, linear memory situations

#39
post #19

Earlier quoted context omitted.

https://github.com/thi-ng/tinyalloc/blob/master/tinyalloc.c#...

Thanks. In my defense, GitHub search is garbage: https://github.com/thi-ng/tinyalloc/search?q=Heap&unscoped_q...

github search is optimized for finding accidentally committed credentials, not useful code.

Re: Tinyalloc: replacement for malloc/free in unmanaged, linear memory situations

#40

As some comments point out that this code might not be production ready, can anyone recommend other solutions that are able to manage a given block of memory? My understanding of libraries like jemalloc is that they are replacing malloc (using system calls brk and mmap [0]) but I can't hand it an already allocated block of memory to manage, right? Something like memory_manager m(some_buffer); allocation a = m.allocat…

https://android.googlesource.com/platform/external/qemu/+/em...

https://android.googlesource.com/platform/external/qemu/+/em...

https://android.googlesource.com/platform/external/qemu/+/em...

These 3 files should be a fairly self contained, minimal way to allocate into existing buffers. No attempt is made at thread safety though (we assume the user synchronizes that themselves)

Post reply on HN