my_memory_mgr::~my_memory_mgr(void) { /* Close all backing store, release all memory. * Releasing pools in reverse order might help avoid fragmentation * with some (brain-damaged) malloc libraries. */ for (int pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) free_pool(pool); /* Release the memory manager control block too. */ // cinfo->mem = NULL; /* ensures I will be called only once */ jpeg_mem_term(cinfo); /* system-dependent cleanup */ }
self_destruct (j_common_ptr cinfo) { int pool; /* Close all backing store, release all memory. * Releasing pools in reverse order might help avoid fragmentation * with some (brain-damaged) malloc libraries. */ for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) { free_pool(cinfo, pool); } /* Release the memory manager control block too. */ jpeg_free_small(cinfo, (void *) cinfo->mem, sizeof(my_memory_mgr)); cinfo->mem = NULL; /* ensures I will be called only once */ jpeg_mem_term(cinfo); /* system-dependent cleanup */ }
jinit_memory_mgr (j_common_ptr cinfo) { my_mem_ptr mem; long max_to_use; int pool; size_t test_mac; cinfo->mem = NULL; /* for safety if init fails */ /* Check for configuration errors. * sizeof(ALIGN_TYPE) should be a power of 2; otherwise, it probably * doesn't reflect any real hardware alignment requirement. * The test is a little tricky: for X>0, X and X-1 have no one-bits * in common if and only if X is a power of 2, ie has only one one-bit. * Some compilers may give an "unreachable code" warning here; ignore it. */ if ((sizeof(ALIGN_TYPE) & (sizeof(ALIGN_TYPE)-1)) != 0) cinfo->ERREXIT(JERR_BAD_ALIGN_TYPE); /* MAX_ALLOC_CHUNK must be representable as type size_t, and must be * a multiple of sizeof(ALIGN_TYPE). * Again, an "unreachable code" warning may be ignored here. * But a "constant too large" warning means you need to fix MAX_ALLOC_CHUNK. */ test_mac = (size_t) MAX_ALLOC_CHUNK; if ((long) test_mac != MAX_ALLOC_CHUNK || (MAX_ALLOC_CHUNK % sizeof(ALIGN_TYPE)) != 0) cinfo->ERREXIT(JERR_BAD_ALLOC_CHUNK); max_to_use = jpeg_mem_init(cinfo); /* system-dependent initialization */ /* Attempt to allocate memory manager's control block */ mem = new my_memory_mgr(cinfo); if (mem == NULL) { jpeg_mem_term(cinfo); /* system-dependent cleanup */ cinfo->ERREXIT1(JERR_OUT_OF_MEMORY, 0); } /* OK, fill in the method pointers */ // mem->pub.alloc_small = alloc_small; // mem->pub.alloc_large = alloc_large; // mem->pub.alloc_sarray = alloc_sarray; // mem->pub.alloc_barray = alloc_barray; // mem->pub.request_virt_sarray = request_virt_sarray; // mem->pub.request_virt_barray = request_virt_barray; // mem->pub.realize_virt_arrays = realize_virt_arrays; // mem->pub.access_virt_sarray = access_virt_sarray; // mem->pub.access_virt_barray = access_virt_barray; // mem->pub.free_pool = free_pool; // mem->pub.self_destruct = self_destruct; /* Make MAX_ALLOC_CHUNK accessible to other modules */ mem->max_alloc_chunk = MAX_ALLOC_CHUNK; /* Initialize working state */ mem->max_memory_to_use = max_to_use; for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) { mem->small_list[pool] = NULL; mem->large_list[pool] = NULL; } mem->virt_sarray_list = NULL; mem->virt_barray_list = NULL; mem->total_space_allocated = sizeof(my_memory_mgr); /* Declare ourselves open for business */ cinfo->mem = mem; /* Check for an environment variable JPEGMEM; if found, override the * default max_memory setting from jpeg_mem_init. Note that the * surrounding application may again override this value. * If your system doesn't support getenv(), define NO_GETENV to disable * this feature. */ #ifndef NO_GETENV { char * memenv; if ((memenv = getenv("JPEGMEM")) != NULL) { char ch = 'x'; if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) { if (ch == 'm' || ch == 'M') max_to_use *= 1000L; mem->max_memory_to_use = max_to_use * 1000L; } } } #endif }