Coverage Report

Created: 2024-07-23 06:24

/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
6.80k
{
26
6.80k
    return pool->capacity;
27
6.80k
}
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
78.4k
{
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
78.4k
    if (size & (PJ_POOL_ALIGNMENT-1)) {
47
0
        size = (size + PJ_POOL_ALIGNMENT) & ~(PJ_POOL_ALIGNMENT-1);
48
0
    }
49
78.4k
    if ((pj_size_t)(block->end - block->cur) >= size) {
50
46.5k
        void *ptr = block->cur;
51
46.5k
        block->cur += size;
52
46.5k
        return ptr;
53
46.5k
    }
54
31.9k
    return NULL;
55
78.4k
}
56
57
PJ_IDEF(void*) pj_pool_alloc( pj_pool_t *pool, pj_size_t size)
58
46.5k
{
59
46.5k
    void *ptr = pj_pool_alloc_from_block(pool->block_list.next, size);
60
46.5k
    if (!ptr)
61
8.84k
        ptr = pj_pool_allocate_find(pool, size);
62
46.5k
    return ptr;
63
46.5k
}
64
65
66
PJ_IDEF(void*) pj_pool_calloc( pj_pool_t *pool, pj_size_t count, pj_size_t size)
67
20.8k
{
68
20.8k
    void *buf = pj_pool_alloc( pool, size*count);
69
20.8k
    if (buf)
70
20.8k
        pj_bzero(buf, size * count);
71
20.8k
    return buf;
72
20.8k
}
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
6.80k
{
85
6.80k
    return (*f->create_pool)(f, name, initial_size, increment_size, callback);
86
6.80k
}
87
88
PJ_IDEF(void) pj_pool_release( pj_pool_t *pool )
89
6.80k
{
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
6.80k
    if (pool->factory->release_pool)
102
6.80k
        (*pool->factory->release_pool)(pool->factory, pool);
103
6.80k
}
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
}