blob: bd4c85bd7c2df7e73915aecd375ba75bceed9bc5 [file] [log] [blame]
Andreas Gampe5e6046b2016-10-25 12:05:53 -07001/* Copyright (C) 2016 The Android Open Source Project
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This file implements interfaces from the file jvmti.h. This implementation
5 * is licensed under the same terms as the file jvmti.h. The
6 * copyright and license information for the file jvmti.h follows.
7 *
8 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10 *
11 * This code is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 only, as
13 * published by the Free Software Foundation. Oracle designates this
14 * particular file as subject to the "Classpath" exception as provided
15 * by Oracle in the LICENSE file that accompanied this code.
16 *
17 * This code is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * version 2 for more details (a copy is included in the LICENSE file that
21 * accompanied this code).
22 *
23 * You should have received a copy of the GNU General Public License version
24 * 2 along with this work; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 *
27 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28 * or visit www.oracle.com if you need additional information or have any
29 * questions.
30 */
31
Andreas Gampe06c42a52017-07-26 14:17:14 -070032#ifndef ART_OPENJDKJVMTI_JVMTI_ALLOCATOR_H_
33#define ART_OPENJDKJVMTI_JVMTI_ALLOCATOR_H_
Andreas Gampe5e6046b2016-10-25 12:05:53 -070034
Andreas Gampe57943812017-12-06 21:39:13 -080035#include <android-base/logging.h>
36#include <android-base/macros.h>
37
Andreas Gampe5e6046b2016-10-25 12:05:53 -070038#include "jvmti.h"
39
Alex Lightc19cd2f2017-07-06 14:12:13 -070040#include "ti_allocator.h"
41
Andreas Gampe5e6046b2016-10-25 12:05:53 -070042namespace openjdkjvmti {
43
44template <typename T> class JvmtiAllocator;
45
46template <>
47class JvmtiAllocator<void> {
48 public:
49 typedef void value_type;
50 typedef void* pointer;
51 typedef const void* const_pointer;
52
53 template <typename U>
54 struct rebind {
55 typedef JvmtiAllocator<U> other;
56 };
57
58 explicit JvmtiAllocator(jvmtiEnv* env) : env_(env) {}
Igor Murashkin2ffb7032017-11-08 13:35:21 -080059 JvmtiAllocator() : env_(nullptr) {}
Andreas Gampe5e6046b2016-10-25 12:05:53 -070060
61 template <typename U>
Igor Murashkin5573c372017-11-16 13:34:30 -080062 JvmtiAllocator(const JvmtiAllocator<U>& other)
Andreas Gampe5e6046b2016-10-25 12:05:53 -070063 : env_(other.env_) {}
64
65 JvmtiAllocator(const JvmtiAllocator& other) = default;
66 JvmtiAllocator& operator=(const JvmtiAllocator& other) = default;
67 ~JvmtiAllocator() = default;
68
69 private:
70 jvmtiEnv* env_;
71
72 template <typename U>
73 friend class JvmtiAllocator;
74
75 template <typename U>
76 friend bool operator==(const JvmtiAllocator<U>& lhs, const JvmtiAllocator<U>& rhs);
77};
78
79template <typename T>
80class JvmtiAllocator {
81 public:
82 typedef T value_type;
83 typedef T* pointer;
84 typedef T& reference;
85 typedef const T* const_pointer;
86 typedef const T& const_reference;
87 typedef size_t size_type;
88 typedef ptrdiff_t difference_type;
89
90 template <typename U>
91 struct rebind {
92 typedef JvmtiAllocator<U> other;
93 };
94
95 explicit JvmtiAllocator(jvmtiEnv* env) : env_(env) {}
Igor Murashkin2ffb7032017-11-08 13:35:21 -080096 JvmtiAllocator() : env_(nullptr) {}
Andreas Gampe5e6046b2016-10-25 12:05:53 -070097
98 template <typename U>
Igor Murashkin5573c372017-11-16 13:34:30 -080099 JvmtiAllocator(const JvmtiAllocator<U>& other)
Andreas Gampe5e6046b2016-10-25 12:05:53 -0700100 : env_(other.env_) {}
101
102 JvmtiAllocator(const JvmtiAllocator& other) = default;
103 JvmtiAllocator& operator=(const JvmtiAllocator& other) = default;
104 ~JvmtiAllocator() = default;
105
106 size_type max_size() const {
107 return static_cast<size_type>(-1) / sizeof(T);
108 }
109
110 pointer address(reference x) const { return &x; }
111 const_pointer address(const_reference x) const { return &x; }
112
113 pointer allocate(size_type n, JvmtiAllocator<void>::pointer hint ATTRIBUTE_UNUSED = nullptr) {
114 DCHECK_LE(n, max_size());
115 if (env_ == nullptr) {
Alex Lightc19cd2f2017-07-06 14:12:13 -0700116 T* result = reinterpret_cast<T*>(AllocUtil::AllocateImpl(n * sizeof(T)));
117 CHECK(result != nullptr || n == 0u); // Abort if AllocateImpl() fails.
Andreas Gampe5e6046b2016-10-25 12:05:53 -0700118 return result;
119 } else {
120 unsigned char* result;
121 jvmtiError alloc_error = env_->Allocate(n * sizeof(T), &result);
122 CHECK(alloc_error == JVMTI_ERROR_NONE);
123 return reinterpret_cast<T*>(result);
124 }
125 }
126 void deallocate(pointer p, size_type n ATTRIBUTE_UNUSED) {
127 if (env_ == nullptr) {
Alex Lightc19cd2f2017-07-06 14:12:13 -0700128 AllocUtil::DeallocateImpl(reinterpret_cast<unsigned char*>(p));
Andreas Gampe5e6046b2016-10-25 12:05:53 -0700129 } else {
130 jvmtiError dealloc_error = env_->Deallocate(reinterpret_cast<unsigned char*>(p));
131 CHECK(dealloc_error == JVMTI_ERROR_NONE);
132 }
133 }
134
135 void construct(pointer p, const_reference val) {
136 new (static_cast<void*>(p)) value_type(val);
137 }
138 template <class U, class... Args>
139 void construct(U* p, Args&&... args) {
140 ::new (static_cast<void*>(p)) U(std::forward<Args>(args)...);
141 }
142 void destroy(pointer p) {
143 p->~value_type();
144 }
145
146 inline bool operator==(JvmtiAllocator const& other) {
147 return env_ == other.env_;
148 }
149 inline bool operator!=(JvmtiAllocator const& other) {
150 return !operator==(other);
151 }
152
153 private:
154 jvmtiEnv* env_;
155
156 template <typename U>
157 friend class JvmtiAllocator;
158
159 template <typename U>
160 friend bool operator==(const JvmtiAllocator<U>& lhs, const JvmtiAllocator<U>& rhs);
161};
162
163template <typename T>
164inline bool operator==(const JvmtiAllocator<T>& lhs, const JvmtiAllocator<T>& rhs) {
165 return lhs.env_ == rhs.env_;
166}
167
168template <typename T>
169inline bool operator!=(const JvmtiAllocator<T>& lhs, const JvmtiAllocator<T>& rhs) {
170 return !(lhs == rhs);
171}
172
173} // namespace openjdkjvmti
174
Andreas Gampe06c42a52017-07-26 14:17:14 -0700175#endif // ART_OPENJDKJVMTI_JVMTI_ALLOCATOR_H_