mem.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2003 FhG Fokus
00003  *
00004  * This file is part of sip-router, a free SIP server.
00005  *
00006  * Permission to use, copy, modify, and distribute this software for any
00007  * purpose with or without fee is hereby granted, provided that the above
00008  * copyright notice and this permission notice appear in all copies.
00009  *
00010  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00011  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00012  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
00013  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00014  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00015  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00016  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00017  *
00018  *
00019  * History:
00020  * --------
00021  *  2003-04-08  init_mallocs split into init_{pkg,shm}_malloc (andrei)
00022  * 
00023  */
00024 
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 #include "../config.h"
00037 #include "../dprint.h"
00038 #include "../globals.h"
00039 #include "mem.h"
00040 
00041 #ifdef PKG_MALLOC
00042 #include "q_malloc.h"
00043 #endif
00044 
00045 #ifdef SHM_MEM
00046 #include "shm_mem.h"
00047 #endif
00048 
00049 #ifdef PKG_MALLOC
00050         #ifndef DL_MALLOC
00051         char* mem_pool = 0;
00052         #endif
00053 
00054         #ifdef F_MALLOC
00055                 struct fm_block* mem_block = 0;
00056         #elif defined DL_MALLOC
00057                 /* don't need this */
00058         #else
00059                 struct qm_block* mem_block = 0;
00060         #endif
00061 #endif
00062 
00063 
00068 int init_pkg_mallocs(void)
00069 {
00070 #ifdef PKG_MALLOC
00071         /*init mem*/
00072         #ifndef DL_MALLOC
00073                 if (pkg_mem_size == 0)
00074                         pkg_mem_size = PKG_MEM_POOL_SIZE;
00075                 mem_pool = malloc(pkg_mem_size);
00076         #endif
00077         #ifdef F_MALLOC
00078                 if (mem_pool)
00079                         mem_block=fm_malloc_init(mem_pool, pkg_mem_size);
00080         #elif DL_MALLOC
00081                 /* don't need this */
00082         #else
00083                 if (mem_pool)
00084                         mem_block=qm_malloc_init(mem_pool, pkg_mem_size);
00085         #endif
00086         #ifndef DL_MALLOC
00087         if (mem_block==0){
00088                 LOG(L_CRIT, "could not initialize memory pool\n");
00089                 fprintf(stderr, "Too much pkg memory demanded: %ld bytes\n",
00090                                                 pkg_mem_size);
00091                 return -1;
00092         }
00093         #endif
00094 #endif
00095         return 0;
00096 }
00097 
00098 
00099 
00103 void destroy_pkg_mallocs(void)
00104 {
00105 #ifdef PKG_MALLOC
00106         #ifndef DL_MALLOC
00107                 if (mem_pool) {
00108                         free(mem_pool);
00109                         mem_pool = 0;
00110                 }
00111         #endif
00112 #endif /* PKG_MALLOC */
00113 }
00114 
00115 
00121 int init_shm_mallocs(int force_alloc)
00122 {
00123 #ifdef SHM_MEM
00124         if (shm_mem_init(force_alloc)<0) {
00125                 LOG(L_CRIT, "could not initialize shared memory pool, exiting...\n");
00126                  fprintf(stderr, "Too much shared memory demanded: %ld\n",
00127                         shm_mem_size );
00128                 return -1;
00129         }
00130 #endif
00131         return 0;
00132 }