Blame gtkmm-osx/jpeg-6b/jmemname.c

darco 56a656
/*
darco 56a656
 * jmemname.c
darco 56a656
 *
darco 56a656
 * Copyright (C) 1992-1997, Thomas G. Lane.
darco 56a656
 * This file is part of the Independent JPEG Group's software.
darco 56a656
 * For conditions of distribution and use, see the accompanying README file.
darco 56a656
 *
darco 56a656
 * This file provides a generic implementation of the system-dependent
darco 56a656
 * portion of the JPEG memory manager.  This implementation assumes that
darco 56a656
 * you must explicitly construct a name for each temp file.
darco 56a656
 * Also, the problem of determining the amount of memory available
darco 56a656
 * is shoved onto the user.
darco 56a656
 */
darco 56a656
darco 56a656
#define JPEG_INTERNALS
darco 56a656
#include "jinclude.h"
darco 56a656
#include "jpeglib.h"
darco 56a656
#include "jmemsys.h"		/* import the system-dependent declarations */
darco 56a656
darco 56a656
#ifndef HAVE_STDLIB_H		/* <stdlib.h> should declare malloc(),free() */</stdlib.h>
darco 56a656
extern void * malloc JPP((size_t size));
darco 56a656
extern void free JPP((void *ptr));
darco 56a656
#endif
darco 56a656
darco 56a656
#ifndef SEEK_SET		/* pre-ANSI systems may not define this; */
darco 56a656
#define SEEK_SET  0		/* if not, assume 0 is correct */
darco 56a656
#endif
darco 56a656
darco 56a656
#ifdef DONT_USE_B_MODE		/* define mode parameters for fopen() */
darco 56a656
#define READ_BINARY	"r"
darco 56a656
#define RW_BINARY	"w+"
darco 56a656
#else
darco 56a656
#ifdef VMS			/* VMS is very nonstandard */
darco 56a656
#define READ_BINARY	"rb", "ctx=stm"
darco 56a656
#define RW_BINARY	"w+b", "ctx=stm"
darco 56a656
#else				/* standard ANSI-compliant case */
darco 56a656
#define READ_BINARY	"rb"
darco 56a656
#define RW_BINARY	"w+b"
darco 56a656
#endif
darco 56a656
#endif
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Selection of a file name for a temporary file.
darco 56a656
 * This is system-dependent!
darco 56a656
 *
darco 56a656
 * The code as given is suitable for most Unix systems, and it is easily
darco 56a656
 * modified for most non-Unix systems.  Some notes:
darco 56a656
 *  1.  The temp file is created in the directory named by TEMP_DIRECTORY.
darco 56a656
 *      The default value is /usr/tmp, which is the conventional place for
darco 56a656
 *      creating large temp files on Unix.  On other systems you'll probably
darco 56a656
 *      want to change the file location.  You can do this by editing the
darco 56a656
 *      #define, or (preferred) by defining TEMP_DIRECTORY in jconfig.h.
darco 56a656
 *
darco 56a656
 *  2.  If you need to change the file name as well as its location,
darco 56a656
 *      you can override the TEMP_FILE_NAME macro.  (Note that this is
darco 56a656
 *      actually a printf format string; it must contain %s and %d.)
darco 56a656
 *      Few people should need to do this.
darco 56a656
 *
darco 56a656
 *  3.  mktemp() is used to ensure that multiple processes running
darco 56a656
 *      simultaneously won't select the same file names.  If your system
darco 56a656
 *      doesn't have mktemp(), define NO_MKTEMP to do it the hard way.
darco 56a656
 *      (If you don't have <errno.h>, also define NO_ERRNO_H.)</errno.h>
darco 56a656
 *
darco 56a656
 *  4.  You probably want to define NEED_SIGNAL_CATCHER so that cjpeg.c/djpeg.c
darco 56a656
 *      will cause the temp files to be removed if you stop the program early.
darco 56a656
 */
darco 56a656
darco 56a656
#ifndef TEMP_DIRECTORY		/* can override from jconfig.h or Makefile */
darco 56a656
#define TEMP_DIRECTORY  "/usr/tmp/" /* recommended setting for Unix */
darco 56a656
#endif
darco 56a656
darco 56a656
static int next_file_num;	/* to distinguish among several temp files */
darco 56a656
darco 56a656
#ifdef NO_MKTEMP
darco 56a656
darco 56a656
#ifndef TEMP_FILE_NAME		/* can override from jconfig.h or Makefile */
darco 56a656
#define TEMP_FILE_NAME  "%sJPG%03d.TMP"
darco 56a656
#endif
darco 56a656
darco 56a656
#ifndef NO_ERRNO_H
darco 56a656
#include <errno.h>		/* to define ENOENT */</errno.h>
darco 56a656
#endif
darco 56a656
darco 56a656
/* ANSI C specifies that errno is a macro, but on older systems it's more
darco 56a656
 * likely to be a plain int variable.  And not all versions of errno.h
darco 56a656
 * bother to declare it, so we have to in order to be most portable.  Thus:
darco 56a656
 */
darco 56a656
#ifndef errno
darco 56a656
extern int errno;
darco 56a656
#endif
darco 56a656
darco 56a656
darco 56a656
LOCAL(void)
darco 56a656
select_file_name (char * fname)
darco 56a656
{
darco 56a656
  FILE * tfile;
darco 56a656
darco 56a656
  /* Keep generating file names till we find one that's not in use */
darco 56a656
  for (;;) {
darco 56a656
    next_file_num++;		/* advance counter */
darco 56a656
    sprintf(fname, TEMP_FILE_NAME, TEMP_DIRECTORY, next_file_num);
darco 56a656
    if ((tfile = fopen(fname, READ_BINARY)) == NULL) {
darco 56a656
      /* fopen could have failed for a reason other than the file not
darco 56a656
       * being there; for example, file there but unreadable.
darco 56a656
       * If <errno.h> isn't available, then we cannot test the cause.</errno.h>
darco 56a656
       */
darco 56a656
#ifdef ENOENT
darco 56a656
      if (errno != ENOENT)
darco 56a656
	continue;
darco 56a656
#endif
darco 56a656
      break;
darco 56a656
    }
darco 56a656
    fclose(tfile);		/* oops, it's there; close tfile & try again */
darco 56a656
  }
darco 56a656
}
darco 56a656
darco 56a656
#else /* ! NO_MKTEMP */
darco 56a656
darco 56a656
/* Note that mktemp() requires the initial filename to end in six X's */
darco 56a656
#ifndef TEMP_FILE_NAME		/* can override from jconfig.h or Makefile */
darco 56a656
#define TEMP_FILE_NAME  "%sJPG%dXXXXXX"
darco 56a656
#endif
darco 56a656
darco 56a656
LOCAL(void)
darco 56a656
select_file_name (char * fname)
darco 56a656
{
darco 56a656
  next_file_num++;		/* advance counter */
darco 56a656
  sprintf(fname, TEMP_FILE_NAME, TEMP_DIRECTORY, next_file_num);
darco 56a656
  mktemp(fname);		/* make sure file name is unique */
darco 56a656
  /* mktemp replaces the trailing XXXXXX with a unique string of characters */
darco 56a656
}
darco 56a656
darco 56a656
#endif /* NO_MKTEMP */
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Memory allocation and freeing are controlled by the regular library
darco 56a656
 * routines malloc() and free().
darco 56a656
 */
darco 56a656
darco 56a656
GLOBAL(void *)
darco 56a656
jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
darco 56a656
{
darco 56a656
  return (void *) malloc(sizeofobject);
darco 56a656
}
darco 56a656
darco 56a656
GLOBAL(void)
darco 56a656
jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
darco 56a656
{
darco 56a656
  free(object);
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * "Large" objects are treated the same as "small" ones.
darco 56a656
 * NB: although we include FAR keywords in the routine declarations,
darco 56a656
 * this file won't actually work in 80x86 small/medium model; at least,
darco 56a656
 * you probably won't be able to process useful-size images in only 64KB.
darco 56a656
 */
darco 56a656
darco 56a656
GLOBAL(void FAR *)
darco 56a656
jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
darco 56a656
{
darco 56a656
  return (void FAR *) malloc(sizeofobject);
darco 56a656
}
darco 56a656
darco 56a656
GLOBAL(void)
darco 56a656
jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
darco 56a656
{
darco 56a656
  free(object);
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * This routine computes the total memory space available for allocation.
darco 56a656
 * It's impossible to do this in a portable way; our current solution is
darco 56a656
 * to make the user tell us (with a default value set at compile time).
darco 56a656
 * If you can actually get the available space, it's a good idea to subtract
darco 56a656
 * a slop factor of 5% or so.
darco 56a656
 */
darco 56a656
darco 56a656
#ifndef DEFAULT_MAX_MEM		/* so can override from makefile */
darco 56a656
#define DEFAULT_MAX_MEM		1000000L /* default: one megabyte */
darco 56a656
#endif
darco 56a656
darco 56a656
GLOBAL(long)
darco 56a656
jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
darco 56a656
		    long max_bytes_needed, long already_allocated)
darco 56a656
{
darco 56a656
  return cinfo->mem->max_memory_to_use - already_allocated;
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Backing store (temporary file) management.
darco 56a656
 * Backing store objects are only used when the value returned by
darco 56a656
 * jpeg_mem_available is less than the total space needed.  You can dispense
darco 56a656
 * with these routines if you have plenty of virtual memory; see jmemnobs.c.
darco 56a656
 */
darco 56a656
darco 56a656
darco 56a656
METHODDEF(void)
darco 56a656
read_backing_store (j_common_ptr cinfo, backing_store_ptr info,
darco 56a656
		    void FAR * buffer_address,
darco 56a656
		    long file_offset, long byte_count)
darco 56a656
{
darco 56a656
  if (fseek(info->temp_file, file_offset, SEEK_SET))
darco 56a656
    ERREXIT(cinfo, JERR_TFILE_SEEK);
darco 56a656
  if (JFREAD(info->temp_file, buffer_address, byte_count)
darco 56a656
      != (size_t) byte_count)
darco 56a656
    ERREXIT(cinfo, JERR_TFILE_READ);
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
METHODDEF(void)
darco 56a656
write_backing_store (j_common_ptr cinfo, backing_store_ptr info,
darco 56a656
		     void FAR * buffer_address,
darco 56a656
		     long file_offset, long byte_count)
darco 56a656
{
darco 56a656
  if (fseek(info->temp_file, file_offset, SEEK_SET))
darco 56a656
    ERREXIT(cinfo, JERR_TFILE_SEEK);
darco 56a656
  if (JFWRITE(info->temp_file, buffer_address, byte_count)
darco 56a656
      != (size_t) byte_count)
darco 56a656
    ERREXIT(cinfo, JERR_TFILE_WRITE);
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
METHODDEF(void)
darco 56a656
close_backing_store (j_common_ptr cinfo, backing_store_ptr info)
darco 56a656
{
darco 56a656
  fclose(info->temp_file);	/* close the file */
darco 56a656
  unlink(info->temp_name);	/* delete the file */
darco 56a656
/* If your system doesn't have unlink(), use remove() instead.
darco 56a656
 * remove() is the ANSI-standard name for this function, but if
darco 56a656
 * your system was ANSI you'd be using jmemansi.c, right?
darco 56a656
 */
darco 56a656
  TRACEMSS(cinfo, 1, JTRC_TFILE_CLOSE, info->temp_name);
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Initial opening of a backing-store object.
darco 56a656
 */
darco 56a656
darco 56a656
GLOBAL(void)
darco 56a656
jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
darco 56a656
			 long total_bytes_needed)
darco 56a656
{
darco 56a656
  select_file_name(info->temp_name);
darco 56a656
  if ((info->temp_file = fopen(info->temp_name, RW_BINARY)) == NULL)
darco 56a656
    ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name);
darco 56a656
  info->read_backing_store = read_backing_store;
darco 56a656
  info->write_backing_store = write_backing_store;
darco 56a656
  info->close_backing_store = close_backing_store;
darco 56a656
  TRACEMSS(cinfo, 1, JTRC_TFILE_OPEN, info->temp_name);
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * These routines take care of any system-dependent initialization and
darco 56a656
 * cleanup required.
darco 56a656
 */
darco 56a656
darco 56a656
GLOBAL(long)
darco 56a656
jpeg_mem_init (j_common_ptr cinfo)
darco 56a656
{
darco 56a656
  next_file_num = 0;		/* initialize temp file name generator */
darco 56a656
  return DEFAULT_MAX_MEM;	/* default for max_memory_to_use */
darco 56a656
}
darco 56a656
darco 56a656
GLOBAL(void)
darco 56a656
jpeg_mem_term (j_common_ptr cinfo)
darco 56a656
{
darco 56a656
  /* no work */
darco 56a656
}