Back to index

lightning-sunbird  0.9+nobinonly
jmemansi.c
Go to the documentation of this file.
00001 /*
00002  * jmemansi.c
00003  *
00004  * Copyright (C) 1992-1996, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file provides a simple generic implementation of the system-
00009  * dependent portion of the JPEG memory manager.  This implementation
00010  * assumes that you have the ANSI-standard library routine tmpfile().
00011  * Also, the problem of determining the amount of memory available
00012  * is shoved onto the user.
00013  */
00014 
00015 #define JPEG_INTERNALS
00016 #include "jinclude.h"
00017 #include "jpeglib.h"
00018 #include "jmemsys.h"        /* import the system-dependent declarations */
00019 
00020 #ifndef HAVE_STDLIB_H              /* <stdlib.h> should declare malloc(),free() */
00021 extern void * malloc JPP((size_t size));
00022 extern void free JPP((void *ptr));
00023 #endif
00024 
00025 #ifndef SEEK_SET            /* pre-ANSI systems may not define this; */
00026 #define SEEK_SET  0         /* if not, assume 0 is correct */
00027 #endif
00028 
00029 
00030 /*
00031  * Memory allocation and freeing are controlled by the regular library
00032  * routines malloc() and free().
00033  */
00034 
00035 GLOBAL(void *)
00036 jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
00037 {
00038   return (void *) malloc(sizeofobject);
00039 }
00040 
00041 GLOBAL(void)
00042 jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
00043 {
00044   free(object);
00045 }
00046 
00047 
00048 /*
00049  * "Large" objects are treated the same as "small" ones.
00050  * NB: although we include FAR keywords in the routine declarations,
00051  * this file won't actually work in 80x86 small/medium model; at least,
00052  * you probably won't be able to process useful-size images in only 64KB.
00053  */
00054 
00055 GLOBAL(void FAR *)
00056 jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
00057 {
00058   return (void FAR *) malloc(sizeofobject);
00059 }
00060 
00061 GLOBAL(void)
00062 jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
00063 {
00064   free(object);
00065 }
00066 
00067 
00068 /*
00069  * This routine computes the total memory space available for allocation.
00070  * It's impossible to do this in a portable way; our current solution is
00071  * to make the user tell us (with a default value set at compile time).
00072  * If you can actually get the available space, it's a good idea to subtract
00073  * a slop factor of 5% or so.
00074  */
00075 
00076 #ifndef DEFAULT_MAX_MEM            /* so can override from makefile */
00077 #define DEFAULT_MAX_MEM            1000000L /* default: one megabyte */
00078 #endif
00079 
00080 GLOBAL(long)
00081 jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
00082                   long max_bytes_needed, long already_allocated)
00083 {
00084   return cinfo->mem->max_memory_to_use - already_allocated;
00085 }
00086 
00087 
00088 /*
00089  * Backing store (temporary file) management.
00090  * Backing store objects are only used when the value returned by
00091  * jpeg_mem_available is less than the total space needed.  You can dispense
00092  * with these routines if you have plenty of virtual memory; see jmemnobs.c.
00093  */
00094 
00095 
00096 METHODDEF(void)
00097 read_backing_store (j_common_ptr cinfo, backing_store_ptr info,
00098                   void FAR * buffer_address,
00099                   long file_offset, long byte_count)
00100 {
00101   if (fseek(info->temp_file, file_offset, SEEK_SET))
00102     ERREXIT(cinfo, JERR_TFILE_SEEK);
00103   if (JFREAD(info->temp_file, buffer_address, byte_count)
00104       != (size_t) byte_count)
00105     ERREXIT(cinfo, JERR_TFILE_READ);
00106 }
00107 
00108 
00109 METHODDEF(void)
00110 write_backing_store (j_common_ptr cinfo, backing_store_ptr info,
00111                    void FAR * buffer_address,
00112                    long file_offset, long byte_count)
00113 {
00114   if (fseek(info->temp_file, file_offset, SEEK_SET))
00115     ERREXIT(cinfo, JERR_TFILE_SEEK);
00116   if (JFWRITE(info->temp_file, buffer_address, byte_count)
00117       != (size_t) byte_count)
00118     ERREXIT(cinfo, JERR_TFILE_WRITE);
00119 }
00120 
00121 
00122 METHODDEF(void)
00123 close_backing_store (j_common_ptr cinfo, backing_store_ptr info)
00124 {
00125   fclose(info->temp_file);
00126   /* Since this implementation uses tmpfile() to create the file,
00127    * no explicit file deletion is needed.
00128    */
00129 }
00130 
00131 
00132 /*
00133  * Initial opening of a backing-store object.
00134  *
00135  * This version uses tmpfile(), which constructs a suitable file name
00136  * behind the scenes.  We don't have to use info->temp_name[] at all;
00137  * indeed, we can't even find out the actual name of the temp file.
00138  */
00139 
00140 GLOBAL(void)
00141 jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
00142                       long total_bytes_needed)
00143 {
00144   if ((info->temp_file = tmpfile()) == NULL)
00145     ERREXITS(cinfo, JERR_TFILE_CREATE, "");
00146   info->read_backing_store = read_backing_store;
00147   info->write_backing_store = write_backing_store;
00148   info->close_backing_store = close_backing_store;
00149 }
00150 
00151 
00152 /*
00153  * These routines take care of any system-dependent initialization and
00154  * cleanup required.
00155  */
00156 
00157 GLOBAL(long)
00158 jpeg_mem_init (j_common_ptr cinfo)
00159 {
00160   return DEFAULT_MAX_MEM;   /* default for max_memory_to_use */
00161 }
00162 
00163 GLOBAL(void)
00164 jpeg_mem_term (j_common_ptr cinfo)
00165 {
00166   /* no work */
00167 }