kusano 7d535a
/*
kusano 7d535a
 * jmemansi.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1992-1996, Thomas G. Lane.
kusano 7d535a
 * This file is part of the Independent JPEG Group's software.
kusano 7d535a
 * For conditions of distribution and use, see the accompanying README file.
kusano 7d535a
 *
kusano 7d535a
 * This file provides a simple generic implementation of the system-
kusano 7d535a
 * dependent portion of the JPEG memory manager.  This implementation
kusano 7d535a
 * assumes that you have the ANSI-standard library routine tmpfile().
kusano 7d535a
 * Also, the problem of determining the amount of memory available
kusano 7d535a
 * is shoved onto the user.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#define JPEG_INTERNALS
kusano 7d535a
#include "jinclude.h"
kusano 7d535a
#include "jpeglib.h"
kusano 7d535a
#include "jmemsys.h"		/* import the system-dependent declarations */
kusano 7d535a
kusano 7d535a
#ifndef HAVE_STDLIB_H		/* <stdlib.h> should declare malloc(),free() */</stdlib.h>
kusano 7d535a
extern void * malloc JPP((size_t size));
kusano 7d535a
extern void free JPP((void *ptr));
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#ifndef SEEK_SET		/* pre-ANSI systems may not define this; */
kusano 7d535a
#define SEEK_SET  0		/* if not, assume 0 is correct */
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Memory allocation and freeing are controlled by the regular library
kusano 7d535a
 * routines malloc() and free().
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void *)
kusano 7d535a
jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
kusano 7d535a
{
kusano 7d535a
  return (void *) malloc(sizeofobject);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
kusano 7d535a
{
kusano 7d535a
  free(object);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * "Large" objects are treated the same as "small" ones.
kusano 7d535a
 * NB: although we include FAR keywords in the routine declarations,
kusano 7d535a
 * this file won't actually work in 80x86 small/medium model; at least,
kusano 7d535a
 * you probably won't be able to process useful-size images in only 64KB.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void FAR *)
kusano 7d535a
jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
kusano 7d535a
{
kusano 7d535a
  return (void FAR *) malloc(sizeofobject);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
kusano 7d535a
{
kusano 7d535a
  free(object);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * This routine computes the total memory space available for allocation.
kusano 7d535a
 * It's impossible to do this in a portable way; our current solution is
kusano 7d535a
 * to make the user tell us (with a default value set at compile time).
kusano 7d535a
 * If you can actually get the available space, it's a good idea to subtract
kusano 7d535a
 * a slop factor of 5% or so.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#ifndef DEFAULT_MAX_MEM		/* so can override from makefile */
kusano 7d535a
#define DEFAULT_MAX_MEM		1000000L /* default: one megabyte */
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
GLOBAL(long)
kusano 7d535a
jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
kusano 7d535a
		    long max_bytes_needed, long already_allocated)
kusano 7d535a
{
kusano 7d535a
  return cinfo->mem->max_memory_to_use - already_allocated;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Backing store (temporary file) management.
kusano 7d535a
 * Backing store objects are only used when the value returned by
kusano 7d535a
 * jpeg_mem_available is less than the total space needed.  You can dispense
kusano 7d535a
 * with these routines if you have plenty of virtual memory; see jmemnobs.c.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
read_backing_store (j_common_ptr cinfo, backing_store_ptr info,
kusano 7d535a
		    void FAR * buffer_address,
kusano 7d535a
		    long file_offset, long byte_count)
kusano 7d535a
{
kusano 7d535a
  if (fseek(info->temp_file, file_offset, SEEK_SET))
kusano 7d535a
    ERREXIT(cinfo, JERR_TFILE_SEEK);
kusano 7d535a
  if (JFREAD(info->temp_file, buffer_address, byte_count)
kusano 7d535a
      != (size_t) byte_count)
kusano 7d535a
    ERREXIT(cinfo, JERR_TFILE_READ);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
write_backing_store (j_common_ptr cinfo, backing_store_ptr info,
kusano 7d535a
		     void FAR * buffer_address,
kusano 7d535a
		     long file_offset, long byte_count)
kusano 7d535a
{
kusano 7d535a
  if (fseek(info->temp_file, file_offset, SEEK_SET))
kusano 7d535a
    ERREXIT(cinfo, JERR_TFILE_SEEK);
kusano 7d535a
  if (JFWRITE(info->temp_file, buffer_address, byte_count)
kusano 7d535a
      != (size_t) byte_count)
kusano 7d535a
    ERREXIT(cinfo, JERR_TFILE_WRITE);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
close_backing_store (j_common_ptr cinfo, backing_store_ptr info)
kusano 7d535a
{
kusano 7d535a
  fclose(info->temp_file);
kusano 7d535a
  /* Since this implementation uses tmpfile() to create the file,
kusano 7d535a
   * no explicit file deletion is needed.
kusano 7d535a
   */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initial opening of a backing-store object.
kusano 7d535a
 *
kusano 7d535a
 * This version uses tmpfile(), which constructs a suitable file name
kusano 7d535a
 * behind the scenes.  We don't have to use info->temp_name[] at all;
kusano 7d535a
 * indeed, we can't even find out the actual name of the temp file.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
kusano 7d535a
			 long total_bytes_needed)
kusano 7d535a
{
kusano 7d535a
  if ((info->temp_file = tmpfile()) == NULL)
kusano 7d535a
    ERREXITS(cinfo, JERR_TFILE_CREATE, "");
kusano 7d535a
  info->read_backing_store = read_backing_store;
kusano 7d535a
  info->write_backing_store = write_backing_store;
kusano 7d535a
  info->close_backing_store = close_backing_store;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * These routines take care of any system-dependent initialization and
kusano 7d535a
 * cleanup required.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(long)
kusano 7d535a
jpeg_mem_init (j_common_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  return DEFAULT_MAX_MEM;	/* default for max_memory_to_use */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_mem_term (j_common_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  /* no work */
kusano 7d535a
}