jmemname.c 7.9 KB

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