Back to index

plt-scheme  4.2.1
jmemname.c
Go to the documentation of this file.
00001 /*
00002  * jmemname.c
00003  *
00004  * Copyright (C) 1992-1997, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file provides a generic implementation of the system-dependent
00009  * portion of the JPEG memory manager.  This implementation assumes that
00010  * you must explicitly construct a name for each temp file.
00011  * Also, the problem of determining the amount of memory available
00012  * is shoved onto the user.
00013  */
00014 
00015 #define JPEG_INTERNALS
00016 #include "jinclude.h"
00017 #include "jpeglib.h"
00018 #include "jmemsys.h"        /* import the system-dependent declarations */
00019 
00020 #ifndef HAVE_STDLIB_H              /* <stdlib.h> should declare malloc(),free() */
00021 extern void * malloc JPP((size_t size));
00022 extern void free JPP((void *ptr));
00023 #endif
00024 
00025 #ifndef SEEK_SET            /* pre-ANSI systems may not define this; */
00026 #define SEEK_SET  0         /* if not, assume 0 is correct */
00027 #endif
00028 
00029 #ifdef DONT_USE_B_MODE             /* define mode parameters for fopen() */
00030 #define READ_BINARY  "r"
00031 #define RW_BINARY    "w+"
00032 #else
00033 #ifdef VMS                  /* VMS is very nonstandard */
00034 #define READ_BINARY  "rb", "ctx=stm"
00035 #define RW_BINARY    "w+b", "ctx=stm"
00036 #else                       /* standard ANSI-compliant case */
00037 #define READ_BINARY  "rb"
00038 #define RW_BINARY    "w+b"
00039 #endif
00040 #endif
00041 
00042 
00043 /*
00044  * Selection of a file name for a temporary file.
00045  * This is system-dependent!
00046  *
00047  * The code as given is suitable for most Unix systems, and it is easily
00048  * modified for most non-Unix systems.  Some notes:
00049  *  1.  The temp file is created in the directory named by TEMP_DIRECTORY.
00050  *      The default value is /usr/tmp, which is the conventional place for
00051  *      creating large temp files on Unix.  On other systems you'll probably
00052  *      want to change the file location.  You can do this by editing the
00053  *      #define, or (preferred) by defining TEMP_DIRECTORY in jconfig.h.
00054  *
00055  *  2.  If you need to change the file name as well as its location,
00056  *      you can override the TEMP_FILE_NAME macro.  (Note that this is
00057  *      actually a printf format string; it must contain %s and %d.)
00058  *      Few people should need to do this.
00059  *
00060  *  3.  mktemp() is used to ensure that multiple processes running
00061  *      simultaneously won't select the same file names.  If your system
00062  *      doesn't have mktemp(), define NO_MKTEMP to do it the hard way.
00063  *      (If you don't have <errno.h>, also define NO_ERRNO_H.)
00064  *
00065  *  4.  You probably want to define NEED_SIGNAL_CATCHER so that cjpeg.c/djpeg.c
00066  *      will cause the temp files to be removed if you stop the program early.
00067  */
00068 
00069 #ifndef TEMP_DIRECTORY             /* can override from jconfig.h or Makefile */
00070 #define TEMP_DIRECTORY  "/usr/tmp/" /* recommended setting for Unix */
00071 #endif
00072 
00073 static int next_file_num;   /* to distinguish among several temp files */
00074 
00075 #ifdef NO_MKTEMP
00076 
00077 #ifndef TEMP_FILE_NAME             /* can override from jconfig.h or Makefile */
00078 #define TEMP_FILE_NAME  "%sJPG%03d.TMP"
00079 #endif
00080 
00081 #ifndef NO_ERRNO_H
00082 #include <errno.h>          /* to define ENOENT */
00083 #endif
00084 
00085 /* ANSI C specifies that errno is a macro, but on older systems it's more
00086  * likely to be a plain int variable.  And not all versions of errno.h
00087  * bother to declare it, so we have to in order to be most portable.  Thus:
00088  */
00089 #ifndef errno
00090 extern int errno;
00091 #endif
00092 
00093 
00094 LOCAL(void)
00095 select_file_name (char * fname)
00096 {
00097   FILE * tfile;
00098 
00099   /* Keep generating file names till we find one that's not in use */
00100   for (;;) {
00101     next_file_num++;        /* advance counter */
00102     sprintf(fname, TEMP_FILE_NAME, TEMP_DIRECTORY, next_file_num);
00103     if ((tfile = fopen(fname, READ_BINARY)) == NULL) {
00104       /* fopen could have failed for a reason other than the file not
00105        * being there; for example, file there but unreadable.
00106        * If <errno.h> isn't available, then we cannot test the cause.
00107        */
00108 #ifdef ENOENT
00109       if (errno != ENOENT)
00110        continue;
00111 #endif
00112       break;
00113     }
00114     fclose(tfile);          /* oops, it's there; close tfile & try again */
00115   }
00116 }
00117 
00118 #else /* ! NO_MKTEMP */
00119 
00120 /* Note that mktemp() requires the initial filename to end in six X's */
00121 #ifndef TEMP_FILE_NAME             /* can override from jconfig.h or Makefile */
00122 #define TEMP_FILE_NAME  "%sJPG%dXXXXXX"
00123 #endif
00124 
00125 LOCAL(void)
00126 select_file_name (char * fname)
00127 {
00128   next_file_num++;          /* advance counter */
00129   sprintf(fname, TEMP_FILE_NAME, TEMP_DIRECTORY, next_file_num);
00130   mktemp(fname);            /* make sure file name is unique */
00131   /* mktemp replaces the trailing XXXXXX with a unique string of characters */
00132 }
00133 
00134 #endif /* NO_MKTEMP */
00135 
00136 
00137 /*
00138  * Memory allocation and freeing are controlled by the regular library
00139  * routines malloc() and free().
00140  */
00141 
00142 GLOBAL(void *)
00143 jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
00144 {
00145   return (void *) malloc(sizeofobject);
00146 }
00147 
00148 GLOBAL(void)
00149 jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
00150 {
00151   free(object);
00152 }
00153 
00154 
00155 /*
00156  * "Large" objects are treated the same as "small" ones.
00157  * NB: although we include FAR keywords in the routine declarations,
00158  * this file won't actually work in 80x86 small/medium model; at least,
00159  * you probably won't be able to process useful-size images in only 64KB.
00160  */
00161 
00162 GLOBAL(void FAR *)
00163 jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
00164 {
00165   return (void FAR *) malloc(sizeofobject);
00166 }
00167 
00168 GLOBAL(void)
00169 jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
00170 {
00171   free(object);
00172 }
00173 
00174 
00175 /*
00176  * This routine computes the total memory space available for allocation.
00177  * It's impossible to do this in a portable way; our current solution is
00178  * to make the user tell us (with a default value set at compile time).
00179  * If you can actually get the available space, it's a good idea to subtract
00180  * a slop factor of 5% or so.
00181  */
00182 
00183 #ifndef DEFAULT_MAX_MEM            /* so can override from makefile */
00184 #define DEFAULT_MAX_MEM            1000000L /* default: one megabyte */
00185 #endif
00186 
00187 GLOBAL(long)
00188 jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
00189                   long max_bytes_needed, long already_allocated)
00190 {
00191   return cinfo->mem->max_memory_to_use - already_allocated;
00192 }
00193 
00194 
00195 /*
00196  * Backing store (temporary file) management.
00197  * Backing store objects are only used when the value returned by
00198  * jpeg_mem_available is less than the total space needed.  You can dispense
00199  * with these routines if you have plenty of virtual memory; see jmemnobs.c.
00200  */
00201 
00202 
00203 METHODDEF(void)
00204 read_backing_store (j_common_ptr cinfo, backing_store_ptr info,
00205                   void FAR * buffer_address,
00206                   long file_offset, long byte_count)
00207 {
00208   if (fseek(info->temp_file, file_offset, SEEK_SET))
00209     ERREXIT(cinfo, JERR_TFILE_SEEK);
00210   if (JFREAD(info->temp_file, buffer_address, byte_count)
00211       != (size_t) byte_count)
00212     ERREXIT(cinfo, JERR_TFILE_READ);
00213 }
00214 
00215 
00216 METHODDEF(void)
00217 write_backing_store (j_common_ptr cinfo, backing_store_ptr info,
00218                    void FAR * buffer_address,
00219                    long file_offset, long byte_count)
00220 {
00221   if (fseek(info->temp_file, file_offset, SEEK_SET))
00222     ERREXIT(cinfo, JERR_TFILE_SEEK);
00223   if (JFWRITE(info->temp_file, buffer_address, byte_count)
00224       != (size_t) byte_count)
00225     ERREXIT(cinfo, JERR_TFILE_WRITE);
00226 }
00227 
00228 
00229 METHODDEF(void)
00230 close_backing_store (j_common_ptr cinfo, backing_store_ptr info)
00231 {
00232   fclose(info->temp_file);  /* close the file */
00233   unlink(info->temp_name);  /* delete the file */
00234 /* If your system doesn't have unlink(), use remove() instead.
00235  * remove() is the ANSI-standard name for this function, but if
00236  * your system was ANSI you'd be using jmemansi.c, right?
00237  */
00238   TRACEMSS(cinfo, 1, JTRC_TFILE_CLOSE, info->temp_name);
00239 }
00240 
00241 
00242 /*
00243  * Initial opening of a backing-store object.
00244  */
00245 
00246 GLOBAL(void)
00247 jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
00248                       long total_bytes_needed)
00249 {
00250   select_file_name(info->temp_name);
00251   if ((info->temp_file = fopen(info->temp_name, RW_BINARY)) == NULL)
00252     ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name);
00253   info->read_backing_store = read_backing_store;
00254   info->write_backing_store = write_backing_store;
00255   info->close_backing_store = close_backing_store;
00256   TRACEMSS(cinfo, 1, JTRC_TFILE_OPEN, info->temp_name);
00257 }
00258 
00259 
00260 /*
00261  * These routines take care of any system-dependent initialization and
00262  * cleanup required.
00263  */
00264 
00265 GLOBAL(long)
00266 jpeg_mem_init (j_common_ptr cinfo)
00267 {
00268   next_file_num = 0;        /* initialize temp file name generator */
00269   return DEFAULT_MAX_MEM;   /* default for max_memory_to_use */
00270 }
00271 
00272 GLOBAL(void)
00273 jpeg_mem_term (j_common_ptr cinfo)
00274 {
00275   /* no work */
00276 }