Overhauled GRUB menus to reduce number of entries, mainly by making use
[cake.git] / compiler / libjpeg / main / jmemname.c
blob76cc48e8f9db0c24ce67f364f783da56bbf182af
1 /*
2 $Id$
3 */
5 /*
6 * jmemname.c
8 * Copyright (C) 1992-1997, 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 generic implementation of the system-dependent
13 * portion of the JPEG memory manager. This implementation assumes that
14 * you must explicitly construct a name for each temp file.
15 * Also, the problem of determining the amount of memory available
16 * is shoved onto the user.
19 #define JPEG_INTERNALS
20 #include "jinclude.h"
21 #include "jpeglib.h"
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));
27 #endif
29 #ifndef SEEK_SET /* pre-ANSI systems may not define this; */
30 #define SEEK_SET 0 /* if not, assume 0 is correct */
31 #endif
33 #ifdef DONT_USE_B_MODE /* define mode parameters for fopen() */
34 #define READ_BINARY "r"
35 #define RW_BINARY "w+"
36 #else
37 #ifdef VMS /* VMS is very nonstandard */
38 #define READ_BINARY "rb", "ctx=stm"
39 #define RW_BINARY "w+b", "ctx=stm"
40 #else /* standard ANSI-compliant case */
41 #define READ_BINARY "rb"
42 #define RW_BINARY "w+b"
43 #endif
44 #endif
48 * Selection of a file name for a temporary file.
49 * This is system-dependent!
51 * The code as given is suitable for most Unix systems, and it is easily
52 * modified for most non-Unix systems. Some notes:
53 * 1. The temp file is created in the directory named by TEMP_DIRECTORY.
54 * The default value is /usr/tmp, which is the conventional place for
55 * creating large temp files on Unix. On other systems you'll probably
56 * want to change the file location. You can do this by editing the
57 * #define, or (preferred) by defining TEMP_DIRECTORY in jconfig.h.
59 * 2. If you need to change the file name as well as its location,
60 * you can override the TEMP_FILE_NAME macro. (Note that this is
61 * actually a printf format string; it must contain %s and %d.)
62 * Few people should need to do this.
64 * 3. mktemp() is used to ensure that multiple processes running
65 * simultaneously won't select the same file names. If your system
66 * doesn't have mktemp(), define NO_MKTEMP to do it the hard way.
67 * (If you don't have <errno.h>, also define NO_ERRNO_H.)
69 * 4. You probably want to define NEED_SIGNAL_CATCHER so that cjpeg.c/djpeg.c
70 * will cause the temp files to be removed if you stop the program early.
73 #ifndef TEMP_DIRECTORY /* can override from jconfig.h or Makefile */
74 #define TEMP_DIRECTORY "/usr/tmp/" /* recommended setting for Unix */
75 #endif
77 static int next_file_num; /* to distinguish among several temp files */
79 #ifdef NO_MKTEMP
81 #ifndef TEMP_FILE_NAME /* can override from jconfig.h or Makefile */
82 #define TEMP_FILE_NAME "%sJPG%03d.TMP"
83 #endif
85 #ifndef NO_ERRNO_H
86 #include <errno.h> /* to define ENOENT */
87 #endif
89 /* ANSI C specifies that errno is a macro, but on older systems it's more
90 * likely to be a plain int variable. And not all versions of errno.h
91 * bother to declare it, so we have to in order to be most portable. Thus:
93 #ifndef errno
94 extern int errno;
95 #endif
98 LOCAL(void)
99 select_file_name (char * fname)
101 FILE * tfile;
103 /* Keep generating file names till we find one that's not in use */
104 for (;;) {
105 next_file_num++; /* advance counter */
106 sprintf(fname, TEMP_FILE_NAME, TEMP_DIRECTORY, next_file_num);
107 if ((tfile = fopen(fname, READ_BINARY)) == NULL) {
108 /* fopen could have failed for a reason other than the file not
109 * being there; for example, file there but unreadable.
110 * If <errno.h> isn't available, then we cannot test the cause.
112 #ifdef ENOENT
113 if (errno != ENOENT)
114 continue;
115 #endif
116 break;
118 fclose(tfile); /* oops, it's there; close tfile & try again */
122 #else /* ! NO_MKTEMP */
124 /* Note that mktemp() requires the initial filename to end in six X's */
125 #ifndef TEMP_FILE_NAME /* can override from jconfig.h or Makefile */
126 #define TEMP_FILE_NAME "%sJPG%dXXXXXX"
127 #endif
129 LOCAL(void)
130 select_file_name (char * fname)
132 next_file_num++; /* advance counter */
133 sprintf(fname, TEMP_FILE_NAME, TEMP_DIRECTORY, next_file_num);
134 mktemp(fname); /* make sure file name is unique */
135 /* mktemp replaces the trailing XXXXXX with a unique string of characters */
138 #endif /* NO_MKTEMP */
142 * Memory allocation and freeing are controlled by the regular library
143 * routines malloc() and free().
146 JGLOBAL(void *)
147 jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
149 return (void *) malloc(sizeofobject);
152 JGLOBAL(void)
153 jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
155 free(object);
160 * "Large" objects are treated the same as "small" ones.
161 * NB: although we include FAR keywords in the routine declarations,
162 * this file won't actually work in 80x86 small/medium model; at least,
163 * you probably won't be able to process useful-size images in only 64KB.
166 JGLOBAL(void FAR *)
167 jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
169 return (void FAR *) malloc(sizeofobject);
172 JGLOBAL(void)
173 jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
175 free(object);
180 * This routine computes the total memory space available for allocation.
181 * It's impossible to do this in a portable way; our current solution is
182 * to make the user tell us (with a default value set at compile time).
183 * If you can actually get the available space, it's a good idea to subtract
184 * a slop factor of 5% or so.
187 #ifndef DEFAULT_MAX_MEM /* so can override from makefile */
188 #define DEFAULT_MAX_MEM 1000000L /* default: one megabyte */
189 #endif
191 JGLOBAL(long)
192 jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
193 long max_bytes_needed, long already_allocated)
195 return cinfo->mem->max_memory_to_use - already_allocated;
200 * Backing store (temporary file) management.
201 * Backing store objects are only used when the value returned by
202 * jpeg_mem_available is less than the total space needed. You can dispense
203 * with these routines if you have plenty of virtual memory; see jmemnobs.c.
207 METHODDEF(void)
208 read_backing_store (j_common_ptr cinfo, backing_store_ptr info,
209 void FAR * buffer_address,
210 long file_offset, long byte_count)
212 if (fseek(info->temp_file, file_offset, SEEK_SET))
213 ERREXIT(cinfo, JERR_TFILE_SEEK);
214 if (JFREAD(info->temp_file, buffer_address, byte_count)
215 != (size_t) byte_count)
216 ERREXIT(cinfo, JERR_TFILE_READ);
220 METHODDEF(void)
221 write_backing_store (j_common_ptr cinfo, backing_store_ptr info,
222 void FAR * buffer_address,
223 long file_offset, long byte_count)
225 if (fseek(info->temp_file, file_offset, SEEK_SET))
226 ERREXIT(cinfo, JERR_TFILE_SEEK);
227 if (JFWRITE(info->temp_file, buffer_address, byte_count)
228 != (size_t) byte_count)
229 ERREXIT(cinfo, JERR_TFILE_WRITE);
233 METHODDEF(void)
234 close_backing_store (j_common_ptr cinfo, backing_store_ptr info)
236 fclose(info->temp_file); /* close the file */
237 unlink(info->temp_name); /* delete the file */
238 /* If your system doesn't have unlink(), use remove() instead.
239 * remove() is the ANSI-standard name for this function, but if
240 * your system was ANSI you'd be using jmemansi.c, right?
242 TRACEMSS(cinfo, 1, JTRC_TFILE_CLOSE, info->temp_name);
247 * Initial opening of a backing-store object.
250 JGLOBAL(void)
251 jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
252 long total_bytes_needed)
254 select_file_name(info->temp_name);
255 if ((info->temp_file = fopen(info->temp_name, RW_BINARY)) == NULL)
256 ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name);
257 info->read_backing_store = read_backing_store;
258 info->write_backing_store = write_backing_store;
259 info->close_backing_store = close_backing_store;
260 TRACEMSS(cinfo, 1, JTRC_TFILE_OPEN, info->temp_name);
265 * These routines take care of any system-dependent initialization and
266 * cleanup required.
269 JGLOBAL(long)
270 jpeg_mem_init (j_common_ptr cinfo)
272 next_file_num = 0; /* initialize temp file name generator */
273 return DEFAULT_MAX_MEM; /* default for max_memory_to_use */
276 JGLOBAL(void)
277 jpeg_mem_term (j_common_ptr cinfo)
279 /* no work */