-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathNativeStruct.java
More file actions
122 lines (105 loc) · 3.51 KB
/
NativeStruct.java
File metadata and controls
122 lines (105 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* NativeStruct.java
*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
package com.wolfssl.wolfcrypt;
/**
* Wrapper for the native WolfCrypt structs.
*/
public abstract class NativeStruct extends WolfObject {
/** Logical mapping of NULL to 0 */
public static final long NULL = 0;
/**
* Create new NativeStruct object
*/
protected NativeStruct() {
/* Native struct allocated in initNativeStruct() upon subclass init */
}
/* points to the internal native structure */
private volatile long pointer = 0;
/** Lock around native pointer use */
protected final Object pointerLock = new Object();
/**
* Allocate and initialize native struct.
*/
protected void initNativeStruct() {
synchronized (pointerLock) {
if (pointer == 0) {
setNativeStruct(mallocNativeStruct());
}
}
}
/**
* Get pointer to wrapped native structure
*
* WARNING: the pointer returned from this function has not been locked
* and may cause threading synchronization issues if used in a
* multi-threaded use case or application.
*
* @return pointer to native structure
*/
public long getNativeStruct() {
synchronized (pointerLock) {
return this.pointer;
}
}
/**
* Set pointer to native structure
*
* If NativeStruct already holds pointer, old pointer will be free()'d
* before resetting to new pointer. Callers of this method should
* synchronize on 'pointerLock' before calling this method.
*
* @param nativeStruct pointer to initialized native structure
*/
private void setNativeStruct(long nativeStruct) {
if (this.pointer != NULL) {
xfree(this.pointer);
}
this.pointer = nativeStruct;
}
/**
* Releases the host data stored in a NativeStruct.
*
* This method provides a way to release host data without depending on the
* garbage collector to get around to releasing it. Derived objects whose
* native data structures have their own free functions, should be override
* this method to call that function.
*/
public void releaseNativeStruct() {
synchronized (pointerLock) {
setNativeStruct(NULL);
}
}
/**
* Malloc native structure pointer
*
* @return allocated pointer to native structure
*
* @throws OutOfMemoryError if native malloc fails with memory error
*/
protected abstract long mallocNativeStruct() throws OutOfMemoryError;
private native void xfree(long pointer);
@SuppressWarnings({"deprecation", "removal"})
@Override
protected void finalize() throws Throwable {
releaseNativeStruct();
super.finalize();
}
}