Source code of Windows XP (NT5)
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.

1118 lines
40 KiB

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