jmemdos.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. Unit JmemDos;
  2. { This file provides an MS-DOS-compatible implementation of the system-
  3. dependent portion of the JPEG memory manager. Temporary data can be
  4. stored in extended or expanded memory as well as in regular DOS files.
  5. If you use this file, you must be sure that NEED_FAR_POINTERS is defined
  6. if you compile in a small-data memory model; it should NOT be defined if
  7. you use a large-data memory model. This file is not recommended if you
  8. are using a flat-memory-space 386 environment such as DJGCC or Watcom C.
  9. Also, this code will NOT work if struct fields are aligned on greater than
  10. 2-byte boundaries.
  11. Based on code contributed by Ge' Weijers. }
  12. { Original: jmemdos.c; Copyright (C) 1992-1996, Thomas G. Lane. }
  13. interface
  14. {$I jconfig.inc}
  15. uses
  16. jmorecfg,
  17. jpeglib;
  18. { If you have both extended and expanded memory, you may want to change the
  19. order in which they are tried in jopen_backing_store. On a 286 machine
  20. expanded memory is usually faster, since extended memory access involves
  21. an expensive protected-mode-and-back switch. On 386 and better, extended
  22. memory is usually faster. As distributed, the code tries extended memory
  23. first (what? not everyone has a 386? :-).
  24. You can disable use of extended/expanded memory entirely by altering these
  25. definitions or overriding them from the Makefile (eg, -DEMS_SUPPORTED=0).}
  26. {GLOBAL}
  27. procedure jpeg_open_backing_store (cinfo : j_common_ptr;
  28. info : backing_store_ptr;
  29. total_bytes_needed : long);
  30. { These routines take care of any system-dependent initialization and
  31. cleanup required. }
  32. {GLOBAL}
  33. function jpeg_mem_init (cinfo : j_common_ptr) : long;
  34. {GLOBAL}
  35. procedure jpeg_mem_term (cinfo : j_common_ptr);
  36. { These two functions are used to allocate and release small chunks of
  37. memory. (Typically the total amount requested through jpeg_get_small is
  38. no more than 20K or so; this will be requested in chunks of a few K each.)
  39. Behavior should be the same as for the standard library functions malloc
  40. and free; in particular, jpeg_get_small must return NIL on failure.
  41. On most systems, these ARE malloc and free. jpeg_free_small is passed the
  42. size of the object being freed, just in case it's needed.
  43. On an 80x86 machine using small-data memory model, these manage near heap. }
  44. { Near-memory allocation and freeing are controlled by the regular library
  45. routines malloc() and free(). }
  46. {GLOBAL}
  47. function jpeg_get_small (cinfo : j_common_ptr;
  48. sizeofobject : size_t) : pointer;
  49. {GLOBAL}
  50. {object is a reserved word in Borland Pascal }
  51. procedure jpeg_free_small (cinfo : j_common_ptr;
  52. an_object : pointer;
  53. sizeofobject : size_t);
  54. { These two functions are used to allocate and release large chunks of
  55. memory (up to the total free space designated by jpeg_mem_available).
  56. The interface is the same as above, except that on an 80x86 machine,
  57. far pointers are used. On most other machines these are identical to
  58. the jpeg_get/free_small routines; but we keep them separate anyway,
  59. in case a different allocation strategy is desirable for large chunks. }
  60. { "Large" objects are allocated in far memory, if possible }
  61. {GLOBAL}
  62. function jpeg_get_large (cinfo : j_common_ptr;
  63. sizeofobject : size_t) : voidp; {far}
  64. {GLOBAL}
  65. procedure jpeg_free_large (cinfo : j_common_ptr;
  66. {var?} an_object : voidp; {FAR}
  67. sizeofobject : size_t);
  68. { This routine computes the total memory space available for allocation.
  69. It's impossible to do this in a portable way; our current solution is
  70. to make the user tell us (with a default value set at compile time).
  71. If you can actually get the available space, it's a good idea to subtract
  72. a slop factor of 5% or so. }
  73. {GLOBAL}
  74. function jpeg_mem_available (cinfo : j_common_ptr;
  75. min_bytes_needed : long;
  76. max_bytes_needed : long;
  77. already_allocated : long) : long;
  78. { The macro MAX_ALLOC_CHUNK designates the maximum number of bytes that may
  79. be requested in a single call to jpeg_get_large (and jpeg_get_small for that
  80. matter, but that case should never come into play). This macro is needed
  81. to model the 64Kb-segment-size limit of far addressing on 80x86 machines.
  82. On those machines, we expect that jconfig.h will provide a proper value.
  83. On machines with 32-bit flat address spaces, any large constant may be used.
  84. NB: jmemmgr.c expects that MAX_ALLOC_CHUNK will be representable as type
  85. size_t and will be a multiple of sizeof(align_type). }
  86. {$ifdef USE_MSDOS_MEMMGR} { Define this if you use jmemdos.c }
  87. const
  88. MAX_ALLOC_CHUNK = long(32752); {65520} { Maximum request to malloc() }
  89. { MAX_ALLOC_CHUNK should be less than 64K. }
  90. {$else}
  91. const
  92. MAX_ALLOC_CHUNK = long(1000000000);
  93. {$endif}
  94. implementation
  95. uses
  96. dos,
  97. jmemdosa,
  98. jdeferr,
  99. jerror;
  100. { Selection of a file name for a temporary file.
  101. This is highly system-dependent, and you may want to customize it. }
  102. var
  103. next_file_num : int; { to distinguish among several temp files }
  104. {LOCAL}
  105. procedure select_file_name (var fname : TEMP_STRING);
  106. var
  107. env : string;
  108. suffix,
  109. prefix : TEMP_STRING;
  110. tfile : FILE;
  111. l : byte;
  112. begin
  113. { Keep generating file names till we find one that's not in use }
  114. while TRUE do
  115. begin
  116. { Get temp directory name from environment TMP or TEMP variable;
  117. if none, use "." }
  118. env := getenv('TMP');
  119. if (env = '') then
  120. begin
  121. env := getenv('TEMP');
  122. if (env = '') then { null string means "." }
  123. env := '.';
  124. end;
  125. prefix := env; { copy name to fname }
  126. { length(fname) > 0 !! }
  127. if (prefix[length(prefix)] <> '\')
  128. and (prefix[length(prefix)] <> '/') then
  129. prefix := prefix + '\'; { append backslash if not in env variable }
  130. { Append a suitable file name }
  131. Inc(next_file_num); { advance counter }
  132. Str(next_file_num, suffix);
  133. for l := Length(suffix)+1 to 3 do
  134. suffix := '0' + suffix;
  135. fname := prefix + 'JPG' + suffix + '.TMP';
  136. { Probe to see if file name is already in use }
  137. system.assign(tfile, fname);
  138. {$ifdef IoCheck} {$I-} {$endif}
  139. system.reset(tfile, 1);
  140. {$ifdef IoCheck} {$I+} {$endif}
  141. if (IOresult <> 0) then
  142. begin
  143. fname := fname + #0;
  144. break;
  145. end;
  146. system.close(tfile); { oops, it's there; close tfile & try again }
  147. end;
  148. end;
  149. { These two functions are used to allocate and release small chunks of
  150. memory. (Typically the total amount requested through jpeg_get_small is
  151. no more than 20K or so; this will be requested in chunks of a few K each.)
  152. Behavior should be the same as for the standard library functions malloc
  153. and free; in particular, jpeg_get_small must return NIL on failure.
  154. On most systems, these ARE malloc and free. jpeg_free_small is passed the
  155. size of the object being freed, just in case it's needed.
  156. On an 80x86 machine using small-data memory model, these manage near heap. }
  157. { Near-memory allocation and freeing are controlled by the regular library
  158. routines malloc() and free(). }
  159. {GLOBAL}
  160. function jpeg_get_small (cinfo : j_common_ptr;
  161. sizeofobject : size_t) : pointer;
  162. var
  163. p : pointer;
  164. begin
  165. getmem(p, sizeofobject);
  166. jpeg_get_small := p;
  167. end;
  168. {GLOBAL}
  169. {object is a reserved word in Borland Pascal }
  170. procedure jpeg_free_small (cinfo : j_common_ptr;
  171. an_object : pointer;
  172. sizeofobject : size_t);
  173. begin
  174. freemem(an_object, sizeofobject);
  175. end;
  176. { These two functions are used to allocate and release large chunks of
  177. memory (up to the total free space designated by jpeg_mem_available).
  178. The interface is the same as above, except that on an 80x86 machine,
  179. far pointers are used. On most other machines these are identical to
  180. the jpeg_get/free_small routines; but we keep them separate anyway,
  181. in case a different allocation strategy is desirable for large chunks. }
  182. {GLOBAL}
  183. function jpeg_get_large (cinfo : j_common_ptr;
  184. sizeofobject : size_t) : voidp; {far}
  185. var
  186. p : voidp; {FAR}
  187. begin
  188. {far_malloc;}
  189. getmem(p, sizeofobject);
  190. jpeg_get_large := p;
  191. end;
  192. {GLOBAL}
  193. procedure jpeg_free_large (cinfo : j_common_ptr;
  194. {var?} an_object : voidp; {FAR}
  195. sizeofobject : size_t);
  196. begin
  197. {far_free(an_object);}
  198. FreeMem(an_object, sizeofobject);
  199. end;
  200. { This routine computes the total space still available for allocation by
  201. jpeg_get_large. If more space than this is needed, backing store will be
  202. used. NOTE: any memory already allocated must not be counted.
  203. There is a minimum space requirement, corresponding to the minimum
  204. feasible buffer sizes; jmemmgr.c will request that much space even if
  205. jpeg_mem_available returns zero. The maximum space needed, enough to hold
  206. all working storage in memory, is also passed in case it is useful.
  207. Finally, the total space already allocated is passed. If no better
  208. method is available, cinfo->mem->max_memory_to_use - already_allocated
  209. is often a suitable calculation.
  210. It is OK for jpeg_mem_available to underestimate the space available
  211. (that'll just lead to more backing-store access than is really necessary).
  212. However, an overestimate will lead to failure. Hence it's wise to subtract
  213. a slop factor from the true available space. 5% should be enough.
  214. On machines with lots of virtual memory, any large constant may be returned.
  215. Conversely, zero may be returned to always use the minimum amount of memory.}
  216. { This routine computes the total memory space available for allocation.
  217. It's impossible to do this in a portable way; our current solution is
  218. to make the user tell us (with a default value set at compile time).
  219. If you can actually get the available space, it's a good idea to subtract
  220. a slop factor of 5% or so. }
  221. const
  222. DEFAULT_MAX_MEM = long(300000); { for total usage about 450K }
  223. {GLOBAL}
  224. function jpeg_mem_available (cinfo : j_common_ptr;
  225. min_bytes_needed : long;
  226. max_bytes_needed : long;
  227. already_allocated : long) : long;
  228. begin
  229. {jpeg_mem_available := cinfo^.mem^.max_memory_to_use - already_allocated;}
  230. jpeg_mem_available := MaxAvail*95 div 100; { 95% }
  231. { Nomssi: limit the available memory for test purpose }
  232. {jpeg_mem_available := 30000;}
  233. end;
  234. { Backing store (temporary file) management.
  235. Backing store objects are only used when the value returned by
  236. jpeg_mem_available is less than the total space needed. You can dispense
  237. with these routines if you have plenty of virtual memory; see jmemnobs.c. }
  238. { For MS-DOS we support three types of backing storage:
  239. 1. Conventional DOS files. We access these by direct DOS calls rather
  240. than via the stdio package. This provides a bit better performance,
  241. but the real reason is that the buffers to be read or written are FAR.
  242. The stdio library for small-data memory models can't cope with that.
  243. 2. Extended memory, accessed per the XMS V2.0 specification.
  244. 3. Expanded memory, accessed per the LIM/EMS 4.0 specification.
  245. You'll need copies of those specs to make sense of the related code.
  246. The specs are available by Internet FTP from the SIMTEL archives
  247. (oak.oakland.edu and its various mirror sites). See files
  248. pub/msdos/microsoft/xms20.arc and pub/msdos/info/limems41.zip. }
  249. { Access methods for a DOS file. }
  250. {METHODDEF}
  251. procedure read_file_store (cinfo : j_common_ptr;
  252. info : backing_store_ptr;
  253. buffer_address : pointer; {FAR}
  254. file_offset : long;
  255. byte_count : long); far;
  256. begin
  257. if jdos_seek(info^.handle.file_handle, file_offset) <> 0 then
  258. ERREXIT(cinfo, JERR_TFILE_SEEK);
  259. { Since MAX_ALLOC_CHUNK is less than 64K, byte_count will be too. }
  260. if (byte_count > long(65535)) then { safety check }
  261. ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
  262. if jdos_read(info^.handle.file_handle, buffer_address,
  263. ushort(byte_count)) <> 0 then
  264. ERREXIT(cinfo, JERR_TFILE_READ);
  265. end;
  266. {METHODDEF}
  267. procedure write_file_store (cinfo : j_common_ptr;
  268. info : backing_store_ptr;
  269. buffer_address : pointer; {FAR}
  270. file_offset : long;
  271. byte_count : long); far;
  272. begin
  273. if (jdos_seek(info^.handle.file_handle, file_offset)) <> 0 then
  274. ERREXIT(cinfo, JERR_TFILE_SEEK);
  275. { Since MAX_ALLOC_CHUNK is less than 64K, byte_count will be too. }
  276. if (byte_count > long(65535)) then { safety check }
  277. ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
  278. if jdos_write(info^.handle.file_handle, buffer_address,
  279. ushort(byte_count)) <> 0 then
  280. ERREXIT(cinfo, JERR_TFILE_WRITE);
  281. end;
  282. {METHODDEF}
  283. procedure close_file_store (cinfo : j_common_ptr;
  284. info : backing_store_ptr); far;
  285. var
  286. f : FILE;
  287. begin
  288. jdos_close(info^.handle.file_handle); { close the file }
  289. system.assign(f, info^.temp_name);
  290. system.erase(f); { delete the file }
  291. { If your system doesn't have remove(), try unlink() instead.
  292. remove() is the ANSI-standard name for this function, but
  293. unlink() was more common in pre-ANSI systems. }
  294. TRACEMSS(cinfo, 1, JTRC_TFILE_CLOSE, info^.temp_name);
  295. end;
  296. {LOCAL}
  297. function open_file_store (cinfo : j_common_ptr;
  298. info : backing_store_ptr;
  299. total_bytes_needed : long): boolean; far;
  300. var
  301. handle : short;
  302. begin
  303. select_file_name(info^.temp_name);
  304. if jdos_open(handle, info^.temp_name[1]) <> 0 then
  305. begin
  306. { might as well exit since jpeg_open_backing_store will fail anyway }
  307. ERREXITS(cinfo, JERR_TFILE_CREATE, info^.temp_name);
  308. open_file_store := FALSE;
  309. exit;
  310. end;
  311. info^.handle.file_handle := handle;
  312. info^.read_backing_store := read_file_store;
  313. info^.write_backing_store := write_file_store;
  314. info^.close_backing_store := close_file_store;
  315. TRACEMSS(cinfo, 1, JTRC_TFILE_OPEN, info^.temp_name);
  316. open_file_store := TRUE; { succeeded }
  317. end;
  318. { Access methods for extended memory. }
  319. {$ifdef XMS_SUPPORTED}
  320. var
  321. xms_driver : XMSDRIVER; { saved address of XMS driver }
  322. type
  323. XMSPTR = record { either long offset or real-mode pointer }
  324. case byte of
  325. 0:(offset : long);
  326. 1:(ptr : pointer {FAR});
  327. end;
  328. type
  329. XMSspec = record { XMS move specification structure }
  330. length : long;
  331. src_handle : XMSH;
  332. src : XMSPTR;
  333. dst_handle : XMSH;
  334. dst : XMSPTR;
  335. end;
  336. type
  337. TByteArray = Array[0..MAX_ALLOC_CHUNK-1] of byte;
  338. {METHODDEF}
  339. procedure read_xms_store (cinfo : j_common_ptr;
  340. info : backing_store_ptr;
  341. buffer_address : pointer; {FAR}
  342. file_offset : long;
  343. byte_count : long); far;
  344. var
  345. ctx : XMScontext;
  346. spec : XMSspec;
  347. endbuffer : packed array[0..1] of byte;
  348. begin
  349. { The XMS driver can't cope with an odd length, so handle the last byte
  350. specially if byte_count is odd. We don't expect this to be common. }
  351. spec.length := byte_count and (not long(1));
  352. spec.src_handle := info^.handle.xms_handle;
  353. spec.src.offset := file_offset;
  354. spec.dst_handle := 0;
  355. spec.dst.ptr := buffer_address;
  356. ctx.ds_si := addr(spec);
  357. ctx.ax := $0b00; { EMB move }
  358. jxms_calldriver(xms_driver, ctx);
  359. if (ctx.ax <> 1) then
  360. ERREXIT(cinfo, JERR_XMS_READ);
  361. if odd(byte_count) then
  362. begin
  363. read_xms_store(cinfo, info, pointer(@endbuffer) {FAR},
  364. file_offset + byte_count - long(1), long(2));
  365. TByteArray(buffer_address^)[byte_count - long(1)] := endbuffer[0];
  366. end;
  367. end;
  368. {METHODDEF}
  369. procedure write_xms_store (cinfo : j_common_ptr;
  370. info : backing_store_ptr;
  371. buffer_address : pointer; {FAR}
  372. file_offset : long;
  373. byte_count : long); far;
  374. var
  375. ctx : XMScontext;
  376. spec : XMSspec;
  377. endbuffer : packed array[0..1] of byte;
  378. begin
  379. { The XMS driver can't cope with an odd length, so handle the last byte
  380. specially if byte_count is odd. We don't expect this to be common. }
  381. spec.length := byte_count and (not long(1));
  382. spec.src_handle := 0;
  383. spec.src.ptr := buffer_address;
  384. spec.dst_handle := info^.handle.xms_handle;
  385. spec.dst.offset := file_offset;
  386. ctx.ds_si := addr(spec);
  387. ctx.ax := $0b00; { EMB move }
  388. jxms_calldriver(xms_driver, ctx);
  389. if (ctx.ax <> 1) then
  390. ERREXIT(cinfo, JERR_XMS_WRITE);
  391. if odd(byte_count) then
  392. begin
  393. read_xms_store(cinfo, info, pointer(@endbuffer) {FAR},
  394. file_offset + byte_count - long(1), long(2));
  395. endbuffer[0] := TByteArray(buffer_address^)[byte_count - long(1)];
  396. write_xms_store(cinfo, info, pointer(@endbuffer) {FAR},
  397. file_offset + byte_count - long(1), long(2));
  398. end;
  399. end;
  400. {METHODDEF}
  401. procedure close_xms_store (cinfo : j_common_ptr;
  402. info : backing_store_ptr); far;
  403. var
  404. ctx : XMScontext;
  405. begin
  406. ctx.dx := info^.handle.xms_handle;
  407. ctx.ax := $0a00;
  408. jxms_calldriver(xms_driver, ctx);
  409. TRACEMS1(cinfo, 1, JTRC_XMS_CLOSE, info^.handle.xms_handle);
  410. { we ignore any error return from the driver }
  411. end;
  412. {LOCAL}
  413. function open_xms_store (cinfo : j_common_ptr;
  414. info : backing_store_ptr;
  415. total_bytes_needed : long) : boolean;
  416. var
  417. ctx : XMScontext;
  418. begin
  419. { Get address of XMS driver }
  420. jxms_getdriver(xms_driver);
  421. if (xms_driver = NIL) then
  422. begin
  423. open_xms_store := FALSE; { no driver to be had }
  424. exit;
  425. end;
  426. { Get version number, must be >= 2.00 }
  427. ctx.ax := $0000;
  428. jxms_calldriver(xms_driver, ctx);
  429. if (ctx.ax < ushort($0200)) then
  430. begin
  431. open_xms_store := FALSE;
  432. exit;
  433. end;
  434. { Try to get space (expressed in kilobytes) }
  435. ctx.dx := ushort ((total_bytes_needed + long(1023)) shr 10);
  436. ctx.ax := $0900;
  437. jxms_calldriver(xms_driver, ctx);
  438. if (ctx.ax <> 1) then
  439. begin
  440. open_xms_store := FALSE;
  441. exit;
  442. end;
  443. { Succeeded, save the handle and away we go }
  444. info^.handle.xms_handle := ctx.dx;
  445. info^.read_backing_store := read_xms_store;
  446. info^.write_backing_store := write_xms_store;
  447. info^.close_backing_store := close_xms_store;
  448. TRACEMS1(cinfo, 1, JTRC_XMS_OPEN, ctx.dx);
  449. open_xms_store := TRUE; { succeeded }
  450. end;
  451. {$endif} { XMS_SUPPORTED }
  452. { Access methods for expanded memory. }
  453. {$ifdef EMS_SUPPORTED}
  454. { The EMS move specification structure requires word and long fields aligned
  455. at odd byte boundaries. Some compilers will align struct fields at even
  456. byte boundaries. While it's usually possible to force byte alignment,
  457. that causes an overall performance penalty and may pose problems in merging
  458. JPEG into a larger application. Instead we accept some rather dirty code
  459. here. Note this code would fail if the hardware did not allow odd-byte
  460. word & long accesses, but all 80x86 CPUs do. }
  461. type
  462. EMSPTR = pointer; {FAR}
  463. { types for accessing misaligned fields }
  464. type
  465. EMSAddrStruct = packed record {Size }
  466. MemType : byte; { emsConventional, emsExpanded } { 1 }
  467. Handle : word; { TEMSHandle; } { 2 }
  468. case integer of {union}
  469. 0 : (Offs : word; { 2 }
  470. Page : word); { 2 }
  471. 1 : (Ptr : pointer); {or 4 }
  472. end;
  473. { EMS move specification structure }
  474. EMSspec = packed record
  475. length : longint; { 4 }
  476. src : EMSAddrStruct; { 7 }
  477. dst : EMSAddrStruct; { 7 }
  478. end;
  479. const
  480. EMSPAGESIZE = long(16384); { gospel, see the EMS specs }
  481. {METHODDEF}
  482. procedure read_ems_store (cinfo : j_common_ptr;
  483. info : backing_store_ptr;
  484. buffer_address : pointer; {FAR}
  485. file_offset : long;
  486. byte_count : long); far;
  487. var
  488. ctx : EMScontext;
  489. spec : EMSspec;
  490. begin
  491. spec.length := byte_count;
  492. spec.src.memtype := 1;
  493. spec.src.handle := info^.handle.ems_handle;
  494. spec.src.page := ushort (file_offset div EMSPAGESIZE);
  495. spec.src.offs := ushort (file_offset mod EMSPAGESIZE);
  496. spec.dst.memtype := 0;
  497. spec.dst.handle := 0;
  498. spec.dst.ptr := buffer_address;
  499. ctx.ds_si := addr(spec);
  500. ctx.ax := $5700; { move memory region }
  501. jems_calldriver(ctx);
  502. if (hi(ctx.ax) <> 0) then
  503. ERREXIT(cinfo, JERR_EMS_READ);
  504. end;
  505. {METHODDEF}
  506. procedure write_ems_store (cinfo : j_common_ptr;
  507. info : backing_store_ptr;
  508. buffer_address : pointer; {FAR}
  509. file_offset : long;
  510. byte_count : long); far;
  511. var
  512. ctx : EMScontext;
  513. spec : EMSspec;
  514. begin
  515. spec.length := byte_count;
  516. spec.src.memtype := 0;
  517. spec.src.handle := 0;
  518. spec.src.ptr := buffer_address;
  519. spec.dst.memtype := 1;
  520. spec.dst.handle := info^.handle.ems_handle;
  521. spec.dst.page := ushort (file_offset div EMSPAGESIZE);
  522. spec.dst.offs := ushort (file_offset mod EMSPAGESIZE);
  523. ctx.ds_si := addr(spec);
  524. ctx.ax := $5700; { move memory region }
  525. jems_calldriver(ctx);
  526. if (hi(ctx.ax) <> 0) then
  527. ERREXIT(cinfo, JERR_EMS_WRITE);
  528. end;
  529. {METHODDEF}
  530. procedure close_ems_store (cinfo : j_common_ptr;
  531. info : backing_store_ptr); far;
  532. var
  533. ctx : EMScontext;
  534. begin
  535. ctx.ax := $4500;
  536. ctx.dx := info^.handle.ems_handle;
  537. jems_calldriver(ctx);
  538. TRACEMS1(cinfo, 1, JTRC_EMS_CLOSE, info^.handle.ems_handle);
  539. { we ignore any error return from the driver }
  540. end;
  541. {LOCAL}
  542. function open_ems_store (cinfo : j_common_ptr;
  543. info : backing_store_ptr;
  544. total_bytes_needed : long) : boolean;
  545. var
  546. ctx : EMScontext;
  547. begin
  548. { Is EMS driver there? }
  549. if (jems_available = 0) then
  550. begin
  551. open_ems_store := FALSE;
  552. exit;
  553. end;
  554. { Get status, make sure EMS is OK }
  555. ctx.ax := $4000;
  556. jems_calldriver(ctx);
  557. if (hi(ctx.ax) <> 0) then
  558. begin
  559. open_ems_store := FALSE;
  560. exit;
  561. end;
  562. { Get version, must be >= 4.0 }
  563. ctx.ax := $4600;
  564. jems_calldriver(ctx);
  565. if (hi(ctx.ax) <> 0) or (lo(ctx.ax) < $40) then
  566. begin
  567. open_ems_store := FALSE;
  568. exit;
  569. end;
  570. { Try to allocate requested space }
  571. ctx.ax := $4300;
  572. ctx.bx := ushort ((total_bytes_needed +
  573. EMSPAGESIZE-long(1)) div EMSPAGESIZE);
  574. jems_calldriver(ctx);
  575. if (hi(ctx.ax) <> 0) then
  576. begin
  577. open_ems_store := FALSE;
  578. exit;
  579. end;
  580. { Succeeded, save the handle and away we go }
  581. info^.handle.ems_handle := ctx.dx;
  582. info^.read_backing_store := read_ems_store;
  583. info^.write_backing_store := write_ems_store;
  584. info^.close_backing_store := close_ems_store;
  585. TRACEMS1(cinfo, 1, JTRC_EMS_OPEN, ctx.dx);
  586. open_ems_store := TRUE; { succeeded }
  587. end;
  588. {$endif} { EMS_SUPPORTED }
  589. { Initial opening of a backing-store object. This must fill in the
  590. read/write/close pointers in the object. The read/write routines
  591. may take an error exit if the specified maximum file size is exceeded.
  592. (If jpeg_mem_available always returns a large value, this routine can
  593. just take an error exit.) }
  594. { Initial opening of a backing-store object. }
  595. {GLOBAL}
  596. procedure jpeg_open_backing_store (cinfo : j_common_ptr;
  597. info : backing_store_ptr;
  598. total_bytes_needed : long);
  599. begin
  600. { Try extended memory, then expanded memory, then regular file. }
  601. {$ifdef XMS_SUPPORTED}
  602. if (open_xms_store(cinfo, info, total_bytes_needed)) then
  603. exit;
  604. {$endif}
  605. {$ifdef EMS_SUPPORTED}
  606. if (open_ems_store(cinfo, info, total_bytes_needed)) then
  607. exit;
  608. {$endif}
  609. if (open_file_store(cinfo, info, total_bytes_needed)) then
  610. exit;
  611. ERREXITS(cinfo, JERR_TFILE_CREATE, '');
  612. end;
  613. { These routines take care of any system-dependent initialization and
  614. cleanup required. jpeg_mem_init will be called before anything is
  615. allocated (and, therefore, nothing in cinfo is of use except the error
  616. manager pointer). It should return a suitable default value for
  617. max_memory_to_use; this may subsequently be overridden by the surrounding
  618. application. (Note that max_memory_to_use is only important if
  619. jpeg_mem_available chooses to consult it ... no one else will.)
  620. jpeg_mem_term may assume that all requested memory has been freed and that
  621. all opened backing-store objects have been closed. }
  622. { These routines take care of any system-dependent initialization and
  623. cleanup required. }
  624. {GLOBAL}
  625. function jpeg_mem_init (cinfo : j_common_ptr) : long;
  626. begin
  627. next_file_num := 0; { initialize temp file name generator }
  628. jpeg_mem_init := DEFAULT_MAX_MEM; { default for max_memory_to_use }
  629. end;
  630. {GLOBAL}
  631. procedure jpeg_mem_term (cinfo : j_common_ptr);
  632. begin
  633. { Microsoft C, at least in v6.00A, will not successfully reclaim freed
  634. blocks of size > 32Kbytes unless we give it a kick in the rear,
  635. like so: }
  636. {$ifdef NEED_FHEAPMIN}
  637. _fheapmin();
  638. {$endif}
  639. end;
  640. end.