blob: 39c04877e1bfc047186b6dcca6d34cd0628d92ea [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
3 * Copyright (C)2003-2006 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
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{
32 return pool->used_size;
33}
34
35PJ_IDEF(void*) pj_pool_alloc_from_block( pj_pool_t *pool,
36 pj_pool_block *block, pj_size_t size )
37{
38 /* The operation below is valid for size==0.
39 * When size==0, the function will return the pointer to the pool
40 * memory address, but no memory will be allocated.
41 */
42 if (size & (PJ_POOL_ALIGNMENT-1)) {
43 size &= ~(PJ_POOL_ALIGNMENT-1);
44 size += PJ_POOL_ALIGNMENT;
45 }
46 if ((unsigned)(block->end - block->cur) >= size) {
47 void *ptr = block->cur;
48 block->cur += size;
49 pool->used_size += size;
50 return ptr;
51 }
52 return NULL;
53}
54
55PJ_IDEF(void*) pj_pool_alloc( pj_pool_t *pool, pj_size_t size)
56{
57 pj_pool_block *block = pool->block_list.next;
58 void *ptr = pj_pool_alloc_from_block(pool, block, size);
59 if (!ptr)
60 ptr = pj_pool_allocate_find(pool, size);
61 return ptr;
62}
63
64
65PJ_IDEF(void*) pj_pool_calloc( pj_pool_t *pool, pj_size_t count, pj_size_t size)
66{
67 void *buf = pj_pool_alloc( pool, size*count);
68 if (buf)
69 pj_memset(buf, 0, size * count);
70 return buf;
71}
72
73PJ_IDEF(const char *) pj_pool_getobjname( const pj_pool_t *pool )
74{
75 return pool->obj_name;
76}
77
78PJ_IDEF(pj_pool_t*) pj_pool_create( pj_pool_factory *f,
79 const char *name,
80 pj_size_t initial_size,
81 pj_size_t increment_size,
82 pj_pool_callback *callback)
83{
84 return (*f->create_pool)(f, name, initial_size, increment_size, callback);
85}
86
87PJ_IDEF(void) pj_pool_release( pj_pool_t *pool )
88{
89 (*pool->factory->release_pool)(pool->factory, pool);
90}
91