OLD | NEW |
| 1 /* |
| 2 * Copyright 2016 The Netty Project |
| 3 * |
| 4 * The Netty Project licenses this file to you under the Apache License, |
| 5 * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 * with 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, WITHOUT |
| 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 * License for the specific language governing permissions and limitations |
| 14 * under the License. |
| 15 */ |
1 /* Licensed to the Apache Software Foundation (ASF) under one or more | 16 /* Licensed to the Apache Software Foundation (ASF) under one or more |
2 * contributor license agreements. See the NOTICE file distributed with | 17 * contributor license agreements. See the NOTICE file distributed with |
3 * this work for additional information regarding copyright ownership. | 18 * this work for additional information regarding copyright ownership. |
4 * The ASF licenses this file to You under the Apache License, Version 2.0 | 19 * 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 | 20 * (the "License"); you may not use this file except in compliance with |
6 * the License. You may obtain a copy of the License at | 21 * the License. You may obtain a copy of the License at |
7 * | 22 * |
8 * http://www.apache.org/licenses/LICENSE-2.0 | 23 * http://www.apache.org/licenses/LICENSE-2.0 |
9 * | 24 * |
10 * Unless required by applicable law or agreed to in writing, software | 25 * Unless required by applicable law or agreed to in writing, software |
11 * distributed under the License is distributed on an "AS IS" BASIS, | 26 * distributed under the License is distributed on an "AS IS" BASIS, |
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 27 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 * See the License for the specific language governing permissions and | 28 * See the License for the specific language governing permissions and |
14 * limitations under the License. | 29 * limitations under the License. |
15 */ | 30 */ |
16 | 31 |
17 /* | |
18 * | |
19 * @author Mladen Turk | |
20 * @version $Id: bb.c 1442587 2013-02-05 13:49:48Z rjung $ | |
21 */ | |
22 | |
23 #include "tcn.h" | 32 #include "tcn.h" |
24 | 33 |
25 /** | |
26 * DirectByteBuffer utilities | |
27 */ | |
28 | |
29 TCN_IMPLEMENT_CALL(jobject, Buffer, malloc)(TCN_STDARGS, jint size) | |
30 { | |
31 void *mem; | |
32 size_t sz = (size_t)APR_ALIGN_DEFAULT(size); | |
33 | |
34 UNREFERENCED(o); | |
35 | |
36 if ((mem = malloc(sz)) != NULL) { | |
37 jobject rv = (*e)->NewDirectByteBuffer(e, mem, (jlong)sz); | |
38 if (rv == NULL) | |
39 free(mem); | |
40 return rv; | |
41 } | |
42 else { | |
43 return NULL; | |
44 } | |
45 } | |
46 | |
47 TCN_IMPLEMENT_CALL(jobject, Buffer, calloc)(TCN_STDARGS, jint num, jint size) | |
48 { | |
49 size_t sz = (size_t)APR_ALIGN_DEFAULT((size * num)); | |
50 void *mem; | |
51 | |
52 UNREFERENCED(o); | |
53 | |
54 if ((mem = calloc(1, sz)) != NULL) { | |
55 jobject rv = (*e)->NewDirectByteBuffer(e, mem, (jlong)sz); | |
56 if (rv == NULL) | |
57 free(mem); | |
58 return rv; | |
59 } | |
60 else { | |
61 return NULL; | |
62 } | |
63 } | |
64 | |
65 TCN_IMPLEMENT_CALL(jobject, Buffer, palloc)(TCN_STDARGS, jlong pool, | |
66 jint size) | |
67 { | |
68 apr_pool_t *p = J2P(pool, apr_pool_t *); | |
69 apr_size_t sz = (apr_size_t)APR_ALIGN_DEFAULT(size); | |
70 void *mem; | |
71 | |
72 UNREFERENCED(o); | |
73 TCN_ASSERT(pool != 0); | |
74 | |
75 if ((mem = apr_palloc(p, sz)) != NULL) | |
76 return (*e)->NewDirectByteBuffer(e, mem, (jlong)sz); | |
77 else | |
78 return NULL; | |
79 } | |
80 | |
81 TCN_IMPLEMENT_CALL(jobject, Buffer, pcalloc)(TCN_STDARGS, jlong pool, | |
82 jint size) | |
83 { | |
84 apr_pool_t *p = J2P(pool, apr_pool_t *); | |
85 apr_size_t sz = (apr_size_t)APR_ALIGN_DEFAULT(size); | |
86 void *mem; | |
87 | |
88 UNREFERENCED(o); | |
89 TCN_ASSERT(pool != 0); | |
90 | |
91 if ((mem = apr_pcalloc(p, sz)) != NULL) | |
92 return (*e)->NewDirectByteBuffer(e, mem, (jlong)sz); | |
93 else | |
94 return NULL; | |
95 } | |
96 | |
97 TCN_IMPLEMENT_CALL(jobject, Buffer, create)(TCN_STDARGS, jlong addr, | |
98 jint size) | |
99 { | |
100 void *mem = J2P(addr, void *); | |
101 | |
102 UNREFERENCED(o); | |
103 TCN_ASSERT(mem != 0); | |
104 TCN_ASSERT(size != 0); | |
105 | |
106 if (mem && size) | |
107 return (*e)->NewDirectByteBuffer(e, mem, (jlong)size); | |
108 else | |
109 return NULL; | |
110 } | |
111 | |
112 TCN_IMPLEMENT_CALL(void, Buffer, free)(TCN_STDARGS, jobject bb) | |
113 { | |
114 void *mem; | |
115 | |
116 UNREFERENCED(o); | |
117 if ((mem = (*e)->GetDirectBufferAddress(e, bb)) != NULL) { | |
118 /* This can cause core dump if address was | |
119 * allocated from the APR pool. | |
120 */ | |
121 free(mem); | |
122 } | |
123 } | |
124 | |
125 TCN_IMPLEMENT_CALL(jlong, Buffer, address)(TCN_STDARGS, jobject bb) | 34 TCN_IMPLEMENT_CALL(jlong, Buffer, address)(TCN_STDARGS, jobject bb) |
126 { | 35 { |
127 UNREFERENCED(o); | 36 UNREFERENCED(o); |
128 return P2J((*e)->GetDirectBufferAddress(e, bb)); | 37 return P2J((*e)->GetDirectBufferAddress(e, bb)); |
129 } | 38 } |
130 | 39 |
131 TCN_IMPLEMENT_CALL(jlong, Buffer, size)(TCN_STDARGS, jobject bb) | 40 TCN_IMPLEMENT_CALL(jlong, Buffer, size)(TCN_STDARGS, jobject bb) |
132 { | 41 { |
133 UNREFERENCED(o); | 42 UNREFERENCED(o); |
134 return (*e)->GetDirectBufferCapacity(e, bb); | 43 return (*e)->GetDirectBufferCapacity(e, bb); |
135 } | 44 } |
OLD | NEW |