1 /* |
1 /* |
2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
43 // oop parameters and return types should be Handles whenever feasible. |
43 // oop parameters and return types should be Handles whenever feasible. |
44 // |
44 // |
45 // Handles are declared in a straight-forward manner, e.g. |
45 // Handles are declared in a straight-forward manner, e.g. |
46 // |
46 // |
47 // oop obj = ...; |
47 // oop obj = ...; |
48 // Handle h1(obj); // allocate new handle |
48 // Handle h2(thread, obj); // allocate a new handle in thread |
49 // Handle h2(thread, obj); // faster allocation when current thread is known |
|
50 // Handle h3; // declare handle only, no allocation occurs |
49 // Handle h3; // declare handle only, no allocation occurs |
51 // ... |
50 // ... |
52 // h3 = h1; // make h3 refer to same indirection as h1 |
51 // h3 = h1; // make h3 refer to same indirection as h1 |
53 // oop obj2 = h2(); // get handle value |
52 // oop obj2 = h2(); // get handle value |
54 // h1->print(); // invoking operation on oop |
53 // h1->print(); // invoking operation on oop |
55 // |
54 // |
56 // Handles are specialized for different oop types to provide extra type |
55 // Handles are specialized for different oop types to provide extra type |
57 // information and avoid unnecessary casting. For each oop type xxxOop |
56 // information and avoid unnecessary casting. For each oop type xxxOop |
58 // there is a corresponding handle called xxxHandle, e.g. |
57 // there is a corresponding handle called xxxHandle. |
59 // |
|
60 // oop Handle |
|
61 // Method* methodHandle |
|
62 // instanceOop instanceHandle |
|
63 |
58 |
64 //------------------------------------------------------------------------------------------------------------------------ |
59 //------------------------------------------------------------------------------------------------------------------------ |
65 // Base class for all handles. Provides overloading of frequently |
60 // Base class for all handles. Provides overloading of frequently |
66 // used operators for ease of use. |
61 // used operators for ease of use. |
67 |
62 |
74 oop non_null_obj() const { assert(_handle != NULL, "resolving NULL handle"); return *_handle; } |
69 oop non_null_obj() const { assert(_handle != NULL, "resolving NULL handle"); return *_handle; } |
75 |
70 |
76 public: |
71 public: |
77 // Constructors |
72 // Constructors |
78 Handle() { _handle = NULL; } |
73 Handle() { _handle = NULL; } |
79 Handle(oop obj); |
|
80 Handle(Thread* thread, oop obj); |
74 Handle(Thread* thread, oop obj); |
81 |
75 |
82 // General access |
76 // General access |
83 oop operator () () const { return obj(); } |
77 oop operator () () const { return obj(); } |
84 oop operator -> () const { return non_null_obj(); } |
78 oop operator -> () const { return non_null_obj(); } |
111 type##Oop non_null_obj() const { return (type##Oop)Handle::non_null_obj(); } \ |
105 type##Oop non_null_obj() const { return (type##Oop)Handle::non_null_obj(); } \ |
112 \ |
106 \ |
113 public: \ |
107 public: \ |
114 /* Constructors */ \ |
108 /* Constructors */ \ |
115 type##Handle () : Handle() {} \ |
109 type##Handle () : Handle() {} \ |
116 type##Handle (type##Oop obj) : Handle((oop)obj) { \ |
|
117 assert(is_null() || ((oop)obj)->is_a(), \ |
|
118 "illegal type"); \ |
|
119 } \ |
|
120 type##Handle (Thread* thread, type##Oop obj) : Handle(thread, (oop)obj) { \ |
110 type##Handle (Thread* thread, type##Oop obj) : Handle(thread, (oop)obj) { \ |
121 assert(is_null() || ((oop)obj)->is_a(), "illegal type"); \ |
111 assert(is_null() || ((oop)obj)->is_a(), "illegal type"); \ |
122 } \ |
112 } \ |
123 \ |
113 \ |
124 /* Operators for ease of use */ \ |
114 /* Operators for ease of use */ \ |
275 // The following code will therefore NOT work: |
265 // The following code will therefore NOT work: |
276 // |
266 // |
277 // Handle h; |
267 // Handle h; |
278 // { |
268 // { |
279 // HandleMark hm; |
269 // HandleMark hm; |
280 // h = Handle(obj); |
270 // h = Handle(THREAD, obj); |
281 // } |
271 // } |
282 // h()->print(); // WRONG, h destroyed by HandleMark destructor. |
272 // h()->print(); // WRONG, h destroyed by HandleMark destructor. |
283 // |
273 // |
284 // If h has to be preserved, it can be converted to an oop or a local JNI handle |
274 // If h has to be preserved, it can be converted to an oop or a local JNI handle |
285 // across the HandleMark boundary. |
275 // across the HandleMark boundary. |