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