DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

jmemansi.cpp (4873B)


      1 /*
      2  * jmemansi.c
      3  *
      4  * Copyright (C) 1992-1994, Thomas G. Lane.
      5  * This file is part of the Independent JPEG Group's software.
      6  * For conditions of distribution and use, see the accompanying README file.
      7  *
      8  * This file provides a simple generic implementation of the system-
      9  * dependent portion of the JPEG memory manager.  This implementation
     10  * assumes that you have the ANSI-standard library routine tmpfile().
     11  * Also, the problem of determining the amount of memory available
     12  * is shoved onto the user.
     13  */
     14 
     15 #define JPEG_INTERNALS
     16 #include "jinclude.h"
     17 #include "jpeglib.h"
     18 #include "jmemsys.h"     /* import the system-dependent declarations */
     19 
     20 #ifndef HAVE_STDLIB_H       /* <stdlib.h> should declare malloc(),free() */
     21 extern void * malloc JPP( (size_t size) );
     22 extern void free JPP( (void * ptr) );
     23 #endif
     24 
     25 #ifndef SEEK_SET        /* pre-ANSI systems may not define this; */
     26 #define SEEK_SET  0     /* if not, assume 0 is correct */
     27 #endif
     28 
     29 
     30 /*
     31  * Memory allocation and freeing are controlled by the regular library
     32  * routines malloc() and free().
     33  */
     34 
     35 GLOBAL void *
     36 jpeg_get_small( j_common_ptr cinfo, size_t sizeofobject ) {
     37     return (void *) malloc( sizeofobject );
     38 }
     39 
     40 GLOBAL void
     41 jpeg_free_small( j_common_ptr cinfo, void * object, size_t sizeofobject ) {
     42     free( object );
     43 }
     44 
     45 
     46 /*
     47  * "Large" objects are treated the same as "small" ones.
     48  * NB: although we include FAR keywords in the routine declarations,
     49  * this file won't actually work in 80x86 small/medium model; at least,
     50  * you probably won't be able to process useful-size images in only 64KB.
     51  */
     52 
     53 GLOBAL void FAR *
     54 jpeg_get_large( j_common_ptr cinfo, size_t sizeofobject ) {
     55     return (void FAR *) malloc( sizeofobject );
     56 }
     57 
     58 GLOBAL void
     59 jpeg_free_large( j_common_ptr cinfo, void FAR * object, size_t sizeofobject ) {
     60     free( object );
     61 }
     62 
     63 
     64 /*
     65  * This routine computes the total memory space available for allocation.
     66  * It's impossible to do this in a portable way; our current solution is
     67  * to make the user tell us (with a default value set at compile time).
     68  * If you can actually get the available space, it's a good idea to subtract
     69  * a slop factor of 5% or so.
     70  */
     71 
     72 #ifndef DEFAULT_MAX_MEM     /* so can override from makefile */
     73 #define DEFAULT_MAX_MEM     1000000L /* default: one megabyte */
     74 #endif
     75 
     76 GLOBAL long
     77 jpeg_mem_available( j_common_ptr cinfo, long min_bytes_needed,
     78                     long max_bytes_needed, long already_allocated ) {
     79     return cinfo->mem->max_memory_to_use - already_allocated;
     80 }
     81 
     82 
     83 /*
     84  * Backing store (temporary file) management.
     85  * Backing store objects are only used when the value returned by
     86  * jpeg_mem_available is less than the total space needed.  You can dispense
     87  * with these routines if you have plenty of virtual memory; see jmemnobs.c.
     88  */
     89 
     90 
     91 METHODDEF void
     92 read_backing_store( j_common_ptr cinfo, backing_store_ptr info,
     93                     void FAR * buffer_address,
     94                     long file_offset, long byte_count ) {
     95     if ( fseek( info->temp_file, file_offset, SEEK_SET ) ) {
     96         ERREXIT( cinfo, JERR_TFILE_SEEK );
     97     }
     98     if ( JFREAD( info->temp_file, buffer_address, byte_count )
     99          != (size_t) byte_count ) {
    100         ERREXIT( cinfo, JERR_TFILE_READ );
    101     }
    102 }
    103 
    104 
    105 METHODDEF void
    106 write_backing_store( j_common_ptr cinfo, backing_store_ptr info,
    107                      void FAR * buffer_address,
    108                      long file_offset, long byte_count ) {
    109     if ( fseek( info->temp_file, file_offset, SEEK_SET ) ) {
    110         ERREXIT( cinfo, JERR_TFILE_SEEK );
    111     }
    112     if ( JFWRITE( info->temp_file, buffer_address, byte_count )
    113          != (size_t) byte_count ) {
    114         ERREXIT( cinfo, JERR_TFILE_WRITE );
    115     }
    116 }
    117 
    118 
    119 METHODDEF void
    120 close_backing_store( j_common_ptr cinfo, backing_store_ptr info ) {
    121     fclose( info->temp_file );
    122     /* Since this implementation uses tmpfile() to create the file,
    123      * no explicit file deletion is needed.
    124      */
    125 }
    126 
    127 
    128 /*
    129  * Initial opening of a backing-store object.
    130  *
    131  * This version uses tmpfile(), which constructs a suitable file name
    132  * behind the scenes.  We don't have to use info->temp_name[] at all;
    133  * indeed, we can't even find out the actual name of the temp file.
    134  */
    135 
    136 GLOBAL void
    137 jpeg_open_backing_store( j_common_ptr cinfo, backing_store_ptr info,
    138                          long total_bytes_needed ) {
    139     if ( ( info->temp_file = tmpfile() ) == NULL ) {
    140         ERREXITS( cinfo, JERR_TFILE_CREATE, "" );
    141     }
    142     info->read_backing_store = read_backing_store;
    143     info->write_backing_store = write_backing_store;
    144     info->close_backing_store = close_backing_store;
    145 }
    146 
    147 
    148 /*
    149  * These routines take care of any system-dependent initialization and
    150  * cleanup required.
    151  */
    152 
    153 GLOBAL long
    154 jpeg_mem_init( j_common_ptr cinfo ) {
    155     return DEFAULT_MAX_MEM; /* default for max_memory_to_use */
    156 }
    157 
    158 GLOBAL void
    159 jpeg_mem_term( j_common_ptr cinfo ) {
    160     /* no work */
    161 }