1 | /* |
2 | * $Id: MallocSpy.java,v 1.2 2005/11/16 21:42:18 hastings Exp $ |
3 | * |
4 | * (c) Copyright, Moebius Solutions, Inc., 2004 |
5 | * |
6 | * All Rights Reserved |
7 | * |
8 | * This material may be reproduced by or for the U. S. Government |
9 | * pursuant to the copyright license under the clause at |
10 | * DFARS 252.227-7014 (OCT 2001). |
11 | */ |
12 | |
13 | package com.moesol.bindings.platform_sdk.component_services; |
14 | |
15 | import com.moesol.bindings.NativeLibraryLoader; |
16 | |
17 | public class MallocSpy { |
18 | static { |
19 | NativeLibraryLoader.loadLibrary("com_moesol_bindings"); |
20 | } |
21 | |
22 | private MallocSpy() { } |
23 | public static void register() { |
24 | if (s_instance != null) { return; } |
25 | s_instance = new MallocSpy(); |
26 | JNI_register(s_instance); |
27 | } |
28 | public static MallocSpy instance() { |
29 | return s_instance; |
30 | } |
31 | public static void unregister() { |
32 | s_instance = null; |
33 | JNI_unregister(); |
34 | } |
35 | public static int getAllocatedBytes() { |
36 | return s_allocated_bytes; |
37 | } |
38 | /** Called from JNI */ |
39 | void LogAlloc(int id, int size) { |
40 | System.out.println("alloc: " + id + "," + size); |
41 | s_allocated_bytes += size; |
42 | } |
43 | /** Called from JNI */ |
44 | void LogFree(int id, int size) { |
45 | System.out.println("free: " + id + "," + size); |
46 | s_allocated_bytes -= size; |
47 | } |
48 | |
49 | /** Exposed for unit test */ |
50 | /* package */ static int CoTaskMemAlloc(int cb) { |
51 | return JNI_CoTaskMemAlloc(cb); |
52 | } |
53 | /** Exposed for unit test */ |
54 | /* package */ static void CoTaskMemFree(int ptr) { |
55 | JNI_CoTaskMemFree(ptr); |
56 | } |
57 | /** Exposed for unit test */ |
58 | /* package */ static int CoTaskMemRealloc(int ptr, int cb) { |
59 | return JNI_CoTaskMemRealloc(ptr, cb); |
60 | } |
61 | /** Exposed for unit test */ |
62 | /* package */ static int CoTaskMemGetSize(int ptr) { |
63 | return JNI_CoTaskMemGetSize(ptr); |
64 | } |
65 | /** Exposed for unit test */ |
66 | /* package */ static boolean CoTaskMemDidAlloc(int ptr) { |
67 | return JNI_CoTaskMemDidAlloc(ptr); |
68 | } |
69 | |
70 | private static native void JNI_register(MallocSpy instance); |
71 | private static native void JNI_unregister(); |
72 | private static native int JNI_CoTaskMemAlloc(int cb); |
73 | private static native void JNI_CoTaskMemFree(int ptr); |
74 | private static native int JNI_CoTaskMemRealloc(int ptr, int cb); |
75 | private static native int JNI_CoTaskMemGetSize(int ptr); |
76 | private static native boolean JNI_CoTaskMemDidAlloc(int ptr); |
77 | |
78 | // |
79 | // State |
80 | // |
81 | |
82 | private static int s_allocated_bytes; |
83 | private static MallocSpy s_instance = null; |
84 | } |