News for hackers. Stuff that matters.
Re:Tinyalloc: replacement for malloc/free in unmanaged, line... by Nursie on Sunday January 06, 2019 @10:52AM (#18838884)
That's cool. I've written small but less flexible things before based on arrays as used/freed block maps rather than lists, from embedded devices with 128K of RAM.

This seems like it has some nice features. Though a 3K overhead could be important in such a constrained environment (I guess if it's only managing 128k that could reduce).

Re:Tinyalloc: replacement for malloc/free in unmanaged, line... by srcmap on Sunday January 06, 2019 @11:41AM (#18839110)
Did similar implementation 15 years ago. I needed a implementation that works in SMP environment and it didn't use lock. I use atomic increment to allocate block index, atomic dec to free the block index. It worked very well with good performance in SMP environment. It was implement as share library and the same code can be link statically in the kernel module. Linux Kernel can allocate the block and pass the pointer (index to the block) as parameter to user space and user space and de reference it directly.

The library was implemented using C++. We also ported that to QNX in addition to Linux Kernel.

I remembered it can do > 1 millions msg block per second between user space programs or similar perf for between user and kernel space.

It did waste some amount of space. That can be config at system startup.

Re:Tinyalloc: replacement for malloc/free in unmanaged, line... by Unklejoe on Sunday January 06, 2019 @11:58AM (#18839221)
Cool. How did you deal with the case where a block gets freed out of order?

For example,

Thread1 allocate (block index++) Thread2 allocate (block index++) Thread1 free (block index--) Thread3 allocate (block index++)

Now thread 3 has a block that is actually still owned by thread 2.

Re:Tinyalloc: replacement for malloc/free in unmanaged, line... by srcmap on Sunday January 06, 2019 @09:09PM (#18842192)
The blocks were actually fix in pre-allocated blocks.

When a thread, process, kernel thread "allocated" a block. It actually acquired and owned the "index" via atomic inc from the free list.

When it free the block, it put the block's index back to the "free" list.

The "free" list is actually a array. Thus the list index can be allocated and free with atomic inc, dec. Thus it can do SMP safe alloc, free operations without any locks and guarantee the correctness.

This was actually a IPC, DIPC library to support TCP session redundancy in a network router product. Other programmers call this library from user, kernel and TCP stack, etc.

Since the blocks and index are all in share memory, there are huge potential for share memory corruptions. In debug mode, there are memory space before and after each block, every allocate, free are checked for ownership, double free, and border space corruption. There are regression tests to cover all aspect of API calls that were run 24/7 check for every commit. When not compile with debug mode, the operations were much faster but there were still quite a bit of assertions check in place.

There were a debug trace system that track all system activities (including Linux, QNX, vxwork context switches.) The log is actually in special region of memory that preserved over warm boot. If the system was completely hang, in 99% of cases, I can recover the memory to do post mordem analysis and know exactly the last user, kernel space threads that were running before the system crash. It was more useful for vxWork, less useful for Linux as majority of code were in user space and didn't take down the system.

The regression test coverage code and the debug mode validations were 80% of code. The actually implementations were very small.

Re:Tinyalloc: replacement for malloc/free in unmanaged, line... by okl on Sunday January 06, 2019 @12:13PM (#18839297)
Freeing is usually easy (for free() and memory pool) because the pointer can be used to compute the index into the bool array or bitfield. Allocating is more difficult since you have to search. You can use a linear search that starts at the last block that was allocated (for memory pools that's usually good enough). You can also use a bloom filter to accelerate the search. Example:

256 blocks of memory, you use 8 words of 32 bits to to indicate used/free status. 1 means free, 0 means occupied. To quickly find a free block you compare the words with zero.

Re:Tinyalloc: replacement for malloc/free in unmanaged, line... by SlowRobotAhead on Sunday January 06, 2019 @11:22AM (#18839029)
3k overhead where you’re trying to reduce from malloc is definitely a lot. One of my products have 8K total, not going to waste 3 of that replacing malloc.

I’m fairly happy that mirsa C forbids malloc. I can’t use it even if I wanted to, so I need to be more careful about how memory is used. Annoying at first, makes a lot of sense later on though.

Re:Tinyalloc: replacement for malloc/free in unmanaged, line... by okl on Sunday January 06, 2019 @11:29AM (#18839058)
It is quite possible (and it can be reasonable) to deviate from that rule. For example you could write a wrapper for malloc that only allows dynamic allocation in the initialization phase of your program. This can be useful to allocate memory for opaque-pointer-style types.
Re:Tinyalloc: replacement for malloc/free in unmanaged, line... by CyberDildonics on Sunday January 06, 2019 @12:29PM (#18839378)
There are other ways to get chunks of memory separate from the stack - static uninitialized byte arrays.
Re:Tinyalloc: replacement for malloc/free in unmanaged, line... by monocasa on Sunday January 06, 2019 @02:15PM (#18839993)
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.
Re:Tinyalloc: replacement for malloc/free in unmanaged, line... by CyberDildonics on Sunday January 06, 2019 @03:27PM (#18840453)
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, line... by monocasa on Sunday January 06, 2019 @04:01PM (#18840688)
I don't think he's talking about using malloc "once", he's talking about only using it during init. In practice you end calling it a bunch in that model, the last design I shipped like that ended up making a few hundred malloc (well new operator because this was C++) calls at init.

Under that system malloc/new was just a pointer bump, and an assert if there wasn't enough room. Free wasn't linked, and delete asserted. That heap region (or mine had several heaps that lived in different memory with different characteristics that could be specified with a new overload) is way easier to define in your linker.

Re:Tinyalloc: replacement for malloc/free in unmanaged, line... by okl on Sunday January 06, 2019 @12:59PM (#18839562)
Of course, but then we are back to memory pools.
Re:Tinyalloc: replacement for malloc/free in unmanaged, line... by CyberDildonics on Sunday January 06, 2019 @03:25PM (#18840438)
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, line... by okl on Sunday January 06, 2019 @03:39PM (#18840552)
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, line... by CyberDildonics on Sunday January 06, 2019 @09:47PM (#18842352)
Why would someone be doing this while trying to fit within the boundaries of the MISRA standard? Also what do you mean by module?
Re:Tinyalloc: replacement for malloc/free in unmanaged, line... by okl on Sunday January 06, 2019 @10:37PM (#18842573)
The rule you are referring to is a "required" (not a "mandatory") rule. You can deviate from it and still be MISRA-C compliant. People write deviations for all kinds of reasons. Here are some rationales for deviations: https://lars-lab.jpl.nasa.gov/JPL_Coding_Standard_C.pdf (Rule 5 on page 10 on malloc) https://www.keil.com/pack/doc/CMSIS/Core/html/coreMISRA_Exce...

The unit of reuse that defines the type, example: http://c-faq.com/struct/sd1.html

Re:Tinyalloc: replacement for malloc/free in unmanaged, line... by CyberDildonics on Monday January 07, 2019 @03:34PM (#18849722)
So your solution, in a thread about using a fixed amount of memory, is to do nothing different and let anything use the heap at any time because it might be possible in some cases to get it past MISRA standards? I'm not really sure of what you are trying to say, it seems you keep skewing further from the original discussion.
Re:Tinyalloc: replacement for malloc/free in unmanaged, line... by Nursie on Monday January 07, 2019 @04:05AM (#18843830)
> I’m fairly happy that mirsa C forbids mallo

We wrote the system I mentioned above (128K of RAM, 256K of flash) without it, I agree, you can do everything you need without malloc when you're careful.

Unfortunately we had a third party dependency that required a malloc implementation to be able to run, so we had to implement a simple one.

Re:Tinyalloc: replacement for malloc/free in unmanaged, line... by SlowRobotAhead on Monday January 07, 2019 @03:38PM (#18849780)
>Unfortunately we had a third party dependency that required a malloc implementation to be able to run, so we had to implement a simple one.

We ran into the same thing. Some open source compression code I wanted to use had malloc, dumped it for something else that didn't. Luckily we found a protobuffers instance that didn't use malloc, that saved some time on a different project.

First thing I do now when looking at example or source we might want to use is a quick scan for malloc.

Re:Tinyalloc: replacement for malloc/free in unmanaged, line... by Nursie on Tuesday January 08, 2019 @05:52AM (#18854686)
Unfortunately for us, this was a certified/approved EMV payment kernel, and about the only one we could get for our architecture, so we had to live with it.

Sometimes you don't get to choose or do it yourself, more's the pity!

Re:Tinyalloc: replacement for malloc/free in unmanaged, line... by okl on Sunday January 06, 2019 @11:24AM (#18839036)
That concept is called a memory pool or storage pool. Very useful, but their greatest disadvantage compared to malloc() is that you waste a lot of memory if your allocation size varies greatly (for example a network protocol where packet size can range from few bytes to KB).
Re:Tinyalloc: replacement for malloc/free in unmanaged, line... by Const-me on Sunday January 06, 2019 @05:17PM (#18841097)
In my experience, best way to implement a network protocol on a small embedded chip is fixed length statically allocated circular buffer, not malloc.
Re:Tinyalloc: replacement for malloc/free in unmanaged, line... by okl on Sunday January 06, 2019 @05:58PM (#18841291)
That would be like a "first-fit-or-die" malloc, wouldn't it? Which mechanism to choose always depends on your (de-)allocation profile and available memory. If you can free out-of-order, the circular buffer will fragment quickly.
Re:Tinyalloc: replacement for malloc/free in unmanaged, line... by Const-me on Sunday January 06, 2019 @07:31PM (#18841767)
> If you can free out-of-order, the circular buffer will fragment quickly.

Right, just when coding a network protocol for embedded, often you can free in the same order, i.e. it’s often a good idea to limit parallelism to a small number of FIFO streams (ideally a single one).

That’s only for small chips. When they aren’t small e.g. you have a quad-core ARM with a gig of RAM you usually have OS anyway and the programming is totally different, much higher level and you guaranteed to have a fast malloc and lots more.

Re:Tinyalloc: replacement for malloc/free in unmanaged, line... by toxmeister on Monday January 07, 2019 @10:52AM (#18846121)
The overhead is not fixed and the 3KB are only for the default config and based on the max number of blocks one might have in flight (256 by default).