OLD | NEW |
| (Empty) |
1 /* Licensed to the Apache Software Foundation (ASF) under one or more | |
2 * contributor license agreements. See the NOTICE file distributed with | |
3 * this work for additional information regarding copyright ownership. | |
4 * The ASF licenses this file to You under the Apache License, Version 2.0 | |
5 * (the "License"); you may not use this file except in compliance with | |
6 * the License. You may obtain a copy of the License at | |
7 * | |
8 * http://www.apache.org/licenses/LICENSE-2.0 | |
9 * | |
10 * Unless required by applicable law or agreed to in writing, software | |
11 * distributed under the License is distributed on an "AS IS" BASIS, | |
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 * See the License for the specific language governing permissions and | |
14 * limitations under the License. | |
15 */ | |
16 | |
17 /* | |
18 * | |
19 * @author Mladen Turk | |
20 * @version $Id: pool.c 1442587 2013-02-05 13:49:48Z rjung $ | |
21 */ | |
22 | |
23 #include "tcn.h" | |
24 | |
25 extern apr_pool_t *tcn_global_pool; | |
26 | |
27 static apr_status_t generic_pool_cleanup(void *data) | |
28 { | |
29 apr_status_t rv = APR_SUCCESS; | |
30 tcn_callback_t *cb = (tcn_callback_t *)data; | |
31 | |
32 if (data) { | |
33 JNIEnv *env; | |
34 tcn_get_java_env(&env); | |
35 if (!TCN_IS_NULL(env, cb->obj)) { | |
36 rv = (*(env))->CallIntMethod(env, cb->obj, cb->mid[0], | |
37 NULL); | |
38 TCN_UNLOAD_CLASS(env, cb->obj); | |
39 } | |
40 free(cb); | |
41 } | |
42 return rv; | |
43 } | |
44 | |
45 TCN_IMPLEMENT_CALL(jlong, Pool, create)(TCN_STDARGS, jlong parent) | |
46 { | |
47 apr_pool_t *p = J2P(parent, apr_pool_t *); | |
48 apr_pool_t *n; | |
49 | |
50 UNREFERENCED(o); | |
51 /* Make sure our global pool is accessor for all pools */ | |
52 if (p == NULL) | |
53 p = tcn_global_pool; | |
54 TCN_THROW_IF_ERR(apr_pool_create(&n, p), n); | |
55 cleanup: | |
56 return P2J(n); | |
57 } | |
58 | |
59 TCN_IMPLEMENT_CALL(void, Pool, clear)(TCN_STDARGS, jlong pool) | |
60 { | |
61 apr_pool_t *p = J2P(pool, apr_pool_t *); | |
62 UNREFERENCED_STDARGS; | |
63 TCN_ASSERT(pool != 0); | |
64 apr_pool_clear(p); | |
65 } | |
66 | |
67 TCN_IMPLEMENT_CALL(void, Pool, destroy)(TCN_STDARGS, jlong pool) | |
68 { | |
69 apr_pool_t *p = J2P(pool, apr_pool_t *); | |
70 UNREFERENCED_STDARGS; | |
71 TCN_ASSERT(pool != 0); | |
72 apr_pool_destroy(p); | |
73 } | |
74 | |
75 TCN_IMPLEMENT_CALL(jlong, Pool, parentGet)(TCN_STDARGS, jlong pool) | |
76 { | |
77 apr_pool_t *p = J2P(pool, apr_pool_t *); | |
78 UNREFERENCED_STDARGS; | |
79 TCN_ASSERT(pool != 0); | |
80 return P2J(apr_pool_parent_get(p)); | |
81 } | |
82 | |
83 TCN_IMPLEMENT_CALL(jboolean, Pool, isAncestor)(TCN_STDARGS, jlong a, jlong b) | |
84 { | |
85 apr_pool_t *pa = J2P(a, apr_pool_t *); | |
86 apr_pool_t *pb = J2P(b, apr_pool_t *); | |
87 UNREFERENCED_STDARGS; | |
88 return apr_pool_is_ancestor(pa, pb) ? JNI_TRUE : JNI_FALSE; | |
89 } | |
90 | |
91 TCN_IMPLEMENT_CALL(jlong, Pool, palloc)(TCN_STDARGS, jlong pool, jint size) | |
92 { | |
93 apr_pool_t *p = J2P(pool, apr_pool_t *); | |
94 UNREFERENCED_STDARGS; | |
95 return P2J(apr_palloc(p, (apr_size_t)size)); | |
96 } | |
97 | |
98 TCN_IMPLEMENT_CALL(jlong, Pool, pcalloc)(TCN_STDARGS, jlong pool, jint size) | |
99 { | |
100 apr_pool_t *p = J2P(pool, apr_pool_t *); | |
101 UNREFERENCED_STDARGS; | |
102 return P2J(apr_pcalloc(p, (apr_size_t)size)); | |
103 } | |
104 | |
105 TCN_IMPLEMENT_CALL(jlong, Pool, cleanupRegister)(TCN_STDARGS, jlong pool, | |
106 jobject obj) | |
107 { | |
108 apr_pool_t *p = J2P(pool, apr_pool_t *); | |
109 tcn_callback_t *cb = (tcn_callback_t *)malloc(sizeof(tcn_callback_t)); | |
110 jclass cls; | |
111 | |
112 UNREFERENCED(o); | |
113 | |
114 if (cb == NULL) { | |
115 TCN_THROW_OS_ERROR(e); | |
116 return 0; | |
117 } | |
118 cls = (*e)->GetObjectClass(e, obj); | |
119 cb->obj = (*e)->NewGlobalRef(e, obj); | |
120 cb->mid[0] = (*e)->GetMethodID(e, cls, "callback", "()I"); | |
121 | |
122 apr_pool_cleanup_register(p, (const void *)cb, | |
123 generic_pool_cleanup, | |
124 apr_pool_cleanup_null); | |
125 | |
126 return P2J(cb); | |
127 } | |
128 | |
129 TCN_IMPLEMENT_CALL(void, Pool, cleanupKill)(TCN_STDARGS, jlong pool, | |
130 jlong data) | |
131 { | |
132 apr_pool_t *p = J2P(pool, apr_pool_t *); | |
133 tcn_callback_t *cb = J2P(data, tcn_callback_t *); | |
134 | |
135 UNREFERENCED(o); | |
136 TCN_ASSERT(pool != 0); | |
137 apr_pool_cleanup_kill(p, cb, generic_pool_cleanup); | |
138 (*e)->DeleteGlobalRef(e, cb->obj); | |
139 free(cb); | |
140 } | |
141 | |
142 TCN_IMPLEMENT_CALL(jobject, Pool, alloc)(TCN_STDARGS, jlong pool, | |
143 jint size) | |
144 { | |
145 apr_pool_t *p = J2P(pool, apr_pool_t *); | |
146 apr_size_t sz = (apr_size_t)size; | |
147 void *mem; | |
148 | |
149 UNREFERENCED(o); | |
150 TCN_ASSERT(pool != 0); | |
151 | |
152 if ((mem = apr_palloc(p, sz)) != NULL) | |
153 return (*e)->NewDirectByteBuffer(e, mem, (jlong)sz); | |
154 else | |
155 return NULL; | |
156 } | |
157 | |
158 TCN_IMPLEMENT_CALL(jobject, Pool, calloc)(TCN_STDARGS, jlong pool, | |
159 jint size) | |
160 { | |
161 apr_pool_t *p = J2P(pool, apr_pool_t *); | |
162 apr_size_t sz = (apr_size_t)size; | |
163 void *mem; | |
164 | |
165 UNREFERENCED(o); | |
166 TCN_ASSERT(pool != 0); | |
167 | |
168 if ((mem = apr_pcalloc(p, sz)) != NULL) | |
169 return (*e)->NewDirectByteBuffer(e, mem, (jlong)sz); | |
170 else | |
171 return NULL; | |
172 } | |
173 | |
174 static apr_status_t generic_pool_data_cleanup(void *data) | |
175 { | |
176 apr_status_t rv = APR_SUCCESS; | |
177 tcn_callback_t *cb = (tcn_callback_t *)data; | |
178 | |
179 if (data) { | |
180 JNIEnv *env; | |
181 tcn_get_java_env(&env); | |
182 | |
183 if (!TCN_IS_NULL(env, cb->obj)) { | |
184 TCN_UNLOAD_CLASS(env, cb->obj); | |
185 } | |
186 free(cb); | |
187 } | |
188 return rv; | |
189 } | |
190 | |
191 TCN_IMPLEMENT_CALL(jint, Pool, dataSet)(TCN_STDARGS, jlong pool, | |
192 jstring key, jobject data) | |
193 { | |
194 apr_pool_t *p = J2P(pool, apr_pool_t *); | |
195 apr_status_t rv = APR_SUCCESS; | |
196 void *old = NULL; | |
197 TCN_ALLOC_CSTRING(key); | |
198 | |
199 UNREFERENCED(o); | |
200 TCN_ASSERT(pool != 0); | |
201 | |
202 if (apr_pool_userdata_get(&old, J2S(key), p) == APR_SUCCESS) { | |
203 if (old) | |
204 apr_pool_cleanup_run(p, old, generic_pool_data_cleanup); | |
205 } | |
206 if (data) { | |
207 JNIEnv *e; | |
208 tcn_callback_t *cb = (tcn_callback_t *)malloc(sizeof(tcn_callback_t)); | |
209 tcn_get_java_env(&e); | |
210 cb->obj = (*e)->NewGlobalRef(e, data); | |
211 if ((rv = apr_pool_userdata_set(cb, J2S(key), generic_pool_data_cleanup, | |
212 p)) != APR_SUCCESS) { | |
213 (*e)->DeleteGlobalRef(e, cb->obj); | |
214 free(cb); | |
215 } | |
216 } | |
217 else { | |
218 /* Clear the exiting user data */ | |
219 rv = apr_pool_userdata_set(NULL, J2S(key), NULL, p); | |
220 } | |
221 TCN_FREE_CSTRING(key); | |
222 return rv; | |
223 } | |
224 | |
225 TCN_IMPLEMENT_CALL(jobject, Pool, dataGet)(TCN_STDARGS, jlong pool, | |
226 jstring key) | |
227 { | |
228 apr_pool_t *p = J2P(pool, apr_pool_t *); | |
229 void *old = NULL; | |
230 TCN_ALLOC_CSTRING(key); | |
231 jobject rv = NULL; | |
232 | |
233 UNREFERENCED(o); | |
234 TCN_ASSERT(pool != 0); | |
235 | |
236 if (apr_pool_userdata_get(&old, J2S(key), p) == APR_SUCCESS) { | |
237 if (old) { | |
238 tcn_callback_t *cb = (tcn_callback_t *)old; | |
239 rv = cb->obj; | |
240 } | |
241 } | |
242 TCN_FREE_CSTRING(key); | |
243 return rv; | |
244 } | |
245 | |
246 TCN_IMPLEMENT_CALL(void, Pool, cleanupForExec)(TCN_STDARGS) | |
247 { | |
248 UNREFERENCED_STDARGS; | |
249 apr_pool_cleanup_for_exec(); | |
250 } | |
OLD | NEW |