/*1* jmemansi.c2*3* Copyright (C) 1992-1996, Thomas G. Lane.4* This file is part of the Independent JPEG Group's software.5* For conditions of distribution and use, see the accompanying README file.6*7* This file provides a simple generic implementation of the system-8* dependent portion of the JPEG memory manager. This implementation9* assumes that you have the ANSI-standard library routine tmpfile().10* Also, the problem of determining the amount of memory available11* is shoved onto the user.12*/1314#define JPEG_INTERNALS15#include "jinclude.h"16#include "jpeglib.h"17#include "jmemsys.h" /* import the system-dependent declarations */1819#ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */20extern void * malloc JPP((size_t size));21extern void free JPP((void *ptr));22#endif2324#ifndef SEEK_SET /* pre-ANSI systems may not define this; */25#define SEEK_SET 0 /* if not, assume 0 is correct */26#endif272829/*30* Memory allocation and freeing are controlled by the regular library31* routines malloc() and free().32*/3334GLOBAL(void *)35jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)36{37return (void *) malloc(sizeofobject);38}3940GLOBAL(void)41jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)42{43free(object);44}454647/*48* "Large" objects are treated the same as "small" ones.49* NB: although we include FAR keywords in the routine declarations,50* this file won't actually work in 80x86 small/medium model; at least,51* you probably won't be able to process useful-size images in only 64KB.52*/5354GLOBAL(void FAR *)55jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)56{57return (void FAR *) malloc(sizeofobject);58}5960GLOBAL(void)61jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)62{63free(object);64}656667/*68* This routine computes the total memory space available for allocation.69* It's impossible to do this in a portable way; our current solution is70* to make the user tell us (with a default value set at compile time).71* If you can actually get the available space, it's a good idea to subtract72* a slop factor of 5% or so.73*/7475#ifndef DEFAULT_MAX_MEM /* so can override from makefile */76#define DEFAULT_MAX_MEM 1000000L /* default: one megabyte */77#endif7879GLOBAL(long)80jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,81long max_bytes_needed, long already_allocated)82{83return cinfo->mem->max_memory_to_use - already_allocated;84}858687/*88* Backing store (temporary file) management.89* Backing store objects are only used when the value returned by90* jpeg_mem_available is less than the total space needed. You can dispense91* with these routines if you have plenty of virtual memory; see jmemnobs.c.92*/939495METHODDEF(void)96read_backing_store (j_common_ptr cinfo, backing_store_ptr info,97void FAR * buffer_address,98long file_offset, long byte_count)99{100if (fseek(info->temp_file, file_offset, SEEK_SET))101ERREXIT(cinfo, JERR_TFILE_SEEK);102if (JFREAD(info->temp_file, buffer_address, byte_count)103!= (size_t) byte_count)104ERREXIT(cinfo, JERR_TFILE_READ);105}106107108METHODDEF(void)109write_backing_store (j_common_ptr cinfo, backing_store_ptr info,110void FAR * buffer_address,111long file_offset, long byte_count)112{113if (fseek(info->temp_file, file_offset, SEEK_SET))114ERREXIT(cinfo, JERR_TFILE_SEEK);115if (JFWRITE(info->temp_file, buffer_address, byte_count)116!= (size_t) byte_count)117ERREXIT(cinfo, JERR_TFILE_WRITE);118}119120121METHODDEF(void)122close_backing_store (j_common_ptr cinfo, backing_store_ptr info)123{124fclose(info->temp_file);125/* Since this implementation uses tmpfile() to create the file,126* no explicit file deletion is needed.127*/128}129130131/*132* Initial opening of a backing-store object.133*134* This version uses tmpfile(), which constructs a suitable file name135* behind the scenes. We don't have to use info->temp_name[] at all;136* indeed, we can't even find out the actual name of the temp file.137*/138139GLOBAL(void)140jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,141long total_bytes_needed)142{143if ((info->temp_file = tmpfile()) == NULL)144ERREXITS(cinfo, JERR_TFILE_CREATE, "");145info->read_backing_store = read_backing_store;146info->write_backing_store = write_backing_store;147info->close_backing_store = close_backing_store;148}149150151/*152* These routines take care of any system-dependent initialization and153* cleanup required.154*/155156GLOBAL(long)157jpeg_mem_init (j_common_ptr cinfo)158{159return DEFAULT_MAX_MEM; /* default for max_memory_to_use */160}161162GLOBAL(void)163jpeg_mem_term (j_common_ptr cinfo)164{165/* no work */166}167168169