Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_RUNTIME_LINEAR_ALLOC_H_ |
| 18 | #define ART_RUNTIME_LINEAR_ALLOC_H_ |
| 19 | |
| 20 | #include "base/arena_allocator.h" |
David Sehr | 3215fff | 2018-04-03 17:10:12 -0700 | [diff] [blame] | 21 | #include "base/mutex.h" |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | class ArenaPool; |
| 26 | |
| 27 | // TODO: Support freeing if we add poor man's class unloading. |
| 28 | class LinearAlloc { |
| 29 | public: |
| 30 | explicit LinearAlloc(ArenaPool* pool); |
| 31 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 32 | void* Alloc(Thread* self, size_t size) REQUIRES(!lock_); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 33 | void* AllocAlign16(Thread* self, size_t size) REQUIRES(!lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 34 | |
| 35 | // Realloc never frees the input pointer, it is the caller's job to do this if necessary. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 36 | void* Realloc(Thread* self, void* ptr, size_t old_size, size_t new_size) REQUIRES(!lock_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 37 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 38 | // Allocate an array of structs of type T. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 39 | template<class T> |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 40 | T* AllocArray(Thread* self, size_t elements) REQUIRES(!lock_) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 41 | return reinterpret_cast<T*>(Alloc(self, elements * sizeof(T))); |
| 42 | } |
| 43 | |
| 44 | // Return the number of bytes used in the allocator. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 45 | size_t GetUsedMemory() const REQUIRES(!lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 46 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 47 | ArenaPool* GetArenaPool() REQUIRES(!lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 48 | |
| 49 | // Return true if the linear alloc contrains an address. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 50 | bool Contains(void* ptr) const REQUIRES(!lock_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 51 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 52 | // Unsafe version of 'Contains' only to be used when the allocator is going |
| 53 | // to be deleted. |
| 54 | bool ContainsUnsafe(void* ptr) const NO_THREAD_SAFETY_ANALYSIS; |
| 55 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 56 | private: |
| 57 | mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 58 | ArenaAllocator allocator_ GUARDED_BY(lock_); |
Mathieu Chartier | 3130cdf | 2015-05-03 15:20:23 -0700 | [diff] [blame] | 59 | |
| 60 | DISALLOW_IMPLICIT_CONSTRUCTORS(LinearAlloc); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | } // namespace art |
| 64 | |
| 65 | #endif // ART_RUNTIME_LINEAR_ALLOC_H_ |