~ubuntu-branches/ubuntu/precise/openarena/precise

« back to all changes in this revision

Viewing changes to code/jpeg-6/jmemname.c

  • Committer: Bazaar Package Importer
  • Author(s): Bruno "Fuddl" Kleinert
  • Date: 2007-01-20 12:28:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070120122809-2yza5ojt7nqiyiam
Tags: upstream-0.6.0
ImportĀ upstreamĀ versionĀ 0.6.0

Show diffs side-by-side

added added

removed removed

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