jmemdos.pas 25 KB

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