Debian Codegraph @

Xpjproject/pjlib/src/pj/pool_policy_new.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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 #include <pj/pool.h>
20 #include <pj/except.h>
21 #include <pj/os.h>
22
23 #if !PJ_HAS_POOL_ALT_API
24
25 /*
26 * This file contains pool default policy definition and implementation.
27 */
28 #include "pool_signature.h"
29
30
31 static void *operator_new(pj_pool_factory *factory, pj_size_t size)
32 {
33 void *mem;
34
35 PJ_CHECK_STACK();
36
37 if (factory->on_block_alloc) {
38 int rc;
39 rc = factory->on_block_alloc(factory, size);
40 if (!rc)
41 return NULL;
42 }
43
44 mem = (void*) new char[size+(SIG_SIZE << 1)];
45
46 /* Exception for new operator may be disabled, so.. */
47 if (mem) {
48 /* Apply signature when PJ_SAFE_POOL is set. It will move
49 * "mem" pointer forward.
50 */
51 APPLY_SIG(mem, size);
52 }
53
54 return mem;
55 }
56
57 static void operator_delete(pj_pool_factory *factory, void *mem, pj_size_t size)
58 {
59 PJ_CHECK_STACK();
60
61 if (factory->on_block_free)
62 factory->on_block_free(factory, size);
63
64 /* Check and remove signature when PJ_SAFE_POOL is set. It will
65 * move "mem" pointer backward.
66 */
67 REMOVE_SIG(mem, size);
68
69 /* Note that when PJ_SAFE_POOL is set, the actual size of the block
70 * is size + SIG_SIZE*2.
71 */
72
73 char *p = (char*)mem;
74 delete [] p;
75 }
76
77 static void default_pool_callback(pj_pool_t *pool, pj_size_t size)
78 {
79 PJ_CHECK_STACK();
80 PJ_UNUSED_ARG(pool);
81 PJ_UNUSED_ARG(size);
82
83 PJ_THROW(PJ_NO_MEMORY_EXCEPTION);
84 }
85
86 PJ_DEF_DATA(pj_pool_factory_policy) pj_pool_factory_default_policy =
87 {
88 &operator_new,
89 &operator_delete,
90 &default_pool_callback,
91 0
92 };
93
94 PJ_DEF(const pj_pool_factory_policy*) pj_pool_factory_get_default_policy(void)
95 {
96 return &pj_pool_factory_default_policy;
97 }
98
99
100 #endif /* PJ_HAS_POOL_ALT_API */
101