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