blob: 1a9b626704e94f9450646227699ffe2a43df2a03 [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C)2003-2007 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
23PJ_DECL(void*) pj_pool_allocate_find(pj_pool_t *pool, unsigned size);
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{
Benny Prijonod4934802005-11-21 16:58:03 +000032 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;
Benny Prijono9033e312005-11-21 02:08:39 +000039}
40
Benny Prijonod4934802005-11-21 16:58:03 +000041PJ_IDEF(void*) pj_pool_alloc_from_block( pj_pool_block *block, pj_size_t size )
Benny Prijono9033e312005-11-21 02:08:39 +000042{
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)) {
Benny Prijonod4934802005-11-21 16:58:03 +000048 size = (size + PJ_POOL_ALIGNMENT) & ~(PJ_POOL_ALIGNMENT-1);
Benny Prijono9033e312005-11-21 02:08:39 +000049 }
50 if ((unsigned)(block->end - block->cur) >= size) {
51 void *ptr = block->cur;
52 block->cur += size;
Benny Prijono9033e312005-11-21 02:08:39 +000053 return ptr;
54 }
55 return NULL;
56}
57
58PJ_IDEF(void*) pj_pool_alloc( pj_pool_t *pool, pj_size_t size)
59{
Benny Prijonod4934802005-11-21 16:58:03 +000060 void *ptr = pj_pool_alloc_from_block(pool->block_list.next, size);
Benny Prijono9033e312005-11-21 02:08:39 +000061 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)
Benny Prijonoac623b32006-07-03 15:19:31 +000071 pj_bzero(buf, size * count);
Benny Prijono9033e312005-11-21 02:08:39 +000072 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{
Benny Prijono7d93d4e2006-09-17 19:54:23 +000091 if (pool->factory->release_pool)
92 (*pool->factory->release_pool)(pool->factory, pool);
Benny Prijono9033e312005-11-21 02:08:39 +000093}
94