Back to index

plt-scheme  4.2.1
jmemmac.c
Go to the documentation of this file.
00001 /*
00002  * jmemmac.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  * jmemmac.c provides an Apple Macintosh implementation of the system-
00009  * dependent portion of the JPEG memory manager.
00010  *
00011  * If you use jmemmac.c, then you must define USE_MAC_MEMMGR in the
00012  * JPEG_INTERNALS part of jconfig.h.
00013  *
00014  * jmemmac.c uses the Macintosh toolbox routines NewPtr and DisposePtr
00015  * instead of malloc and free.  It accurately determines the amount of
00016  * memory available by using CompactMem.  Notice that if left to its
00017  * own devices, this code can chew up all available space in the
00018  * application's zone, with the exception of the rather small "slop"
00019  * factor computed in jpeg_mem_available().  The application can ensure
00020  * that more space is left over by reducing max_memory_to_use.
00021  *
00022  * Large images are swapped to disk using temporary files and System 7.0+'s
00023  * temporary folder functionality.
00024  *
00025  * Note that jmemmac.c depends on two features of MacOS that were first
00026  * introduced in System 7: FindFolder and the FSSpec-based calls.
00027  * If your application uses jmemmac.c and is run under System 6 or earlier,
00028  * and the jpeg library decides it needs a temporary file, it will abort,
00029  * printing error messages about requiring System 7.  (If no temporary files
00030  * are created, it will run fine.)
00031  *
00032  * If you want to use jmemmac.c in an application that might be used with
00033  * System 6 or earlier, then you should remove dependencies on FindFolder
00034  * and the FSSpec calls.  You will need to replace FindFolder with some
00035  * other mechanism for finding a place to put temporary files, and you
00036  * should replace the FSSpec calls with their HFS equivalents:
00037  *
00038  *     FSpDelete     ->  HDelete
00039  *     FSpGetFInfo   ->  HGetFInfo
00040  *     FSpCreate     ->  HCreate
00041  *     FSpOpenDF     ->  HOpen      *** Note: not HOpenDF ***
00042  *     FSMakeFSSpec  ->  (fill in spec by hand.)
00043  *
00044  * (Use HOpen instead of HOpenDF.  HOpen is just a glue-interface to PBHOpen,
00045  * which is on all HFS macs.  HOpenDF is a System 7 addition which avoids the
00046  * ages-old problem of names starting with a period.)
00047  *
00048  * Contributed by Sam Bushell (jsam@iagu.on.net) and
00049  * Dan Gildor (gyld@in-touch.com).
00050  */
00051 
00052 #define JPEG_INTERNALS
00053 #include "jinclude.h"
00054 #include "jpeglib.h"
00055 #include "jmemsys.h"    /* import the system-dependent declarations */
00056 
00057 #ifndef USE_MAC_MEMMGR      /* make sure user got configuration right */
00058   You forgot to define USE_MAC_MEMMGR in jconfig.h. /* deliberate syntax error */
00059 #endif
00060 
00061 #include <Memory.h>     /* we use the MacOS memory manager */
00062 #include <Files.h>      /* we use the MacOS File stuff */
00063 #include <Folders.h>    /* we use the MacOS HFS stuff */
00064 #include <Script.h>     /* for smSystemScript */
00065 #include <Gestalt.h>    /* we use Gestalt to test for specific functionality */
00066 
00067 #ifndef TEMP_FILE_NAME             /* can override from jconfig.h or Makefile */
00068 #define TEMP_FILE_NAME  "JPG%03d.TMP"
00069 #endif
00070 
00071 static int next_file_num;   /* to distinguish among several temp files */
00072 
00073 
00074 /*
00075  * Memory allocation and freeing are controlled by the MacOS library
00076  * routines NewPtr() and DisposePtr(), which allocate fixed-address
00077  * storage.  Unfortunately, the IJG library isn't smart enough to cope
00078  * with relocatable storage.
00079  */
00080 
00081 GLOBAL(void *)
00082 jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
00083 {
00084   return (void *) NewPtr(sizeofobject);
00085 }
00086 
00087 GLOBAL(void)
00088 jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
00089 {
00090   DisposePtr((Ptr) object);
00091 }
00092 
00093 
00094 /*
00095  * "Large" objects are treated the same as "small" ones.
00096  * NB: we include FAR keywords in the routine declarations simply for
00097  * consistency with the rest of the IJG code; FAR should expand to empty
00098  * on rational architectures like the Mac.
00099  */
00100 
00101 GLOBAL(void FAR *)
00102 jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
00103 {
00104   return (void FAR *) NewPtr(sizeofobject);
00105 }
00106 
00107 GLOBAL(void)
00108 jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
00109 {
00110   DisposePtr((Ptr) object);
00111 }
00112 
00113 
00114 /*
00115  * This routine computes the total memory space available for allocation.
00116  */
00117 
00118 GLOBAL(long)
00119 jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
00120                   long max_bytes_needed, long already_allocated)
00121 {
00122   long limit = cinfo->mem->max_memory_to_use - already_allocated;
00123   long slop, mem;
00124 
00125   /* Don't ask for more than what application has told us we may use */
00126   if (max_bytes_needed > limit && limit > 0)
00127     max_bytes_needed = limit;
00128   /* Find whether there's a big enough free block in the heap.
00129    * CompactMem tries to create a contiguous block of the requested size,
00130    * and then returns the size of the largest free block (which could be
00131    * much more or much less than we asked for).
00132    * We add some slop to ensure we don't use up all available memory.
00133    */
00134   slop = max_bytes_needed / 16 + 32768L;
00135   mem = CompactMem(max_bytes_needed + slop) - slop;
00136   if (mem < 0)
00137     mem = 0;                /* sigh, couldn't even get the slop */
00138   /* Don't take more than the application says we can have */
00139   if (mem > limit && limit > 0)
00140     mem = limit;
00141   return mem;
00142 }
00143 
00144 
00145 /*
00146  * Backing store (temporary file) management.
00147  * Backing store objects are only used when the value returned by
00148  * jpeg_mem_available is less than the total space needed.  You can dispense
00149  * with these routines if you have plenty of virtual memory; see jmemnobs.c.
00150  */
00151 
00152 
00153 METHODDEF(void)
00154 read_backing_store (j_common_ptr cinfo, backing_store_ptr info,
00155                   void FAR * buffer_address,
00156                   long file_offset, long byte_count)
00157 {
00158   long bytes = byte_count;
00159   long retVal;
00160 
00161   if ( SetFPos ( info->temp_file, fsFromStart, file_offset ) != noErr )
00162     ERREXIT(cinfo, JERR_TFILE_SEEK);
00163 
00164   retVal = FSRead ( info->temp_file, &bytes,
00165                   (unsigned char *) buffer_address );
00166   if ( retVal != noErr || bytes != byte_count )
00167     ERREXIT(cinfo, JERR_TFILE_READ);
00168 }
00169 
00170 
00171 METHODDEF(void)
00172 write_backing_store (j_common_ptr cinfo, backing_store_ptr info,
00173                    void FAR * buffer_address,
00174                    long file_offset, long byte_count)
00175 {
00176   long bytes = byte_count;
00177   long retVal;
00178 
00179   if ( SetFPos ( info->temp_file, fsFromStart, file_offset ) != noErr )
00180     ERREXIT(cinfo, JERR_TFILE_SEEK);
00181 
00182   retVal = FSWrite ( info->temp_file, &bytes,
00183                    (unsigned char *) buffer_address );
00184   if ( retVal != noErr || bytes != byte_count )
00185     ERREXIT(cinfo, JERR_TFILE_WRITE);
00186 }
00187 
00188 
00189 METHODDEF(void)
00190 close_backing_store (j_common_ptr cinfo, backing_store_ptr info)
00191 {
00192   FSClose ( info->temp_file );
00193   FSpDelete ( &(info->tempSpec) );
00194 }
00195 
00196 
00197 /*
00198  * Initial opening of a backing-store object.
00199  *
00200  * This version uses FindFolder to find the Temporary Items folder,
00201  * and puts the temporary file in there.
00202  */
00203 
00204 GLOBAL(void)
00205 jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
00206                       long total_bytes_needed)
00207 {
00208   short         tmpRef, vRefNum;
00209   long          dirID;
00210   FInfo         finderInfo;
00211   FSSpec        theSpec;
00212   Str255        fName;
00213   OSErr         osErr;
00214   long          gestaltResponse = 0;
00215 
00216   /* Check that FSSpec calls are available. */
00217   osErr = Gestalt( gestaltFSAttr, &gestaltResponse );
00218   if ( ( osErr != noErr )
00219        || !( gestaltResponse & (1<<gestaltHasFSSpecCalls) ) )
00220     ERREXITS(cinfo, JERR_TFILE_CREATE, "- System 7.0 or later required");
00221   /* TO DO: add a proper error message to jerror.h. */
00222 
00223   /* Check that FindFolder is available. */
00224   osErr = Gestalt( gestaltFindFolderAttr, &gestaltResponse );
00225   if ( ( osErr != noErr )
00226        || !( gestaltResponse & (1<<gestaltFindFolderPresent) ) )
00227     ERREXITS(cinfo, JERR_TFILE_CREATE, "- System 7.0 or later required.");
00228   /* TO DO: add a proper error message to jerror.h. */
00229 
00230   osErr = FindFolder ( kOnSystemDisk, kTemporaryFolderType, kCreateFolder,
00231                        &vRefNum, &dirID );
00232   if ( osErr != noErr )
00233     ERREXITS(cinfo, JERR_TFILE_CREATE, "- temporary items folder unavailable");
00234   /* TO DO: Try putting the temp files somewhere else. */
00235 
00236   /* Keep generating file names till we find one that's not in use */
00237   for (;;) {
00238     next_file_num++;        /* advance counter */
00239 
00240     sprintf(info->temp_name, TEMP_FILE_NAME, next_file_num);
00241     strcpy ( (Ptr)fName+1, info->temp_name );
00242     *fName = strlen (info->temp_name);
00243     osErr = FSMakeFSSpec ( vRefNum, dirID, fName, &theSpec );
00244 
00245     if ( (osErr = FSpGetFInfo ( &theSpec, &finderInfo ) ) != noErr )
00246       break;
00247   }
00248 
00249   osErr = FSpCreate ( &theSpec, '????', '????', smSystemScript );
00250   if ( osErr != noErr )
00251     ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name);
00252 
00253   osErr = FSpOpenDF ( &theSpec, fsRdWrPerm, &(info->temp_file) );
00254   if ( osErr != noErr )
00255     ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name);
00256 
00257   info->tempSpec = theSpec;
00258 
00259   info->read_backing_store = read_backing_store;
00260   info->write_backing_store = write_backing_store;
00261   info->close_backing_store = close_backing_store;
00262   TRACEMSS(cinfo, 1, JTRC_TFILE_OPEN, info->temp_name);
00263 }
00264 
00265 
00266 /*
00267  * These routines take care of any system-dependent initialization and
00268  * cleanup required.
00269  */
00270 
00271 GLOBAL(long)
00272 jpeg_mem_init (j_common_ptr cinfo)
00273 {
00274   next_file_num = 0;
00275 
00276   /* max_memory_to_use will be initialized to FreeMem()'s result;
00277    * the calling application might later reduce it, for example
00278    * to leave room to invoke multiple JPEG objects.
00279    * Note that FreeMem returns the total number of free bytes;
00280    * it may not be possible to allocate a single block of this size.
00281    */
00282   return FreeMem();
00283 }
00284 
00285 GLOBAL(void)
00286 jpeg_mem_term (j_common_ptr cinfo)
00287 {
00288   /* no work */
00289 }