1 /*
2  * Copyright (C) 2005 iptelorg GmbH
3  *
4  * This file is part of ser, a free SIP server.
5  *
6  * ser is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version
10  *
11  * For a license to use the ser software under conditions
12  * other than those described here, or to purchase support for this
13  * software, please contact iptel.org by e-mail at the following addresses:
14  *    info@iptel.org
15  *
16  * ser is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
24  */
25 
26 #ifndef __CDS_MEMORY_H
27 #define __CDS_MEMORY_H
28 
29 /* #define TRACE_CDS_MEMORY */
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 /** \ingroup cds
36  * @{
37  *
38  * \defgroup cds_memory Memory management
39  *
40  * Memory operations are common for whole CDS library. Because it must work
41  together with SER's memory management and must work without it too, there are
42  wrapper macros for memory allocation/deallocation.
43  *
44  * @{ */
45 
46 /* typedef void*(*cds_malloc_func)(unsigned int size);
47 typedef void(*cds_free_func)(void *ptr);
48 
49 extern cds_malloc_func cds_malloc;
50 extern cds_free_func cds_free;
51 
52 void cds_set_memory_functions(cds_malloc_func _malloc, cds_free_func _free); */
53 
54 /** \def cds_malloc(s)
55  * Function/macro for memory allocation. Which function is choosen depends on
56  * SER and TRACE_CDS_MEMORY defines.
57  *
58  * When SER is defined shm_malloc is choosen, standard malloc otherwise. */
59 
60 /** \def cds_free(p)
61  * Function/macro for memory deallocation. Which function is choosen depends
62  * on SER and TRACE_CDS_MEMORY defines.
63  *
64  * If SER is defined shm_free is choosen, standard free otherwise. */
65 
66 /** \def cds_malloc_ptr
67  * Function/macro for memory allocation when pointer to function needed. Which
68  * function is choosen depends on SER and TRACE_CDS_MEMORY defines.
69  *
70  * If SER is defined shm_malloc is choosen, standard malloc otherwise.  */
71 
72 /** \def cds_free_ptr
73  * Function/macro for memory deallocation when pointer to function needed.
74  * Which function is choosen depends on SER and TRACE_CDS_MEMORY defines.
75  *
76  * If SER is defined shm_free is choosen, standard free otherwise.  */
77 
78 /** \def cds_malloc_pkg(s)
79  * Function/macro for 'local' memory allocation. Which function is choosen
80  * depends on SER and TRACE_CDS_MEMORY defines.
81  *
82  * When SER is defined pkg_malloc is choosen, standard malloc otherwise. */
83 
84 /** \def cds_free_pkg(p)
85  * Function/macro for 'local' memory deallocation. Which function is choosen
86  * depends on SER and TRACE_CDS_MEMORY defines.
87  *
88  * When SER is defined pkg_free is choosen, standard free otherwise. */
89 
90 #ifdef TRACE_CDS_MEMORY
91 
92 /** \internal Debugging variant of alloc function */
93 void *debug_malloc(int size, const char *file, int line);
94 
95 /** \internal Debugging variant of free function */
96 void debug_free(void *block, const char *file, int line);
97 
98 /** \internal Another debugging variant of alloc function - used when pointer
99  * to function needed. */
100 void *debug_malloc_ex(unsigned int size);
101 
102 /** \internal Another debugging variant of free function - used when pointer to
103  * function needed. */
104 void debug_free_ex(void *block);
105 
106 /* \internal Helper function for debugging - shows some debugging information about
107  * memory allocations (currently only the number of allocated blocks). */
108 void cds_memory_trace(char *dst, int dst_len);
109 
110 /** \internal Helper function which is useful for memory debugging only - initializes
111  * internal variables for memory tracing */
112 void cds_memory_trace_init();
113 
114 #define cds_malloc(s)	debug_malloc(s,__FILE__, __LINE__)
115 #define cds_free(p)		debug_free(p,__FILE__, __LINE__)
116 #define cds_free_ptr	debug_free_ex
117 #define cds_malloc_ptr	debug_malloc_ex
118 #define cds_malloc_pkg(s)	debug_malloc(s,__FILE__, __LINE__)
119 #define cds_free_pkg(p)		debug_free(p,__FILE__, __LINE__)
120 
121 #else /* !TRACE */
122 
123 #ifdef SER
124 
125 #include <mem/mem.h>
126 #include <mem/shm_mem.h>
127 
128 void* shm_malloc_x(unsigned int size);
129 void shm_free_x(void *ptr);
130 
131 #define cds_malloc(s)	shm_malloc(s)
132 #define cds_free(p)		shm_free(p)
133 #define cds_malloc_ptr	shm_malloc_x
134 #define cds_free_ptr	shm_free_x
135 #define cds_malloc_pkg(s)	pkg_malloc(s)
136 #define cds_free_pkg(p)		pkg_free(p)
137 
138 #else /* !SER */
139 
140 #include <stdlib.h>
141 
142 #define cds_malloc(s)	malloc(s)
143 #define cds_free(p)		free(p)
144 #define cds_malloc_ptr	malloc
145 #define cds_free_ptr	free
146 #define cds_malloc_pkg(s)	malloc(s)
147 #define cds_free_pkg(p)		free(p)
148 
149 #endif /* !SER */
150 
151 #endif /* !TRACE_CDS_MEMORY */
152 
153 #ifdef __cplusplus
154 }
155 #endif
156 
157 /** @}
158  * @} */
159 
160 #endif
161 
162