00001
00002 /*----------------------------------------------------------------------------
00003 E.S.O.
00004 -----------------------------------------------------------------------------
00005 File name : memory.h
00006 Author : Nicolas Devillard
00007 Created on : Nov 07, 1995
00008 Hardware : Sun Sparc 20
00009 Software : ANSI C under Solaris Unix
00010 Part of ECLIPSE library for Adonis
00011 Description : memory handling and swapping routines
00012 ---------------------------------------------------------------------------*/
00013
00014 /*
00015 $Id: memory.h,v 1.2 2005/04/08 13:37:49 amodigli Exp $
00016 $Author: amodigli $
00017 $Date: 2005/04/08 13:37:49 $
00018 $Revision: 1.2 $
00019
00020 */
00021
00022 #ifndef _ECLIPSE_MEMORY_H_
00023 #define _ECLIPSE_MEMORY_H_
00024
00025
00026 /*
00027 * The following must be defined to use this module out of
00028 * the eclipse library
00029 */
00030
00031 /* #define EXPORT_MEMORY_H */
00032
00033 /*----------------------------------------------------------------------------
00034 * Includes
00035 *--------------------------------------------------------------------------*/
00036
00037 #include <sys/types.h>
00038 #include <sys/stat.h>
00039 #include <unistd.h>
00040 #include <stdio.h>
00041 #include <stdlib.h>
00042 #include <string.h>
00043 #include <malloc.h>
00044 #include <signal.h>
00045 #include <errno.h>
00046 #include <fcntl.h>
00047 #include <sys/mman.h>
00048 #include <sys/time.h>
00049 #include <sys/resource.h>
00050
00051 #ifndef EXPORT_MEMORY_H
00052 #include "local_types.h"
00053 #include "globals.h"
00054 #include "comm.h"
00055 #include "filesys.h"
00056 #include "e_config.h"
00057 #endif
00058
00059 #ifdef EXPORT_MEMORY_H
00060 #define e_error printf
00061 #define e_warning printf
00062 #endif
00063
00064 /*----------------------------------------------------------------------------
00065 * Defines
00066 *--------------------------------------------------------------------------*/
00067
00068
00069 #define ONE_MEG (size_t)1048576
00070
00071
00072 /*
00073 * Allow here extended memory.
00074 */
00075
00076 #define ENABLE_EXTENDED_MEMORY
00077
00078 #ifdef ENABLE_EXTENDED_MEMORY
00079 #define malloc(s) ext_malloc_x(s,__FILE__,__LINE__)
00080 #define calloc(n,s) ext_calloc_x(n,s,__FILE__,__LINE__)
00081 #define free(p) ext_free_x(p,__FILE__,__LINE__)
00082 #define strdup2(s) ext_strdup_x(s,__FILE__,__LINE__)
00083 #else
00084 #define strdup2(s) strdup(s)
00085 #endif
00086
00087
00088 /*----------------------------------------------------------------------------
00089 * Macros
00090 *--------------------------------------------------------------------------*/
00091
00092 /*
00093 * Probably the most useful macro: NONNULL(ptr,retval) will exit the
00094 * current function and return [retval] if ptr is found to be 0
00095 */
00096
00097 #define NONNULL(ptr, retval) { if (!ptr) return retval ; }
00098
00099 #ifdef ENABLE_EXTENDED_MEMORY
00100
00101 #define ext_malloc(s) ext_malloc_x(s,__FILE__,__LINE__)
00102 #define ext_calloc(n,s) ext_calloc_x(n,s,__FILE__,__LINE__)
00103 #define ext_free(p) ext_free_x(p,__FILE__,__LINE__)
00104 #define swap_malloc(s) swap_malloc_x(s,__FILE__,__LINE__)
00105 #define swap_free(p) swap_free_x(p,__FILE__,__LINE__)
00106
00107 #else
00108
00109 #define ext_malloc malloc
00110 #define ext_calloc calloc
00111 #define ext_free free
00112
00113 #endif
00114
00115
00116 /*----------------------------------------------------------------------------
00117 * Function ANSI C prototypes
00118 *--------------------------------------------------------------------------*/
00119
00120
00121 /*----------------------------------------------------------------------------
00122 * Function : ext_malloc_x()
00123 * In : size_t
00124 * Out : pointer to allocated zone
00125 * Job : allocate memory in RAM or SWAP
00126 * Notice :
00127 *--------------------------------------------------------------------------*/
00128
00129 void *
00130 ext_malloc_x(
00131 size_t size,
00132 char * filename,
00133 int lineno
00134 ) ;
00135
00136
00137 /*----------------------------------------------------------------------------
00138 * Function : ext_calloc_x()
00139 * In : size_t
00140 * Out : pointer to allocated (and zero-filled) zone
00141 * Job : allocate memory in RAM or SWAP
00142 * Notice :
00143 *--------------------------------------------------------------------------*/
00144
00145 void *
00146 ext_calloc_x(
00147 size_t n_elem,
00148 size_t size,
00149 char * filename,
00150 int lineno
00151 ) ;
00152
00153
00154 /*---------------------------------------------------------------------------
00155 * Function : ram_malloc()
00156 * In : Number of Bytes
00157 * Out : pointer to allocated zone
00158 * Job : Allocates memory (RAM)
00159 * Notice : returns NULL in case of error
00160 *--------------------------------------------------------------------------*/
00161
00162 void *
00163 ram_malloc(
00164 size_t size,
00165 char * filename,
00166 int lineno
00167 ) ;
00168
00169
00170 /*---------------------------------------------------------------------------
00171 * Function : ram_calloc()
00172 * In : number of elements, size of each element
00173 * Out : pointer to allocated zone
00174 * Job : Allocates memory (RAM)
00175 * Notice : returns NULL in case of error
00176 *--------------------------------------------------------------------------*/
00177
00178 void *
00179 ram_calloc(
00180 size_t n_elem,
00181 size_t size,
00182 char * filename,
00183 int lineno
00184 ) ;
00185
00186
00187
00188 /*---------------------------------------------------------------------------
00189 * Function : ext_free_x()
00190 * In : Pointer to de-allocate
00191 * Out : void
00192 * Job : handles pointer free
00193 * Notice :
00194 *--------------------------------------------------------------------------*/
00195
00196 void
00197 ext_free_x(
00198 void * pointer,
00199 char * filename,
00200 int lineno
00201 ) ;
00202
00203
00204
00205 /*---------------------------------------------------------------------------
00206 Function : swap_malloc_x()
00207 In : Number of bytes to allocate in swap area
00208 Out : pointer to allocated zone
00209 Job : Allocates memory in swap space, returns NULL if cannot
00210 Notice :
00211 memory should be freed with swap_free_monit()
00212 this function should not be called directly but through
00213 the swap_malloc() macro
00214 ---------------------------------------------------------------------------*/
00215
00216 void *
00217 swap_malloc_x(
00218 size_t size,
00219 char * filename,
00220 int lineno
00221 ) ;
00222
00223
00224
00225 /*---------------------------------------------------------------------------
00226 * Function : swap_free_x()
00227 * In : SWAP pointer to de-allocate
00228 * Out : void
00229 * Job : handles swap pointer free
00230 * Notice : don't even try to understand...
00231 *--------------------------------------------------------------------------*/
00232
00233 void
00234 swap_free_x(
00235 void * pointer,
00236 char * filename,
00237 int lineno
00238 ) ;
00239
00240
00241 /*---------------------------------------------------------------------------
00242 Function : ext_strdup_x()
00243 In : char *
00244 Out : char *
00245 Job : copy a string to a newly allocated string
00246 Notice : returned string must be freed by free()
00247 ---------------------------------------------------------------------------*/
00248
00249 char * ext_strdup_x(char * s, char * file, int lineno) ;
00250
00251
00252
00253 /*----------------------------------------------------------------------------
00254 * Function : get_memory_parameter()
00255 * In : allocated character string
00256 * Out : size_t
00257 * Job : get info about memory handling configuration
00258 * Notice : The I/O mapping is the following:
00259
00260 Input char string output value
00261
00262 "max_ram" maximum RAM to allocate
00263 "max_swap" maximum SWAP to allocate
00264 "vm_page_size" page size for virtual memory
00265 "total_alloc" total allocated memory so far
00266 "total_alloc_ram" total allocated RAM so far
00267 "total_alloc_swap" total allocated SWAP so far
00268
00269 *--------------------------------------------------------------------------*/
00270 size_t get_memory_parameter(char * s) ;
00271
00272
00273
00274 /*----------------------------------------------------------------------------
00275 * Function : set_memory_parameter()
00276 * In : char string, size_t
00277 * Out : void
00278 * Job : set info about memory handling configuration
00279 * Notice : The I/O mapping is the following:
00280
00281 Input char string output value
00282
00283 "max_ram" maximum RAM to allocate
00284 "max_swap" maximum SWAP to allocate
00285 "vm_page_size" page size for virtual memory
00286
00287 *--------------------------------------------------------------------------*/
00288 void set_memory_parameter(const char * s, size_t val) ;
00289
00290
00291
00292 /*---------------------------------------------------------------------------
00293 Function : print_memory_parameters()
00294 In : void
00295 Out : void, messages to stderr
00296 Job : print out current memory configuration
00297 Notice :
00298 ---------------------------------------------------------------------------*/
00299 void print_memory_parameters(void) ;
00300
00301
00302
00303 /*---------------------------------------------------------------------------
00304 * Function : get_tempfile_name()
00305 * In : void
00306 * Out : newly allocated character string
00307 * Job : return a different file name at each call
00308 * Notice :
00309 *--------------------------------------------------------------------------*/
00310 char * get_tempfile_name(void) ;
00311
00312
00313 /*---------------------------------------------------------------------------
00314 Function : set_tmpdirname()
00315 In : char *
00316 Out : void
00317 Job : sets the name of the temporary directory for VM
00318 Notice :
00319 ---------------------------------------------------------------------------*/
00320 void set_tmpdirname(const char * s) ;
00321
00322
00323 /*---------------------------------------------------------------------------
00324 * Function : memory_status()
00325 * In : void
00326 * Out : prints out memory status on stderr
00327 * Notice :
00328 *--------------------------------------------------------------------------*/
00329 /*<python>*/
00330 void memory_status(void) ;
00331 /*</python>*/
00332
00333
00334 /*----------------------------------------------------------------------------
00335 * Function : cleanup_mess()
00336 * In : void
00337 * Out : void
00338 * Job : clean up temporary files created by the current process
00339 * Notice :
00340 *--------------------------------------------------------------------------*/
00341
00342 void cleanup_mess(void) ;
00343
00344
00345 /*----------------------------------------------------------------------------
00346 * Function : signal_catch()
00347 * In : signal to catch, function to call in this case
00348 * Out : int 0 if worked, -1 otherwise
00349 * Job : catch an interrupt and launch the appropriate function
00350 * Notice : directly taken from MIDAS
00351 *--------------------------------------------------------------------------*/
00352
00353 int signal_catch(int sig, void (*f)()) ;
00354
00355
00356 /*---------------------------------------------------------------------------
00357 Function : fatal_signal_handler()
00358 In : void
00359 Out : void
00360 Job : prints out a message to stderr and call cleanup
00361 Notice : called from SIGSEGV, SIGBUS, SIGXCPU, SIGXFSZ
00362 ---------------------------------------------------------------------------*/
00363
00364 void fatal_signal_handler(void) ;
00365
00366
00367 /*----------------------------------------------------------------------------
00368 * Function : eclipse_init()
00369 * In : void
00370 * Out : void
00371 * Job : install the atexit() routine and interrupt_catch() to
00372 * take care of removing temporary files in case of sudden
00373 * process death.
00374 * Notice : must be called from main()
00375 *--------------------------------------------------------------------------*/
00376
00377 void eclipse_init(void) ;
00378
00379
00380 /*---------------------------------------------------------------------------
00381 Function : enable_virtual_memory()
00382 In : void
00383 Out : int 0 if Ok, anything else otherwise
00384 Job : enables the use of virtual memory, i.e. sets the size of
00385 one virtual memory page, and checks the available disk
00386 space to see if max_swap fits.
00387 Notice : should always be called before any use of virtual
00388 memory.
00389 ---------------------------------------------------------------------------*/
00390
00391 int enable_virtual_memory(void) ;
00392
00393
00394 /*---------------------------------------------------------------------------
00395 Function : dump_stack()
00396 In : void
00397 Out : void, messages on stderr
00398 Job : dump the current stack to stderr, using dbx
00399 Notice : Probably Solaris specific, with little efforts portable
00400 other Unixes. From the comp.unix.programmer FAQ.
00401 ---------------------------------------------------------------------------*/
00402
00403 void dump_stack(void);
00404
00405
00406 #endif
00407 /*--------------------------------------------------------------------------*/
1.2.13.1 written by Dimitri van Heesch,
© 1997-2001