kusano 7d535a
/*
kusano 7d535a
 * jmemmac.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1992-1997, Thomas G. Lane.
kusano 7d535a
 * This file is part of the Independent JPEG Group's software.
kusano 7d535a
 * For conditions of distribution and use, see the accompanying README file.
kusano 7d535a
 *
kusano 7d535a
 * jmemmac.c provides an Apple Macintosh implementation of the system-
kusano 7d535a
 * dependent portion of the JPEG memory manager.
kusano 7d535a
 *
kusano 7d535a
 * If you use jmemmac.c, then you must define USE_MAC_MEMMGR in the
kusano 7d535a
 * JPEG_INTERNALS part of jconfig.h.
kusano 7d535a
 *
kusano 7d535a
 * jmemmac.c uses the Macintosh toolbox routines NewPtr and DisposePtr
kusano 7d535a
 * instead of malloc and free.  It accurately determines the amount of
kusano 7d535a
 * memory available by using CompactMem.  Notice that if left to its
kusano 7d535a
 * own devices, this code can chew up all available space in the
kusano 7d535a
 * application's zone, with the exception of the rather small "slop"
kusano 7d535a
 * factor computed in jpeg_mem_available().  The application can ensure
kusano 7d535a
 * that more space is left over by reducing max_memory_to_use.
kusano 7d535a
 *
kusano 7d535a
 * Large images are swapped to disk using temporary files and System 7.0+'s
kusano 7d535a
 * temporary folder functionality.
kusano 7d535a
 *
kusano 7d535a
 * Note that jmemmac.c depends on two features of MacOS that were first
kusano 7d535a
 * introduced in System 7: FindFolder and the FSSpec-based calls.
kusano 7d535a
 * If your application uses jmemmac.c and is run under System 6 or earlier,
kusano 7d535a
 * and the jpeg library decides it needs a temporary file, it will abort,
kusano 7d535a
 * printing error messages about requiring System 7.  (If no temporary files
kusano 7d535a
 * are created, it will run fine.)
kusano 7d535a
 *
kusano 7d535a
 * If you want to use jmemmac.c in an application that might be used with
kusano 7d535a
 * System 6 or earlier, then you should remove dependencies on FindFolder
kusano 7d535a
 * and the FSSpec calls.  You will need to replace FindFolder with some
kusano 7d535a
 * other mechanism for finding a place to put temporary files, and you
kusano 7d535a
 * should replace the FSSpec calls with their HFS equivalents:
kusano 7d535a
 *
kusano 7d535a
 *     FSpDelete     ->  HDelete
kusano 7d535a
 *     FSpGetFInfo   ->  HGetFInfo
kusano 7d535a
 *     FSpCreate     ->  HCreate
kusano 7d535a
 *     FSpOpenDF     ->  HOpen      *** Note: not HOpenDF ***
kusano 7d535a
 *     FSMakeFSSpec  ->  (fill in spec by hand.)
kusano 7d535a
 *
kusano 7d535a
 * (Use HOpen instead of HOpenDF.  HOpen is just a glue-interface to PBHOpen,
kusano 7d535a
 * which is on all HFS macs.  HOpenDF is a System 7 addition which avoids the
kusano 7d535a
 * ages-old problem of names starting with a period.)
kusano 7d535a
 *
kusano 7d535a
 * Contributed by Sam Bushell (jsam@iagu.on.net) and
kusano 7d535a
 * Dan Gildor (gyld@in-touch.com).
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#define JPEG_INTERNALS
kusano 7d535a
#include "jinclude.h"
kusano 7d535a
#include "jpeglib.h"
kusano 7d535a
#include "jmemsys.h"    /* import the system-dependent declarations */
kusano 7d535a
kusano 7d535a
#ifndef USE_MAC_MEMMGR	/* make sure user got configuration right */
kusano 7d535a
  You forgot to define USE_MAC_MEMMGR in jconfig.h. /* deliberate syntax error */
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#include <memory.h>     /* we use the MacOS memory manager */</memory.h>
kusano 7d535a
#include <files.h>      /* we use the MacOS File stuff */</files.h>
kusano 7d535a
#include <folders.h>    /* we use the MacOS HFS stuff */</folders.h>
kusano 7d535a
#include <script.h>     /* for smSystemScript */</script.h>
kusano 7d535a
#include <gestalt.h>    /* we use Gestalt to test for specific functionality */</gestalt.h>
kusano 7d535a
kusano 7d535a
#ifndef TEMP_FILE_NAME		/* can override from jconfig.h or Makefile */
kusano 7d535a
#define TEMP_FILE_NAME  "JPG%03d.TMP"
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
static int next_file_num;	/* to distinguish among several temp files */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Memory allocation and freeing are controlled by the MacOS library
kusano 7d535a
 * routines NewPtr() and DisposePtr(), which allocate fixed-address
kusano 7d535a
 * storage.  Unfortunately, the IJG library isn't smart enough to cope
kusano 7d535a
 * with relocatable storage.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void *)
kusano 7d535a
jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
kusano 7d535a
{
kusano 7d535a
  return (void *) NewPtr(sizeofobject);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
kusano 7d535a
{
kusano 7d535a
  DisposePtr((Ptr) object);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * "Large" objects are treated the same as "small" ones.
kusano 7d535a
 * NB: we include FAR keywords in the routine declarations simply for
kusano 7d535a
 * consistency with the rest of the IJG code; FAR should expand to empty
kusano 7d535a
 * on rational architectures like the Mac.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void FAR *)
kusano 7d535a
jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
kusano 7d535a
{
kusano 7d535a
  return (void FAR *) NewPtr(sizeofobject);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
kusano 7d535a
{
kusano 7d535a
  DisposePtr((Ptr) object);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * This routine computes the total memory space available for allocation.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(long)
kusano 7d535a
jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
kusano 7d535a
		    long max_bytes_needed, long already_allocated)
kusano 7d535a
{
kusano 7d535a
  long limit = cinfo->mem->max_memory_to_use - already_allocated;
kusano 7d535a
  long slop, mem;
kusano 7d535a
kusano 7d535a
  /* Don't ask for more than what application has told us we may use */
kusano 7d535a
  if (max_bytes_needed > limit && limit > 0)
kusano 7d535a
    max_bytes_needed = limit;
kusano 7d535a
  /* Find whether there's a big enough free block in the heap.
kusano 7d535a
   * CompactMem tries to create a contiguous block of the requested size,
kusano 7d535a
   * and then returns the size of the largest free block (which could be
kusano 7d535a
   * much more or much less than we asked for).
kusano 7d535a
   * We add some slop to ensure we don't use up all available memory.
kusano 7d535a
   */
kusano 7d535a
  slop = max_bytes_needed / 16 + 32768L;
kusano 7d535a
  mem = CompactMem(max_bytes_needed + slop) - slop;
kusano 7d535a
  if (mem < 0)
kusano 7d535a
    mem = 0;			/* sigh, couldn't even get the slop */
kusano 7d535a
  /* Don't take more than the application says we can have */
kusano 7d535a
  if (mem > limit && limit > 0)
kusano 7d535a
    mem = limit;
kusano 7d535a
  return mem;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Backing store (temporary file) management.
kusano 7d535a
 * Backing store objects are only used when the value returned by
kusano 7d535a
 * jpeg_mem_available is less than the total space needed.  You can dispense
kusano 7d535a
 * with these routines if you have plenty of virtual memory; see jmemnobs.c.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
read_backing_store (j_common_ptr cinfo, backing_store_ptr info,
kusano 7d535a
		    void FAR * buffer_address,
kusano 7d535a
		    long file_offset, long byte_count)
kusano 7d535a
{
kusano 7d535a
  long bytes = byte_count;
kusano 7d535a
  long retVal;
kusano 7d535a
kusano 7d535a
  if ( SetFPos ( info->temp_file, fsFromStart, file_offset ) != noErr )
kusano 7d535a
    ERREXIT(cinfo, JERR_TFILE_SEEK);
kusano 7d535a
kusano 7d535a
  retVal = FSRead ( info->temp_file, &bytes,
kusano 7d535a
		    (unsigned char *) buffer_address );
kusano 7d535a
  if ( retVal != noErr || bytes != byte_count )
kusano 7d535a
    ERREXIT(cinfo, JERR_TFILE_READ);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
write_backing_store (j_common_ptr cinfo, backing_store_ptr info,
kusano 7d535a
		     void FAR * buffer_address,
kusano 7d535a
		     long file_offset, long byte_count)
kusano 7d535a
{
kusano 7d535a
  long bytes = byte_count;
kusano 7d535a
  long retVal;
kusano 7d535a
kusano 7d535a
  if ( SetFPos ( info->temp_file, fsFromStart, file_offset ) != noErr )
kusano 7d535a
    ERREXIT(cinfo, JERR_TFILE_SEEK);
kusano 7d535a
kusano 7d535a
  retVal = FSWrite ( info->temp_file, &bytes,
kusano 7d535a
		     (unsigned char *) buffer_address );
kusano 7d535a
  if ( retVal != noErr || bytes != byte_count )
kusano 7d535a
    ERREXIT(cinfo, JERR_TFILE_WRITE);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
close_backing_store (j_common_ptr cinfo, backing_store_ptr info)
kusano 7d535a
{
kusano 7d535a
  FSClose ( info->temp_file );
kusano 7d535a
  FSpDelete ( &(info->tempSpec) );
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initial opening of a backing-store object.
kusano 7d535a
 *
kusano 7d535a
 * This version uses FindFolder to find the Temporary Items folder,
kusano 7d535a
 * and puts the temporary file in there.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
kusano 7d535a
			 long total_bytes_needed)
kusano 7d535a
{
kusano 7d535a
  short         tmpRef, vRefNum;
kusano 7d535a
  long          dirID;
kusano 7d535a
  FInfo         finderInfo;
kusano 7d535a
  FSSpec        theSpec;
kusano 7d535a
  Str255        fName;
kusano 7d535a
  OSErr         osErr;
kusano 7d535a
  long          gestaltResponse = 0;
kusano 7d535a
kusano 7d535a
  /* Check that FSSpec calls are available. */
kusano 7d535a
  osErr = Gestalt( gestaltFSAttr, &gestaltResponse );
kusano 7d535a
  if ( ( osErr != noErr )
kusano 7d535a
       || !( gestaltResponse & (1<
kusano 7d535a
    ERREXITS(cinfo, JERR_TFILE_CREATE, "- System 7.0 or later required");
kusano 7d535a
  /* TO DO: add a proper error message to jerror.h. */
kusano 7d535a
kusano 7d535a
  /* Check that FindFolder is available. */
kusano 7d535a
  osErr = Gestalt( gestaltFindFolderAttr, &gestaltResponse );
kusano 7d535a
  if ( ( osErr != noErr )
kusano 7d535a
       || !( gestaltResponse & (1<
kusano 7d535a
    ERREXITS(cinfo, JERR_TFILE_CREATE, "- System 7.0 or later required.");
kusano 7d535a
  /* TO DO: add a proper error message to jerror.h. */
kusano 7d535a
kusano 7d535a
  osErr = FindFolder ( kOnSystemDisk, kTemporaryFolderType, kCreateFolder,
kusano 7d535a
                       &vRefNum, &dirID );
kusano 7d535a
  if ( osErr != noErr )
kusano 7d535a
    ERREXITS(cinfo, JERR_TFILE_CREATE, "- temporary items folder unavailable");
kusano 7d535a
  /* TO DO: Try putting the temp files somewhere else. */
kusano 7d535a
kusano 7d535a
  /* Keep generating file names till we find one that's not in use */
kusano 7d535a
  for (;;) {
kusano 7d535a
    next_file_num++;		/* advance counter */
kusano 7d535a
kusano 7d535a
    sprintf(info->temp_name, TEMP_FILE_NAME, next_file_num);
kusano 7d535a
    strcpy ( (Ptr)fName+1, info->temp_name );
kusano 7d535a
    *fName = strlen (info->temp_name);
kusano 7d535a
    osErr = FSMakeFSSpec ( vRefNum, dirID, fName, &theSpec );
kusano 7d535a
kusano 7d535a
    if ( (osErr = FSpGetFInfo ( &theSpec, &finderInfo ) ) != noErr )
kusano 7d535a
      break;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  osErr = FSpCreate ( &theSpec, '????', '????', smSystemScript );
kusano 7d535a
  if ( osErr != noErr )
kusano 7d535a
    ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name);
kusano 7d535a
kusano 7d535a
  osErr = FSpOpenDF ( &theSpec, fsRdWrPerm, &(info->temp_file) );
kusano 7d535a
  if ( osErr != noErr )
kusano 7d535a
    ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name);
kusano 7d535a
kusano 7d535a
  info->tempSpec = theSpec;
kusano 7d535a
kusano 7d535a
  info->read_backing_store = read_backing_store;
kusano 7d535a
  info->write_backing_store = write_backing_store;
kusano 7d535a
  info->close_backing_store = close_backing_store;
kusano 7d535a
  TRACEMSS(cinfo, 1, JTRC_TFILE_OPEN, info->temp_name);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * These routines take care of any system-dependent initialization and
kusano 7d535a
 * cleanup required.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(long)
kusano 7d535a
jpeg_mem_init (j_common_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  next_file_num = 0;
kusano 7d535a
kusano 7d535a
  /* max_memory_to_use will be initialized to FreeMem()'s result;
kusano 7d535a
   * the calling application might later reduce it, for example
kusano 7d535a
   * to leave room to invoke multiple JPEG objects.
kusano 7d535a
   * Note that FreeMem returns the total number of free bytes;
kusano 7d535a
   * it may not be possible to allocate a single block of this size.
kusano 7d535a
   */
kusano 7d535a
  return FreeMem();
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_mem_term (j_common_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  /* no work */
kusano 7d535a
}