19f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project/*
29f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * jmemansi.c
39f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project *
49f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * Copyright (C) 1992-1996, Thomas G. Lane.
59f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * This file is part of the Independent JPEG Group's software.
69f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * For conditions of distribution and use, see the accompanying README file.
79f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project *
89f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * This file provides a simple generic implementation of the system-
99f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * dependent portion of the JPEG memory manager.  This implementation
109f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * assumes that you have the ANSI-standard library routine tmpfile().
119f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * Also, the problem of determining the amount of memory available
129f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * is shoved onto the user.
139f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project */
149f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
159f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project#define JPEG_INTERNALS
169f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project#include "jinclude.h"
179f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project#include "jpeglib.h"
189f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project#include "jmemsys.h"		/* import the system-dependent declarations */
199f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
209f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project#ifndef HAVE_STDLIB_H		/* <stdlib.h> should declare malloc(),free() */
219f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectextern void * malloc JPP((size_t size));
229f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectextern void free JPP((void *ptr));
239f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project#endif
249f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
259f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project#ifndef SEEK_SET		/* pre-ANSI systems may not define this; */
269f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project#define SEEK_SET  0		/* if not, assume 0 is correct */
279f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project#endif
289f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
299f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
309f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project/*
319f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * Memory allocation and freeing are controlled by the regular library
329f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * routines malloc() and free().
339f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project */
349f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
359f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source ProjectGLOBAL(void *)
369f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectjpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
379f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project{
389f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project  return (void *) malloc(sizeofobject);
399f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project}
409f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
419f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source ProjectGLOBAL(void)
429f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectjpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
439f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project{
449f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project  free(object);
459f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project}
469f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
479f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
489f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project/*
499f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * "Large" objects are treated the same as "small" ones.
509f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * NB: although we include FAR keywords in the routine declarations,
519f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * this file won't actually work in 80x86 small/medium model; at least,
529f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * you probably won't be able to process useful-size images in only 64KB.
539f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project */
549f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
559f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source ProjectGLOBAL(void FAR *)
569f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectjpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
579f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project{
589f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project  return (void FAR *) malloc(sizeofobject);
599f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project}
609f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
619f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source ProjectGLOBAL(void)
629f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectjpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
639f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project{
649f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project  free(object);
659f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project}
669f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
679f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
689f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project/*
699f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * This routine computes the total memory space available for allocation.
709f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * It's impossible to do this in a portable way; our current solution is
719f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * to make the user tell us (with a default value set at compile time).
729f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * If you can actually get the available space, it's a good idea to subtract
739f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * a slop factor of 5% or so.
749f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project */
759f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
769f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project#ifndef DEFAULT_MAX_MEM		/* so can override from makefile */
779f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project#define DEFAULT_MAX_MEM		1000000L /* default: one megabyte */
789f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project#endif
799f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
809f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source ProjectGLOBAL(long)
819f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectjpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
829f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project		    long max_bytes_needed, long already_allocated)
839f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project{
849f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project  return cinfo->mem->max_memory_to_use - already_allocated;
859f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project}
869f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
879f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
889f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project/*
899f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * Backing store (temporary file) management.
909f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * Backing store objects are only used when the value returned by
919f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * jpeg_mem_available is less than the total space needed.  You can dispense
929f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * with these routines if you have plenty of virtual memory; see jmemnobs.c.
939f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project */
949f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
959f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
969f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source ProjectMETHODDEF(void)
979f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectread_backing_store (j_common_ptr cinfo, backing_store_ptr info,
989f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project		    void FAR * buffer_address,
999f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project		    long file_offset, long byte_count)
1009f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project{
1019f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project  if (fseek(info->temp_file, file_offset, SEEK_SET))
1029f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    ERREXIT(cinfo, JERR_TFILE_SEEK);
1039f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project  if (JFREAD(info->temp_file, buffer_address, byte_count)
1049f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project      != (size_t) byte_count)
1059f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    ERREXIT(cinfo, JERR_TFILE_READ);
1069f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project}
1079f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1089f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1099f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source ProjectMETHODDEF(void)
1109f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectwrite_backing_store (j_common_ptr cinfo, backing_store_ptr info,
1119f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project		     void FAR * buffer_address,
1129f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project		     long file_offset, long byte_count)
1139f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project{
1149f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project  if (fseek(info->temp_file, file_offset, SEEK_SET))
1159f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    ERREXIT(cinfo, JERR_TFILE_SEEK);
1169f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project  if (JFWRITE(info->temp_file, buffer_address, byte_count)
1179f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project      != (size_t) byte_count)
1189f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    ERREXIT(cinfo, JERR_TFILE_WRITE);
1199f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project}
1209f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1219f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1229f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source ProjectMETHODDEF(void)
1239f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectclose_backing_store (j_common_ptr cinfo, backing_store_ptr info)
1249f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project{
1259f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project  fclose(info->temp_file);
1269f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project  /* Since this implementation uses tmpfile() to create the file,
1279f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project   * no explicit file deletion is needed.
1289f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project   */
1299f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project}
1309f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1319f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1329f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project/*
1339f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * Initial opening of a backing-store object.
1349f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project *
1359f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * This version uses tmpfile(), which constructs a suitable file name
1369f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * behind the scenes.  We don't have to use info->temp_name[] at all;
1379f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * indeed, we can't even find out the actual name of the temp file.
1389f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project */
1399f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1409f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source ProjectGLOBAL(void)
1419f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectjpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
1429f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project			 long total_bytes_needed)
1439f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project{
1449f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project  if ((info->temp_file = tmpfile()) == NULL)
1459f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project    ERREXITS(cinfo, JERR_TFILE_CREATE, "");
1469f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project  info->read_backing_store = read_backing_store;
1479f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project  info->write_backing_store = write_backing_store;
1489f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project  info->close_backing_store = close_backing_store;
1499f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project}
1509f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1519f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1529f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project/*
1539f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * These routines take care of any system-dependent initialization and
1549f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project * cleanup required.
1559f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project */
1569f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1579f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source ProjectGLOBAL(long)
1589f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectjpeg_mem_init (j_common_ptr cinfo)
1599f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project{
1609f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project  return DEFAULT_MAX_MEM;	/* default for max_memory_to_use */
1619f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project}
1629f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project
1639f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source ProjectGLOBAL(void)
1649f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Projectjpeg_mem_term (j_common_ptr cinfo)
1659f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project{
1669f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project  /* no work */
1679f5d49a1588e438ae7ceabd0c94172117e3303aaThe Android Open Source Project}
168