jmemdos.c 18 KB

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