Repo for the search and displace ingest module that takes odf, docx and pdf and transforms it into .md to be used with search and displace operations
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.

1283 lines
44 KiB

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