nDPI icon indicating copy to clipboard operation
nDPI copied to clipboard

Custom memory allocator

Open IvanNardi opened this issue 4 years ago • 2 comments

nDPI has always been able to use custom memory allocators, via set_ndpi_malloc() and set_ndpi_free() functions. But looking at the code, I started to have a doubt: are these functions supposed to be used only by the library itself or by both the library and the application?

ndpiReader application is not always coherent: for the most part it uses them, but not always. More importantly, it doesn't set them at the very beginning, so it ends up with the pattern:

ptr = ndpi_malloc();
...
set_ndpi_malloc(); set_ndpi_free();
...
ndpi_free(ptr)

which triggers a memory corruption whenever you use some really custom allocators (and not some trivial malloc/free wrappers). This code needs to be fixed in some way, anyway.

But the real point I would like to discuss is the allocators scope.

I think that it would be more useful if these custom allocators are used only by the library (i.e. no ndpi_malloc()/ndpi_free() calls in ndpiReader.c, reader_utils.c, ...). For example, I would like to have some memory usage statistics per-flow, and i think it would be easier to get them if only the libray uses ndpi_malloc()/ndpi_free() functions

I would like to hear other opinions about that.

IvanNardi avatar Aug 24 '21 19:08 IvanNardi

If we develop this idea further, then we need to store pointers to alloc()/free() in the ndpi_detection_module_struct structure :) Is it worth it?

I would replace the set_ndpi_malloc(), set_ndpi_free(), set_ndpi_flow_malloc(), set_ndpi_flow_free() functions with one ndpi_library_init() function with a list of pointers. A call to ndpi_library_init() would resolve only once and only before the first call to ndpi_init_detection_module(). By default, ndpi_malloc()/ndpi_free() should call abort(). Calling ndpi_init_detection_module() without executing ndpi_library_init() must end with abort(). Then memory corruption according to your scenario will be impossible.

IMHO Disallowing ndpi_malloc()/ndpi_free() calls from outside the library will be difficult to implement given the variety of platforms.

Maybe it makes sense to abandon pointers to alloc()/free() altogether and make these functions external?

vel21ripn avatar Aug 24 '21 21:08 vel21ripn

what about migrate the project to C++17 ?

adibhannachi avatar Jun 29 '22 11:06 adibhannachi