Debian Codegraph @

Xpjproject/pjlib/src/pj/pool_dbg.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include <pj/pool.h>
20 #include <pj/assert.h>
21 #include <pj/string.h>
22
23 #if PJ_HAS_POOL_ALT_API
24
25 #if PJ_HAS_MALLOC_H
26 # include <malloc.h>
27 #endif
28
29
30 #if PJ_HAS_STDLIB_H
31 # include <stdlib.h>
32 #endif
33
34
35 #if ((defined(PJ_WIN32) && PJ_WIN32!=0) || \
36 (defined(PJ_WIN64) && PJ_WIN64 != 0)) && \
37 defined(PJ_DEBUG) && PJ_DEBUG!=0 && !PJ_NATIVE_STRING_IS_UNICODE
38 # include <windows.h>
39 # define TRACE_(msg) OutputDebugString(msg)
40 #endif
41
42 /* Uncomment this to enable TRACE_ */
43 //#undef TRACE_
44
45 #define IS_POWER_OF_TWO(val) (((val)>0) && ((val) & ((val)-1))==0)
46
47 PJ_DEF_DATA(int) PJ_NO_MEMORY_EXCEPTION;
48
49
50 PJ_DEF(int) pj_NO_MEMORY_EXCEPTION()
51 {
52 return PJ_NO_MEMORY_EXCEPTION;
53 }
54
55 /* Create pool */
56 PJ_DEF(pj_pool_t*) pj_pool_create_imp( const char *file, int line,
57 void *factory,
58 const char *name,
59 pj_size_t initial_size,
60 pj_size_t increment_size,
61 pj_size_t alignment,
62 pj_pool_callback *callback)
63 {
64 pj_pool_t *pool;
65
66 PJ_UNUSED_ARG(file);
67 PJ_UNUSED_ARG(line);
68 PJ_UNUSED_ARG(factory);
69 PJ_UNUSED_ARG(initial_size);
70 PJ_UNUSED_ARG(increment_size);
71
72 if (!alignment)
73 alignment = PJ_POOL_ALIGNMENT;
74
75 PJ_ASSERT_RETURN(IS_POWER_OF_TWO(alignment), NULL);
76
77 pool = malloc(sizeof(struct pj_pool_t));
78 if (!pool)
79 return NULL;
80
81 if (name) {
82 char *p = pj_ansi_strchr(name, '%');
83 if (p && *(p+1)=='p' && *(p+2)=='\0') {
84 /* Special name with "%p" suffix */
85 pj_ansi_snprintf(pool->obj_name, sizeof(pool->obj_name),
86 name, pool);
87 } else {
88 pj_ansi_strxcpy(pool->obj_name, name, PJ_MAX_OBJ_NAME);
89 }
90 } else {
91 pj_ansi_strxcpy(pool->obj_name, "altpool", sizeof(pool->obj_name));
92 }
93
94 pool->factory = NULL;
95 pool->first_mem = NULL;
96 pool->used_size = 0;
97 pool->alignment = alignment;
98 pool->cb = callback;
99
100 return pool;
101 }
102
103
104 /* Release pool */
105 PJ_DEF(void) pj_pool_release_imp(pj_pool_t *pool)
106 {
107 pj_pool_reset(pool);
108 free(pool);
109 }
110
111 /* Safe release pool */
112 PJ_DEF(void) pj_pool_safe_release_imp( pj_pool_t **ppool )
113 {
114 pj_pool_t *pool = *ppool;
115 *ppool = NULL;
116 if (pool)
117 pj_pool_release(pool);
118 }
119
120 /* Secure release pool */
121 PJ_DEF(void) pj_pool_secure_release_imp( pj_pool_t **ppool )
122 {
123 /* Secure release is not implemented, so we just call
124 * safe release.
125 */
126 pj_pool_safe_release_imp(ppool);
127 }
128
129 /* Get pool name */
130 PJ_DEF(const char*) pj_pool_getobjname_imp(pj_pool_t *pool)
131 {
132 PJ_UNUSED_ARG(pool);
133 return "pooldbg";
134 }
135
136 /* Reset pool */
137 PJ_DEF(void) pj_pool_reset_imp(pj_pool_t *pool)
138 {
139 struct pj_pool_mem *mem;
140
141 mem = pool->first_mem;
142 while (mem) {
143 struct pj_pool_mem *next = mem->next;
144 free(mem);
145 mem = next;
146 }
147
148 pool->first_mem = NULL;
149 }
150
151 /* Get capacity */
152 PJ_DEF(pj_size_t) pj_pool_get_capacity_imp(pj_pool_t *pool)
153 {
154 PJ_UNUSED_ARG(pool);
155
156 /* Unlimited capacity */
157 return 0x7FFFFFFFUL;
158 }
159
160 /* Get total used size */
161 PJ_DEF(pj_size_t) pj_pool_get_used_size_imp(pj_pool_t *pool)
162 {
163 return pool->used_size;
164 }
165
166 /* Allocate memory from the pool */
167 PJ_DEF(void*) pj_pool_alloc_imp( const char *file, int line,
168 pj_pool_t *pool, pj_size_t alignment,
169 pj_size_t sz)
170 {
171 struct pj_pool_mem *mem;
172 char *buf;
173
174 PJ_UNUSED_ARG(file);
175 PJ_UNUSED_ARG(line);
176
177 if (!alignment)
178 alignment = pool->alignment;
179
180 PJ_ASSERT_RETURN(IS_POWER_OF_TWO(alignment), NULL);
181
182 /* obey alignment request from user */
183 if (sz & (alignment - 1)) {
184 sz = (sz + alignment) & ~(alignment - 1);
185 }
186 mem = malloc(sz + /* allocation size, already aligned */
187 alignment-1 + /*gap [0:alignment-1] to align allocation*/
188 sizeof(struct pj_pool_mem)); /*block header, may be unaligned*/
189 if (!mem) {
190 if (pool->cb)
191 (*pool->cb)(pool, sz);
192 return NULL;
193 }
194
195 mem->next = pool->first_mem;
196 pool->first_mem = mem;
197
198 #ifdef TRACE_
199 {
200 char msg[120];
201 pj_ansi_snprintf(msg, sizeof(msg),
202 "Mem %X (%u+%u bytes) allocated by %s:%d\r\n",
203 (unsigned)(intptr_t)mem, (unsigned)sz,
204 (unsigned)sizeof(struct pj_pool_mem),
205 file, line);
206 TRACE_(msg);
207 }
208 #endif
209 buf = ((char*)mem) + sizeof(struct pj_pool_mem);
210 return (buf + (-(pj_ssize_t)buf & (alignment-1)));
211 }
212
213 /* Allocate memory from the pool and zero the memory */
214 PJ_DEF(void*) pj_pool_calloc_imp( const char *file, int line,
215 pj_pool_t *pool, unsigned cnt,
216 unsigned elemsz)
217 {
218 void *mem;
219
220 mem = pj_pool_alloc_imp(file, line, pool, 0, cnt*elemsz);
221 if (!mem)
222 return NULL;
223
224 pj_bzero(mem, cnt*elemsz);
225 return mem;
226 }
227
228 /* Allocate memory from the pool and zero the memory */
229 PJ_DEF(void*) pj_pool_zalloc_imp( const char *file, int line,
230 pj_pool_t *pool, pj_size_t sz)
231 {
232 return pj_pool_calloc_imp(file, line, pool, 1, (unsigned)sz);
233 }
234
235
236
237 #endif /* PJ_HAS_POOL_ALT_API */