Blame gtkmm-osx/jpeg-6b/jmemmac.c

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