home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / compresn / jpegv3sr / jmemname.c < prev    next >
C/C++ Source or Header  |  1992-03-17  |  8KB  |  249 lines

  1. /*
  2.  * jmemname.c  (jmemsys.c)
  3.  *
  4.  * Copyright (C) 1992, 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.  
  15. #include "jinclude.h"
  16. #include "jmemsys.h"
  17.  
  18. #ifdef INCLUDES_ARE_ANSI
  19. #include <stdlib.h>        /* to declare malloc(), free() */
  20. #else
  21. extern void * malloc PP((size_t size));
  22. extern void free PP((void *ptr));
  23. #endif
  24.  
  25. #ifndef SEEK_SET        /* pre-ANSI systems may not define this; */
  26. #define SEEK_SET  0        /* if not, assume 0 is correct */
  27. #endif
  28.  
  29. #ifdef DONT_USE_B_MODE        /* define mode parameters for fopen() */
  30. #define READ_BINARY    "r"
  31. #define RW_BINARY    "w+"
  32. #else
  33. #define READ_BINARY    "rb"
  34. #define RW_BINARY    "w+b"
  35. #endif
  36.  
  37.  
  38. static external_methods_ptr methods; /* saved for access to error_exit */
  39.  
  40. static long total_used;        /* total memory requested so far */
  41.  
  42.  
  43. /*
  44.  * Selection of a file name for a temporary file.
  45.  * This is system-dependent!
  46.  *
  47.  * The code as given is suitable for most Unix systems, and it is easily
  48.  * modified for most non-Unix systems.  Some notes:
  49.  *  1.  The temp file is created in the directory named by TEMP_DIRECTORY.
  50.  *      The default value is /usr/tmp, which is the conventional place for
  51.  *      creating large temp files on Unix.  On other systems you'll probably
  52.  *      want to change the file location.  You can do this by editing the
  53.  *      #define, or by defining TEMP_DIRECTORY in CFLAGS in the Makefile.
  54.  *      For example, you might say
  55.  *          CFLAGS= ... '-DTEMP_DIRECTORY="/tmp/"'
  56.  *      Note that double quotes are needed in the text of the macro.
  57.  *      With most make systems you have to put single quotes around the
  58.  *      -D construct to preserve the double quotes.
  59.  *    (Amiga SAS C has trouble with ":" and such in command-line options,
  60.  *    so we've put in a special case for the preferred Amiga temp directory.)
  61.  *
  62.  *  2.  If you need to change the file name as well as its location,
  63.  *      you can override the TEMP_FILE_NAME macro.  (Note that this is
  64.  *      actually a printf format string; it must contain %s and %d.)
  65.  *      Few people should need to do this.
  66.  *
  67.  *  3.  mktemp() is used to ensure that multiple processes running
  68.  *      simultaneously won't select the same file names.  If your system
  69.  *      doesn't have mktemp(), define NO_MKTEMP to do it the hard way.
  70.  *
  71.  *  4.  You probably want to define NEED_SIGNAL_CATCHER so that jcmain/jdmain
  72.  *      will cause the temp files to be removed if you stop the program early.
  73.  */
  74.  
  75. #ifndef TEMP_DIRECTORY        /* so can override from Makefile */
  76. #ifdef AMIGA
  77. #define TEMP_DIRECTORY  "JPEGTMP:"  /* recommended setting for Amiga */
  78. #else
  79. #define TEMP_DIRECTORY  "/usr/tmp/" /* recommended setting for Unix */
  80. #endif
  81. #endif
  82.  
  83. static int next_file_num;    /* to distinguish among several temp files */
  84.  
  85. #ifdef NO_MKTEMP
  86.  
  87. #ifndef TEMP_FILE_NAME        /* so can override from Makefile */
  88. #define TEMP_FILE_NAME  "%sJPG%03d.TMP"
  89. #endif
  90.  
  91. LOCAL void
  92. select_file_name (char * fname)
  93. {
  94.   FILE * tfile;
  95.  
  96.   /* Keep generating file names till we find one that's not in use */
  97.   for (;;) {
  98.     next_file_num++;        /* advance counter */
  99.     sprintf(fname, TEMP_FILE_NAME, TEMP_DIRECTORY, next_file_num);
  100.     if ((tfile = fopen(fname, READ_BINARY)) == NULL)
  101.       break;
  102.     fclose(tfile);        /* oops, it's there; close tfile & try again */
  103.   }
  104. }
  105.  
  106. #else /* ! NO_MKTEMP */
  107.  
  108. /* Note that mktemp() requires the initial filename to end in six X's */
  109. #ifndef TEMP_FILE_NAME        /* so can override from Makefile */
  110. #define TEMP_FILE_NAME  "%sJPG%dXXXXXX"
  111. #endif
  112.  
  113. LOCAL void
  114. select_file_name (char * fname)
  115. {
  116.   next_file_num++;        /* advance counter */
  117.   sprintf(fname, TEMP_FILE_NAME, TEMP_DIRECTORY, next_file_num);
  118.   mktemp(fname);        /* make sure file name is unique */
  119.   /* mktemp replaces the trailing XXXXXX with a unique string of characters */
  120. }
  121.  
  122. #endif /* NO_MKTEMP */
  123.  
  124.  
  125. /*
  126.  * Memory allocation and freeing are controlled by the regular library
  127.  * routines malloc() and free().
  128.  */
  129.  
  130. GLOBAL void *
  131. jget_small (size_t sizeofobject)
  132. {
  133.   total_used += sizeofobject;
  134.   return (void *) malloc(sizeofobject);
  135. }
  136.  
  137. GLOBAL void
  138. jfree_small (void * object)
  139. {
  140.   free(object);
  141. }
  142.  
  143. /*
  144.  * We assume NEED_FAR_POINTERS is not defined and so the separate entry points
  145.  * jget_large, jfree_large are not needed.
  146.  */
  147.  
  148.  
  149. /*
  150.  * This routine computes the total memory space available for allocation.
  151.  * It's impossible to do this in a portable way; our current solution is
  152.  * to make the user tell us (with a default value set at compile time).
  153.  * If you can actually get the available space, it's a good idea to subtract
  154.  * a slop factor of 5% or so.
  155.  */
  156.  
  157. #ifndef DEFAULT_MAX_MEM        /* so can override from makefile */
  158. #define DEFAULT_MAX_MEM        1000000L /* default: one megabyte */
  159. #endif
  160.  
  161. GLOBAL long
  162. jmem_available (long min_bytes_needed, long max_bytes_needed)
  163. {
  164.   return methods->max_memory_to_use - total_used;
  165. }
  166.  
  167.  
  168. /*
  169.  * Backing store (temporary file) management.
  170.  * Backing store objects are only used when the value returned by
  171.  * jmem_available is less than the total space needed.  You can dispense
  172.  * with these routines if you have plenty of virtual memory; see jmemnobs.c.
  173.  */
  174.  
  175.  
  176. METHODDEF void
  177. read_backing_store (backing_store_ptr info, void FAR * buffer_address,
  178.             long file_offset, long byte_count)
  179. {
  180.   if (fseek(info->temp_file, file_offset, SEEK_SET))
  181.     ERREXIT(methods, "fseek failed on temporary file");
  182.   if (JFREAD(info->temp_file, buffer_address, byte_count)
  183.       != (size_t) byte_count)
  184.     ERREXIT(methods, "fread failed on temporary file");
  185. }
  186.  
  187.  
  188. METHODDEF void
  189. write_backing_store (backing_store_ptr info, void FAR * buffer_address,
  190.              long file_offset, long byte_count)
  191. {
  192.   if (fseek(info->temp_file, file_offset, SEEK_SET))
  193.     ERREXIT(methods, "fseek failed on temporary file");
  194.   if (JFWRITE(info->temp_file, buffer_address, byte_count)
  195.       != (size_t) byte_count)
  196.     ERREXIT(methods, "fwrite failed on temporary file --- out of disk space?");
  197. }
  198.  
  199.  
  200. METHODDEF void
  201. close_backing_store (backing_store_ptr info)
  202. {
  203.   fclose(info->temp_file);    /* close the file */
  204.   unlink(info->temp_name);    /* delete the file */
  205. /* If your system doesn't have unlink(), use remove() instead.
  206.  * remove() is the ANSI-standard name for this function, but if
  207.  * your system was ANSI you'd be using jmemansi.c, right?
  208.  */
  209. }
  210.  
  211.  
  212. GLOBAL void
  213. jopen_backing_store (backing_store_ptr info, long total_bytes_needed)
  214. {
  215.   char tracemsg[TEMP_NAME_LENGTH+40];
  216.  
  217.   select_file_name(info->temp_name);
  218.   if ((info->temp_file = fopen(info->temp_name, RW_BINARY)) == NULL)
  219.     ERREXIT(methods, "Failed to create temporary file");
  220.   info->read_backing_store = read_backing_store;
  221.   info->write_backing_store = write_backing_store;
  222.   info->close_backing_store = close_backing_store;
  223.   /* hack to get around TRACEMS' inability to handle string parameters */
  224.   sprintf(tracemsg, "Using temp file %s", info->temp_name);
  225.   TRACEMS(methods, 1, tracemsg);
  226. }
  227.  
  228.  
  229. /*
  230.  * These routines take care of any system-dependent initialization and
  231.  * cleanup required.  Keep in mind that jmem_term may be called more than
  232.  * once.
  233.  */
  234.  
  235. GLOBAL void
  236. jmem_init (external_methods_ptr emethods)
  237. {
  238.   methods = emethods;        /* save struct addr for error exit access */
  239.   emethods->max_memory_to_use = DEFAULT_MAX_MEM;
  240.   total_used = 0;
  241.   next_file_num = 0;
  242. }
  243.  
  244. GLOBAL void
  245. jmem_term (void)
  246. {
  247.   /* no work */
  248. }
  249.