blob: 376fd6ac2025eb391b21db4ced8368f1c11fe26e [file] [log] [blame]
Alexandre Lision8af73cb2013-12-10 14:11:20 -05001/* $Id: pool_i.h 4298 2012-11-22 05:00:01Z nanang $ */
Tristan Matthews0a329cc2013-07-17 13:20:14 -04002/*
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
25PJ_IDEF(pj_size_t) pj_pool_get_capacity( pj_pool_t *pool )
26{
27 return pool->capacity;
28}
29
30PJ_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
41PJ_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
58PJ_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
67PJ_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
75PJ_IDEF(const char *) pj_pool_getobjname( const pj_pool_t *pool )
76{
77 return pool->obj_name;
78}
79
80PJ_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
89PJ_IDEF(void) pj_pool_release( pj_pool_t *pool )
90{
91 if (pool->factory->release_pool)
92 (*pool->factory->release_pool)(pool->factory, pool);
93}
94