blob: 1cc5bccd7ea3d3cf599bbc189192ab38d01d75d2 [file] [log] [blame]
Benny Prijono8508aa02006-03-30 15:56:01 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C)2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono8508aa02006-03-30 15:56:01 +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#include <pj/pool.h>
20#include <pj/string.h>
21
Benny Prijonod7c6d052008-07-12 09:31:34 +000022#if PJ_HAS_POOL_ALT_API
Benny Prijono8508aa02006-03-30 15:56:01 +000023
24#if PJ_HAS_MALLOC_H
25# include <malloc.h>
26#endif
27
28
29#if PJ_HAS_STDLIB_H
30# include <stdlib.h>
31#endif
32
33
34#if defined(PJ_WIN32) && PJ_WIN32!=0 && defined(PJ_DEBUG) && PJ_DEBUG!=0 \
35 && !PJ_NATIVE_STRING_IS_UNICODE
36# include <windows.h>
37# define TRACE_(msg) OutputDebugString(msg)
38#endif
39
40/* Uncomment this to enable TRACE_ */
41//#undef TRACE_
42
43
Benny Prijono8508aa02006-03-30 15:56:01 +000044
45int PJ_NO_MEMORY_EXCEPTION;
46
47
Benny Prijonod7c6d052008-07-12 09:31:34 +000048PJ_DEF(int) pj_NO_MEMORY_EXCEPTION()
49{
50 return PJ_NO_MEMORY_EXCEPTION;
51}
52
Benny Prijono8508aa02006-03-30 15:56:01 +000053/* Create pool */
54PJ_DEF(pj_pool_t*) pj_pool_create_imp( const char *file, int line,
55 void *factory,
56 const char *name,
57 pj_size_t initial_size,
58 pj_size_t increment_size,
59 pj_pool_callback *callback)
60{
61 pj_pool_t *pool;
62
63 PJ_UNUSED_ARG(file);
64 PJ_UNUSED_ARG(line);
65 PJ_UNUSED_ARG(factory);
Benny Prijono8508aa02006-03-30 15:56:01 +000066 PJ_UNUSED_ARG(initial_size);
67 PJ_UNUSED_ARG(increment_size);
68
69 pool = malloc(sizeof(struct pj_pool_t));
70 if (!pool)
71 return NULL;
72
Benny Prijonod7c6d052008-07-12 09:31:34 +000073 if (name) {
74 pj_ansi_strncpy(pool->obj_name, name, sizeof(pool->obj_name));
75 pool->obj_name[sizeof(pool->obj_name)-1] = '\0';
76 } else {
77 strcpy(pool->obj_name, "altpool");
78 }
79
80 pool->factory = NULL;
Benny Prijono8508aa02006-03-30 15:56:01 +000081 pool->first_mem = NULL;
82 pool->used_size = 0;
83 pool->cb = callback;
84
85 return pool;
86}
87
88
89/* Release pool */
90PJ_DEF(void) pj_pool_release_imp(pj_pool_t *pool)
91{
92 pj_pool_reset(pool);
93 free(pool);
94}
95
96/* Get pool name */
97PJ_DEF(const char*) pj_pool_getobjname_imp(pj_pool_t *pool)
98{
99 PJ_UNUSED_ARG(pool);
100 return "pooldbg";
101}
102
103/* Reset pool */
104PJ_DEF(void) pj_pool_reset_imp(pj_pool_t *pool)
105{
106 struct pj_pool_mem *mem;
107
108 mem = pool->first_mem;
109 while (mem) {
110 struct pj_pool_mem *next = mem->next;
111 free(mem);
112 mem = next;
113 }
114
115 pool->first_mem = NULL;
116}
117
118/* Get capacity */
119PJ_DEF(pj_size_t) pj_pool_get_capacity_imp(pj_pool_t *pool)
120{
121 PJ_UNUSED_ARG(pool);
122
123 /* Unlimited capacity */
124 return 0x7FFFFFFFUL;
125}
126
127/* Get total used size */
128PJ_DEF(pj_size_t) pj_pool_get_used_size_imp(pj_pool_t *pool)
129{
130 return pool->used_size;
131}
132
133/* Allocate memory from the pool */
134PJ_DEF(void*) pj_pool_alloc_imp( const char *file, int line,
135 pj_pool_t *pool, pj_size_t sz)
136{
137 struct pj_pool_mem *mem;
138
139 PJ_UNUSED_ARG(file);
140 PJ_UNUSED_ARG(line);
141
142 mem = malloc(sz + sizeof(struct pj_pool_mem));
143 if (!mem) {
144 if (pool->cb)
145 (*pool->cb)(pool, sz);
146 return NULL;
147 }
148
149 mem->next = pool->first_mem;
150 pool->first_mem = mem;
151
152#ifdef TRACE_
153 {
154 char msg[120];
155 pj_ansi_sprintf(msg, "Mem %X (%d+%d bytes) allocated by %s:%d\r\n",
156 mem, sz, sizeof(struct pj_pool_mem),
157 file, line);
158 TRACE_(msg);
159 }
160#endif
161
162 return ((char*)mem) + sizeof(struct pj_pool_mem);
163}
164
165/* Allocate memory from the pool and zero the memory */
166PJ_DEF(void*) pj_pool_calloc_imp( const char *file, int line,
167 pj_pool_t *pool, unsigned cnt,
168 unsigned elemsz)
169{
170 void *mem;
171
172 mem = pj_pool_alloc_imp(file, line, pool, cnt*elemsz);
173 if (!mem)
174 return NULL;
175
Benny Prijonoac623b32006-07-03 15:19:31 +0000176 pj_bzero(mem, cnt*elemsz);
Benny Prijono8508aa02006-03-30 15:56:01 +0000177 return mem;
178}
179
180/* Allocate memory from the pool and zero the memory */
181PJ_DEF(void*) pj_pool_zalloc_imp( const char *file, int line,
182 pj_pool_t *pool, pj_size_t sz)
183{
Benny Prijonod7c6d052008-07-12 09:31:34 +0000184 return pj_pool_calloc_imp(file, line, pool, 1, sz);
Benny Prijono8508aa02006-03-30 15:56:01 +0000185}
186
187
Benny Prijonod7c6d052008-07-12 09:31:34 +0000188
189#endif /* PJ_HAS_POOL_ALT_API */