Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide memory manager overriding api #325

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 23 additions & 5 deletions jcapimin.c
Expand Up @@ -30,13 +30,15 @@
* The error manager must already be set up (in case memory manager fails).
*/

GLOBAL(void)
jpeg_CreateCompress(j_compress_ptr cinfo, int version, size_t structsize)
LOCAL(void)
jpeg_create_compress_internal(j_compress_ptr cinfo, int version, size_t structsize, boolean withmem)
{
int i;

if (!withmem) {
cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */
}
/* Guard against version mismatches between library and caller. */
cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */
if (version != JPEG_LIB_VERSION)
ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
if (structsize != sizeof(struct jpeg_compress_struct))
Expand All @@ -52,14 +54,18 @@ jpeg_CreateCompress(j_compress_ptr cinfo, int version, size_t structsize)
{
struct jpeg_error_mgr *err = cinfo->err;
void *client_data = cinfo->client_data; /* ignore Purify complaint here */
struct jpeg_memory_mgr *mem = cinfo->mem;
MEMZERO(cinfo, sizeof(struct jpeg_compress_struct));
cinfo->err = err;
cinfo->client_data = client_data;
cinfo->mem = mem;
}
cinfo->is_decompressor = FALSE;

/* Initialize a memory manager instance for this object */
jinit_memory_mgr((j_common_ptr)cinfo);
if (cinfo->mem == NULL) {
/* Initialize a memory manager instance for this object */
jinit_memory_mgr((j_common_ptr)cinfo);
}

/* Zero out pointers to permanent structures. */
cinfo->progress = NULL;
Expand Down Expand Up @@ -94,6 +100,18 @@ jpeg_CreateCompress(j_compress_ptr cinfo, int version, size_t structsize)
cinfo->global_state = CSTATE_START;
}

GLOBAL(void)
jpeg_CreateCompressWithMem(j_compress_ptr cinfo, int version, size_t structsize)
{
jpeg_create_compress_internal(cinfo, version, structsize, TRUE);
}

GLOBAL(void)
jpeg_CreateCompress(j_compress_ptr cinfo, int version, size_t structsize)
{
jpeg_create_compress_internal(cinfo, version, structsize, FALSE);
}


/*
* Destruction of a JPEG compression object
Expand Down
28 changes: 23 additions & 5 deletions jdapimin.c
Expand Up @@ -30,13 +30,15 @@
* The error manager must already be set up (in case memory manager fails).
*/

GLOBAL(void)
jpeg_CreateDecompress(j_decompress_ptr cinfo, int version, size_t structsize)
LOCAL(void)
jpeg_create_decompress_internal(j_decompress_ptr cinfo, int version, size_t structsize, boolean withmem)
{
int i;

if (!withmem) {
cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */
}
/* Guard against version mismatches between library and caller. */
cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */
if (version != JPEG_LIB_VERSION)
ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
if (structsize != sizeof(struct jpeg_decompress_struct))
Expand All @@ -52,14 +54,18 @@ jpeg_CreateDecompress(j_decompress_ptr cinfo, int version, size_t structsize)
{
struct jpeg_error_mgr *err = cinfo->err;
void *client_data = cinfo->client_data; /* ignore Purify complaint here */
struct jpeg_memory_mgr *mem = cinfo->mem;
MEMZERO(cinfo, sizeof(struct jpeg_decompress_struct));
cinfo->err = err;
cinfo->client_data = client_data;
cinfo->mem = mem;
}
cinfo->is_decompressor = TRUE;

/* Initialize a memory manager instance for this object */
jinit_memory_mgr((j_common_ptr)cinfo);
if (cinfo->mem == NULL) {
/* Initialize a memory manager instance for this object */
jinit_memory_mgr((j_common_ptr)cinfo);
}

/* Zero out pointers to permanent structures. */
cinfo->progress = NULL;
Expand Down Expand Up @@ -94,6 +100,18 @@ jpeg_CreateDecompress(j_decompress_ptr cinfo, int version, size_t structsize)
MEMZERO(cinfo->master, sizeof(my_decomp_master));
}

GLOBAL(void)
jpeg_CreateDecompressWithMem(j_decompress_ptr cinfo, int version, size_t structsize)
{
jpeg_create_decompress_internal(cinfo, version, structsize, TRUE);
}

GLOBAL(void)
jpeg_CreateDecompress(j_decompress_ptr cinfo, int version, size_t structsize)
{
jpeg_create_decompress_internal(cinfo, version, structsize, FALSE);
}


/*
* Destruction of a JPEG decompression object
Expand Down
11 changes: 11 additions & 0 deletions jpeglib.h
Expand Up @@ -903,10 +903,21 @@ EXTERN(struct jpeg_error_mgr *) jpeg_std_error(struct jpeg_error_mgr *err);
#define jpeg_create_decompress(cinfo) \
jpeg_CreateDecompress((cinfo), JPEG_LIB_VERSION, \
(size_t)sizeof(struct jpeg_decompress_struct))
#define jpeg_create_compress_with_mem(cinfo) \
jpeg_CreateCompressWithMem((cinfo), JPEG_LIB_VERSION, \
(size_t)sizeof(struct jpeg_compress_struct))
#define jpeg_create_decompress_with_mem(cinfo) \
jpeg_CreateDecompressWithMem((cinfo), JPEG_LIB_VERSION, \
(size_t)sizeof(struct jpeg_decompress_struct))

EXTERN(void) jpeg_CreateCompress(j_compress_ptr cinfo, int version,
size_t structsize);
EXTERN(void) jpeg_CreateDecompress(j_decompress_ptr cinfo, int version,
size_t structsize);
EXTERN(void) jpeg_CreateCompressWithMem(j_compress_ptr cinfo, int version,
size_t structsize);
EXTERN(void) jpeg_CreateDecompressWithMem(j_decompress_ptr cinfo, int version,
size_t structsize);
/* Destruction of JPEG compression objects */
EXTERN(void) jpeg_destroy_compress(j_compress_ptr cinfo);
EXTERN(void) jpeg_destroy_decompress(j_decompress_ptr cinfo);
Expand Down
19 changes: 18 additions & 1 deletion libjpeg.txt
Expand Up @@ -52,6 +52,7 @@ Advanced features:
Really raw data: DCT coefficients
Progress monitoring
Memory management
Custom memory management
Memory usage
Library compile-time options
Portability considerations
Expand Down Expand Up @@ -3000,6 +3001,23 @@ malloc()s and free()s virtual arrays, and an error occurs if the required
memory exceeds the limit specified in cinfo->mem->max_memory_to_use.


Custom memory management
------------------------

The default memory manager can be replaced with a custom one for special cases
where the default memory manager is not sufficient. This is done by filling out
the jpeg_memory_manager structure before calling one of the jpeg_create_
functions. When the jpeg_create_ function is called it needs to be one of the
_with_mem versions (jpeg_create_compress_with_mem or
jpeg_create_decompress_with_mem). If one of the jpeg_create_ functions that
does not have _with_mem is called the custom memory manager will be replaced
with the standard one, so it is important to call the _with_mem versions.

The custom memory manager must fill out the jpeg_memory_manager structure with
functions for all of the callbacks in the jpeg_memory_mgr structure, as these
will all be called by libjpeg.


Memory usage
------------

Expand Down Expand Up @@ -3045,7 +3063,6 @@ requested.
If you need more detailed information about memory usage in a particular
situation, you can enable the MEM_STATS code in jmemmgr.c.


Library compile-time options
----------------------------

Expand Down
2 changes: 2 additions & 0 deletions win/jpeg62-memsrcdst.def
Expand Up @@ -106,3 +106,5 @@ EXPORTS
jpeg_crop_scanline @ 105 ;
jpeg_read_icc_profile @ 106 ;
jpeg_write_icc_profile @ 107 ;
jpeg_CreateCompressWithMem @ 108 ;
jpeg_CreateDecompressWithMem @ 109 ;
2 changes: 2 additions & 0 deletions win/jpeg62.def
Expand Up @@ -104,3 +104,5 @@ EXPORTS
jpeg_crop_scanline @ 103 ;
jpeg_read_icc_profile @ 104 ;
jpeg_write_icc_profile @ 105 ;
jpeg_CreateCompressWithMem @ 106 ;
jpeg_CreateDecompressWithMem @ 107 ;
2 changes: 2 additions & 0 deletions win/jpeg7-memsrcdst.def
Expand Up @@ -108,3 +108,5 @@ EXPORTS
jpeg_crop_scanline @ 107 ;
jpeg_read_icc_profile @ 108 ;
jpeg_write_icc_profile @ 109 ;
jpeg_CreateCompressWithMem @ 110 ;
jpeg_CreateDecompressWithMem @ 111 ;
2 changes: 2 additions & 0 deletions win/jpeg7.def
Expand Up @@ -106,3 +106,5 @@ EXPORTS
jpeg_crop_scanline @ 105 ;
jpeg_read_icc_profile @ 106 ;
jpeg_write_icc_profile @ 107 ;
jpeg_CreateCompressWithMem @ 108 ;
jpeg_CreateDecompressWithMem @ 109 ;
2 changes: 2 additions & 0 deletions win/jpeg8.def
Expand Up @@ -109,3 +109,5 @@ EXPORTS
jpeg_crop_scanline @ 108 ;
jpeg_read_icc_profile @ 109 ;
jpeg_write_icc_profile @ 110 ;
jpeg_CreateCompressWithMem @ 111 ;
jpeg_CreateDecompressWithMem @ 112 ;