Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a C library offer ability to use custom memory allocators?

I see that some C libraries have ability to specify custom memory allocators (malloc/free replacements).

  • In what systems/environments/conditions is that useful? Isn't this feature just a leftover from MSDOS era or similar no-longer-relevant problems?

Background story:

I'm planning to make pngquant a library that can be embedded in various software (from iOS apps to Apache modules). I'm using malloc()/free() and my own memory pools for small allocations. I use 2MB-50MB of memory in total. I use threads, but only need to alloc on the main thread.

like image 692
Kornel Avatar asked Oct 23 '22 03:10

Kornel


2 Answers

In any application where control over memory allocation is critical (for example my field, game development, or other real or near real time systems) the inability to control memory allocations in a library immediately disqualifies it from use.

like image 176
Andreas Brinck Avatar answered Oct 27 '22 09:10

Andreas Brinck


Many malloc/free algorithms exist. The system malloc is sometimes not optimized for the task that the library is handling, so the caller might want to try a few different ones to optimize performance.

A few that come to mind are:

  • dlmalloc
  • jemalloc
  • TCMalloc

There are also Garbage Collection libraries such as the Boehm Garbage Collector which are usable in C by calling the provided malloc/free replacements (even though free is then a dummy function call, kept for compatibility).

There are also many possible uses, for example one may write a debug malloc/free function that could trace memory allocations and liberations in the library, such as one that I wrote that uses SQLite to record statistics about how the memory is used (admittedly at the cost of performance, but it is a debugging situation).

like image 40
SirDarius Avatar answered Oct 27 '22 10:10

SirDarius