jmemname.c 8.1 KB

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