1
2
3
4 package joeq.Allocator;
5
6 import joeq.Allocator.CodeAllocator.x86CodeBuffer;
7 import joeq.Class.PrimordialClassLoader;
8 import joeq.Class.jq_Class;
9 import joeq.Class.jq_StaticField;
10 import joeq.Memory.Address;
11 import joeq.Memory.CodeAddress;
12 import joeq.Runtime.Unsafe;
13
14 /***
15 * Provides access functions to the default code allocator.
16 * If the default_allocator is set, it is used as the default global code allocator.
17 * Otherwise, the code allocator of the current thread is used.
18 *
19 * @author John Whaley <jwhaley@alum.mit.edu>
20 * @version $Id: DefaultCodeAllocator.java 2125 2005-01-21 07:13:15Z joewhaley $
21 */
22 public abstract class DefaultCodeAllocator {
23
24 /***
25 * The default global code allocator. If this is set, all threads use this
26 * allocator instead of their thread-local allocators.
27 */
28 public static CodeAllocator default_allocator;
29
30 /***
31 * Gets the default code allocator for the current thread.
32 *
33 * @return default code allocator for the current thread
34 */
35 public static final CodeAllocator def() {
36 if (default_allocator != null) return default_allocator;
37 return Unsafe.getThreadBlock().getNativeThread().getCodeAllocator();
38 }
39
40 /***
41 * Initialize the default code allocator.
42 */
43 public static final void init() {
44 def().init();
45 }
46
47 /***
48 * Get a code buffer from the default code allocator.
49 *
50 * @param estimatedSize
51 * @param offset
52 * @param alignment
53 * @return a new code buffer
54 */
55 public static final x86CodeBuffer getCodeBuffer(int estimatedSize, int offset, int alignment) {
56 x86CodeBuffer o = def().getCodeBuffer(estimatedSize, offset, alignment);
57 return o;
58 }
59
60 /***
61 * Patch the code address to point to the given heap address in the default
62 * code allocator.
63 *
64 * @param code
65 * @param heap
66 */
67 public static final void patchAbsolute(Address code, Address heap) {
68 def().patchAbsolute(code, heap);
69 }
70
71 /***
72 * Patch the code address to be a relative offset to another code address.
73 *
74 * @param code
75 * @param target
76 */
77 public static final void patchRelativeOffset(CodeAddress code, CodeAddress target) {
78 def().patchRelativeOffset(code, target);
79 }
80
81 public static final jq_StaticField _default_allocator;
82 static {
83 jq_Class k = (jq_Class)PrimordialClassLoader.loader.getOrCreateBSType("Ljoeq/Allocator/DefaultCodeAllocator;");
84 _default_allocator = k.getOrCreateStaticField("default_allocator", "Ljoeq/Allocator/CodeAllocator;");
85 }
86 }