Question

There is precious little information online or on stackoverflow with regards to a function I recently encountered called zmalloc. (In fact, this is only the 3rd zmalloc-tagged question on SO).

I gleaned the following:

  • zmalloc automatically keeps track of, and frees unfreed memory, similar to C++ smart pointers.
  • zmalloc apparently enables some metrics, at least in the case of the redis source.

So my questions are:

  1. What flexibility does one lose, then, in using zmalloc over malloc? i.e. what benefits do malloc continue to offer that zmalloc does not?
  2. Is zmalloc non-standard in C11? Is this a custom-built function?
Was it helpful?

Solution

It looks like zmalloc is part of the redis-tools (https://github.com/antirez/redis-tools). redis is a kind of database which keeps stuff in memory (http://redis.io/).

Typically malloc replacements are developed because some target systems do not provide a suitable malloc, or because the caller needs extra functionality. I think zmalloc is a pretty simple wrapper of the system malloc/free, just keeping track of the overall memory allocated. No automatic free involved. The post you pointed to also explains the need: The database can be configured to not use more than some amount of memory and thus needs to keep track of the overall consumption.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top