jmemdos.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. #if 0
  2. /*
  3. * jmemdos.c
  4. *
  5. * Copyright (C) 1992-1997, Thomas G. Lane.
  6. * This file is part of the Independent JPEG Group's software.
  7. * For conditions of distribution and use, see the accompanying README file.
  8. *
  9. * This file provides an MS-DOS-compatible implementation of the system-
  10. * dependent portion of the JPEG memory manager. Temporary data can be
  11. * stored in extended or expanded memory as well as in regular DOS files.
  12. *
  13. * If you use this file, you must be sure that NEED_FAR_POINTERS is defined
  14. * if you compile in a small-data memory model; it should NOT be defined if
  15. * you use a large-data memory model. This file is not recommended if you
  16. * are using a flat-memory-space 386 environment such as DJGCC or Watcom C.
  17. * Also, this code will NOT work if struct fields are aligned on greater than
  18. * 2-byte boundaries.
  19. *
  20. * Based on code contributed by Ge' Weijers.
  21. */
  22. /*
  23. * If you have both extended and expanded memory, you may want to change the
  24. * order in which they are tried in jopen_backing_store. On a 286 machine
  25. * expanded memory is usually faster, since extended memory access involves
  26. * an expensive protected-mode-and-back switch. On 386 and better, extended
  27. * memory is usually faster. As distributed, the code tries extended memory
  28. * first (what? not everyone has a 386? :-).
  29. *
  30. * You can disable use of extended/expanded memory entirely by altering these
  31. * definitions or overriding them from the Makefile (eg, -DEMS_SUPPORTED=0).
  32. */
  33. #ifndef XMS_SUPPORTED
  34. #define XMS_SUPPORTED 1
  35. #endif
  36. #ifndef EMS_SUPPORTED
  37. #define EMS_SUPPORTED 1
  38. #endif
  39. #define JPEG_INTERNALS
  40. #include "jinclude.h"
  41. #include "jpeglib.h"
  42. #include "jmemsys.h" /* import the system-dependent declarations */
  43. #ifndef HAVE_STDLIB_H /* <stdlib.h> should declare these */
  44. extern void * malloc JPP((size_t size));
  45. extern void free JPP((void *ptr));
  46. extern char * getenv JPP((const char * name));
  47. #endif
  48. #ifdef NEED_FAR_POINTERS
  49. #ifdef __TURBOC__
  50. /* These definitions work for Borland C (Turbo C) */
  51. #include <alloc.h> /* need farmalloc(), farfree() */
  52. #define far_malloc(x) farmalloc(x)
  53. #define far_free(x) farfree(x)
  54. #else
  55. /* These definitions work for Microsoft C and compatible compilers */
  56. #include <malloc.h> /* need _fmalloc(), _ffree() */
  57. #define far_malloc(x) _fmalloc(x)
  58. #define far_free(x) _ffree(x)
  59. #endif
  60. #else /* not NEED_FAR_POINTERS */
  61. #define far_malloc(x) malloc(x)
  62. #define far_free(x) free(x)
  63. #endif /* NEED_FAR_POINTERS */
  64. #ifdef DONT_USE_B_MODE /* define mode parameters for fopen() */
  65. #define READ_BINARY "r"
  66. #else
  67. #define READ_BINARY "rb"
  68. #endif
  69. #ifndef USE_MSDOS_MEMMGR /* make sure user got configuration right */
  70. You forgot to define USE_MSDOS_MEMMGR in jconfig.h. /* deliberate syntax error */
  71. #endif
  72. #if MAX_ALLOC_CHUNK >= 65535L /* make sure jconfig.h got this right */
  73. MAX_ALLOC_CHUNK should be less than 64K. /* deliberate syntax error */
  74. #endif
  75. /*
  76. * Declarations for assembly-language support routines (see jmemdosa.asm).
  77. *
  78. * The functions are declared "far" as are all their pointer arguments;
  79. * this ensures the assembly source code will work regardless of the
  80. * compiler memory model. We assume "short" is 16 bits, "long" is 32.
  81. */
  82. typedef void far * XMSDRIVER; /* actually a pointer to code */
  83. typedef struct { /* registers for calling XMS driver */
  84. unsigned short ax, dx, bx;
  85. void far * ds_si;
  86. } XMScontext;
  87. typedef struct { /* registers for calling EMS driver */
  88. unsigned short ax, dx, bx;
  89. void far * ds_si;
  90. } EMScontext;
  91. extern short far jdos_open JPP((short far * handle, char far * filename));
  92. extern short far jdos_close JPP((short handle));
  93. extern short far jdos_seek JPP((short handle, long offset));
  94. extern short far jdos_read JPP((short handle, void far * buffer,
  95. unsigned short count));
  96. extern short far jdos_write JPP((short handle, void far * buffer,
  97. unsigned short count));
  98. extern void far jxms_getdriver JPP((XMSDRIVER far *));
  99. extern void far jxms_calldriver JPP((XMSDRIVER, XMScontext far *));
  100. extern short far jems_available JPP((void));
  101. extern void far jems_calldriver JPP((EMScontext far *));
  102. /*
  103. * Selection of a file name for a temporary file.
  104. * This is highly system-dependent, and you may want to customize it.
  105. */
  106. static int next_file_num; /* to distinguish among several temp files */
  107. LOCAL(void)
  108. select_file_name (char * fname)
  109. {
  110. const char * env;
  111. char * ptr;
  112. FILE * tfile;
  113. /* Keep generating file names till we find one that's not in use */
  114. for (;;) {
  115. /* Get temp directory name from environment TMP or TEMP variable;
  116. * if none, use "."
  117. */
  118. if ((env = (const char *) getenv("TMP")) == NULL)
  119. if ((env = (const char *) getenv("TEMP")) == NULL)
  120. env = ".";
  121. if (*env == '\0') /* null string means "." */
  122. env = ".";
  123. ptr = fname; /* copy name to fname */
  124. while (*env != '\0')
  125. *ptr++ = *env++;
  126. if (ptr[-1] != '\\' && ptr[-1] != '/')
  127. *ptr++ = '\\'; /* append backslash if not in env variable */
  128. /* Append a suitable file name */
  129. next_file_num++; /* advance counter */
  130. sprintf(ptr, "JPG%03d.TMP", next_file_num);
  131. /* Probe to see if file name is already in use */
  132. if ((tfile = fopen(fname, READ_BINARY)) == NULL)
  133. break;
  134. fclose(tfile); /* oops, it's there; close tfile & try again */
  135. }
  136. }
  137. /*
  138. * Near-memory allocation and freeing are controlled by the regular library
  139. * routines malloc() and free().
  140. */
  141. GLOBAL(void *)
  142. jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
  143. {
  144. return (void *) malloc(sizeofobject);
  145. }
  146. GLOBAL(void)
  147. jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
  148. {
  149. free(object);
  150. }
  151. /*
  152. * "Large" objects are allocated in far memory, if possible
  153. */
  154. GLOBAL(void FAR *)
  155. jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
  156. {
  157. return (void FAR *) far_malloc(sizeofobject);
  158. }
  159. GLOBAL(void)
  160. jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
  161. {
  162. far_free(object);
  163. }
  164. /*
  165. * This routine computes the total memory space available for allocation.
  166. * It's impossible to do this in a portable way; our current solution is
  167. * to make the user tell us (with a default value set at compile time).
  168. * If you can actually get the available space, it's a good idea to subtract
  169. * a slop factor of 5% or so.
  170. */
  171. #ifndef DEFAULT_MAX_MEM /* so can override from makefile */
  172. #define DEFAULT_MAX_MEM 300000L /* for total usage about 450K */
  173. #endif
  174. GLOBAL(long)
  175. jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
  176. long max_bytes_needed, long already_allocated)
  177. {
  178. return cinfo->mem->max_memory_to_use - already_allocated;
  179. }
  180. /*
  181. * Backing store (temporary file) management.
  182. * Backing store objects are only used when the value returned by
  183. * jpeg_mem_available is less than the total space needed. You can dispense
  184. * with these routines if you have plenty of virtual memory; see jmemnobs.c.
  185. */
  186. /*
  187. * For MS-DOS we support three types of backing storage:
  188. * 1. Conventional DOS files. We access these by direct DOS calls rather
  189. * than via the stdio package. This provides a bit better performance,
  190. * but the real reason is that the buffers to be read or written are FAR.
  191. * The stdio library for small-data memory models can't cope with that.
  192. * 2. Extended memory, accessed per the XMS V2.0 specification.
  193. * 3. Expanded memory, accessed per the LIM/EMS 4.0 specification.
  194. * You'll need copies of those specs to make sense of the related code.
  195. * The specs are available by Internet FTP from the SIMTEL archives
  196. * (oak.oakland.edu and its various mirror sites). See files
  197. * pub/msdos/microsoft/xms20.arc and pub/msdos/info/limems41.zip.
  198. */
  199. /*
  200. * Access methods for a DOS file.
  201. */
  202. METHODDEF(void)
  203. read_file_store (j_common_ptr cinfo, backing_store_ptr info,
  204. void FAR * buffer_address,
  205. long file_offset, long byte_count)
  206. {
  207. if (jdos_seek(info->handle.file_handle, file_offset))
  208. ERREXIT(cinfo, JERR_TFILE_SEEK);
  209. /* Since MAX_ALLOC_CHUNK is less than 64K, byte_count will be too. */
  210. if (byte_count > 65535L) /* safety check */
  211. ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
  212. if (jdos_read(info->handle.file_handle, buffer_address,
  213. (unsigned short) byte_count))
  214. ERREXIT(cinfo, JERR_TFILE_READ);
  215. }
  216. METHODDEF(void)
  217. write_file_store (j_common_ptr cinfo, backing_store_ptr info,
  218. void FAR * buffer_address,
  219. long file_offset, long byte_count)
  220. {
  221. if (jdos_seek(info->handle.file_handle, file_offset))
  222. ERREXIT(cinfo, JERR_TFILE_SEEK);
  223. /* Since MAX_ALLOC_CHUNK is less than 64K, byte_count will be too. */
  224. if (byte_count > 65535L) /* safety check */
  225. ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
  226. if (jdos_write(info->handle.file_handle, buffer_address,
  227. (unsigned short) byte_count))
  228. ERREXIT(cinfo, JERR_TFILE_WRITE);
  229. }
  230. METHODDEF(void)
  231. close_file_store (j_common_ptr cinfo, backing_store_ptr info)
  232. {
  233. jdos_close(info->handle.file_handle); /* close the file */
  234. remove(info->temp_name); /* delete the file */
  235. /* If your system doesn't have remove(), try unlink() instead.
  236. * remove() is the ANSI-standard name for this function, but
  237. * unlink() was more common in pre-ANSI systems.
  238. */
  239. TRACEMSS(cinfo, 1, JTRC_TFILE_CLOSE, info->temp_name);
  240. }
  241. LOCAL(boolean)
  242. open_file_store (j_common_ptr cinfo, backing_store_ptr info,
  243. long total_bytes_needed)
  244. {
  245. short handle;
  246. select_file_name(info->temp_name);
  247. if (jdos_open((short far *) & handle, (char far *) info->temp_name)) {
  248. /* might as well exit since jpeg_open_backing_store will fail anyway */
  249. ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name);
  250. return FALSE;
  251. }
  252. info->handle.file_handle = handle;
  253. info->read_backing_store = read_file_store;
  254. info->write_backing_store = write_file_store;
  255. info->close_backing_store = close_file_store;
  256. TRACEMSS(cinfo, 1, JTRC_TFILE_OPEN, info->temp_name);
  257. return TRUE; /* succeeded */
  258. }
  259. /*
  260. * Access methods for extended memory.
  261. */
  262. #if XMS_SUPPORTED
  263. static XMSDRIVER xms_driver; /* saved address of XMS driver */
  264. typedef union { /* either long offset or real-mode pointer */
  265. long offset;
  266. void far * ptr;
  267. } XMSPTR;
  268. typedef struct { /* XMS move specification structure */
  269. long length;
  270. XMSH src_handle;
  271. XMSPTR src;
  272. XMSH dst_handle;
  273. XMSPTR dst;
  274. } XMSspec;
  275. #define ODD(X) (((X) & 1L) != 0)
  276. METHODDEF(void)
  277. read_xms_store (j_common_ptr cinfo, backing_store_ptr info,
  278. void FAR * buffer_address,
  279. long file_offset, long byte_count)
  280. {
  281. XMScontext ctx;
  282. XMSspec spec;
  283. char endbuffer[2];
  284. /* The XMS driver can't cope with an odd length, so handle the last byte
  285. * specially if byte_count is odd. We don't expect this to be common.
  286. */
  287. spec.length = byte_count & (~ 1L);
  288. spec.src_handle = info->handle.xms_handle;
  289. spec.src.offset = file_offset;
  290. spec.dst_handle = 0;
  291. spec.dst.ptr = buffer_address;
  292. ctx.ds_si = (void far *) & spec;
  293. ctx.ax = 0x0b00; /* EMB move */
  294. jxms_calldriver(xms_driver, (XMScontext far *) & ctx);
  295. if (ctx.ax != 1)
  296. ERREXIT(cinfo, JERR_XMS_READ);
  297. if (ODD(byte_count)) {
  298. read_xms_store(cinfo, info, (void FAR *) endbuffer,
  299. file_offset + byte_count - 1L, 2L);
  300. ((char FAR *) buffer_address)[byte_count - 1L] = endbuffer[0];
  301. }
  302. }
  303. METHODDEF(void)
  304. write_xms_store (j_common_ptr cinfo, backing_store_ptr info,
  305. void FAR * buffer_address,
  306. long file_offset, long byte_count)
  307. {
  308. XMScontext ctx;
  309. XMSspec spec;
  310. char endbuffer[2];
  311. /* The XMS driver can't cope with an odd length, so handle the last byte
  312. * specially if byte_count is odd. We don't expect this to be common.
  313. */
  314. spec.length = byte_count & (~ 1L);
  315. spec.src_handle = 0;
  316. spec.src.ptr = buffer_address;
  317. spec.dst_handle = info->handle.xms_handle;
  318. spec.dst.offset = file_offset;
  319. ctx.ds_si = (void far *) & spec;
  320. ctx.ax = 0x0b00; /* EMB move */
  321. jxms_calldriver(xms_driver, (XMScontext far *) & ctx);
  322. if (ctx.ax != 1)
  323. ERREXIT(cinfo, JERR_XMS_WRITE);
  324. if (ODD(byte_count)) {
  325. read_xms_store(cinfo, info, (void FAR *) endbuffer,
  326. file_offset + byte_count - 1L, 2L);
  327. endbuffer[0] = ((char FAR *) buffer_address)[byte_count - 1L];
  328. write_xms_store(cinfo, info, (void FAR *) endbuffer,
  329. file_offset + byte_count - 1L, 2L);
  330. }
  331. }
  332. METHODDEF(void)
  333. close_xms_store (j_common_ptr cinfo, backing_store_ptr info)
  334. {
  335. XMScontext ctx;
  336. ctx.dx = info->handle.xms_handle;
  337. ctx.ax = 0x0a00;
  338. jxms_calldriver(xms_driver, (XMScontext far *) & ctx);
  339. TRACEMS1(cinfo, 1, JTRC_XMS_CLOSE, info->handle.xms_handle);
  340. /* we ignore any error return from the driver */
  341. }
  342. LOCAL(boolean)
  343. open_xms_store (j_common_ptr cinfo, backing_store_ptr info,
  344. long total_bytes_needed)
  345. {
  346. XMScontext ctx;
  347. /* Get address of XMS driver */
  348. jxms_getdriver((XMSDRIVER far *) & xms_driver);
  349. if (xms_driver == NULL)
  350. return FALSE; /* no driver to be had */
  351. /* Get version number, must be >= 2.00 */
  352. ctx.ax = 0x0000;
  353. jxms_calldriver(xms_driver, (XMScontext far *) & ctx);
  354. if (ctx.ax < (unsigned short) 0x0200)
  355. return FALSE;
  356. /* Try to get space (expressed in kilobytes) */
  357. ctx.dx = (unsigned short) ((total_bytes_needed + 1023L) >> 10);
  358. ctx.ax = 0x0900;
  359. jxms_calldriver(xms_driver, (XMScontext far *) & ctx);
  360. if (ctx.ax != 1)
  361. return FALSE;
  362. /* Succeeded, save the handle and away we go */
  363. info->handle.xms_handle = ctx.dx;
  364. info->read_backing_store = read_xms_store;
  365. info->write_backing_store = write_xms_store;
  366. info->close_backing_store = close_xms_store;
  367. TRACEMS1(cinfo, 1, JTRC_XMS_OPEN, ctx.dx);
  368. return TRUE; /* succeeded */
  369. }
  370. #endif /* XMS_SUPPORTED */
  371. /*
  372. * Access methods for expanded memory.
  373. */
  374. #if EMS_SUPPORTED
  375. /* The EMS move specification structure requires word and long fields aligned
  376. * at odd byte boundaries. Some compilers will align struct fields at even
  377. * byte boundaries. While it's usually possible to force byte alignment,
  378. * that causes an overall performance penalty and may pose problems in merging
  379. * JPEG into a larger application. Instead we accept some rather dirty code
  380. * here. Note this code would fail if the hardware did not allow odd-byte
  381. * word & long accesses, but all 80x86 CPUs do.
  382. */
  383. typedef void far * EMSPTR;
  384. typedef union { /* EMS move specification structure */
  385. long length; /* It's easy to access first 4 bytes */
  386. char bytes[18]; /* Misaligned fields in here! */
  387. } EMSspec;
  388. /* Macros for accessing misaligned fields */
  389. #define FIELD_AT(spec,offset,type) (*((type *) &(spec.bytes[offset])))
  390. #define SRC_TYPE(spec) FIELD_AT(spec,4,char)
  391. #define SRC_HANDLE(spec) FIELD_AT(spec,5,EMSH)
  392. #define SRC_OFFSET(spec) FIELD_AT(spec,7,unsigned short)
  393. #define SRC_PAGE(spec) FIELD_AT(spec,9,unsigned short)
  394. #define SRC_PTR(spec) FIELD_AT(spec,7,EMSPTR)
  395. #define DST_TYPE(spec) FIELD_AT(spec,11,char)
  396. #define DST_HANDLE(spec) FIELD_AT(spec,12,EMSH)
  397. #define DST_OFFSET(spec) FIELD_AT(spec,14,unsigned short)
  398. #define DST_PAGE(spec) FIELD_AT(spec,16,unsigned short)
  399. #define DST_PTR(spec) FIELD_AT(spec,14,EMSPTR)
  400. #define EMSPAGESIZE 16384L /* gospel, see the EMS specs */
  401. #define HIBYTE(W) (((W) >> 8) & 0xFF)
  402. #define LOBYTE(W) ((W) & 0xFF)
  403. METHODDEF(void)
  404. read_ems_store (j_common_ptr cinfo, backing_store_ptr info,
  405. void FAR * buffer_address,
  406. long file_offset, long byte_count)
  407. {
  408. EMScontext ctx;
  409. EMSspec spec;
  410. spec.length = byte_count;
  411. SRC_TYPE(spec) = 1;
  412. SRC_HANDLE(spec) = info->handle.ems_handle;
  413. SRC_PAGE(spec) = (unsigned short) (file_offset / EMSPAGESIZE);
  414. SRC_OFFSET(spec) = (unsigned short) (file_offset % EMSPAGESIZE);
  415. DST_TYPE(spec) = 0;
  416. DST_HANDLE(spec) = 0;
  417. DST_PTR(spec) = buffer_address;
  418. ctx.ds_si = (void far *) & spec;
  419. ctx.ax = 0x5700; /* move memory region */
  420. jems_calldriver((EMScontext far *) & ctx);
  421. if (HIBYTE(ctx.ax) != 0)
  422. ERREXIT(cinfo, JERR_EMS_READ);
  423. }
  424. METHODDEF(void)
  425. write_ems_store (j_common_ptr cinfo, backing_store_ptr info,
  426. void FAR * buffer_address,
  427. long file_offset, long byte_count)
  428. {
  429. EMScontext ctx;
  430. EMSspec spec;
  431. spec.length = byte_count;
  432. SRC_TYPE(spec) = 0;
  433. SRC_HANDLE(spec) = 0;
  434. SRC_PTR(spec) = buffer_address;
  435. DST_TYPE(spec) = 1;
  436. DST_HANDLE(spec) = info->handle.ems_handle;
  437. DST_PAGE(spec) = (unsigned short) (file_offset / EMSPAGESIZE);
  438. DST_OFFSET(spec) = (unsigned short) (file_offset % EMSPAGESIZE);
  439. ctx.ds_si = (void far *) & spec;
  440. ctx.ax = 0x5700; /* move memory region */
  441. jems_calldriver((EMScontext far *) & ctx);
  442. if (HIBYTE(ctx.ax) != 0)
  443. ERREXIT(cinfo, JERR_EMS_WRITE);
  444. }
  445. METHODDEF(void)
  446. close_ems_store (j_common_ptr cinfo, backing_store_ptr info)
  447. {
  448. EMScontext ctx;
  449. ctx.ax = 0x4500;
  450. ctx.dx = info->handle.ems_handle;
  451. jems_calldriver((EMScontext far *) & ctx);
  452. TRACEMS1(cinfo, 1, JTRC_EMS_CLOSE, info->handle.ems_handle);
  453. /* we ignore any error return from the driver */
  454. }
  455. LOCAL(boolean)
  456. open_ems_store (j_common_ptr cinfo, backing_store_ptr info,
  457. long total_bytes_needed)
  458. {
  459. EMScontext ctx;
  460. /* Is EMS driver there? */
  461. if (! jems_available())
  462. return FALSE;
  463. /* Get status, make sure EMS is OK */
  464. ctx.ax = 0x4000;
  465. jems_calldriver((EMScontext far *) & ctx);
  466. if (HIBYTE(ctx.ax) != 0)
  467. return FALSE;
  468. /* Get version, must be >= 4.0 */
  469. ctx.ax = 0x4600;
  470. jems_calldriver((EMScontext far *) & ctx);
  471. if (HIBYTE(ctx.ax) != 0 || LOBYTE(ctx.ax) < 0x40)
  472. return FALSE;
  473. /* Try to allocate requested space */
  474. ctx.ax = 0x4300;
  475. ctx.bx = (unsigned short) ((total_bytes_needed + EMSPAGESIZE-1L) / EMSPAGESIZE);
  476. jems_calldriver((EMScontext far *) & ctx);
  477. if (HIBYTE(ctx.ax) != 0)
  478. return FALSE;
  479. /* Succeeded, save the handle and away we go */
  480. info->handle.ems_handle = ctx.dx;
  481. info->read_backing_store = read_ems_store;
  482. info->write_backing_store = write_ems_store;
  483. info->close_backing_store = close_ems_store;
  484. TRACEMS1(cinfo, 1, JTRC_EMS_OPEN, ctx.dx);
  485. return TRUE; /* succeeded */
  486. }
  487. #endif /* EMS_SUPPORTED */
  488. /*
  489. * Initial opening of a backing-store object.
  490. */
  491. GLOBAL(void)
  492. jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
  493. long total_bytes_needed)
  494. {
  495. /* Try extended memory, then expanded memory, then regular file. */
  496. #if XMS_SUPPORTED
  497. if (open_xms_store(cinfo, info, total_bytes_needed))
  498. return;
  499. #endif
  500. #if EMS_SUPPORTED
  501. if (open_ems_store(cinfo, info, total_bytes_needed))
  502. return;
  503. #endif
  504. if (open_file_store(cinfo, info, total_bytes_needed))
  505. return;
  506. ERREXITS(cinfo, JERR_TFILE_CREATE, "");
  507. }
  508. /*
  509. * These routines take care of any system-dependent initialization and
  510. * cleanup required.
  511. */
  512. GLOBAL(long)
  513. jpeg_mem_init (j_common_ptr cinfo)
  514. {
  515. next_file_num = 0; /* initialize temp file name generator */
  516. return DEFAULT_MAX_MEM; /* default for max_memory_to_use */
  517. }
  518. GLOBAL(void)
  519. jpeg_mem_term (j_common_ptr cinfo)
  520. {
  521. /* Microsoft C, at least in v6.00A, will not successfully reclaim freed
  522. * blocks of size > 32Kbytes unless we give it a kick in the rear, like so:
  523. */
  524. #ifdef NEED_FHEAPMIN
  525. _fheapmin();
  526. #endif
  527. }
  528. #endif