17dd7cddfSDavid du Colombier /* 27dd7cddfSDavid du Colombier * jmemsys.h 37dd7cddfSDavid du Colombier * 4*593dc095SDavid du Colombier * Copyright (C) 1992-1997, Thomas G. Lane. 57dd7cddfSDavid du Colombier * This file is part of the Independent JPEG Group's software. 67dd7cddfSDavid du Colombier * For conditions of distribution and use, see the accompanying README file. 77dd7cddfSDavid du Colombier * 87dd7cddfSDavid du Colombier * This include file defines the interface between the system-independent 97dd7cddfSDavid du Colombier * and system-dependent portions of the JPEG memory manager. No other 107dd7cddfSDavid du Colombier * modules need include it. (The system-independent portion is jmemmgr.c; 117dd7cddfSDavid du Colombier * there are several different versions of the system-dependent portion.) 127dd7cddfSDavid du Colombier * 137dd7cddfSDavid du Colombier * This file works as-is for the system-dependent memory managers supplied 147dd7cddfSDavid du Colombier * in the IJG distribution. You may need to modify it if you write a 157dd7cddfSDavid du Colombier * custom memory manager. If system-dependent changes are needed in 167dd7cddfSDavid du Colombier * this file, the best method is to #ifdef them based on a configuration 17*593dc095SDavid du Colombier * symbol supplied in jconfig.h, as we have done with USE_MSDOS_MEMMGR 18*593dc095SDavid du Colombier * and USE_MAC_MEMMGR. 197dd7cddfSDavid du Colombier */ 207dd7cddfSDavid du Colombier 217dd7cddfSDavid du Colombier 227dd7cddfSDavid du Colombier /* Short forms of external names for systems with brain-damaged linkers. */ 237dd7cddfSDavid du Colombier 247dd7cddfSDavid du Colombier #ifdef NEED_SHORT_EXTERNAL_NAMES 257dd7cddfSDavid du Colombier #define jpeg_get_small jGetSmall 267dd7cddfSDavid du Colombier #define jpeg_free_small jFreeSmall 277dd7cddfSDavid du Colombier #define jpeg_get_large jGetLarge 287dd7cddfSDavid du Colombier #define jpeg_free_large jFreeLarge 297dd7cddfSDavid du Colombier #define jpeg_mem_available jMemAvail 307dd7cddfSDavid du Colombier #define jpeg_open_backing_store jOpenBackStore 317dd7cddfSDavid du Colombier #define jpeg_mem_init jMemInit 327dd7cddfSDavid du Colombier #define jpeg_mem_term jMemTerm 337dd7cddfSDavid du Colombier #endif /* NEED_SHORT_EXTERNAL_NAMES */ 347dd7cddfSDavid du Colombier 357dd7cddfSDavid du Colombier 367dd7cddfSDavid du Colombier /* 377dd7cddfSDavid du Colombier * These two functions are used to allocate and release small chunks of 387dd7cddfSDavid du Colombier * memory. (Typically the total amount requested through jpeg_get_small is 397dd7cddfSDavid du Colombier * no more than 20K or so; this will be requested in chunks of a few K each.) 407dd7cddfSDavid du Colombier * Behavior should be the same as for the standard library functions malloc 417dd7cddfSDavid du Colombier * and free; in particular, jpeg_get_small must return NULL on failure. 427dd7cddfSDavid du Colombier * On most systems, these ARE malloc and free. jpeg_free_small is passed the 437dd7cddfSDavid du Colombier * size of the object being freed, just in case it's needed. 447dd7cddfSDavid du Colombier * On an 80x86 machine using small-data memory model, these manage near heap. 457dd7cddfSDavid du Colombier */ 467dd7cddfSDavid du Colombier 477dd7cddfSDavid du Colombier EXTERN(void *) jpeg_get_small JPP((j_common_ptr cinfo, size_t sizeofobject)); 487dd7cddfSDavid du Colombier EXTERN(void) jpeg_free_small JPP((j_common_ptr cinfo, void * object, 497dd7cddfSDavid du Colombier size_t sizeofobject)); 507dd7cddfSDavid du Colombier 517dd7cddfSDavid du Colombier /* 527dd7cddfSDavid du Colombier * These two functions are used to allocate and release large chunks of 537dd7cddfSDavid du Colombier * memory (up to the total free space designated by jpeg_mem_available). 547dd7cddfSDavid du Colombier * The interface is the same as above, except that on an 80x86 machine, 557dd7cddfSDavid du Colombier * far pointers are used. On most other machines these are identical to 567dd7cddfSDavid du Colombier * the jpeg_get/free_small routines; but we keep them separate anyway, 577dd7cddfSDavid du Colombier * in case a different allocation strategy is desirable for large chunks. 587dd7cddfSDavid du Colombier */ 597dd7cddfSDavid du Colombier 607dd7cddfSDavid du Colombier EXTERN(void FAR *) jpeg_get_large JPP((j_common_ptr cinfo, 617dd7cddfSDavid du Colombier size_t sizeofobject)); 627dd7cddfSDavid du Colombier EXTERN(void) jpeg_free_large JPP((j_common_ptr cinfo, void FAR * object, 637dd7cddfSDavid du Colombier size_t sizeofobject)); 647dd7cddfSDavid du Colombier 657dd7cddfSDavid du Colombier /* 667dd7cddfSDavid du Colombier * The macro MAX_ALLOC_CHUNK designates the maximum number of bytes that may 677dd7cddfSDavid du Colombier * be requested in a single call to jpeg_get_large (and jpeg_get_small for that 687dd7cddfSDavid du Colombier * matter, but that case should never come into play). This macro is needed 697dd7cddfSDavid du Colombier * to model the 64Kb-segment-size limit of far addressing on 80x86 machines. 707dd7cddfSDavid du Colombier * On those machines, we expect that jconfig.h will provide a proper value. 717dd7cddfSDavid du Colombier * On machines with 32-bit flat address spaces, any large constant may be used. 727dd7cddfSDavid du Colombier * 737dd7cddfSDavid du Colombier * NB: jmemmgr.c expects that MAX_ALLOC_CHUNK will be representable as type 747dd7cddfSDavid du Colombier * size_t and will be a multiple of sizeof(align_type). 757dd7cddfSDavid du Colombier */ 767dd7cddfSDavid du Colombier 777dd7cddfSDavid du Colombier #ifndef MAX_ALLOC_CHUNK /* may be overridden in jconfig.h */ 787dd7cddfSDavid du Colombier #define MAX_ALLOC_CHUNK 1000000000L 797dd7cddfSDavid du Colombier #endif 807dd7cddfSDavid du Colombier 817dd7cddfSDavid du Colombier /* 827dd7cddfSDavid du Colombier * This routine computes the total space still available for allocation by 837dd7cddfSDavid du Colombier * jpeg_get_large. If more space than this is needed, backing store will be 847dd7cddfSDavid du Colombier * used. NOTE: any memory already allocated must not be counted. 857dd7cddfSDavid du Colombier * 867dd7cddfSDavid du Colombier * There is a minimum space requirement, corresponding to the minimum 877dd7cddfSDavid du Colombier * feasible buffer sizes; jmemmgr.c will request that much space even if 887dd7cddfSDavid du Colombier * jpeg_mem_available returns zero. The maximum space needed, enough to hold 897dd7cddfSDavid du Colombier * all working storage in memory, is also passed in case it is useful. 907dd7cddfSDavid du Colombier * Finally, the total space already allocated is passed. If no better 917dd7cddfSDavid du Colombier * method is available, cinfo->mem->max_memory_to_use - already_allocated 927dd7cddfSDavid du Colombier * is often a suitable calculation. 937dd7cddfSDavid du Colombier * 947dd7cddfSDavid du Colombier * It is OK for jpeg_mem_available to underestimate the space available 957dd7cddfSDavid du Colombier * (that'll just lead to more backing-store access than is really necessary). 967dd7cddfSDavid du Colombier * However, an overestimate will lead to failure. Hence it's wise to subtract 977dd7cddfSDavid du Colombier * a slop factor from the true available space. 5% should be enough. 987dd7cddfSDavid du Colombier * 997dd7cddfSDavid du Colombier * On machines with lots of virtual memory, any large constant may be returned. 1007dd7cddfSDavid du Colombier * Conversely, zero may be returned to always use the minimum amount of memory. 1017dd7cddfSDavid du Colombier */ 1027dd7cddfSDavid du Colombier 1037dd7cddfSDavid du Colombier EXTERN(long) jpeg_mem_available JPP((j_common_ptr cinfo, 1047dd7cddfSDavid du Colombier long min_bytes_needed, 1057dd7cddfSDavid du Colombier long max_bytes_needed, 1067dd7cddfSDavid du Colombier long already_allocated)); 1077dd7cddfSDavid du Colombier 1087dd7cddfSDavid du Colombier 1097dd7cddfSDavid du Colombier /* 1107dd7cddfSDavid du Colombier * This structure holds whatever state is needed to access a single 1117dd7cddfSDavid du Colombier * backing-store object. The read/write/close method pointers are called 1127dd7cddfSDavid du Colombier * by jmemmgr.c to manipulate the backing-store object; all other fields 1137dd7cddfSDavid du Colombier * are private to the system-dependent backing store routines. 1147dd7cddfSDavid du Colombier */ 1157dd7cddfSDavid du Colombier 1167dd7cddfSDavid du Colombier #define TEMP_NAME_LENGTH 64 /* max length of a temporary file's name */ 1177dd7cddfSDavid du Colombier 118*593dc095SDavid du Colombier 1197dd7cddfSDavid du Colombier #ifdef USE_MSDOS_MEMMGR /* DOS-specific junk */ 1207dd7cddfSDavid du Colombier 1217dd7cddfSDavid du Colombier typedef unsigned short XMSH; /* type of extended-memory handles */ 1227dd7cddfSDavid du Colombier typedef unsigned short EMSH; /* type of expanded-memory handles */ 1237dd7cddfSDavid du Colombier 1247dd7cddfSDavid du Colombier typedef union { 1257dd7cddfSDavid du Colombier short file_handle; /* DOS file handle if it's a temp file */ 1267dd7cddfSDavid du Colombier XMSH xms_handle; /* handle if it's a chunk of XMS */ 1277dd7cddfSDavid du Colombier EMSH ems_handle; /* handle if it's a chunk of EMS */ 1287dd7cddfSDavid du Colombier } handle_union; 1297dd7cddfSDavid du Colombier 1307dd7cddfSDavid du Colombier #endif /* USE_MSDOS_MEMMGR */ 1317dd7cddfSDavid du Colombier 132*593dc095SDavid du Colombier #ifdef USE_MAC_MEMMGR /* Mac-specific junk */ 133*593dc095SDavid du Colombier #include <Files.h> 134*593dc095SDavid du Colombier #endif /* USE_MAC_MEMMGR */ 135*593dc095SDavid du Colombier 136*593dc095SDavid du Colombier 1377dd7cddfSDavid du Colombier typedef struct backing_store_struct * backing_store_ptr; 1387dd7cddfSDavid du Colombier 1397dd7cddfSDavid du Colombier typedef struct backing_store_struct { 1407dd7cddfSDavid du Colombier /* Methods for reading/writing/closing this backing-store object */ 1417dd7cddfSDavid du Colombier JMETHOD(void, read_backing_store, (j_common_ptr cinfo, 1427dd7cddfSDavid du Colombier backing_store_ptr info, 1437dd7cddfSDavid du Colombier void FAR * buffer_address, 1447dd7cddfSDavid du Colombier long file_offset, long byte_count)); 1457dd7cddfSDavid du Colombier JMETHOD(void, write_backing_store, (j_common_ptr cinfo, 1467dd7cddfSDavid du Colombier backing_store_ptr info, 1477dd7cddfSDavid du Colombier void FAR * buffer_address, 1487dd7cddfSDavid du Colombier long file_offset, long byte_count)); 1497dd7cddfSDavid du Colombier JMETHOD(void, close_backing_store, (j_common_ptr cinfo, 1507dd7cddfSDavid du Colombier backing_store_ptr info)); 1517dd7cddfSDavid du Colombier 1527dd7cddfSDavid du Colombier /* Private fields for system-dependent backing-store management */ 1537dd7cddfSDavid du Colombier #ifdef USE_MSDOS_MEMMGR 1547dd7cddfSDavid du Colombier /* For the MS-DOS manager (jmemdos.c), we need: */ 1557dd7cddfSDavid du Colombier handle_union handle; /* reference to backing-store storage object */ 1567dd7cddfSDavid du Colombier char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */ 1577dd7cddfSDavid du Colombier #else 158*593dc095SDavid du Colombier #ifdef USE_MAC_MEMMGR 159*593dc095SDavid du Colombier /* For the Mac manager (jmemmac.c), we need: */ 160*593dc095SDavid du Colombier short temp_file; /* file reference number to temp file */ 161*593dc095SDavid du Colombier FSSpec tempSpec; /* the FSSpec for the temp file */ 162*593dc095SDavid du Colombier char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */ 163*593dc095SDavid du Colombier #else 1647dd7cddfSDavid du Colombier /* For a typical implementation with temp files, we need: */ 1657dd7cddfSDavid du Colombier FILE * temp_file; /* stdio reference to temp file */ 1667dd7cddfSDavid du Colombier char temp_name[TEMP_NAME_LENGTH]; /* name of temp file */ 1677dd7cddfSDavid du Colombier #endif 168*593dc095SDavid du Colombier #endif 1697dd7cddfSDavid du Colombier } backing_store_info; 1707dd7cddfSDavid du Colombier 171*593dc095SDavid du Colombier 1727dd7cddfSDavid du Colombier /* 1737dd7cddfSDavid du Colombier * Initial opening of a backing-store object. This must fill in the 1747dd7cddfSDavid du Colombier * read/write/close pointers in the object. The read/write routines 1757dd7cddfSDavid du Colombier * may take an error exit if the specified maximum file size is exceeded. 1767dd7cddfSDavid du Colombier * (If jpeg_mem_available always returns a large value, this routine can 1777dd7cddfSDavid du Colombier * just take an error exit.) 1787dd7cddfSDavid du Colombier */ 1797dd7cddfSDavid du Colombier 1807dd7cddfSDavid du Colombier EXTERN(void) jpeg_open_backing_store JPP((j_common_ptr cinfo, 1817dd7cddfSDavid du Colombier backing_store_ptr info, 1827dd7cddfSDavid du Colombier long total_bytes_needed)); 1837dd7cddfSDavid du Colombier 1847dd7cddfSDavid du Colombier 1857dd7cddfSDavid du Colombier /* 1867dd7cddfSDavid du Colombier * These routines take care of any system-dependent initialization and 1877dd7cddfSDavid du Colombier * cleanup required. jpeg_mem_init will be called before anything is 1887dd7cddfSDavid du Colombier * allocated (and, therefore, nothing in cinfo is of use except the error 1897dd7cddfSDavid du Colombier * manager pointer). It should return a suitable default value for 1907dd7cddfSDavid du Colombier * max_memory_to_use; this may subsequently be overridden by the surrounding 1917dd7cddfSDavid du Colombier * application. (Note that max_memory_to_use is only important if 1927dd7cddfSDavid du Colombier * jpeg_mem_available chooses to consult it ... no one else will.) 1937dd7cddfSDavid du Colombier * jpeg_mem_term may assume that all requested memory has been freed and that 1947dd7cddfSDavid du Colombier * all opened backing-store objects have been closed. 1957dd7cddfSDavid du Colombier */ 1967dd7cddfSDavid du Colombier 1977dd7cddfSDavid du Colombier EXTERN(long) jpeg_mem_init JPP((j_common_ptr cinfo)); 1987dd7cddfSDavid du Colombier EXTERN(void) jpeg_mem_term JPP((j_common_ptr cinfo)); 199