kusano 7d535a
/*
kusano 7d535a
 * jmemname.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1992-1997, 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 generic implementation of the system-dependent
kusano 7d535a
 * portion of the JPEG memory manager.  This implementation assumes that
kusano 7d535a
 * you must explicitly construct a name for each temp file.
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
#ifdef DONT_USE_B_MODE		/* define mode parameters for fopen() */
kusano 7d535a
#define READ_BINARY	"r"
kusano 7d535a
#define RW_BINARY	"w+"
kusano 7d535a
#else
kusano 7d535a
#ifdef VMS			/* VMS is very nonstandard */
kusano 7d535a
#define READ_BINARY	"rb", "ctx=stm"
kusano 7d535a
#define RW_BINARY	"w+b", "ctx=stm"
kusano 7d535a
#else				/* standard ANSI-compliant case */
kusano 7d535a
#define READ_BINARY	"rb"
kusano 7d535a
#define RW_BINARY	"w+b"
kusano 7d535a
#endif
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Selection of a file name for a temporary file.
kusano 7d535a
 * This is system-dependent!
kusano 7d535a
 *
kusano 7d535a
 * The code as given is suitable for most Unix systems, and it is easily
kusano 7d535a
 * modified for most non-Unix systems.  Some notes:
kusano 7d535a
 *  1.  The temp file is created in the directory named by TEMP_DIRECTORY.
kusano 7d535a
 *      The default value is /usr/tmp, which is the conventional place for
kusano 7d535a
 *      creating large temp files on Unix.  On other systems you'll probably
kusano 7d535a
 *      want to change the file location.  You can do this by editing the
kusano 7d535a
 *      #define, or (preferred) by defining TEMP_DIRECTORY in jconfig.h.
kusano 7d535a
 *
kusano 7d535a
 *  2.  If you need to change the file name as well as its location,
kusano 7d535a
 *      you can override the TEMP_FILE_NAME macro.  (Note that this is
kusano 7d535a
 *      actually a printf format string; it must contain %s and %d.)
kusano 7d535a
 *      Few people should need to do this.
kusano 7d535a
 *
kusano 7d535a
 *  3.  mktemp() is used to ensure that multiple processes running
kusano 7d535a
 *      simultaneously won't select the same file names.  If your system
kusano 7d535a
 *      doesn't have mktemp(), define NO_MKTEMP to do it the hard way.
kusano 7d535a
 *      (If you don't have <errno.h>, also define NO_ERRNO_H.)</errno.h>
kusano 7d535a
 *
kusano 7d535a
 *  4.  You probably want to define NEED_SIGNAL_CATCHER so that cjpeg.c/djpeg.c
kusano 7d535a
 *      will cause the temp files to be removed if you stop the program early.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#ifndef TEMP_DIRECTORY		/* can override from jconfig.h or Makefile */
kusano 7d535a
#define TEMP_DIRECTORY  "/usr/tmp/" /* recommended setting for Unix */
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
static int next_file_num;	/* to distinguish among several temp files */
kusano 7d535a
kusano 7d535a
#ifdef NO_MKTEMP
kusano 7d535a
kusano 7d535a
#ifndef TEMP_FILE_NAME		/* can override from jconfig.h or Makefile */
kusano 7d535a
#define TEMP_FILE_NAME  "%sJPG%03d.TMP"
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#ifndef NO_ERRNO_H
kusano 7d535a
#include <errno.h>		/* to define ENOENT */</errno.h>
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
/* ANSI C specifies that errno is a macro, but on older systems it's more
kusano 7d535a
 * likely to be a plain int variable.  And not all versions of errno.h
kusano 7d535a
 * bother to declare it, so we have to in order to be most portable.  Thus:
kusano 7d535a
 */
kusano 7d535a
#ifndef errno
kusano 7d535a
extern int errno;
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
select_file_name (char * fname)
kusano 7d535a
{
kusano 7d535a
  FILE * tfile;
kusano 7d535a
kusano 7d535a
  /* Keep generating file names till we find one that's not in use */
kusano 7d535a
  for (;;) {
kusano 7d535a
    next_file_num++;		/* advance counter */
kusano 7d535a
    sprintf(fname, TEMP_FILE_NAME, TEMP_DIRECTORY, next_file_num);
kusano 7d535a
    if ((tfile = fopen(fname, READ_BINARY)) == NULL) {
kusano 7d535a
      /* fopen could have failed for a reason other than the file not
kusano 7d535a
       * being there; for example, file there but unreadable.
kusano 7d535a
       * If <errno.h> isn't available, then we cannot test the cause.</errno.h>
kusano 7d535a
       */
kusano 7d535a
#ifdef ENOENT
kusano 7d535a
      if (errno != ENOENT)
kusano 7d535a
	continue;
kusano 7d535a
#endif
kusano 7d535a
      break;
kusano 7d535a
    }
kusano 7d535a
    fclose(tfile);		/* oops, it's there; close tfile & try again */
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#else /* ! NO_MKTEMP */
kusano 7d535a
kusano 7d535a
/* Note that mktemp() requires the initial filename to end in six X's */
kusano 7d535a
#ifndef TEMP_FILE_NAME		/* can override from jconfig.h or Makefile */
kusano 7d535a
#define TEMP_FILE_NAME  "%sJPG%dXXXXXX"
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
select_file_name (char * fname)
kusano 7d535a
{
kusano 7d535a
  next_file_num++;		/* advance counter */
kusano 7d535a
  sprintf(fname, TEMP_FILE_NAME, TEMP_DIRECTORY, next_file_num);
kusano 7d535a
  mktemp(fname);		/* make sure file name is unique */
kusano 7d535a
  /* mktemp replaces the trailing XXXXXX with a unique string of characters */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#endif /* NO_MKTEMP */
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);	/* close the file */
kusano 7d535a
  unlink(info->temp_name);	/* delete the file */
kusano 7d535a
/* If your system doesn't have unlink(), use remove() instead.
kusano 7d535a
 * remove() is the ANSI-standard name for this function, but if
kusano 7d535a
 * your system was ANSI you'd be using jmemansi.c, right?
kusano 7d535a
 */
kusano 7d535a
  TRACEMSS(cinfo, 1, JTRC_TFILE_CLOSE, info->temp_name);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initial opening of a backing-store object.
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
  select_file_name(info->temp_name);
kusano 7d535a
  if ((info->temp_file = fopen(info->temp_name, RW_BINARY)) == NULL)
kusano 7d535a
    ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name);
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
  TRACEMSS(cinfo, 1, JTRC_TFILE_OPEN, info->temp_name);
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
  next_file_num = 0;		/* initialize temp file name generator */
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
}