1 /* $Id$ */
2 /*
3  * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4  * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5  *
6  * This program 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  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 
21 
22 #include <pj/string.h>
23 
24 
pj_pool_get_capacity(pj_pool_t * pool)25 PJ_IDEF(pj_size_t) pj_pool_get_capacity( pj_pool_t *pool )
26 {
27     return pool->capacity;
28 }
29 
pj_pool_get_used_size(pj_pool_t * pool)30 PJ_IDEF(pj_size_t) pj_pool_get_used_size( pj_pool_t *pool )
31 {
32     pj_pool_block *b = pool->block_list.next;
33     pj_size_t used_size = sizeof(pj_pool_t);
34     while (b != &pool->block_list) {
35 	used_size += (b->cur - b->buf) + sizeof(pj_pool_block);
36 	b = b->next;
37     }
38     return used_size;
39 }
40 
pj_pool_alloc_from_block(pj_pool_block * block,pj_size_t size)41 PJ_IDEF(void*) pj_pool_alloc_from_block( pj_pool_block *block, pj_size_t size )
42 {
43     /* The operation below is valid for size==0.
44      * When size==0, the function will return the pointer to the pool
45      * memory address, but no memory will be allocated.
46      */
47     if (size & (PJ_POOL_ALIGNMENT-1)) {
48 	size = (size + PJ_POOL_ALIGNMENT) & ~(PJ_POOL_ALIGNMENT-1);
49     }
50     if ((pj_size_t)(block->end - block->cur) >= size) {
51 	void *ptr = block->cur;
52 	block->cur += size;
53 	return ptr;
54     }
55     return NULL;
56 }
57 
pj_pool_alloc(pj_pool_t * pool,pj_size_t size)58 PJ_IDEF(void*) pj_pool_alloc( pj_pool_t *pool, pj_size_t size)
59 {
60     void *ptr = pj_pool_alloc_from_block(pool->block_list.next, size);
61     if (!ptr)
62 	ptr = pj_pool_allocate_find(pool, size);
63     return ptr;
64 }
65 
66 
pj_pool_calloc(pj_pool_t * pool,pj_size_t count,pj_size_t size)67 PJ_IDEF(void*) pj_pool_calloc( pj_pool_t *pool, pj_size_t count, pj_size_t size)
68 {
69     void *buf = pj_pool_alloc( pool, size*count);
70     if (buf)
71 	pj_bzero(buf, size * count);
72     return buf;
73 }
74 
pj_pool_getobjname(const pj_pool_t * pool)75 PJ_IDEF(const char *) pj_pool_getobjname( const pj_pool_t *pool )
76 {
77     return pool->obj_name;
78 }
79 
pj_pool_create(pj_pool_factory * f,const char * name,pj_size_t initial_size,pj_size_t increment_size,pj_pool_callback * callback)80 PJ_IDEF(pj_pool_t*) pj_pool_create( pj_pool_factory *f,
81 				    const char *name,
82 				    pj_size_t initial_size,
83 				    pj_size_t increment_size,
84 				    pj_pool_callback *callback)
85 {
86     return (*f->create_pool)(f, name, initial_size, increment_size, callback);
87 }
88 
pj_pool_release(pj_pool_t * pool)89 PJ_IDEF(void) pj_pool_release( pj_pool_t *pool )
90 {
91 #if PJ_POOL_RELEASE_WIPE_DATA
92     pj_pool_block *b;
93 
94     b = pool->block_list.next;
95     while (b != &pool->block_list) {
96 	volatile unsigned char *p = b->buf;
97 	while (p < b->end) *p++ = 0;
98 	b = b->next;
99     }
100 #endif
101 
102     if (pool->factory->release_pool)
103 	(*pool->factory->release_pool)(pool->factory, pool);
104 }
105 
106 
pj_pool_safe_release(pj_pool_t ** ppool)107 PJ_IDEF(void) pj_pool_safe_release( pj_pool_t **ppool )
108 {
109     pj_pool_t *pool = *ppool;
110     *ppool = NULL;
111     if (pool)
112 	pj_pool_release(pool);
113 }
114 
pj_pool_secure_release(pj_pool_t ** ppool)115 PJ_IDEF(void) pj_pool_secure_release( pj_pool_t **ppool )
116 {
117     pj_pool_block *b;
118     pj_pool_t *pool = *ppool;
119     *ppool = NULL;
120 
121     if (!pool)
122 	return;
123 
124     b = pool->block_list.next;
125     while (b != &pool->block_list) {
126 	volatile unsigned char *p = b->buf;
127 	while (p < b->end) *p++ = 0;
128 	b = b->next;
129     }
130 
131     pj_pool_release(pool);
132 }
133