ironbee/libhtp — pull request #67 by inliniac (2014-01-23 16:13:25+00:00)
Title: [rfc] Memory wrappers and accounting v4

**Not for merge.**

In my quest to find where Suricata spends it's memory I've created this branch to track libhtp's memory allocations and frees. What it does is create wrapper functions for malloc, calloc and friends: htp_malloc, htp_calloc, etc.

Each of those then updates a global counter (using gcc atomics to be thread safe), which indicates the current amount of memory in use by libhtp.

A simple new function htp_memory_get_memuse() exposes this value to the application.

To make this work, all calls to 'htp_free' and 'htp_realloc' had to be changed to take the current size of the memory as an argument.

I think the wrappers themselves make sense, although the size arguments to free and realloc are a bit painful and easy to mess up. The counter implementation is probably a problem. Maybe we could have a callback or a set of callbacks that is called for each allocation. The memuse counter could then move into the application:

void *htp_malloc(size_t size) {
void *ptr = malloc(size);
if (ptr && htp_malloc_callback)
htp_malloc_callback(size);
return ptr;
}

My callback (in Suricata) would then be:

void HtpMallocUpdate(size_t size) {
SC_ATOMIC_ADD(libhtp_memuse, size);
}

Or we could to a more generic update callback, that pushes a signed int so both memory use grow and shrink operations call the same callback.