Skip to content

Commit 26df1e7

Browse files
committed
Move NativeAllocationTracer class to tools package
1 parent 322bbfb commit 26df1e7

2 files changed

Lines changed: 26 additions & 24 deletions

File tree

src/main/java/org/bytedeco/javacpp/Pointer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.bytedeco.javacpp.annotation.Raw;
3737
import org.bytedeco.javacpp.tools.Generator;
3838
import org.bytedeco.javacpp.tools.Logger;
39+
import org.bytedeco.javacpp.tools.NativeAllocationTracer;
3940

4041
/**
4142
* All peer classes to native types must be descended from Pointer, the topmost class.

src/main/java/org/bytedeco/javacpp/NativeAllocationTracer.java renamed to src/main/java/org/bytedeco/javacpp/tools/NativeAllocationTracer.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
* limitations under the License.
2121
*/
2222

23-
package org.bytedeco.javacpp;
23+
package org.bytedeco.javacpp.tools;
2424

25-
import org.bytedeco.javacpp.tools.Logger;
25+
import org.bytedeco.javacpp.Pointer;
2626

27+
import java.lang.ref.PhantomReference;
2728
import java.util.Collection;
2829
import java.util.HashMap;
2930
import java.util.Objects;
@@ -226,14 +227,14 @@ public String toString() {
226227
}
227228
}
228229

229-
private static final Logger logger = Logger.create(SizeTPointer.class);
230+
private static final Logger logger = Logger.create(NativeAllocationTracer.class);
230231

231232
/** Maps source code locations to their allocation statistics */
232233
private static final HashMap<Location, Site> sites = new HashMap<>();
233234
/** Maps for remember where each Pointer object was created */
234235
private static final WeakHashMap<Pointer, Location> pointerLocations = new WeakHashMap<>();
235-
/** Maps deallocators to allocation sites for GC-time tracking */
236-
private static final WeakHashMap<Pointer.NativeDeallocator, Location> nativeDeallocatorLocations = new WeakHashMap<>();
236+
/** Maps phantom references to allocation sites for GC-time tracking */
237+
private static final WeakHashMap<PhantomReference<Pointer>, Location> pointerReferenceLocations = new WeakHashMap<>();
237238

238239
/**
239240
* Retrieves a collection of all currently tracked allocation sites.
@@ -249,7 +250,7 @@ public static Collection<Site> getSites() {
249250
*
250251
* @param pointer the Pointer object to be marked and tracked
251252
*/
252-
static void markPointer(Pointer pointer) {
253+
public static void markPointer(Pointer pointer) {
253254
Location location = captureCreationLocation(pointer.getClass());
254255

255256
if (location == null) {
@@ -279,18 +280,18 @@ static void markPointer(Pointer pointer) {
279280
* First attempts to retrieve the location from the associated Pointer, then falls back
280281
* to capturing the current stack frame location if not found.
281282
*
282-
* @param nativeDeallocator the native deallocator to be marked
283-
* @param pointer the Pointer object associated with the deallocator
283+
* @param pointerReference the phantom reference to be marked and tracked
284+
* @param pointer the Pointer object associated with the phantom reference
284285
*/
285-
static void markDeallocator(Pointer.NativeDeallocator nativeDeallocator, Pointer pointer) {
286+
public static void markDeallocator(PhantomReference<Pointer> pointerReference, Pointer pointer) {
286287
Location location = pointerLocations.get(pointer);
287288

288289
if (location == null) {
289290
location = captureCreationLocation(pointer.getClass());
290291

291292
if (location == null) {
292293
if (logger.isWarnEnabled()) {
293-
logger.warn("Could not get creation location for " + nativeDeallocator);
294+
logger.warn("Could not get creation location for " + pointerReference);
294295
}
295296
}
296297
}
@@ -302,26 +303,26 @@ static void markDeallocator(Pointer.NativeDeallocator nativeDeallocator, Pointer
302303
}
303304

304305
if (logger.isDebugEnabled()) {
305-
logger.debug("Mark location for " + nativeDeallocator + ": " + location);
306+
logger.debug("Mark location for " + pointerReference + ": " + location);
306307
}
307308

308-
nativeDeallocatorLocations.put(nativeDeallocator, location);
309+
pointerReferenceLocations.put(pointerReference, location);
309310
}
310311
}
311312

312313
/**
313314
* Records when native memory is allocated, updating total and live statistics.
314315
*
315-
* @param nativeDeallocator the deallocator associated with the allocation
316+
* @param pointerReference the phantom reference associated with the allocation
316317
* @param size the number of bytes allocated
317318
*/
318-
static void recordAllocation(Pointer.NativeDeallocator nativeDeallocator, long size) {
319-
Location location = nativeDeallocatorLocations.get(nativeDeallocator);
319+
public static void recordAllocation(PhantomReference<Pointer> pointerReference, long size) {
320+
Location location = pointerReferenceLocations.get(pointerReference);
320321
Site site = sites.get(location);
321322

322323
if (site == null) {
323324
if (logger.isWarnEnabled()) {
324-
logger.warn("Could not find allocation site for " + nativeDeallocator + ": " + location);
325+
logger.warn("Could not find allocation site for " + pointerReference + ": " + location);
325326
}
326327
return;
327328
}
@@ -335,16 +336,16 @@ static void recordAllocation(Pointer.NativeDeallocator nativeDeallocator, long s
335336
/**
336337
* Records when native memory is manually deallocated, updating live statistics.
337338
*
338-
* @param nativeDeallocator the deallocator associated with the deallocation
339+
* @param pointerReference the phantom reference associated with the deallocation
339340
* @param size the number of bytes deallocated
340341
*/
341-
static void recordDeallocation(Pointer.NativeDeallocator nativeDeallocator, long size) {
342-
Location location = nativeDeallocatorLocations.get(nativeDeallocator);
342+
public static void recordDeallocation(PhantomReference<Pointer> pointerReference, long size) {
343+
Location location = pointerReferenceLocations.get(pointerReference);
343344
Site site = sites.get(location);
344345

345346
if (site == null) {
346347
if (logger.isWarnEnabled()) {
347-
logger.warn("Could not find allocation site for " + nativeDeallocator + ": " + location);
348+
logger.warn("Could not find allocation site for " + pointerReference + ": " + location);
348349
}
349350
return;
350351
}
@@ -356,16 +357,16 @@ static void recordDeallocation(Pointer.NativeDeallocator nativeDeallocator, long
356357
/**
357358
* Records when native memory is garbage collected, updating collection statistics.
358359
*
359-
* @param nativeDeallocator the deallocator associated with the collected memory
360+
* @param pointerReference the phantom reference associated with the garbage collected memory
360361
* @param size the number of bytes garbage collected
361362
*/
362-
static void recordCollection(Pointer.NativeDeallocator nativeDeallocator, long size) {
363-
Location location = nativeDeallocatorLocations.get(nativeDeallocator);
363+
public static void recordCollection(PhantomReference<Pointer> pointerReference, long size) {
364+
Location location = pointerReferenceLocations.get(pointerReference);
364365
Site site = sites.get(location);
365366

366367
if (site == null) {
367368
if (logger.isWarnEnabled()) {
368-
logger.warn("Could not find allocation site for " + nativeDeallocator + ": " + location);
369+
logger.warn("Could not find allocation site for " + pointerReference + ": " + location);
369370
}
370371
return;
371372
}

0 commit comments

Comments
 (0)