Debian Codegraph @

Xpjproject/pjlib/src/pj/pool_buf.c

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_buf.h>
20 #include <pj/assert.h>
21 #include <pj/os.h>
22
23 static struct pj_pool_factory stack_based_factory;
24
25 struct creation_param
26 {
27 void *stack_buf;
28 pj_size_t size;
29 };
30
31 static int is_initialized;
32 static long tls = -1;
33 static void* stack_alloc(pj_pool_factory *factory, pj_size_t size);
34
35 static void pool_buf_cleanup(void)
36 {
37 if (tls != -1) {
38 pj_thread_local_free(tls);
39 tls = -1;
40 }
41 if (is_initialized)
42 is_initialized = 0;
43 }
44
45 static pj_status_t pool_buf_initialize(void)
46 {
47 pj_atexit(&pool_buf_cleanup);
48
49 stack_based_factory.policy.block_alloc = &stack_alloc;
50 return pj_thread_local_alloc(&tls);
51 }
52
53 static void* stack_alloc(pj_pool_factory *factory, pj_size_t size)
54 {
55 struct creation_param *param;
56 void *buf;
57
58 PJ_UNUSED_ARG(factory);
59
60 param = (struct creation_param*) pj_thread_local_get(tls);
61 if (param == NULL) {
62 /* Don't assert(), this is normal no-memory situation */
63 return NULL;
64 }
65
66 pj_thread_local_set(tls, NULL);
67
68 PJ_ASSERT_RETURN(size <= param->size, NULL);
69
70 buf = param->stack_buf;
71
72 /* Prevent the buffer from being reused */
73 param->stack_buf = NULL;
74
75 return buf;
76 }
77
78
79 PJ_DEF(pj_pool_t*) pj_pool_create_on_buf(const char *name,
80 void *buf,
81 pj_size_t size)
82 {
83 #if PJ_HAS_POOL_ALT_API == 0
84 struct creation_param param;
85 pj_size_t align_diff;
86
87 PJ_ASSERT_RETURN(buf && size, NULL);
88
89 if (!is_initialized) {
90 if (pool_buf_initialize() != PJ_SUCCESS)
91 return NULL;
92 is_initialized = 1;
93 }
94
95 /* Check and align buffer */
96 align_diff = (pj_size_t)buf;
97 if (align_diff & (PJ_POOL_ALIGNMENT-1)) {
98 align_diff &= (PJ_POOL_ALIGNMENT-1);
99 buf = (void*) (((char*)buf) + align_diff);
100 size -= align_diff;
101 }
102
103 param.stack_buf = buf;
104 param.size = size;
105 pj_thread_local_set(tls, &param);
106
107 return pj_pool_create_int(&stack_based_factory, name, size, 0,
108 PJ_POOL_ALIGNMENT,
109 pj_pool_factory_default_policy.callback);
110 #else
111 PJ_UNUSED_ARG(buf);
112 PJ_UNUSED_ARG(pool_buf_initialize);
113 return pj_pool_create(NULL, name, size, size, NULL);
114 #endif
115 }
116