110f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch/*
210f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch * jmemansi.c
310f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch *
410f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch * Copyright (C) 1992-1996, Thomas G. Lane.
510f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch * This file is part of the Independent JPEG Group's software.
610f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch * For conditions of distribution and use, see the accompanying README file.
710f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch *
8323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles) * This file provides a simple generic implementation of the system-
910f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch * dependent portion of the JPEG memory manager.  This implementation
1010f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch * assumes that you have the ANSI-standard library routine tmpfile().
11c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles) * Also, the problem of determining the amount of memory available
1210f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch * is shoved onto the user.
13197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch */
14197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
15197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#define JPEG_INTERNALS
1610f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch#include "jinclude.h"
1710f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch#include "jpeglib.h"
18323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)#include "jmemsys.h"		/* import the system-dependent declarations */
19323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)
20323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)#ifndef HAVE_STDLIB_H		/* <stdlib.h> should declare malloc(),free() */
21323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)extern void * malloc JPP((size_t size));
2210f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdochextern void free JPP((void *ptr));
23323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)#endif
2410f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch
2510f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch#ifndef SEEK_SET		/* pre-ANSI systems may not define this; */
2610f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch#define SEEK_SET  0		/* if not, assume 0 is correct */
2710f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch#endif
2810f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch
2910f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch
30f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)/*
31e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles) * Memory allocation and freeing are controlled by the regular library
32197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch * routines malloc() and free().
33323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles) */
34323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)
35323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)GLOBAL(void *)
3610f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdochjpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
3710f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch{
3810f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch  return (void *) malloc(sizeofobject);
39323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)}
4010f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch
4110f88d5669dbd969c059d61ba09fa37dd72ac559Ben MurdochGLOBAL(void)
42323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
4310f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch{
4410f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch  free(object);
4510f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch}
4610f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch
4710f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch
4810f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch/*
4910f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch * "Large" objects are treated the same as "small" ones.
5010f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch * NB: although we include FAR keywords in the routine declarations,
51e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles) * this file won't actually work in 80x86 small/medium model; at least,
52323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles) * you probably won't be able to process useful-size images in only 64KB.
53323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles) */
54e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)
55323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)GLOBAL(void FAR *)
56e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
57e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles){
58e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)  return (void FAR *) malloc(sizeofobject);
59e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)}
60323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)
6110f88d5669dbd969c059d61ba09fa37dd72ac559Ben MurdochGLOBAL(void)
62323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
63f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu{
6410f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch  free(object);
6510f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch}
6610f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch
6710f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch
6810f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch/*
6910f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch * This routine computes the total memory space available for allocation.
7010f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch * It's impossible to do this in a portable way; our current solution is
7110f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch * to make the user tell us (with a default value set at compile time).
7210f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch * If you can actually get the available space, it's a good idea to subtract
7310f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch * a slop factor of 5% or so.
7410f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch */
7510f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch
7610f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch#ifndef DEFAULT_MAX_MEM		/* so can override from makefile */
7710f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch#define DEFAULT_MAX_MEM		1000000L /* default: one megabyte */
7810f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch#endif
7910f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch
8010f88d5669dbd969c059d61ba09fa37dd72ac559Ben MurdochGLOBAL(long)
8110f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdochjpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
8210f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch		    long max_bytes_needed, long already_allocated)
8310f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch{
8410f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch  return cinfo->mem->max_memory_to_use - already_allocated;
85e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)}
86e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)
87e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)
88e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)/*
89e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles) * Backing store (temporary file) management.
9010f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch * Backing store objects are only used when the value returned by
9110f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch * jpeg_mem_available is less than the total space needed.  You can dispense
9210f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch * with these routines if you have plenty of virtual memory; see jmemnobs.c.
9310f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch */
9410f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch
9510f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch
96323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)METHODDEF(void)
9710f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdochread_backing_store (j_common_ptr cinfo, backing_store_ptr info,
98323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)		    void FAR * buffer_address,
9910f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch		    long file_offset, long byte_count)
100323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles){
101323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)  if (fseek(info->temp_file, file_offset, SEEK_SET))
10210f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    ERREXIT(cinfo, JERR_TFILE_SEEK);
10310f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch  if (JFREAD(info->temp_file, buffer_address, byte_count)
104c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)      != (size_t) byte_count)
10510f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    ERREXIT(cinfo, JERR_TFILE_READ);
10610f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch}
107
108
109METHODDEF(void)
110write_backing_store (j_common_ptr cinfo, backing_store_ptr info,
111		     void FAR * buffer_address,
112		     long file_offset, long byte_count)
113{
114  if (fseek(info->temp_file, file_offset, SEEK_SET))
115    ERREXIT(cinfo, JERR_TFILE_SEEK);
116  if (JFWRITE(info->temp_file, buffer_address, byte_count)
117      != (size_t) byte_count)
118    ERREXIT(cinfo, JERR_TFILE_WRITE);
119}
120
121
122METHODDEF(void)
123close_backing_store (j_common_ptr cinfo, backing_store_ptr info)
124{
125  fclose(info->temp_file);
126  /* Since this implementation uses tmpfile() to create the file,
127   * no explicit file deletion is needed.
128   */
129}
130
131
132/*
133 * Initial opening of a backing-store object.
134 *
135 * This version uses tmpfile(), which constructs a suitable file name
136 * behind the scenes.  We don't have to use info->temp_name[] at all;
137 * indeed, we can't even find out the actual name of the temp file.
138 */
139
140GLOBAL(void)
141jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
142			 long total_bytes_needed)
143{
144  if ((info->temp_file = tmpfile()) == NULL)
145    ERREXITS(cinfo, JERR_TFILE_CREATE, "");
146  info->read_backing_store = read_backing_store;
147  info->write_backing_store = write_backing_store;
148  info->close_backing_store = close_backing_store;
149}
150
151
152/*
153 * These routines take care of any system-dependent initialization and
154 * cleanup required.
155 */
156
157GLOBAL(long)
158jpeg_mem_init (j_common_ptr cinfo)
159{
160  return DEFAULT_MAX_MEM;	/* default for max_memory_to_use */
161}
162
163GLOBAL(void)
164jpeg_mem_term (j_common_ptr cinfo)
165{
166  /* no work */
167}
168