home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / GRAPHICS / JPEGSRC.V4.lzh / jmemsys.c < prev    next >
Text File  |  1993-01-14  |  8KB  |  254 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. #elif defined (OSK)
  79. #define TEMP_DIRECTORY  "/dd/TEMP"
  80. #else
  81. #define TEMP_DIRECTORY  "/usr/tmp/" /* recommended setting for Unix */
  82. #endif
  83. #endif
  84.  
  85. static int next_file_num;    /* to distinguish among several temp files */
  86.  
  87. #ifdef NO_MKTEMP
  88.  
  89. #ifndef TEMP_FILE_NAME        /* so can override from Makefile */
  90. #define TEMP_FILE_NAME  "%sJPG%03d.TMP"
  91. #endif
  92.  
  93. LOCAL void
  94. select_file_name (char * fname)
  95. {
  96.   FILE * tfile;
  97.  
  98.   /* Keep generating file names till we find one that's not in use */
  99.   for (;;) {
  100.     next_file_num++;        /* advance counter */
  101.     sprintf(fname, TEMP_FILE_NAME, TEMP_DIRECTORY, next_file_num);
  102.     if ((tfile = fopen(fname, READ_BINARY)) == NULL)
  103.       break;
  104.     fclose(tfile);        /* oops, it's there; close tfile & try again */
  105.   }
  106. }
  107.  
  108. #else /* ! NO_MKTEMP */
  109.  
  110. /* Note that mktemp() requires the initial filename to end in six X's */
  111. #ifndef TEMP_FILE_NAME        /* so can override from Makefile */
  112. #define TEMP_FILE_NAME  "%sJPG%dXXXXXX"
  113. #endif
  114.  
  115. LOCAL void
  116. select_file_name (char * fname)
  117. {
  118.   next_file_num++;        /* advance counter */
  119.   sprintf(fname, TEMP_FILE_NAME, TEMP_DIRECTORY, next_file_num);
  120.   mktemp(fname);        /* make sure file name is unique */
  121.   /* mktemp replaces the trailing XXXXXX with a unique string of characters */
  122. }
  123.  
  124. #endif /* NO_MKTEMP */
  125.  
  126.  
  127. /*
  128.  * Memory allocation and freeing are controlled by the regular library
  129.  * routines malloc() and free().
  130.  */
  131.  
  132. GLOBAL void *
  133. jget_small (size_t sizeofobject)
  134. {
  135.   total_used += sizeofobject;
  136.   return (void *) malloc(sizeofobject);
  137. }
  138.  
  139. GLOBAL void
  140. jfree_small (void * object)
  141. {
  142.   free(object);
  143. }
  144.  
  145. /*
  146.  * We assume NEED_FAR_POINTERS is not defined and so the separate entry points
  147.  * jget_large, jfree_large are not needed.
  148.  */
  149.  
  150.  
  151. /*
  152.  * This routine computes the total memory space available for allocation.
  153.  * It's impossible to do this in a portable way; our current solution is
  154.  * to make the user tell us (with a default value set at compile time).
  155.  * If you can actually get the available space, it's a good idea to subtract
  156.  * a slop factor of 5% or so.
  157.  */
  158.  
  159. #ifndef DEFAULT_MAX_MEM        /* so can override from makefile */
  160. #define DEFAULT_MAX_MEM        1000000L /* default: one megabyte */
  161. #endif
  162.  
  163. GLOBAL long
  164. jmem_available (long min_bytes_needed, long max_bytes_needed)
  165. {
  166.   return methods->max_memory_to_use - total_used;
  167. }
  168.  
  169.  
  170. /*
  171.  * Backing store (temporary file) management.
  172.  * Backing store objects are only used when the value returned by
  173.  * jmem_available is less than the total space needed.  You can dispense
  174.  * with these routines if you have plenty of virtual memory; see jmemnobs.c.
  175.  */
  176.  
  177.  
  178. METHODDEF void
  179. read_backing_store (backing_store_ptr info, void FAR * buffer_address,
  180.             long file_offset, long byte_count)
  181. {
  182.   if (fseek(info->temp_file, file_offset, SEEK_SET))
  183.     ERREXIT(methods, "fseek failed on temporary file");
  184.   if (JFREAD(info->temp_file, buffer_address, byte_count)
  185.       != (size_t) byte_count)
  186.     ERREXIT(methods, "fread failed on temporary file");
  187. }
  188.  
  189.  
  190. METHODDEF void
  191. write_backing_store (backing_store_ptr info, void FAR * buffer_address,
  192.              long file_offset, long byte_count)
  193. {
  194.   if (fseek(info->temp_file, file_offset, SEEK_SET))
  195.     ERREXIT(methods, "fseek failed on temporary file");
  196.   if (JFWRITE(info->temp_file, buffer_address, byte_count)
  197.       != (size_t) byte_count)
  198.     ERREXIT(methods, "fwrite failed on temporary file --- out of disk space?");
  199. }
  200.  
  201.  
  202. METHODDEF void
  203. close_backing_store (backing_store_ptr info)
  204. {
  205.   fclose(info->temp_file);    /* close the file */
  206.   unlink(info->temp_name);    /* delete the file */
  207. /* If your system doesn't have unlink(), use remove() instead.
  208.  * remove() is the ANSI-standard name for this function, but if
  209.  * your system was ANSI you'd be using jmemansi.c, right?
  210.  */
  211. }
  212.  
  213.  
  214. GLOBAL void
  215. jopen_backing_store (backing_store_ptr info, long total_bytes_needed)
  216. {
  217.   char tracemsg[TEMP_NAME_LENGTH+40];
  218.  
  219.   select_file_name(info->temp_name);
  220.   if ((info->temp_file = fopen(info->temp_name, RW_BINARY)) == NULL) {
  221.     /* hack to get around ERREXIT's inability to handle string parameters */
  222.     sprintf(tracemsg, "Failed to create temporary file %s", info->temp_name);
  223.     ERREXIT(methods, tracemsg);
  224.   }
  225.   info->read_backing_store = read_backing_store;
  226.   info->write_backing_store = write_backing_store;
  227.   info->close_backing_store = close_backing_store;
  228.   /* hack to get around TRACEMS' inability to handle string parameters */
  229.   sprintf(tracemsg, "Using temp file %s", info->temp_name);
  230.   TRACEMS(methods, 1, tracemsg);
  231. }
  232.  
  233.  
  234. /*
  235.  * These routines take care of any system-dependent initialization and
  236.  * cleanup required.  Keep in mind that jmem_term may be called more than
  237.  * once.
  238.  */
  239.  
  240. GLOBAL void
  241. jmem_init (external_methods_ptr emethods)
  242. {
  243.   methods = emethods;        /* save struct addr for error exit access */
  244.   emethods->max_memory_to_use = DEFAULT_MAX_MEM;
  245.   total_used = 0;
  246.   next_file_num = 0;
  247. }
  248.  
  249. GLOBAL void
  250. jmem_term (void)
  251. {
  252.   /* no work */
  253. }
  254.