How to create a custom memory manager / allocator without having to recompile the library

30 views
Skip to first unread message

tenspd137

unread,
Oct 29, 2018, 3:28:36 PM10/29/18
to libjpeg-turbo User Discussion/Support
Hi all -

I haven't been able to find much on this, but is there a way to tell libjpeg to use a custom memory manager without having to change the library so that the system jpeg can be used?  I was thinking similar to how you can specify a custom error handler - can you somehow supply a compress/decompress structure with functions to use for allocation and deallocaion?  I have been googling and reading, but there seems to be no real information on how to do this.

Any help would be appreciated!

Thanks!

DRC

unread,
Oct 29, 2018, 5:26:11 PM10/29/18
to libjpeg-t...@googlegroups.com
No. Why would you need a different memory manager? It sounds like
you're trying to do something that is wildly unsupported.

tenspd137

unread,
Oct 29, 2018, 5:37:58 PM10/29/18
to libjpeg-t...@googlegroups.com
In an embedded system that has it's own memory manager that tracks
allocations, but as I write this, the correct way to do it would be to
have the system override the memory management calls so that the
memory manager in jpeg is using that memory manager instead of
supplying a memory manager. Sooo - never mind.....

Thanks!
> --
> You received this message because you are subscribed to the Google Groups "libjpeg-turbo User Discussion/Support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to libjpeg-turbo-u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/libjpeg-turbo-users/03bd1907-521b-1fd2-6460-0f8681506f3b%40virtualgl.org.
> For more options, visit https://groups.google.com/d/optout.

tenspd137

unread,
Oct 29, 2018, 6:15:45 PM10/29/18
to libjpeg-t...@googlegroups.com
So I may have been hasty. Basically, there is an embedded system with
multiple pools of memory, and I would like libjpeg to grab memory from
a certain pool. I know that my ends can be met by modifying the
library, but I was hoping to avoid that. So I was hoping there was
some way I could do something like hand cinfo an allocator and tell it
to use that.

On the flip side, if something like that isn't available now, would it
be a useful feature - to be able to tell libjpeg where to grab the
memory from?

Thanks again!

DRC

unread,
Oct 29, 2018, 7:20:03 PM10/29/18
to libjpeg-t...@googlegroups.com
You can't replace jpeg_get_small(), jpeg_get_large(), jpeg_free_small(),
and jpeg_free_large() without customizing the libjpeg-turbo source code,
but you can replace the higher-level functions in jmemmgr.c at run time.
How you would do that is:

- Implement your own versions of jpeg_get_*() and jpeg_free_*() in your
own program. You can name these, for example, my_jpeg_get_*() and
my_jpeg_free_*(). You can just copy the function prototypes from
jmemnobs.c and change the function names.

- Implement your own versions of alloc_small(), alloc_large(),
free_pool(), and self_destruct() in your own program, using the code in
jmemmgr.c as a template (you can simply replace all calls to jpeg_*()
with calls to my_jpeg_*().)

- At run time, after calling jpeg_create_compress(), modify
cinfo->mem->[alloc_small|alloc_large|free_pool|self_destruct] to point
to your custom functions.

tenspd137

unread,
Oct 30, 2018, 12:04:26 AM10/30/18
to libjpeg-t...@googlegroups.com
Much appreciated!  I just needed a direction to start.

Thanks for your time.

Reply all
Reply to author
Forward
0 new messages