8 * Copyright (C) 1992-1996, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file provides a simple generic implementation of the system-
13 * dependent portion of the JPEG memory manager. This implementation
14 * assumes that you have the ANSI-standard library routine tmpfile().
15 * Also, the problem of determining the amount of memory available
16 * is shoved onto the user.
19 #define JPEG_INTERNALS
22 #include "jmemsys.h" /* import the system-dependent declarations */
24 #ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */
25 extern void * malloc
JPP((size_t size
));
26 extern void free
JPP((void *ptr
));
29 #ifndef SEEK_SET /* pre-ANSI systems may not define this; */
30 #define SEEK_SET 0 /* if not, assume 0 is correct */
35 * Memory allocation and freeing are controlled by the regular library
36 * routines malloc() and free().
40 jpeg_get_small (j_common_ptr cinfo
, size_t sizeofobject
)
42 return (void *) malloc(sizeofobject
);
46 jpeg_free_small (j_common_ptr cinfo
, void * object
, size_t sizeofobject
)
53 * "Large" objects are treated the same as "small" ones.
54 * NB: although we include FAR keywords in the routine declarations,
55 * this file won't actually work in 80x86 small/medium model; at least,
56 * you probably won't be able to process useful-size images in only 64KB.
60 jpeg_get_large (j_common_ptr cinfo
, size_t sizeofobject
)
62 return (void FAR
*) malloc(sizeofobject
);
66 jpeg_free_large (j_common_ptr cinfo
, void FAR
* object
, size_t sizeofobject
)
73 * This routine computes the total memory space available for allocation.
74 * It's impossible to do this in a portable way; our current solution is
75 * to make the user tell us (with a default value set at compile time).
76 * If you can actually get the available space, it's a good idea to subtract
77 * a slop factor of 5% or so.
80 #ifndef DEFAULT_MAX_MEM /* so can override from makefile */
81 #define DEFAULT_MAX_MEM 1000000L /* default: one megabyte */
85 jpeg_mem_available (j_common_ptr cinfo
, long min_bytes_needed
,
86 long max_bytes_needed
, long already_allocated
)
88 return cinfo
->mem
->max_memory_to_use
- already_allocated
;
93 * Backing store (temporary file) management.
94 * Backing store objects are only used when the value returned by
95 * jpeg_mem_available is less than the total space needed. You can dispense
96 * with these routines if you have plenty of virtual memory; see jmemnobs.c.
101 read_backing_store (j_common_ptr cinfo
, backing_store_ptr info
,
102 void FAR
* buffer_address
,
103 long file_offset
, long byte_count
)
105 if (fseek(info
->temp_file
, file_offset
, SEEK_SET
))
106 ERREXIT(cinfo
, JERR_TFILE_SEEK
);
107 if (JFREAD(info
->temp_file
, buffer_address
, byte_count
)
108 != (size_t) byte_count
)
109 ERREXIT(cinfo
, JERR_TFILE_READ
);
114 write_backing_store (j_common_ptr cinfo
, backing_store_ptr info
,
115 void FAR
* buffer_address
,
116 long file_offset
, long byte_count
)
118 if (fseek(info
->temp_file
, file_offset
, SEEK_SET
))
119 ERREXIT(cinfo
, JERR_TFILE_SEEK
);
120 if (JFWRITE(info
->temp_file
, buffer_address
, byte_count
)
121 != (size_t) byte_count
)
122 ERREXIT(cinfo
, JERR_TFILE_WRITE
);
127 close_backing_store (j_common_ptr cinfo
, backing_store_ptr info
)
129 fclose(info
->temp_file
);
130 /* Since this implementation uses tmpfile() to create the file,
131 * no explicit file deletion is needed.
137 * Initial opening of a backing-store object.
139 * This version uses tmpfile(), which constructs a suitable file name
140 * behind the scenes. We don't have to use info->temp_name[] at all;
141 * indeed, we can't even find out the actual name of the temp file.
145 jpeg_open_backing_store (j_common_ptr cinfo
, backing_store_ptr info
,
146 long total_bytes_needed
)
148 if ((info
->temp_file
= tmpfile()) == NULL
)
149 ERREXITS(cinfo
, JERR_TFILE_CREATE
, "");
150 info
->read_backing_store
= read_backing_store
;
151 info
->write_backing_store
= write_backing_store
;
152 info
->close_backing_store
= close_backing_store
;
157 * These routines take care of any system-dependent initialization and
162 jpeg_mem_init (j_common_ptr cinfo
)
164 return DEFAULT_MAX_MEM
; /* default for max_memory_to_use */
168 jpeg_mem_term (j_common_ptr cinfo
)