/src/pjsip/pjlib/include/pj/pool_i.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
3 | | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
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 | | |
24 | | PJ_IDEF(pj_size_t) pj_pool_get_capacity( pj_pool_t *pool ) |
25 | 0 | { |
26 | 0 | return pool->capacity; |
27 | 0 | } |
28 | | |
29 | | PJ_IDEF(pj_size_t) pj_pool_get_used_size( pj_pool_t *pool ) |
30 | 0 | { |
31 | 0 | pj_pool_block *b = pool->block_list.next; |
32 | 0 | pj_size_t used_size = sizeof(pj_pool_t); |
33 | 0 | while (b != &pool->block_list) { |
34 | 0 | used_size += (b->cur - b->buf) + sizeof(pj_pool_block); |
35 | 0 | b = b->next; |
36 | 0 | } |
37 | 0 | return used_size; |
38 | 0 | } |
39 | | |
40 | | PJ_IDEF(void*) pj_pool_alloc_from_block( pj_pool_block *block, pj_size_t size ) |
41 | 436 | { |
42 | | /* The operation below is valid for size==0. |
43 | | * When size==0, the function will return the pointer to the pool |
44 | | * memory address, but no memory will be allocated. |
45 | | */ |
46 | 436 | if (size & (PJ_POOL_ALIGNMENT-1)) { |
47 | 0 | size = (size + PJ_POOL_ALIGNMENT) & ~(PJ_POOL_ALIGNMENT-1); |
48 | 0 | } |
49 | 436 | if ((pj_size_t)(block->end - block->cur) >= size) { |
50 | 436 | void *ptr = block->cur; |
51 | 436 | block->cur += size; |
52 | 436 | return ptr; |
53 | 436 | } |
54 | 0 | return NULL; |
55 | 436 | } |
56 | | |
57 | | PJ_IDEF(void*) pj_pool_alloc( pj_pool_t *pool, pj_size_t size) |
58 | 436 | { |
59 | 436 | void *ptr = pj_pool_alloc_from_block(pool->block_list.next, size); |
60 | 436 | if (!ptr) |
61 | 0 | ptr = pj_pool_allocate_find(pool, size); |
62 | 436 | return ptr; |
63 | 436 | } |
64 | | |
65 | | |
66 | | PJ_IDEF(void*) pj_pool_calloc( pj_pool_t *pool, pj_size_t count, pj_size_t size) |
67 | 0 | { |
68 | 0 | void *buf = pj_pool_alloc( pool, size*count); |
69 | 0 | if (buf) |
70 | 0 | pj_bzero(buf, size * count); |
71 | 0 | return buf; |
72 | 0 | } |
73 | | |
74 | | PJ_IDEF(const char *) pj_pool_getobjname( const pj_pool_t *pool ) |
75 | 0 | { |
76 | 0 | return pool->obj_name; |
77 | 0 | } |
78 | | |
79 | | PJ_IDEF(pj_pool_t*) pj_pool_create( pj_pool_factory *f, |
80 | | const char *name, |
81 | | pj_size_t initial_size, |
82 | | pj_size_t increment_size, |
83 | | pj_pool_callback *callback) |
84 | 0 | { |
85 | 0 | return (*f->create_pool)(f, name, initial_size, increment_size, callback); |
86 | 0 | } |
87 | | |
88 | | PJ_IDEF(void) pj_pool_release( pj_pool_t *pool ) |
89 | 0 | { |
90 | | #if PJ_POOL_RELEASE_WIPE_DATA |
91 | | pj_pool_block *b; |
92 | | |
93 | | b = pool->block_list.next; |
94 | | while (b != &pool->block_list) { |
95 | | volatile unsigned char *p = b->buf; |
96 | | while (p < b->end) *p++ = 0; |
97 | | b = b->next; |
98 | | } |
99 | | #endif |
100 | |
|
101 | 0 | if (pool->factory->release_pool) |
102 | 0 | (*pool->factory->release_pool)(pool->factory, pool); |
103 | 0 | } |
104 | | |
105 | | |
106 | | PJ_IDEF(void) pj_pool_safe_release( pj_pool_t **ppool ) |
107 | 0 | { |
108 | 0 | pj_pool_t *pool = *ppool; |
109 | 0 | *ppool = NULL; |
110 | 0 | if (pool) |
111 | 0 | pj_pool_release(pool); |
112 | 0 | } |
113 | | |
114 | | PJ_IDEF(void) pj_pool_secure_release( pj_pool_t **ppool ) |
115 | 0 | { |
116 | 0 | pj_pool_block *b; |
117 | 0 | pj_pool_t *pool = *ppool; |
118 | 0 | *ppool = NULL; |
119 | |
|
120 | 0 | if (!pool) |
121 | 0 | return; |
122 | | |
123 | 0 | b = pool->block_list.next; |
124 | 0 | while (b != &pool->block_list) { |
125 | 0 | volatile unsigned char *p = b->buf; |
126 | 0 | while (p < b->end) *p++ = 0; |
127 | 0 | b = b->next; |
128 | 0 | } |
129 | |
|
130 | 0 | pj_pool_release(pool); |
131 | 0 | } |