blob: 40c348eaa6579536e8bd45839ef44fae0fe2a9e8 [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C)2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono9033e312005-11-21 02:08:39 +00004 *
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
Benny Prijono9033e312005-11-21 02:08:39 +000023
24PJ_IDEF(pj_size_t) pj_pool_get_capacity( pj_pool_t *pool )
25{
26 return pool->capacity;
27}
28
29PJ_IDEF(pj_size_t) pj_pool_get_used_size( pj_pool_t *pool )
30{
Benny Prijonod4934802005-11-21 16:58:03 +000031 pj_pool_block *b = pool->block_list.next;
32 pj_size_t used_size = sizeof(pj_pool_t);
33 while (b != &pool->block_list) {
34 used_size += (b->cur - b->buf) + sizeof(pj_pool_block);
35 b = b->next;
36 }
37 return used_size;
Benny Prijono9033e312005-11-21 02:08:39 +000038}
39
Benny Prijonod4934802005-11-21 16:58:03 +000040PJ_IDEF(void*) pj_pool_alloc_from_block( pj_pool_block *block, pj_size_t size )
Benny Prijono9033e312005-11-21 02:08:39 +000041{
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 if (size & (PJ_POOL_ALIGNMENT-1)) {
Benny Prijonod4934802005-11-21 16:58:03 +000047 size = (size + PJ_POOL_ALIGNMENT) & ~(PJ_POOL_ALIGNMENT-1);
Benny Prijono9033e312005-11-21 02:08:39 +000048 }
49 if ((unsigned)(block->end - block->cur) >= size) {
50 void *ptr = block->cur;
51 block->cur += size;
Benny Prijono9033e312005-11-21 02:08:39 +000052 return ptr;
53 }
54 return NULL;
55}
56
57PJ_IDEF(void*) pj_pool_alloc( pj_pool_t *pool, pj_size_t size)
58{
Benny Prijonod4934802005-11-21 16:58:03 +000059 void *ptr = pj_pool_alloc_from_block(pool->block_list.next, size);
Benny Prijono9033e312005-11-21 02:08:39 +000060 if (!ptr)
61 ptr = pj_pool_allocate_find(pool, size);
62 return ptr;
63}
64
65
66PJ_IDEF(void*) pj_pool_calloc( pj_pool_t *pool, pj_size_t count, pj_size_t size)
67{
68 void *buf = pj_pool_alloc( pool, size*count);
69 if (buf)
Benny Prijonoac623b32006-07-03 15:19:31 +000070 pj_bzero(buf, size * count);
Benny Prijono9033e312005-11-21 02:08:39 +000071 return buf;
72}
73
74PJ_IDEF(const char *) pj_pool_getobjname( const pj_pool_t *pool )
75{
76 return pool->obj_name;
77}
78
79PJ_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{
85 return (*f->create_pool)(f, name, initial_size, increment_size, callback);
86}
87
88PJ_IDEF(void) pj_pool_release( pj_pool_t *pool )
89{
Benny Prijono7d93d4e2006-09-17 19:54:23 +000090 if (pool->factory->release_pool)
91 (*pool->factory->release_pool)(pool->factory, pool);
Benny Prijono9033e312005-11-21 02:08:39 +000092}
93