jmemdos.c 19 KB

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