4
* Copyright (C) 1992-1997, 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.
8
* jmemmac.c provides an Apple Macintosh implementation of the system-
9
* dependent portion of the JPEG memory manager.
11
* If you use jmemmac.c, then you must define USE_MAC_MEMMGR in the
12
* JPEG_INTERNALS part of jconfig.h.
14
* jmemmac.c uses the Macintosh toolbox routines NewPtr and DisposePtr
15
* instead of malloc and free. It accurately determines the amount of
16
* memory available by using CompactMem. Notice that if left to its
17
* own devices, this code can chew up all available space in the
18
* application's zone, with the exception of the rather small "slop"
19
* factor computed in jpeg_mem_available(). The application can ensure
20
* that more space is left over by reducing max_memory_to_use.
22
* Large images are swapped to disk using temporary files and System 7.0+'s
23
* temporary folder functionality.
25
* Note that jmemmac.c depends on two features of MacOS that were first
26
* introduced in System 7: FindFolder and the FSSpec-based calls.
27
* If your application uses jmemmac.c and is run under System 6 or earlier,
28
* and the jpeg library decides it needs a temporary file, it will abort,
29
* printing error messages about requiring System 7. (If no temporary files
30
* are created, it will run fine.)
32
* If you want to use jmemmac.c in an application that might be used with
33
* System 6 or earlier, then you should remove dependencies on FindFolder
34
* and the FSSpec calls. You will need to replace FindFolder with some
35
* other mechanism for finding a place to put temporary files, and you
36
* should replace the FSSpec calls with their HFS equivalents:
38
* FSpDelete -> HDelete
39
* FSpGetFInfo -> HGetFInfo
40
* FSpCreate -> HCreate
41
* FSpOpenDF -> HOpen *** Note: not HOpenDF ***
42
* FSMakeFSSpec -> (fill in spec by hand.)
44
* (Use HOpen instead of HOpenDF. HOpen is just a glue-interface to PBHOpen,
45
* which is on all HFS macs. HOpenDF is a System 7 addition which avoids the
46
* ages-old problem of names starting with a period.)
48
* Contributed by Sam Bushell (jsam@iagu.on.net) and
49
* Dan Gildor (gyld@in-touch.com).
52
#define JPEG_INTERNALS
55
#include "jmemsys.h" /* import the system-dependent declarations */
57
#ifndef USE_MAC_MEMMGR /* make sure user got configuration right */
58
You forgot to define USE_MAC_MEMMGR in jconfig.h. /* deliberate syntax error */
61
#include <Memory.h> /* we use the MacOS memory manager */
62
#include <Files.h> /* we use the MacOS File stuff */
63
#include <Folders.h> /* we use the MacOS HFS stuff */
64
#include <Script.h> /* for smSystemScript */
65
#include <Gestalt.h> /* we use Gestalt to test for specific functionality */
67
#ifndef TEMP_FILE_NAME /* can override from jconfig.h or Makefile */
68
#define TEMP_FILE_NAME "JPG%03d.TMP"
71
static int next_file_num; /* to distinguish among several temp files */
75
* Memory allocation and freeing are controlled by the MacOS library
76
* routines NewPtr() and DisposePtr(), which allocate fixed-address
77
* storage. Unfortunately, the IJG library isn't smart enough to cope
78
* with relocatable storage.
82
jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
84
return (void *) NewPtr(sizeofobject);
88
jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
90
DisposePtr((Ptr) object);
95
* "Large" objects are treated the same as "small" ones.
96
* NB: we include FAR keywords in the routine declarations simply for
97
* consistency with the rest of the IJG code; FAR should expand to empty
98
* on rational architectures like the Mac.
102
jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
104
return (void FAR *) NewPtr(sizeofobject);
108
jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
110
DisposePtr((Ptr) object);
115
* This routine computes the total memory space available for allocation.
119
jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
120
long max_bytes_needed, long already_allocated)
122
long limit = cinfo->mem->max_memory_to_use - already_allocated;
125
/* Don't ask for more than what application has told us we may use */
126
if (max_bytes_needed > limit && limit > 0)
127
max_bytes_needed = limit;
128
/* Find whether there's a big enough free block in the heap.
129
* CompactMem tries to create a contiguous block of the requested size,
130
* and then returns the size of the largest free block (which could be
131
* much more or much less than we asked for).
132
* We add some slop to ensure we don't use up all available memory.
134
slop = max_bytes_needed / 16 + 32768L;
135
mem = CompactMem(max_bytes_needed + slop) - slop;
137
mem = 0; /* sigh, couldn't even get the slop */
138
/* Don't take more than the application says we can have */
139
if (mem > limit && limit > 0)
146
* Backing store (temporary file) management.
147
* Backing store objects are only used when the value returned by
148
* jpeg_mem_available is less than the total space needed. You can dispense
149
* with these routines if you have plenty of virtual memory; see jmemnobs.c.
154
read_backing_store (j_common_ptr cinfo, backing_store_ptr info,
155
void FAR * buffer_address,
156
long file_offset, long byte_count)
158
long bytes = byte_count;
161
if ( SetFPos ( info->temp_file, fsFromStart, file_offset ) != noErr )
162
ERREXIT(cinfo, JERR_TFILE_SEEK);
164
retVal = FSRead ( info->temp_file, &bytes,
165
(unsigned char *) buffer_address );
166
if ( retVal != noErr || bytes != byte_count )
167
ERREXIT(cinfo, JERR_TFILE_READ);
172
write_backing_store (j_common_ptr cinfo, backing_store_ptr info,
173
void FAR * buffer_address,
174
long file_offset, long byte_count)
176
long bytes = byte_count;
179
if ( SetFPos ( info->temp_file, fsFromStart, file_offset ) != noErr )
180
ERREXIT(cinfo, JERR_TFILE_SEEK);
182
retVal = FSWrite ( info->temp_file, &bytes,
183
(unsigned char *) buffer_address );
184
if ( retVal != noErr || bytes != byte_count )
185
ERREXIT(cinfo, JERR_TFILE_WRITE);
190
close_backing_store (j_common_ptr cinfo, backing_store_ptr info)
192
FSClose ( info->temp_file );
193
FSpDelete ( &(info->tempSpec) );
198
* Initial opening of a backing-store object.
200
* This version uses FindFolder to find the Temporary Items folder,
201
* and puts the temporary file in there.
205
jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
206
long total_bytes_needed)
208
short tmpRef, vRefNum;
214
long gestaltResponse = 0;
216
/* Check that FSSpec calls are available. */
217
osErr = Gestalt( gestaltFSAttr, &gestaltResponse );
218
if ( ( osErr != noErr )
219
|| !( gestaltResponse & (1<<gestaltHasFSSpecCalls) ) )
220
ERREXITS(cinfo, JERR_TFILE_CREATE, "- System 7.0 or later required");
221
/* TO DO: add a proper error message to jerror.h. */
223
/* Check that FindFolder is available. */
224
osErr = Gestalt( gestaltFindFolderAttr, &gestaltResponse );
225
if ( ( osErr != noErr )
226
|| !( gestaltResponse & (1<<gestaltFindFolderPresent) ) )
227
ERREXITS(cinfo, JERR_TFILE_CREATE, "- System 7.0 or later required.");
228
/* TO DO: add a proper error message to jerror.h. */
230
osErr = FindFolder ( kOnSystemDisk, kTemporaryFolderType, kCreateFolder,
232
if ( osErr != noErr )
233
ERREXITS(cinfo, JERR_TFILE_CREATE, "- temporary items folder unavailable");
234
/* TO DO: Try putting the temp files somewhere else. */
236
/* Keep generating file names till we find one that's not in use */
238
next_file_num++; /* advance counter */
240
sprintf(info->temp_name, TEMP_FILE_NAME, next_file_num);
241
strcpy ( (Ptr)fName+1, info->temp_name );
242
*fName = strlen (info->temp_name);
243
osErr = FSMakeFSSpec ( vRefNum, dirID, fName, &theSpec );
245
if ( (osErr = FSpGetFInfo ( &theSpec, &finderInfo ) ) != noErr )
249
osErr = FSpCreate ( &theSpec, '????', '????', smSystemScript );
250
if ( osErr != noErr )
251
ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name);
253
osErr = FSpOpenDF ( &theSpec, fsRdWrPerm, &(info->temp_file) );
254
if ( osErr != noErr )
255
ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name);
257
info->tempSpec = theSpec;
259
info->read_backing_store = read_backing_store;
260
info->write_backing_store = write_backing_store;
261
info->close_backing_store = close_backing_store;
262
TRACEMSS(cinfo, 1, JTRC_TFILE_OPEN, info->temp_name);
267
* These routines take care of any system-dependent initialization and
272
jpeg_mem_init (j_common_ptr cinfo)
276
/* max_memory_to_use will be initialized to FreeMem()'s result;
277
* the calling application might later reduce it, for example
278
* to leave room to invoke multiple JPEG objects.
279
* Note that FreeMem returns the total number of free bytes;
280
* it may not be possible to allocate a single block of this size.
286
jpeg_mem_term (j_common_ptr cinfo)