Hello everybody,
I have a problem with libpng library, I am trying to use my own memory allocation routines. So, as stays in doc, I define PNG_USER_MEM_SUPPORTED and want to use png_create_read_struct_2() instead of png_create_read_struct(). png_create_read_struct_2 takes following parameters:

png_structp png_ptr = png_create_read_struct_2
       (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
        user_error_fn, user_warning_fn, (png_voidp)
        user_mem_ptr, user_malloc_fn, user_free_fn);

I'm interested in last three params.
I've wrote my_malloc and my_free, but my question is, what is the user_mem_ptr, and if anyone worked with it and used it, please give me a clue, or link me to some example (I haven't found a well explained example on web :-| ).
Thank you in advance.

Dani AI

Generated

The original question from asked what the opaque user_mem_ptr is for when registering custom allocators; pointed to the manual but the thread missed a concise, practical description. In libpng the user_mem_ptr is simply a user-supplied context pointer that libpng stores inside the internal png_struct. Its purpose is to carry allocator state (pool or arena handles, locks, bookkeeping structs, etc.) so the custom malloc/free callbacks can access that state. The library exposes that pointer (and passes png_structp into the callbacks) so the allocator can retrieve the context when it runs. (libpng.org)

A typical pattern is to put allocator state in a small struct and cast the stored pointer back inside the callbacks. Example sketch (replace pool_alloc/pool_free with application code):

struct MyPool { /* pool fields */ };

void *my_malloc(png_structp png_ptr, png_size_t size) {
    struct MyPool *pool = (struct MyPool*)png_get_mem_ptr(png_ptr);
    return pool_alloc(pool, size);
}

void my_free(png_structp png_ptr, void *ptr) {
    struct MyPool *pool = (struct MyPool*)png_get_mem_ptr(png_ptr);
    pool_free(pool, ptr);
}

/* pass the &my_pool as the mem_ptr when creating the png_struct (or call png_set_mem_fn) */

The official docs show these prototypes and the png_get_mem_ptr accessor. (docs.oracle.com)

Troubleshooting notes: make the custom malloc return NULL on failure (libpng will handle/report it); free should not expect to be called with NULL (libpng checks for NULL before calling it); do not mix allocators (memory allocated by one system must be freed by the same system); ensure the mem_ptr object outlives the png struct (destroy png structs before freeing pool state); and keep thread-safety in mind if the pool is shared. The libpng distribution also includes example programs (example.c, pngtest.c) that demonstrate the general setup. (docs.oracle.com)

Hi all,
I passed null as user_mem_ptr, and it seems to work. But I still wonder what is it for. Anyway, if I find out, i'll let you know. Greets

this is manual (old version) I was quoting from, but there is no explanation of using user_mem_ptr param, well, thanks anyway for your time :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.