Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1119 lines
41KB

  1. /*
  2. * jmemmgr.c
  3. *
  4. * Copyright (C) 1991-1997, Thomas G. Lane.
  5. * This file is part of the Independent JPEG Group's software.
  6. * For conditions of distribution and use, see the accompanying README file.
  7. *
  8. * This file contains the JPEG system-independent memory management
  9. * routines. This code is usable across a wide variety of machines; most
  10. * of the system dependencies have been isolated in a separate file.
  11. * The major functions provided here are:
  12. * * pool-based allocation and freeing of memory;
  13. * * policy decisions about how to divide available memory among the
  14. * virtual arrays;
  15. * * control logic for swapping virtual arrays between main memory and
  16. * backing storage.
  17. * The separate system-dependent file provides the actual backing-storage
  18. * access code, and it contains the policy decision about how much total
  19. * main memory to use.
  20. * This file is system-dependent in the sense that some of its functions
  21. * are unnecessary in some systems. For example, if there is enough virtual
  22. * memory so that backing storage will never be used, much of the virtual
  23. * array control logic could be removed. (Of course, if you have that much
  24. * memory then you shouldn't care about a little bit of unused code...)
  25. */
  26. #define JPEG_INTERNALS
  27. #define AM_MEMORY_MANAGER /* we define jvirt_Xarray_control structs */
  28. #include "jinclude.h"
  29. #include "jpeglib.h"
  30. #include "jmemsys.h" /* import the system-dependent declarations */
  31. #ifndef NO_GETENV
  32. #ifndef HAVE_STDLIB_H /* <stdlib.h> should declare getenv() */
  33. extern char * getenv JPP((const char * name));
  34. #endif
  35. #endif
  36. /*
  37. * Some important notes:
  38. * The allocation routines provided here must never return NULL.
  39. * They should exit to error_exit if unsuccessful.
  40. *
  41. * It's not a good idea to try to merge the sarray and barray routines,
  42. * even though they are textually almost the same, because samples are
  43. * usually stored as bytes while coefficients are shorts or ints. Thus,
  44. * in machines where byte pointers have a different representation from
  45. * word pointers, the resulting machine code could not be the same.
  46. */
  47. /*
  48. * Many machines require storage alignment: longs must start on 4-byte
  49. * boundaries, doubles on 8-byte boundaries, etc. On such machines, malloc()
  50. * always returns pointers that are multiples of the worst-case alignment
  51. * requirement, and we had better do so too.
  52. * There isn't any really portable way to determine the worst-case alignment
  53. * requirement. This module assumes that the alignment requirement is
  54. * multiples of sizeof(ALIGN_TYPE).
  55. * By default, we define ALIGN_TYPE as double. This is necessary on some
  56. * workstations (where doubles really do need 8-byte alignment) and will work
  57. * fine on nearly everything. If your machine has lesser alignment needs,
  58. * you can save a few bytes by making ALIGN_TYPE smaller.
  59. * The only place I know of where this will NOT work is certain Macintosh
  60. * 680x0 compilers that define double as a 10-byte IEEE extended float.
  61. * Doing 10-byte alignment is counterproductive because longwords won't be
  62. * aligned well. Put "#define ALIGN_TYPE long" in jconfig.h if you have
  63. * such a compiler.
  64. */
  65. #ifndef ALIGN_TYPE /* so can override from jconfig.h */
  66. #define ALIGN_TYPE double
  67. #endif
  68. /*
  69. * We allocate objects from "pools", where each pool is gotten with a single
  70. * request to jpeg_get_small() or jpeg_get_large(). There is no per-object
  71. * overhead within a pool, except for alignment padding. Each pool has a
  72. * header with a link to the next pool of the same class.
  73. * Small and large pool headers are identical except that the latter's
  74. * link pointer must be FAR on 80x86 machines.
  75. * Notice that the "real" header fields are union'ed with a dummy ALIGN_TYPE
  76. * field. This forces the compiler to make SIZEOF(small_pool_hdr) a multiple
  77. * of the alignment requirement of ALIGN_TYPE.
  78. */
  79. typedef union small_pool_struct * small_pool_ptr;
  80. typedef union small_pool_struct {
  81. struct {
  82. small_pool_ptr next; /* next in list of pools */
  83. size_t bytes_used; /* how many bytes already used within pool */
  84. size_t bytes_left; /* bytes still available in this pool */
  85. } hdr;
  86. ALIGN_TYPE dummy; /* included in union to ensure alignment */
  87. } small_pool_hdr;
  88. typedef union large_pool_struct FAR * large_pool_ptr;
  89. typedef union large_pool_struct {
  90. struct {
  91. large_pool_ptr next; /* next in list of pools */
  92. size_t bytes_used; /* how many bytes already used within pool */
  93. size_t bytes_left; /* bytes still available in this pool */
  94. } hdr;
  95. ALIGN_TYPE dummy; /* included in union to ensure alignment */
  96. } large_pool_hdr;
  97. /*
  98. * Here is the full definition of a memory manager object.
  99. */
  100. typedef struct {
  101. struct jpeg_memory_mgr pub; /* public fields */
  102. /* Each pool identifier (lifetime class) names a linked list of pools. */
  103. small_pool_ptr small_list[JPOOL_NUMPOOLS];
  104. large_pool_ptr large_list[JPOOL_NUMPOOLS];
  105. /* Since we only have one lifetime class of virtual arrays, only one
  106. * linked list is necessary (for each datatype). Note that the virtual
  107. * array control blocks being linked together are actually stored somewhere
  108. * in the small-pool list.
  109. */
  110. jvirt_sarray_ptr virt_sarray_list;
  111. jvirt_barray_ptr virt_barray_list;
  112. /* This counts total space obtained from jpeg_get_small/large */
  113. long total_space_allocated;
  114. /* alloc_sarray and alloc_barray set this value for use by virtual
  115. * array routines.
  116. */
  117. JDIMENSION last_rowsperchunk; /* from most recent alloc_sarray/barray */
  118. } my_memory_mgr;
  119. typedef my_memory_mgr * my_mem_ptr;
  120. /*
  121. * The control blocks for virtual arrays.
  122. * Note that these blocks are allocated in the "small" pool area.
  123. * System-dependent info for the associated backing store (if any) is hidden
  124. * inside the backing_store_info struct.
  125. */
  126. struct jvirt_sarray_control {
  127. JSAMPARRAY mem_buffer; /* => the in-memory buffer */
  128. JDIMENSION rows_in_array; /* total virtual array height */
  129. JDIMENSION samplesperrow; /* width of array (and of memory buffer) */
  130. JDIMENSION maxaccess; /* max rows accessed by access_virt_sarray */
  131. JDIMENSION rows_in_mem; /* height of memory buffer */
  132. JDIMENSION rowsperchunk; /* allocation chunk size in mem_buffer */
  133. JDIMENSION cur_start_row; /* first logical row # in the buffer */
  134. JDIMENSION first_undef_row; /* row # of first uninitialized row */
  135. boolean pre_zero; /* pre-zero mode requested? */
  136. boolean dirty; /* do current buffer contents need written? */
  137. boolean b_s_open; /* is backing-store data valid? */
  138. jvirt_sarray_ptr next; /* link to next virtual sarray control block */
  139. backing_store_info b_s_info; /* System-dependent control info */
  140. };
  141. struct jvirt_barray_control {
  142. JBLOCKARRAY mem_buffer; /* => the in-memory buffer */
  143. JDIMENSION rows_in_array; /* total virtual array height */
  144. JDIMENSION blocksperrow; /* width of array (and of memory buffer) */
  145. JDIMENSION maxaccess; /* max rows accessed by access_virt_barray */
  146. JDIMENSION rows_in_mem; /* height of memory buffer */
  147. JDIMENSION rowsperchunk; /* allocation chunk size in mem_buffer */
  148. JDIMENSION cur_start_row; /* first logical row # in the buffer */
  149. JDIMENSION first_undef_row; /* row # of first uninitialized row */
  150. boolean pre_zero; /* pre-zero mode requested? */
  151. boolean dirty; /* do current buffer contents need written? */
  152. boolean b_s_open; /* is backing-store data valid? */
  153. jvirt_barray_ptr next; /* link to next virtual barray control block */
  154. backing_store_info b_s_info; /* System-dependent control info */
  155. };
  156. #ifdef MEM_STATS /* optional extra stuff for statistics */
  157. LOCAL(void)
  158. print_mem_stats (j_common_ptr cinfo, int pool_id)
  159. {
  160. my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  161. small_pool_ptr shdr_ptr;
  162. large_pool_ptr lhdr_ptr;
  163. /* Since this is only a debugging stub, we can cheat a little by using
  164. * fprintf directly rather than going through the trace message code.
  165. * This is helpful because message parm array can't handle longs.
  166. */
  167. fprintf(stderr, "Freeing pool %d, total space = %ld\n",
  168. pool_id, mem->total_space_allocated);
  169. for (lhdr_ptr = mem->large_list[pool_id]; lhdr_ptr != NULL;
  170. lhdr_ptr = lhdr_ptr->hdr.next) {
  171. fprintf(stderr, " Large chunk used %ld\n",
  172. (long) lhdr_ptr->hdr.bytes_used);
  173. }
  174. for (shdr_ptr = mem->small_list[pool_id]; shdr_ptr != NULL;
  175. shdr_ptr = shdr_ptr->hdr.next) {
  176. fprintf(stderr, " Small chunk used %ld free %ld\n",
  177. (long) shdr_ptr->hdr.bytes_used,
  178. (long) shdr_ptr->hdr.bytes_left);
  179. }
  180. }
  181. #endif /* MEM_STATS */
  182. LOCAL(void)
  183. out_of_memory (j_common_ptr cinfo, int which)
  184. /* Report an out-of-memory error and stop execution */
  185. /* If we compiled MEM_STATS support, report alloc requests before dying */
  186. {
  187. #ifdef MEM_STATS
  188. cinfo->err->trace_level = 2; /* force self_destruct to report stats */
  189. #endif
  190. ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, which);
  191. }
  192. /*
  193. * Allocation of "small" objects.
  194. *
  195. * For these, we use pooled storage. When a new pool must be created,
  196. * we try to get enough space for the current request plus a "slop" factor,
  197. * where the slop will be the amount of leftover space in the new pool.
  198. * The speed vs. space tradeoff is largely determined by the slop values.
  199. * A different slop value is provided for each pool class (lifetime),
  200. * and we also distinguish the first pool of a class from later ones.
  201. * NOTE: the values given work fairly well on both 16- and 32-bit-int
  202. * machines, but may be too small if longs are 64 bits or more.
  203. */
  204. static const size_t first_pool_slop[JPOOL_NUMPOOLS] =
  205. {
  206. 1600, /* first PERMANENT pool */
  207. 16000 /* first IMAGE pool */
  208. };
  209. static const size_t extra_pool_slop[JPOOL_NUMPOOLS] =
  210. {
  211. 0, /* additional PERMANENT pools */
  212. 5000 /* additional IMAGE pools */
  213. };
  214. #define MIN_SLOP 50 /* greater than 0 to avoid futile looping */
  215. METHODDEF(void *)
  216. alloc_small (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
  217. /* Allocate a "small" object */
  218. {
  219. my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  220. small_pool_ptr hdr_ptr, prev_hdr_ptr;
  221. char * data_ptr;
  222. size_t odd_bytes, min_request, slop;
  223. /* Check for unsatisfiable request (do now to ensure no overflow below) */
  224. if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(small_pool_hdr)))
  225. out_of_memory(cinfo, 1); /* request exceeds malloc's ability */
  226. /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
  227. odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
  228. if (odd_bytes > 0)
  229. sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;
  230. /* See if space is available in any existing pool */
  231. if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
  232. ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
  233. prev_hdr_ptr = NULL;
  234. hdr_ptr = mem->small_list[pool_id];
  235. while (hdr_ptr != NULL) {
  236. if (hdr_ptr->hdr.bytes_left >= sizeofobject)
  237. break; /* found pool with enough space */
  238. prev_hdr_ptr = hdr_ptr;
  239. hdr_ptr = hdr_ptr->hdr.next;
  240. }
  241. /* Time to make a new pool? */
  242. if (hdr_ptr == NULL) {
  243. /* min_request is what we need now, slop is what will be leftover */
  244. min_request = sizeofobject + SIZEOF(small_pool_hdr);
  245. if (prev_hdr_ptr == NULL) /* first pool in class? */
  246. slop = first_pool_slop[pool_id];
  247. else
  248. slop = extra_pool_slop[pool_id];
  249. /* Don't ask for more than MAX_ALLOC_CHUNK */
  250. if (slop > (size_t) (MAX_ALLOC_CHUNK-min_request))
  251. slop = (size_t) (MAX_ALLOC_CHUNK-min_request);
  252. /* Try to get space, if fail reduce slop and try again */
  253. for (;;) {
  254. hdr_ptr = (small_pool_ptr) jpeg_get_small(cinfo, min_request + slop);
  255. if (hdr_ptr != NULL)
  256. break;
  257. slop /= 2;
  258. if (slop < MIN_SLOP) /* give up when it gets real small */
  259. out_of_memory(cinfo, 2); /* jpeg_get_small failed */
  260. }
  261. mem->total_space_allocated += min_request + slop;
  262. /* Success, initialize the new pool header and add to end of list */
  263. hdr_ptr->hdr.next = NULL;
  264. hdr_ptr->hdr.bytes_used = 0;
  265. hdr_ptr->hdr.bytes_left = sizeofobject + slop;
  266. if (prev_hdr_ptr == NULL) /* first pool in class? */
  267. mem->small_list[pool_id] = hdr_ptr;
  268. else
  269. prev_hdr_ptr->hdr.next = hdr_ptr;
  270. }
  271. /* OK, allocate the object from the current pool */
  272. data_ptr = (char *) (hdr_ptr + 1); /* point to first data byte in pool */
  273. data_ptr += hdr_ptr->hdr.bytes_used; /* point to place for object */
  274. hdr_ptr->hdr.bytes_used += sizeofobject;
  275. hdr_ptr->hdr.bytes_left -= sizeofobject;
  276. return (void *) data_ptr;
  277. }
  278. /*
  279. * Allocation of "large" objects.
  280. *
  281. * The external semantics of these are the same as "small" objects,
  282. * except that FAR pointers are used on 80x86. However the pool
  283. * management heuristics are quite different. We assume that each
  284. * request is large enough that it may as well be passed directly to
  285. * jpeg_get_large; the pool management just links everything together
  286. * so that we can free it all on demand.
  287. * Note: the major use of "large" objects is in JSAMPARRAY and JBLOCKARRAY
  288. * structures. The routines that create these structures (see below)
  289. * deliberately bunch rows together to ensure a large request size.
  290. */
  291. METHODDEF(void FAR *)
  292. alloc_large (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
  293. /* Allocate a "large" object */
  294. {
  295. my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  296. large_pool_ptr hdr_ptr;
  297. size_t odd_bytes;
  298. /* Check for unsatisfiable request (do now to ensure no overflow below) */
  299. if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)))
  300. out_of_memory(cinfo, 3); /* request exceeds malloc's ability */
  301. /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
  302. odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
  303. if (odd_bytes > 0)
  304. sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;
  305. /* Always make a new pool */
  306. if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
  307. ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
  308. hdr_ptr = (large_pool_ptr) jpeg_get_large(cinfo, sizeofobject +
  309. SIZEOF(large_pool_hdr));
  310. if (hdr_ptr == NULL)
  311. out_of_memory(cinfo, 4); /* jpeg_get_large failed */
  312. mem->total_space_allocated += sizeofobject + SIZEOF(large_pool_hdr);
  313. /* Success, initialize the new pool header and add to list */
  314. hdr_ptr->hdr.next = mem->large_list[pool_id];
  315. /* We maintain space counts in each pool header for statistical purposes,
  316. * even though they are not needed for allocation.
  317. */
  318. hdr_ptr->hdr.bytes_used = sizeofobject;
  319. hdr_ptr->hdr.bytes_left = 0;
  320. mem->large_list[pool_id] = hdr_ptr;
  321. return (void FAR *) (hdr_ptr + 1); /* point to first data byte in pool */
  322. }
  323. /*
  324. * Creation of 2-D sample arrays.
  325. * The pointers are in near heap, the samples themselves in FAR heap.
  326. *
  327. * To minimize allocation overhead and to allow I/O of large contiguous
  328. * blocks, we allocate the sample rows in groups of as many rows as possible
  329. * without exceeding MAX_ALLOC_CHUNK total bytes per allocation request.
  330. * NB: the virtual array control routines, later in this file, know about
  331. * this chunking of rows. The rowsperchunk value is left in the mem manager
  332. * object so that it can be saved away if this sarray is the workspace for
  333. * a virtual array.
  334. */
  335. METHODDEF(JSAMPARRAY)
  336. alloc_sarray (j_common_ptr cinfo, int pool_id,
  337. JDIMENSION samplesperrow, JDIMENSION numrows)
  338. /* Allocate a 2-D sample array */
  339. {
  340. my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  341. JSAMPARRAY result;
  342. JSAMPROW workspace;
  343. JDIMENSION rowsperchunk, currow, i;
  344. long ltemp;
  345. /* Calculate max # of rows allowed in one allocation chunk */
  346. ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
  347. ((long) samplesperrow * SIZEOF(JSAMPLE));
  348. if (ltemp <= 0)
  349. ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  350. if (ltemp < (long) numrows)
  351. rowsperchunk = (JDIMENSION) ltemp;
  352. else
  353. rowsperchunk = numrows;
  354. mem->last_rowsperchunk = rowsperchunk;
  355. /* Get space for row pointers (small object) */
  356. result = (JSAMPARRAY) alloc_small(cinfo, pool_id,
  357. (size_t) (numrows * SIZEOF(JSAMPROW)));
  358. /* Get the rows themselves (large objects) */
  359. currow = 0;
  360. while (currow < numrows) {
  361. rowsperchunk = MIN(rowsperchunk, numrows - currow);
  362. workspace = (JSAMPROW) alloc_large(cinfo, pool_id,
  363. (size_t) ((size_t) rowsperchunk * (size_t) samplesperrow
  364. * SIZEOF(JSAMPLE)));
  365. for (i = rowsperchunk; i > 0; i--) {
  366. result[currow++] = workspace;
  367. workspace += samplesperrow;
  368. }
  369. }
  370. return result;
  371. }
  372. /*
  373. * Creation of 2-D coefficient-block arrays.
  374. * This is essentially the same as the code for sample arrays, above.
  375. */
  376. METHODDEF(JBLOCKARRAY)
  377. alloc_barray (j_common_ptr cinfo, int pool_id,
  378. JDIMENSION blocksperrow, JDIMENSION numrows)
  379. /* Allocate a 2-D coefficient-block array */
  380. {
  381. my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  382. JBLOCKARRAY result;
  383. JBLOCKROW workspace;
  384. JDIMENSION rowsperchunk, currow, i;
  385. long ltemp;
  386. /* Calculate max # of rows allowed in one allocation chunk */
  387. ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
  388. ((long) blocksperrow * SIZEOF(JBLOCK));
  389. if (ltemp <= 0)
  390. ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  391. if (ltemp < (long) numrows)
  392. rowsperchunk = (JDIMENSION) ltemp;
  393. else
  394. rowsperchunk = numrows;
  395. mem->last_rowsperchunk = rowsperchunk;
  396. /* Get space for row pointers (small object) */
  397. result = (JBLOCKARRAY) alloc_small(cinfo, pool_id,
  398. (size_t) (numrows * SIZEOF(JBLOCKROW)));
  399. /* Get the rows themselves (large objects) */
  400. currow = 0;
  401. while (currow < numrows) {
  402. rowsperchunk = MIN(rowsperchunk, numrows - currow);
  403. workspace = (JBLOCKROW) alloc_large(cinfo, pool_id,
  404. (size_t) ((size_t) rowsperchunk * (size_t) blocksperrow
  405. * SIZEOF(JBLOCK)));
  406. for (i = rowsperchunk; i > 0; i--) {
  407. result[currow++] = workspace;
  408. workspace += blocksperrow;
  409. }
  410. }
  411. return result;
  412. }
  413. /*
  414. * About virtual array management:
  415. *
  416. * The above "normal" array routines are only used to allocate strip buffers
  417. * (as wide as the image, but just a few rows high). Full-image-sized buffers
  418. * are handled as "virtual" arrays. The array is still accessed a strip at a
  419. * time, but the memory manager must save the whole array for repeated
  420. * accesses. The intended implementation is that there is a strip buffer in
  421. * memory (as high as is possible given the desired memory limit), plus a
  422. * backing file that holds the rest of the array.
  423. *
  424. * The request_virt_array routines are told the total size of the image and
  425. * the maximum number of rows that will be accessed at once. The in-memory
  426. * buffer must be at least as large as the maxaccess value.
  427. *
  428. * The request routines create control blocks but not the in-memory buffers.
  429. * That is postponed until realize_virt_arrays is called. At that time the
  430. * total amount of space needed is known (approximately, anyway), so free
  431. * memory can be divided up fairly.
  432. *
  433. * The access_virt_array routines are responsible for making a specific strip
  434. * area accessible (after reading or writing the backing file, if necessary).
  435. * Note that the access routines are told whether the caller intends to modify
  436. * the accessed strip; during a read-only pass this saves having to rewrite
  437. * data to disk. The access routines are also responsible for pre-zeroing
  438. * any newly accessed rows, if pre-zeroing was requested.
  439. *
  440. * In current usage, the access requests are usually for nonoverlapping
  441. * strips; that is, successive access start_row numbers differ by exactly
  442. * num_rows = maxaccess. This means we can get good performance with simple
  443. * buffer dump/reload logic, by making the in-memory buffer be a multiple
  444. * of the access height; then there will never be accesses across bufferload
  445. * boundaries. The code will still work with overlapping access requests,
  446. * but it doesn't handle bufferload overlaps very efficiently.
  447. */
  448. METHODDEF(jvirt_sarray_ptr)
  449. request_virt_sarray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
  450. JDIMENSION samplesperrow, JDIMENSION numrows,
  451. JDIMENSION maxaccess)
  452. /* Request a virtual 2-D sample array */
  453. {
  454. my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  455. jvirt_sarray_ptr result;
  456. /* Only IMAGE-lifetime virtual arrays are currently supported */
  457. if (pool_id != JPOOL_IMAGE)
  458. ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
  459. /* get control block */
  460. result = (jvirt_sarray_ptr) alloc_small(cinfo, pool_id,
  461. SIZEOF(struct jvirt_sarray_control));
  462. result->mem_buffer = NULL; /* marks array not yet realized */
  463. result->rows_in_array = numrows;
  464. result->samplesperrow = samplesperrow;
  465. result->maxaccess = maxaccess;
  466. result->pre_zero = pre_zero;
  467. result->b_s_open = FALSE; /* no associated backing-store object */
  468. result->next = mem->virt_sarray_list; /* add to list of virtual arrays */
  469. mem->virt_sarray_list = result;
  470. return result;
  471. }
  472. METHODDEF(jvirt_barray_ptr)
  473. request_virt_barray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
  474. JDIMENSION blocksperrow, JDIMENSION numrows,
  475. JDIMENSION maxaccess)
  476. /* Request a virtual 2-D coefficient-block array */
  477. {
  478. my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  479. jvirt_barray_ptr result;
  480. /* Only IMAGE-lifetime virtual arrays are currently supported */
  481. if (pool_id != JPOOL_IMAGE)
  482. ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
  483. /* get control block */
  484. result = (jvirt_barray_ptr) alloc_small(cinfo, pool_id,
  485. SIZEOF(struct jvirt_barray_control));
  486. result->mem_buffer = NULL; /* marks array not yet realized */
  487. result->rows_in_array = numrows;
  488. result->blocksperrow = blocksperrow;
  489. result->maxaccess = maxaccess;
  490. result->pre_zero = pre_zero;
  491. result->b_s_open = FALSE; /* no associated backing-store object */
  492. result->next = mem->virt_barray_list; /* add to list of virtual arrays */
  493. mem->virt_barray_list = result;
  494. return result;
  495. }
  496. METHODDEF(void)
  497. realize_virt_arrays (j_common_ptr cinfo)
  498. /* Allocate the in-memory buffers for any unrealized virtual arrays */
  499. {
  500. my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  501. long space_per_minheight, maximum_space, avail_mem;
  502. long minheights, max_minheights;
  503. jvirt_sarray_ptr sptr;
  504. jvirt_barray_ptr bptr;
  505. /* Compute the minimum space needed (maxaccess rows in each buffer)
  506. * and the maximum space needed (full image height in each buffer).
  507. * These may be of use to the system-dependent jpeg_mem_available routine.
  508. */
  509. space_per_minheight = 0;
  510. maximum_space = 0;
  511. for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
  512. if (sptr->mem_buffer == NULL) { /* if not realized yet */
  513. space_per_minheight += (long) sptr->maxaccess *
  514. (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
  515. maximum_space += (long) sptr->rows_in_array *
  516. (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
  517. }
  518. }
  519. for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
  520. if (bptr->mem_buffer == NULL) { /* if not realized yet */
  521. space_per_minheight += (long) bptr->maxaccess *
  522. (long) bptr->blocksperrow * SIZEOF(JBLOCK);
  523. maximum_space += (long) bptr->rows_in_array *
  524. (long) bptr->blocksperrow * SIZEOF(JBLOCK);
  525. }
  526. }
  527. if (space_per_minheight <= 0)
  528. return; /* no unrealized arrays, no work */
  529. /* Determine amount of memory to actually use; this is system-dependent. */
  530. avail_mem = jpeg_mem_available(cinfo, space_per_minheight, maximum_space,
  531. mem->total_space_allocated);
  532. /* If the maximum space needed is available, make all the buffers full
  533. * height; otherwise parcel it out with the same number of minheights
  534. * in each buffer.
  535. */
  536. if (avail_mem >= maximum_space)
  537. max_minheights = 1000000000L;
  538. else {
  539. max_minheights = avail_mem / space_per_minheight;
  540. /* If there doesn't seem to be enough space, try to get the minimum
  541. * anyway. This allows a "stub" implementation of jpeg_mem_available().
  542. */
  543. if (max_minheights <= 0)
  544. max_minheights = 1;
  545. }
  546. /* Allocate the in-memory buffers and initialize backing store as needed. */
  547. for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
  548. if (sptr->mem_buffer == NULL) { /* if not realized yet */
  549. minheights = ((long) sptr->rows_in_array - 1L) / sptr->maxaccess + 1L;
  550. if (minheights <= max_minheights) {
  551. /* This buffer fits in memory */
  552. sptr->rows_in_mem = sptr->rows_in_array;
  553. } else {
  554. /* It doesn't fit in memory, create backing store. */
  555. sptr->rows_in_mem = (JDIMENSION) (max_minheights * sptr->maxaccess);
  556. jpeg_open_backing_store(cinfo, & sptr->b_s_info,
  557. (long) sptr->rows_in_array *
  558. (long) sptr->samplesperrow *
  559. (long) SIZEOF(JSAMPLE));
  560. sptr->b_s_open = TRUE;
  561. }
  562. sptr->mem_buffer = alloc_sarray(cinfo, JPOOL_IMAGE,
  563. sptr->samplesperrow, sptr->rows_in_mem);
  564. sptr->rowsperchunk = mem->last_rowsperchunk;
  565. sptr->cur_start_row = 0;
  566. sptr->first_undef_row = 0;
  567. sptr->dirty = FALSE;
  568. }
  569. }
  570. for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
  571. if (bptr->mem_buffer == NULL) { /* if not realized yet */
  572. minheights = ((long) bptr->rows_in_array - 1L) / bptr->maxaccess + 1L;
  573. if (minheights <= max_minheights) {
  574. /* This buffer fits in memory */
  575. bptr->rows_in_mem = bptr->rows_in_array;
  576. } else {
  577. /* It doesn't fit in memory, create backing store. */
  578. bptr->rows_in_mem = (JDIMENSION) (max_minheights * bptr->maxaccess);
  579. jpeg_open_backing_store(cinfo, & bptr->b_s_info,
  580. (long) bptr->rows_in_array *
  581. (long) bptr->blocksperrow *
  582. (long) SIZEOF(JBLOCK));
  583. bptr->b_s_open = TRUE;
  584. }
  585. bptr->mem_buffer = alloc_barray(cinfo, JPOOL_IMAGE,
  586. bptr->blocksperrow, bptr->rows_in_mem);
  587. bptr->rowsperchunk = mem->last_rowsperchunk;
  588. bptr->cur_start_row = 0;
  589. bptr->first_undef_row = 0;
  590. bptr->dirty = FALSE;
  591. }
  592. }
  593. }
  594. LOCAL(void)
  595. do_sarray_io (j_common_ptr cinfo, jvirt_sarray_ptr ptr, boolean writing)
  596. /* Do backing store read or write of a virtual sample array */
  597. {
  598. long bytesperrow, file_offset, byte_count, rows, thisrow, i;
  599. bytesperrow = (long) ptr->samplesperrow * SIZEOF(JSAMPLE);
  600. file_offset = ptr->cur_start_row * bytesperrow;
  601. /* Loop to read or write each allocation chunk in mem_buffer */
  602. for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
  603. /* One chunk, but check for short chunk at end of buffer */
  604. rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
  605. /* Transfer no more than is currently defined */
  606. thisrow = (long) ptr->cur_start_row + i;
  607. rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
  608. /* Transfer no more than fits in file */
  609. rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
  610. if (rows <= 0) /* this chunk might be past end of file! */
  611. break;
  612. byte_count = rows * bytesperrow;
  613. if (writing)
  614. (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
  615. (void FAR *) ptr->mem_buffer[i],
  616. file_offset, byte_count);
  617. else
  618. (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
  619. (void FAR *) ptr->mem_buffer[i],
  620. file_offset, byte_count);
  621. file_offset += byte_count;
  622. }
  623. }
  624. LOCAL(void)
  625. do_barray_io (j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing)
  626. /* Do backing store read or write of a virtual coefficient-block array */
  627. {
  628. long bytesperrow, file_offset, byte_count, rows, thisrow, i;
  629. bytesperrow = (long) ptr->blocksperrow * SIZEOF(JBLOCK);
  630. file_offset = ptr->cur_start_row * bytesperrow;
  631. /* Loop to read or write each allocation chunk in mem_buffer */
  632. for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
  633. /* One chunk, but check for short chunk at end of buffer */
  634. rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
  635. /* Transfer no more than is currently defined */
  636. thisrow = (long) ptr->cur_start_row + i;
  637. rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
  638. /* Transfer no more than fits in file */
  639. rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
  640. if (rows <= 0) /* this chunk might be past end of file! */
  641. break;
  642. byte_count = rows * bytesperrow;
  643. if (writing)
  644. (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
  645. (void FAR *) ptr->mem_buffer[i],
  646. file_offset, byte_count);
  647. else
  648. (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
  649. (void FAR *) ptr->mem_buffer[i],
  650. file_offset, byte_count);
  651. file_offset += byte_count;
  652. }
  653. }
  654. METHODDEF(JSAMPARRAY)
  655. access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr,
  656. JDIMENSION start_row, JDIMENSION num_rows,
  657. boolean writable)
  658. /* Access the part of a virtual sample array starting at start_row */
  659. /* and extending for num_rows rows. writable is true if */
  660. /* caller intends to modify the accessed area. */
  661. {
  662. JDIMENSION end_row = start_row + num_rows;
  663. JDIMENSION undef_row;
  664. /* debugging check */
  665. if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
  666. ptr->mem_buffer == NULL)
  667. ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
  668. /* Make the desired part of the virtual array accessible */
  669. if (start_row < ptr->cur_start_row ||
  670. end_row > ptr->cur_start_row+ptr->rows_in_mem) {
  671. if (! ptr->b_s_open)
  672. ERREXIT(cinfo, JERR_VIRTUAL_BUG);
  673. /* Flush old buffer contents if necessary */
  674. if (ptr->dirty) {
  675. do_sarray_io(cinfo, ptr, TRUE);
  676. ptr->dirty = FALSE;
  677. }
  678. /* Decide what part of virtual array to access.
  679. * Algorithm: if target address > current window, assume forward scan,
  680. * load starting at target address. If target address < current window,
  681. * assume backward scan, load so that target area is top of window.
  682. * Note that when switching from forward write to forward read, will have
  683. * start_row = 0, so the limiting case applies and we load from 0 anyway.
  684. */
  685. if (start_row > ptr->cur_start_row) {
  686. ptr->cur_start_row = start_row;
  687. } else {
  688. /* use long arithmetic here to avoid overflow & unsigned problems */
  689. long ltemp;
  690. ltemp = (long) end_row - (long) ptr->rows_in_mem;
  691. if (ltemp < 0)
  692. ltemp = 0; /* don't fall off front end of file */
  693. ptr->cur_start_row = (JDIMENSION) ltemp;
  694. }
  695. /* Read in the selected part of the array.
  696. * During the initial write pass, we will do no actual read
  697. * because the selected part is all undefined.
  698. */
  699. do_sarray_io(cinfo, ptr, FALSE);
  700. }
  701. /* Ensure the accessed part of the array is defined; prezero if needed.
  702. * To improve locality of access, we only prezero the part of the array
  703. * that the caller is about to access, not the entire in-memory array.
  704. */
  705. if (ptr->first_undef_row < end_row) {
  706. if (ptr->first_undef_row < start_row) {
  707. if (writable) /* writer skipped over a section of array */
  708. ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
  709. undef_row = start_row; /* but reader is allowed to read ahead */
  710. } else {
  711. undef_row = ptr->first_undef_row;
  712. }
  713. if (writable)
  714. ptr->first_undef_row = end_row;
  715. if (ptr->pre_zero) {
  716. size_t bytesperrow = (size_t) ptr->samplesperrow * SIZEOF(JSAMPLE);
  717. undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
  718. end_row -= ptr->cur_start_row;
  719. while (undef_row < end_row) {
  720. jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
  721. undef_row++;
  722. }
  723. } else {
  724. if (! writable) /* reader looking at undefined data */
  725. ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
  726. }
  727. }
  728. /* Flag the buffer dirty if caller will write in it */
  729. if (writable)
  730. ptr->dirty = TRUE;
  731. /* Return address of proper part of the buffer */
  732. return ptr->mem_buffer + (start_row - ptr->cur_start_row);
  733. }
  734. METHODDEF(JBLOCKARRAY)
  735. access_virt_barray (j_common_ptr cinfo, jvirt_barray_ptr ptr,
  736. JDIMENSION start_row, JDIMENSION num_rows,
  737. boolean writable)
  738. /* Access the part of a virtual block array starting at start_row */
  739. /* and extending for num_rows rows. writable is true if */
  740. /* caller intends to modify the accessed area. */
  741. {
  742. JDIMENSION end_row = start_row + num_rows;
  743. JDIMENSION undef_row;
  744. /* debugging check */
  745. if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
  746. ptr->mem_buffer == NULL)
  747. ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
  748. /* Make the desired part of the virtual array accessible */
  749. if (start_row < ptr->cur_start_row ||
  750. end_row > ptr->cur_start_row+ptr->rows_in_mem) {
  751. if (! ptr->b_s_open)
  752. ERREXIT(cinfo, JERR_VIRTUAL_BUG);
  753. /* Flush old buffer contents if necessary */
  754. if (ptr->dirty) {
  755. do_barray_io(cinfo, ptr, TRUE);
  756. ptr->dirty = FALSE;
  757. }
  758. /* Decide what part of virtual array to access.
  759. * Algorithm: if target address > current window, assume forward scan,
  760. * load starting at target address. If target address < current window,
  761. * assume backward scan, load so that target area is top of window.
  762. * Note that when switching from forward write to forward read, will have
  763. * start_row = 0, so the limiting case applies and we load from 0 anyway.
  764. */
  765. if (start_row > ptr->cur_start_row) {
  766. ptr->cur_start_row = start_row;
  767. } else {
  768. /* use long arithmetic here to avoid overflow & unsigned problems */
  769. long ltemp;
  770. ltemp = (long) end_row - (long) ptr->rows_in_mem;
  771. if (ltemp < 0)
  772. ltemp = 0; /* don't fall off front end of file */
  773. ptr->cur_start_row = (JDIMENSION) ltemp;
  774. }
  775. /* Read in the selected part of the array.
  776. * During the initial write pass, we will do no actual read
  777. * because the selected part is all undefined.
  778. */
  779. do_barray_io(cinfo, ptr, FALSE);
  780. }
  781. /* Ensure the accessed part of the array is defined; prezero if needed.
  782. * To improve locality of access, we only prezero the part of the array
  783. * that the caller is about to access, not the entire in-memory array.
  784. */
  785. if (ptr->first_undef_row < end_row) {
  786. if (ptr->first_undef_row < start_row) {
  787. if (writable) /* writer skipped over a section of array */
  788. ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
  789. undef_row = start_row; /* but reader is allowed to read ahead */
  790. } else {
  791. undef_row = ptr->first_undef_row;
  792. }
  793. if (writable)
  794. ptr->first_undef_row = end_row;
  795. if (ptr->pre_zero) {
  796. size_t bytesperrow = (size_t) ptr->blocksperrow * SIZEOF(JBLOCK);
  797. undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
  798. end_row -= ptr->cur_start_row;
  799. while (undef_row < end_row) {
  800. jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
  801. undef_row++;
  802. }
  803. } else {
  804. if (! writable) /* reader looking at undefined data */
  805. ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
  806. }
  807. }
  808. /* Flag the buffer dirty if caller will write in it */
  809. if (writable)
  810. ptr->dirty = TRUE;
  811. /* Return address of proper part of the buffer */
  812. return ptr->mem_buffer + (start_row - ptr->cur_start_row);
  813. }
  814. /*
  815. * Release all objects belonging to a specified pool.
  816. */
  817. METHODDEF(void)
  818. free_pool (j_common_ptr cinfo, int pool_id)
  819. {
  820. my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  821. small_pool_ptr shdr_ptr;
  822. large_pool_ptr lhdr_ptr;
  823. size_t space_freed;
  824. if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
  825. ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
  826. #ifdef MEM_STATS
  827. if (cinfo->err->trace_level > 1)
  828. print_mem_stats(cinfo, pool_id); /* print pool's memory usage statistics */
  829. #endif
  830. /* If freeing IMAGE pool, close any virtual arrays first */
  831. if (pool_id == JPOOL_IMAGE) {
  832. jvirt_sarray_ptr sptr;
  833. jvirt_barray_ptr bptr;
  834. for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
  835. if (sptr->b_s_open) { /* there may be no backing store */
  836. sptr->b_s_open = FALSE; /* prevent recursive close if error */
  837. (*sptr->b_s_info.close_backing_store) (cinfo, & sptr->b_s_info);
  838. }
  839. }
  840. mem->virt_sarray_list = NULL;
  841. for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
  842. if (bptr->b_s_open) { /* there may be no backing store */
  843. bptr->b_s_open = FALSE; /* prevent recursive close if error */
  844. (*bptr->b_s_info.close_backing_store) (cinfo, & bptr->b_s_info);
  845. }
  846. }
  847. mem->virt_barray_list = NULL;
  848. }
  849. /* Release large objects */
  850. lhdr_ptr = mem->large_list[pool_id];
  851. mem->large_list[pool_id] = NULL;
  852. while (lhdr_ptr != NULL) {
  853. large_pool_ptr next_lhdr_ptr = lhdr_ptr->hdr.next;
  854. space_freed = lhdr_ptr->hdr.bytes_used +
  855. lhdr_ptr->hdr.bytes_left +
  856. SIZEOF(large_pool_hdr);
  857. jpeg_free_large(cinfo, (void FAR *) lhdr_ptr, space_freed);
  858. mem->total_space_allocated -= space_freed;
  859. lhdr_ptr = next_lhdr_ptr;
  860. }
  861. /* Release small objects */
  862. shdr_ptr = mem->small_list[pool_id];
  863. mem->small_list[pool_id] = NULL;
  864. while (shdr_ptr != NULL) {
  865. small_pool_ptr next_shdr_ptr = shdr_ptr->hdr.next;
  866. space_freed = shdr_ptr->hdr.bytes_used +
  867. shdr_ptr->hdr.bytes_left +
  868. SIZEOF(small_pool_hdr);
  869. jpeg_free_small(cinfo, (void *) shdr_ptr, space_freed);
  870. mem->total_space_allocated -= space_freed;
  871. shdr_ptr = next_shdr_ptr;
  872. }
  873. }
  874. /*
  875. * Close up shop entirely.
  876. * Note that this cannot be called unless cinfo->mem is non-NULL.
  877. */
  878. METHODDEF(void)
  879. self_destruct (j_common_ptr cinfo)
  880. {
  881. int pool;
  882. /* Close all backing store, release all memory.
  883. * Releasing pools in reverse order might help avoid fragmentation
  884. * with some (brain-damaged) malloc libraries.
  885. */
  886. for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
  887. free_pool(cinfo, pool);
  888. }
  889. /* Release the memory manager control block too. */
  890. jpeg_free_small(cinfo, (void *) cinfo->mem, SIZEOF(my_memory_mgr));
  891. cinfo->mem = NULL; /* ensures I will be called only once */
  892. jpeg_mem_term(cinfo); /* system-dependent cleanup */
  893. }
  894. /*
  895. * Memory manager initialization.
  896. * When this is called, only the error manager pointer is valid in cinfo!
  897. */
  898. GLOBAL(void)
  899. jinit_memory_mgr (j_common_ptr cinfo)
  900. {
  901. my_mem_ptr mem;
  902. long max_to_use;
  903. int pool;
  904. size_t test_mac;
  905. cinfo->mem = NULL; /* for safety if init fails */
  906. /* Check for configuration errors.
  907. * SIZEOF(ALIGN_TYPE) should be a power of 2; otherwise, it probably
  908. * doesn't reflect any real hardware alignment requirement.
  909. * The test is a little tricky: for X>0, X and X-1 have no one-bits
  910. * in common if and only if X is a power of 2, ie has only one one-bit.
  911. * Some compilers may give an "unreachable code" warning here; ignore it.
  912. */
  913. if ((SIZEOF(ALIGN_TYPE) & (SIZEOF(ALIGN_TYPE)-1)) != 0)
  914. ERREXIT(cinfo, JERR_BAD_ALIGN_TYPE);
  915. /* MAX_ALLOC_CHUNK must be representable as type size_t, and must be
  916. * a multiple of SIZEOF(ALIGN_TYPE).
  917. * Again, an "unreachable code" warning may be ignored here.
  918. * But a "constant too large" warning means you need to fix MAX_ALLOC_CHUNK.
  919. */
  920. test_mac = (size_t) MAX_ALLOC_CHUNK;
  921. if ((long) test_mac != MAX_ALLOC_CHUNK ||
  922. (MAX_ALLOC_CHUNK % SIZEOF(ALIGN_TYPE)) != 0)
  923. ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
  924. max_to_use = jpeg_mem_init(cinfo); /* system-dependent initialization */
  925. /* Attempt to allocate memory manager's control block */
  926. mem = (my_mem_ptr) jpeg_get_small(cinfo, SIZEOF(my_memory_mgr));
  927. if (mem == NULL) {
  928. jpeg_mem_term(cinfo); /* system-dependent cleanup */
  929. ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 0);
  930. }
  931. /* OK, fill in the method pointers */
  932. mem->pub.alloc_small = alloc_small;
  933. mem->pub.alloc_large = alloc_large;
  934. mem->pub.alloc_sarray = alloc_sarray;
  935. mem->pub.alloc_barray = alloc_barray;
  936. mem->pub.request_virt_sarray = request_virt_sarray;
  937. mem->pub.request_virt_barray = request_virt_barray;
  938. mem->pub.realize_virt_arrays = realize_virt_arrays;
  939. mem->pub.access_virt_sarray = access_virt_sarray;
  940. mem->pub.access_virt_barray = access_virt_barray;
  941. mem->pub.free_pool = free_pool;
  942. mem->pub.self_destruct = self_destruct;
  943. /* Make MAX_ALLOC_CHUNK accessible to other modules */
  944. mem->pub.max_alloc_chunk = MAX_ALLOC_CHUNK;
  945. /* Initialize working state */
  946. mem->pub.max_memory_to_use = max_to_use;
  947. for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
  948. mem->small_list[pool] = NULL;
  949. mem->large_list[pool] = NULL;
  950. }
  951. mem->virt_sarray_list = NULL;
  952. mem->virt_barray_list = NULL;
  953. mem->total_space_allocated = SIZEOF(my_memory_mgr);
  954. /* Declare ourselves open for business */
  955. cinfo->mem = & mem->pub;
  956. /* Check for an environment variable JPEGMEM; if found, override the
  957. * default max_memory setting from jpeg_mem_init. Note that the
  958. * surrounding application may again override this value.
  959. * If your system doesn't support getenv(), define NO_GETENV to disable
  960. * this feature.
  961. */
  962. #ifndef NO_GETENV
  963. { char * memenv;
  964. if ((memenv = getenv("JPEGMEM")) != NULL) {
  965. char ch = 'x';
  966. if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) {
  967. if (ch == 'm' || ch == 'M')
  968. max_to_use *= 1000L;
  969. mem->pub.max_memory_to_use = max_to_use * 1000L;
  970. }
  971. }
  972. }
  973. #endif
  974. }