forked from kamailio/kamailio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem.c
135 lines (120 loc) · 2.97 KB
/
mem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright (C) 2001-2003 FhG Fokus
*
* This file is part of Kamailio, a free SIP server.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*
*/
/**
* \file
* \brief Main definitions for memory manager
*
* Main definitions for memory manager, like malloc, free and realloc
* \ingroup mem
*/
#include <stdio.h>
#include <stdlib.h>
#include "../config.h"
#include "../dprint.h"
#include "../globals.h"
#include "mem.h"
#ifdef PKG_MALLOC
#include "q_malloc.h"
#endif
#ifdef SHM_MEM
#include "shm_mem.h"
#endif
#if 0
#ifdef PKG_MALLOC
#ifndef DL_MALLOC
char* mem_pool = 0;
#endif
#ifdef F_MALLOC
struct fm_block* mem_block = 0;
#elif defined DL_MALLOC
/* don't need this */
#elif defined TLSF_MALLOC
tlsf_t mem_block = 0;
#else
struct qm_block* mem_block = 0;
#endif
#endif
/**
* \brief Initialize private memory pool
* \return 0 if the memory allocation was successful, -1 otherwise
*/
int init_pkg_mallocs(void)
{
#ifdef PKG_MALLOC
/*init mem*/
#ifndef DL_MALLOC
if (pkg_mem_size == 0)
pkg_mem_size = PKG_MEM_POOL_SIZE;
mem_pool = malloc(pkg_mem_size);
#endif
#ifdef F_MALLOC
if (mem_pool)
mem_block=fm_malloc_init(mem_pool, pkg_mem_size, MEM_TYPE_PKG);
#elif DL_MALLOC
/* don't need this */
#elif TLSF_MALLOC
mem_block = tlsf_create_with_pool(mem_pool, pkg_mem_size);
#else
if (mem_pool)
mem_block=qm_malloc_init(mem_pool, pkg_mem_size, MEM_TYPE_PKG);
#endif
#ifndef DL_MALLOC
if (mem_block==0){
LOG(L_CRIT, "could not initialize memory pool\n");
fprintf(stderr, "Too much pkg memory demanded: %ld bytes\n",
pkg_mem_size);
return -1;
}
#endif
#endif
return 0;
}
/**
* \brief Destroy private memory pool
*/
void destroy_pkg_mallocs(void)
{
#ifdef PKG_MALLOC
#ifndef DL_MALLOC
if (mem_pool) {
free(mem_pool);
mem_pool = 0;
}
#endif
#endif /* PKG_MALLOC */
}
/**
* \brief Initialize shared memory pool
* \param force_alloc Force allocation of memory, e.g. initialize complete block with zero
* \return 0 if the memory allocation was successful, -1 otherwise
*/
int init_shm_mallocs(int force_alloc)
{
#ifdef SHM_MEM
if (shm_mem_init(force_alloc)<0) {
LOG(L_CRIT, "could not initialize shared memory pool, exiting...\n");
fprintf(stderr, "Too much shared memory demanded: %ld\n",
shm_mem_size );
return -1;
}
#endif
return 0;
}
#endif