jmemmgr.cpp 41 KB

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