/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++ memory.h K. Banse ESO - Garching 991022 this is the `memory.h' of N. Devillard with some cosmetic cleanup +++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ /*---------------------------------------------------------------------------- * Includes *--------------------------------------------------------------------------*/ #include #include #include #include #include #include #include #include #include #include #include /*---------------------------------------------------------------------------- * Defines *--------------------------------------------------------------------------*/ #define Error printf #define Warning printf #define ONE_MEG (size_t)1048576 #ifndef NAME_SIZE #define NAME_SIZE 512 #endif /* * Allow here extended memory. */ #define ENABLE_EXTENDED_MEMORY #define malloc(s) ext_malloc_x(s,__FILE__,__LINE__) #define calloc(n,s) ext_calloc_x(n,s,__FILE__,__LINE__) #define free(p) ext_free_x(p,__FILE__,__LINE__) /*---------------------------------------------------------------------------- * Macros *--------------------------------------------------------------------------*/ /* * Probably the most useful macro: NONNULL(ptr,retval) will exit the * current function and return [retval] if ptr is found to be 0 */ #define NONNULL(ptr, retval) { if (!ptr) return retval ; } #define ext_malloc(s) ext_malloc_x(s,__FILE__,__LINE__) #define ext_calloc(n,s) ext_calloc_x(n,s,__FILE__,__LINE__) #define ext_free(p) ext_free_x(p,__FILE__,__LINE__) #define swap_malloc(s) swap_malloc_x(s,__FILE__,__LINE__) #define swap_free(p) swap_free_x(p,__FILE__,__LINE__) extern char tmpdirname[] ; /*---------------------------------------------------------------------------- * Function ANSI C prototypes *--------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------- * Function : ext_malloc_x() * In : size_t * Out : pointer to allocated zone * Job : allocate memory in RAM or SWAP * Notice : *--------------------------------------------------------------------------*/ void * ext_malloc_x( size_t size, char * filename, int lineno ) ; /*---------------------------------------------------------------------------- * Function : ext_calloc_x() * In : size_t * Out : pointer to allocated (and zero-filled) zone * Job : allocate memory in RAM or SWAP * Notice : *--------------------------------------------------------------------------*/ void * ext_calloc_x( size_t n_elem, size_t size, char * filename, int lineno ) ; /*--------------------------------------------------------------------------- * Function : ram_malloc() * In : Number of Bytes * Out : pointer to allocated zone * Job : Allocates memory (RAM) * Notice : returns NULL in case of error *--------------------------------------------------------------------------*/ void * ram_malloc( size_t size, char * filename, int lineno ) ; /*--------------------------------------------------------------------------- * Function : ram_calloc() * In : number of elements, size of each element * Out : pointer to allocated zone * Job : Allocates memory (RAM) * Notice : returns NULL in case of error *--------------------------------------------------------------------------*/ void * ram_calloc( size_t n_elem, size_t size, char * filename, int lineno ) ; /*--------------------------------------------------------------------------- * Function : ext_free_x() * In : Pointer to de-allocate * Out : void * Job : handles pointer free * Notice : *--------------------------------------------------------------------------*/ void ext_free_x( void * pointer, char * filename, int lineno ) ; /*--------------------------------------------------------------------------- Function : swap_malloc_x() In : Number of bytes to allocate in swap area Out : pointer to allocated zone Job : Allocates memory in swap space, returns NULL if cannot Notice : memory should be freed with swap_free_monit() this function should not be called directly but through the swap_malloc() macro ---------------------------------------------------------------------------*/ void * swap_malloc_x( size_t size, char * filename, int lineno ) ; /*--------------------------------------------------------------------------- * Function : swap_free_x() * In : SWAP pointer to de-allocate * Out : void * Job : handles swap pointer free * Notice : don't even try to understand... *--------------------------------------------------------------------------*/ void swap_free_x( void * pointer, char * filename, int lineno ) ; /*---------------------------------------------------------------------------- * Function : get_memory_parameter() * In : allocated character string * Out : size_t * Job : get info about memory handling configuration * Notice : The I/O mapping is the following: Input char string output value "max_ram" maximum RAM to allocate "max_swap" maximum SWAP to allocate "vm_page_size" page size for virtual memory "total_alloc" total allocated memory so far "total_alloc_ram" total allocated RAM so far "total_alloc_swap" total allocated SWAP so far *--------------------------------------------------------------------------*/ size_t get_memory_parameter(char * s) ; /*---------------------------------------------------------------------------- * Function : set_memory_parameter() * In : char string, size_t * Out : void * Job : set info about memory handling configuration * Notice : The I/O mapping is the following: Input char string output value "max_ram" maximum RAM to allocate "max_swap" maximum SWAP to allocate "vm_page_size" page size for virtual memory *--------------------------------------------------------------------------*/ void set_memory_parameter(char * s, size_t val) ; /*--------------------------------------------------------------------------- * Function : memory_check() * In : size (size_t) * Out : flag : DISK or MEMORY * Job : Determines if allocating the requested size will go * above * the upper memory limit or not. If it does, the function * returns DISK, which means that data is no loaded into * memory yet. If allocation is possible, it returns MEMORY * Notice : *--------------------------------------------------------------------------*/ char memory_check(size_t size) ; /*--------------------------------------------------------------------------- * Function : get_tempfile_name() * In : void * Out : newly allocated character string * Job : return a different file name at each call * Notice : *--------------------------------------------------------------------------*/ char * get_tempfile_name(void) ; /*--------------------------------------------------------------------------- * Function : memory_status() * In : void * Out : prints out memory status on stderr * Notice : *--------------------------------------------------------------------------*/ void memory_status(void) ; /*---------------------------------------------------------------------------- * Function : cleanup_mess() * In : void * Out : void * Job : clean up temporary files created by the current process * Notice : *--------------------------------------------------------------------------*/ void cleanup_mess(void) ; /*---------------------------------------------------------------------------- * Function : interrupt_catch() * In : signal to catch, function to call in this case * Out : int 0 if worked, -1 otherwise * Job : catch an interrupt and launch the appropriate function * Notice : directly taken from MIDAS *--------------------------------------------------------------------------*/ int interrupt_catch(void (*f)()) ; /*--------------------------------------------------------------------------*/