jmemname.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * jmemname.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 a generic implementation of the system-dependent
  9. * portion of the JPEG memory manager. This implementation assumes that
  10. * you must explicitly construct a name for each temp file.
  11. * Also, the problem of determining the amount of memory available
  12. * is shoved onto the user.
  13. */
  14. #define JPEG_INTERNALS
  15. #include "jinclude.h"
  16. #include "jpeglib.h"
  17. #include "jmemsys.h" /* import the system-dependent declarations */
  18. #ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */
  19. extern void * malloc JPP((size_t size));
  20. extern void free JPP((void *ptr));
  21. #endif
  22. #ifndef SEEK_SET /* pre-ANSI systems may not define this; */
  23. #define SEEK_SET 0 /* if not, assume 0 is correct */
  24. #endif
  25. #ifdef DONT_USE_B_MODE /* define mode parameters for fopen() */
  26. #define READ_BINARY "r"
  27. #define RW_BINARY "w+"
  28. #else
  29. #ifdef VMS /* VMS is very nonstandard */
  30. #define READ_BINARY "rb", "ctx=stm"
  31. #define RW_BINARY "w+b", "ctx=stm"
  32. #else /* standard ANSI-compliant case */
  33. #define READ_BINARY "rb"
  34. #define RW_BINARY "w+b"
  35. #endif
  36. #endif
  37. /*
  38. * Selection of a file name for a temporary file.
  39. * This is system-dependent!
  40. *
  41. * The code as given is suitable for most Unix systems, and it is easily
  42. * modified for most non-Unix systems. Some notes:
  43. * 1. The temp file is created in the directory named by TEMP_DIRECTORY.
  44. * The default value is /usr/tmp, which is the conventional place for
  45. * creating large temp files on Unix. On other systems you'll probably
  46. * want to change the file location. You can do this by editing the
  47. * #define, or (preferred) by defining TEMP_DIRECTORY in jconfig.h.
  48. *
  49. * 2. If you need to change the file name as well as its location,
  50. * you can override the TEMP_FILE_NAME macro. (Note that this is
  51. * actually a printf format string; it must contain %s and %d.)
  52. * Few people should need to do this.
  53. *
  54. * 3. mktemp() is used to ensure that multiple processes running
  55. * simultaneously won't select the same file names. If your system
  56. * doesn't have mktemp(), define NO_MKTEMP to do it the hard way.
  57. * (If you don't have <errno.h>, also define NO_ERRNO_H.)
  58. *
  59. * 4. You probably want to define NEED_SIGNAL_CATCHER so that cjpeg.c/djpeg.c
  60. * will cause the temp files to be removed if you stop the program early.
  61. */
  62. #ifndef TEMP_DIRECTORY /* can override from jconfig.h or Makefile */
  63. #define TEMP_DIRECTORY "/usr/tmp/" /* recommended setting for Unix */
  64. #endif
  65. static int next_file_num; /* to distinguish among several temp files */
  66. #ifdef NO_MKTEMP
  67. #ifndef TEMP_FILE_NAME /* can override from jconfig.h or Makefile */
  68. #define TEMP_FILE_NAME "%sJPG%03d.TMP"
  69. #endif
  70. #ifndef NO_ERRNO_H
  71. #include <errno.h> /* to define ENOENT */
  72. #endif
  73. /* ANSI C specifies that errno is a macro, but on older systems it's more
  74. * likely to be a plain int variable. And not all versions of errno.h
  75. * bother to declare it, so we have to in order to be most portable. Thus:
  76. */
  77. #ifndef errno
  78. extern int errno;
  79. #endif
  80. LOCAL(void)
  81. select_file_name (char * fname)
  82. {
  83. FILE * tfile;
  84. /* Keep generating file names till we find one that's not in use */
  85. for (;;) {
  86. next_file_num++; /* advance counter */
  87. sprintf(fname, TEMP_FILE_NAME, TEMP_DIRECTORY, next_file_num);
  88. if ((tfile = fopen(fname, READ_BINARY)) == NULL) {
  89. /* fopen could have failed for a reason other than the file not
  90. * being there; for example, file there but unreadable.
  91. * If <errno.h> isn't available, then we cannot test the cause.
  92. */
  93. #ifdef ENOENT
  94. if (errno != ENOENT)
  95. continue;
  96. #endif
  97. break;
  98. }
  99. fclose(tfile); /* oops, it's there; close tfile & try again */
  100. }
  101. }
  102. #else /* ! NO_MKTEMP */
  103. /* Note that mktemp() requires the initial filename to end in six X's */
  104. #ifndef TEMP_FILE_NAME /* can override from jconfig.h or Makefile */
  105. #define TEMP_FILE_NAME "%sJPG%dXXXXXX"
  106. #endif
  107. LOCAL(void)
  108. select_file_name (char * fname)
  109. {
  110. next_file_num++; /* advance counter */
  111. sprintf(fname, TEMP_FILE_NAME, TEMP_DIRECTORY, next_file_num);
  112. mktemp(fname); /* make sure file name is unique */
  113. /* mktemp replaces the trailing XXXXXX with a unique string of characters */
  114. }
  115. #endif /* NO_MKTEMP */
  116. /*
  117. * Memory allocation and freeing are controlled by the regular library
  118. * routines malloc() and free().
  119. */
  120. GLOBAL(void *)
  121. jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
  122. {
  123. return (void *) malloc(sizeofobject);
  124. }
  125. GLOBAL(void)
  126. jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
  127. {
  128. free(object);
  129. }
  130. /*
  131. * "Large" objects are treated the same as "small" ones.
  132. * NB: although we include FAR keywords in the routine declarations,
  133. * this file won't actually work in 80x86 small/medium model; at least,
  134. * you probably won't be able to process useful-size images in only 64KB.
  135. */
  136. GLOBAL(void FAR *)
  137. jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
  138. {
  139. return (void FAR *) malloc(sizeofobject);
  140. }
  141. GLOBAL(void)
  142. jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
  143. {
  144. free(object);
  145. }
  146. /*
  147. * This routine computes the total memory space available for allocation.
  148. * It's impossible to do this in a portable way; our current solution is
  149. * to make the user tell us (with a default value set at compile time).
  150. * If you can actually get the available space, it's a good idea to subtract
  151. * a slop factor of 5% or so.
  152. */
  153. #ifndef DEFAULT_MAX_MEM /* so can override from makefile */
  154. #define DEFAULT_MAX_MEM 1000000L /* default: one megabyte */
  155. #endif
  156. GLOBAL(long)
  157. jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
  158. long max_bytes_needed, long already_allocated)
  159. {
  160. return cinfo->mem->max_memory_to_use - already_allocated;
  161. }
  162. /*
  163. * Backing store (temporary file) management.
  164. * Backing store objects are only used when the value returned by
  165. * jpeg_mem_available is less than the total space needed. You can dispense
  166. * with these routines if you have plenty of virtual memory; see jmemnobs.c.
  167. */
  168. METHODDEF(void)
  169. read_backing_store (j_common_ptr cinfo, backing_store_ptr info,
  170. void FAR * buffer_address,
  171. long file_offset, long byte_count)
  172. {
  173. if (fseek(info->temp_file, file_offset, SEEK_SET))
  174. ERREXIT(cinfo, JERR_TFILE_SEEK);
  175. if (JFREAD(info->temp_file, buffer_address, byte_count)
  176. != (size_t) byte_count)
  177. ERREXIT(cinfo, JERR_TFILE_READ);
  178. }
  179. METHODDEF(void)
  180. write_backing_store (j_common_ptr cinfo, backing_store_ptr info,
  181. void FAR * buffer_address,
  182. long file_offset, long byte_count)
  183. {
  184. if (fseek(info->temp_file, file_offset, SEEK_SET))
  185. ERREXIT(cinfo, JERR_TFILE_SEEK);
  186. if (JFWRITE(info->temp_file, buffer_address, byte_count)
  187. != (size_t) byte_count)
  188. ERREXIT(cinfo, JERR_TFILE_WRITE);
  189. }
  190. METHODDEF(void)
  191. close_backing_store (j_common_ptr cinfo, backing_store_ptr info)
  192. {
  193. fclose(info->temp_file); /* close the file */
  194. unlink(info->temp_name); /* delete the file */
  195. /* If your system doesn't have unlink(), use remove() instead.
  196. * remove() is the ANSI-standard name for this function, but if
  197. * your system was ANSI you'd be using jmemansi.c, right?
  198. */
  199. TRACEMSS(cinfo, 1, JTRC_TFILE_CLOSE, info->temp_name);
  200. }
  201. /*
  202. * Initial opening of a backing-store object.
  203. */
  204. GLOBAL(void)
  205. jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
  206. long total_bytes_needed)
  207. {
  208. select_file_name(info->temp_name);
  209. if ((info->temp_file = fopen(info->temp_name, RW_BINARY)) == NULL)
  210. ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name);
  211. info->read_backing_store = read_backing_store;
  212. info->write_backing_store = write_backing_store;
  213. info->close_backing_store = close_backing_store;
  214. TRACEMSS(cinfo, 1, JTRC_TFILE_OPEN, info->temp_name);
  215. }
  216. /*
  217. * These routines take care of any system-dependent initialization and
  218. * cleanup required.
  219. */
  220. GLOBAL(long)
  221. jpeg_mem_init (j_common_ptr cinfo)
  222. {
  223. next_file_num = 0; /* initialize temp file name generator */
  224. return DEFAULT_MAX_MEM; /* default for max_memory_to_use */
  225. }
  226. GLOBAL(void)
  227. jpeg_mem_term (j_common_ptr cinfo)
  228. {
  229. /* no work */
  230. }