jmemname.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. FILE * tfile;
  78. /* Keep generating file names till we find one that's not in use */
  79. for (;; ) {
  80. next_file_num++; /* advance counter */
  81. sprintf( fname, TEMP_FILE_NAME, TEMP_DIRECTORY, next_file_num );
  82. if ( ( tfile = fopen( fname, READ_BINARY ) ) == NULL ) {
  83. /* fopen could have failed for a reason other than the file not
  84. * being there; for example, file there but unreadable.
  85. * If <errno.h> isn't available, then we cannot test the cause.
  86. */
  87. #ifdef ENOENT
  88. if ( errno != ENOENT ) {
  89. continue;
  90. }
  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. next_file_num++; /* advance counter */
  105. sprintf( fname, TEMP_FILE_NAME, TEMP_DIRECTORY, next_file_num );
  106. mktemp( fname ); /* make sure file name is unique */
  107. /* mktemp replaces the trailing XXXXXX with a unique string of characters */
  108. }
  109. #endif /* NO_MKTEMP */
  110. /*
  111. * Memory allocation and freeing are controlled by the regular library
  112. * routines malloc() and free().
  113. */
  114. GLOBAL void *
  115. jpeg_get_small( j_common_ptr cinfo, size_t sizeofobject ) {
  116. return (void *) malloc( sizeofobject );
  117. }
  118. GLOBAL void
  119. jpeg_free_small( j_common_ptr cinfo, void * object, size_t sizeofobject ) {
  120. free( object );
  121. }
  122. /*
  123. * "Large" objects are treated the same as "small" ones.
  124. * NB: although we include FAR keywords in the routine declarations,
  125. * this file won't actually work in 80x86 small/medium model; at least,
  126. * you probably won't be able to process useful-size images in only 64KB.
  127. */
  128. GLOBAL void FAR *
  129. jpeg_get_large( j_common_ptr cinfo, size_t sizeofobject ) {
  130. return (void FAR *) malloc( sizeofobject );
  131. }
  132. GLOBAL void
  133. jpeg_free_large( j_common_ptr cinfo, void FAR * object, size_t sizeofobject ) {
  134. free( object );
  135. }
  136. /*
  137. * This routine computes the total memory space available for allocation.
  138. * It's impossible to do this in a portable way; our current solution is
  139. * to make the user tell us (with a default value set at compile time).
  140. * If you can actually get the available space, it's a good idea to subtract
  141. * a slop factor of 5% or so.
  142. */
  143. #ifndef DEFAULT_MAX_MEM /* so can override from makefile */
  144. #define DEFAULT_MAX_MEM 1000000L /* default: one megabyte */
  145. #endif
  146. GLOBAL long
  147. jpeg_mem_available( j_common_ptr cinfo, long min_bytes_needed,
  148. long max_bytes_needed, long already_allocated ) {
  149. return cinfo->mem->max_memory_to_use - already_allocated;
  150. }
  151. /*
  152. * Backing store (temporary file) management.
  153. * Backing store objects are only used when the value returned by
  154. * jpeg_mem_available is less than the total space needed. You can dispense
  155. * with these routines if you have plenty of virtual memory; see jmemnobs.c.
  156. */
  157. METHODDEF void
  158. read_backing_store( j_common_ptr cinfo, backing_store_ptr info,
  159. void FAR * buffer_address,
  160. long file_offset, long byte_count ) {
  161. if ( fseek( info->temp_file, file_offset, SEEK_SET ) ) {
  162. ERREXIT( cinfo, JERR_TFILE_SEEK );
  163. }
  164. if ( JFREAD( info->temp_file, buffer_address, byte_count )
  165. != (size_t) byte_count ) {
  166. ERREXIT( cinfo, JERR_TFILE_READ );
  167. }
  168. }
  169. METHODDEF void
  170. write_backing_store( j_common_ptr cinfo, backing_store_ptr info,
  171. void FAR * buffer_address,
  172. long file_offset, long byte_count ) {
  173. if ( fseek( info->temp_file, file_offset, SEEK_SET ) ) {
  174. ERREXIT( cinfo, JERR_TFILE_SEEK );
  175. }
  176. if ( JFWRITE( info->temp_file, buffer_address, byte_count )
  177. != (size_t) byte_count ) {
  178. ERREXIT( cinfo, JERR_TFILE_WRITE );
  179. }
  180. }
  181. METHODDEF void
  182. close_backing_store( j_common_ptr cinfo, backing_store_ptr info ) {
  183. fclose( info->temp_file );/* close the file */
  184. unlink( info->temp_name );/* delete the file */
  185. /* If your system doesn't have unlink(), use remove() instead.
  186. * remove() is the ANSI-standard name for this function, but if
  187. * your system was ANSI you'd be using jmemansi.c, right?
  188. */
  189. TRACEMSS( cinfo, 1, JTRC_TFILE_CLOSE, info->temp_name );
  190. }
  191. /*
  192. * Initial opening of a backing-store object.
  193. */
  194. GLOBAL void
  195. jpeg_open_backing_store( j_common_ptr cinfo, backing_store_ptr info,
  196. long total_bytes_needed ) {
  197. select_file_name( info->temp_name );
  198. if ( ( info->temp_file = fopen( info->temp_name, RW_BINARY ) ) == NULL ) {
  199. ERREXITS( cinfo, JERR_TFILE_CREATE, info->temp_name );
  200. }
  201. info->read_backing_store = read_backing_store;
  202. info->write_backing_store = write_backing_store;
  203. info->close_backing_store = close_backing_store;
  204. TRACEMSS( cinfo, 1, JTRC_TFILE_OPEN, info->temp_name );
  205. }
  206. /*
  207. * These routines take care of any system-dependent initialization and
  208. * cleanup required.
  209. */
  210. GLOBAL long
  211. jpeg_mem_init( j_common_ptr cinfo ) {
  212. next_file_num = 0; /* initialize temp file name generator */
  213. return DEFAULT_MAX_MEM; /* default for max_memory_to_use */
  214. }
  215. GLOBAL void
  216. jpeg_mem_term( j_common_ptr cinfo ) {
  217. /* no work */
  218. }