Debian Codegraph @

Xpjproject/pjlib/include/pj/pool_i.h

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
20
21 #include <pj/string.h>
22
23 #define PJ_POOL_ALIGN_PTR(PTR,ALIGNMENT) (PTR + (-(pj_ssize_t)(PTR) & (ALIGNMENT-1)))
24 #define PJ_IS_POWER_OF_TWO(val) (((val)>0) && ((val) & ((val)-1))==0)
25 #define PJ_IS_ALIGNED(PTR, ALIGNMENT) (!((pj_ssize_t)(PTR) & ((ALIGNMENT)-1)))
26
27 PJ_IDEF(pj_size_t) pj_pool_get_capacity( pj_pool_t *pool )
28 {
29 return pool->capacity;
30 }
31
32 PJ_IDEF(pj_size_t) pj_pool_get_used_size( pj_pool_t *pool )
33 {
34 pj_pool_block *b = pool->block_list.next;
35 pj_size_t used_size = sizeof(pj_pool_t);
36 while (b != &pool->block_list) {
37 used_size += (b->cur - b->buf) + sizeof(pj_pool_block);
38 b = b->next;
39 }
40 return used_size;
41 }
42
43 PJ_IDEF(void*) pj_pool_alloc_from_block( pj_pool_block *block, pj_size_t alignment,
44 pj_size_t size )
45 {
46 unsigned char *ptr;
47
48 pj_assert(PJ_IS_POWER_OF_TWO(alignment));
49 // Size should be already aligned.
50 // this code was moved up to pj_pool_aligned_alloc.
51 // ...and then removed there
52 //
53 ///* The operation below is valid for size==0.
54 // * When size==0, the function will return the pointer to the pool
55 // * memory address, but no memory will be allocated.
56 // */
57 //if (size & (alignment -1)) {
58 // size = (size + alignment) & ~(alignment -1);
59 //}
60 ptr = PJ_POOL_ALIGN_PTR(block->cur, alignment);
61 if (block->cur <= ptr && /* check pointer not overflowing */
62 ptr <= block->end && /* check alignment not exceeding the upper limit */
63 (pj_size_t)(block->end - ptr) >= size) /* check available size */
64 {
65 block->cur = ptr + size;
66 return ptr;
67 }
68 return NULL;
69 }
70
71 PJ_IDEF(void*) pj_pool_alloc( pj_pool_t *pool, pj_size_t size)
72 {
73 return pj_pool_aligned_alloc(pool, 0, size);
74 }
75
76 PJ_IDEF(void*) pj_pool_aligned_alloc(pj_pool_t *pool, pj_size_t alignment,
77 pj_size_t size)
78 {
79 void *ptr;
80
81 PJ_ASSERT_RETURN(!alignment || PJ_IS_POWER_OF_TWO(alignment), NULL);
82
83 if (!alignment)
84 alignment = pool->alignment;
85
86 #if 0
87 //Rounding up is the compiler's job, not the allocator's.
88
89 /* The operation below is valid for size==0.
90 * When size==0, the function will return the pointer to the pool
91 * memory address, but no memory will be allocated.
92 */
93 if (size & (alignment -1)) {
94 size = (size + alignment) & ~(alignment -1);
95 }
96 pj_assert(PJ_IS_ALIGNED(size, alignment));
97 #endif
98
99 ptr = pj_pool_alloc_from_block(pool->block_list.next,
100 alignment, size);
101 if (!ptr)
102 ptr = pj_pool_allocate_find(pool, alignment, size);
103 return ptr;
104 }
105
106
107 PJ_IDEF(void*) pj_pool_calloc( pj_pool_t *pool, pj_size_t count, pj_size_t size)
108 {
109 void *buf = pj_pool_alloc( pool, size*count);
110 if (buf)
111 pj_bzero(buf, size * count);
112 return buf;
113 }
114
115 PJ_IDEF(const char *) pj_pool_getobjname( const pj_pool_t *pool )
116 {
117 return pool->obj_name;
118 }
119
120 PJ_IDEF(pj_pool_t*) pj_pool_create( pj_pool_factory *f,
121 const char *name,
122 pj_size_t initial_size,
123 pj_size_t increment_size,
124 pj_pool_callback *callback)
125 {
126 return pj_pool_aligned_create(f, name, initial_size, increment_size, 0, callback);
127 }
128
129 PJ_IDEF(pj_pool_t*) pj_pool_aligned_create(pj_pool_factory *f,
130 const char *name,
131 pj_size_t initial_size,
132 pj_size_t increment_size,
133 pj_size_t alignment,
134 pj_pool_callback *callback)
135 {
136 return (*f->create_pool)(f, name, initial_size, increment_size, alignment, callback);
137 }
138
139 PJ_IDEF(void) pj_pool_release( pj_pool_t *pool )
140 {
141 #if PJ_POOL_RELEASE_WIPE_DATA
142 pj_pool_block *b;
143
144 b = pool->block_list.next;
145 while (b != &pool->block_list) {
146 volatile unsigned char *p = b->buf;
147 while (p < b->end) *p++ = 0;
148 b = b->next;
149 }
150 #endif
151
152 if (pool->factory->release_pool)
153 (*pool->factory->release_pool)(pool->factory, pool);
154 }
155
156
157 PJ_IDEF(void) pj_pool_safe_release( pj_pool_t **ppool )
158 {
159 pj_pool_t *pool = *ppool;
160 *ppool = NULL;
161 if (pool)
162 pj_pool_release(pool);
163 }
164
165 PJ_IDEF(void) pj_pool_secure_release( pj_pool_t **ppool )
166 {
167 pj_pool_block *b;
168 pj_pool_t *pool = *ppool;
169 *ppool = NULL;
170
171 if (!pool)
172 return;
173
174 b = pool->block_list.next;
175 while (b != &pool->block_list) {
176 volatile unsigned char *p = b->buf;
177 while (p < b->end) *p++ = 0;
178 b = b->next;
179 }
180
181 pj_pool_release(pool);
182 }