blob: 384b2e3243495024752530fac623b2a9a008ffd9 [file] [log] [blame]
Mathieu Chartierc7853442015-03-27 14:35:38 -07001/*
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"
21
22namespace art {
23
24class ArenaPool;
25
26// TODO: Support freeing if we add poor man's class unloading.
27class LinearAlloc {
28 public:
29 explicit LinearAlloc(ArenaPool* pool);
30
Mathieu Chartier90443472015-07-16 20:32:27 -070031 void* Alloc(Thread* self, size_t size) REQUIRES(!lock_);
Vladimir Markof44d36c2017-03-14 14:18:46 +000032 void* AllocAlign16(Thread* self, size_t size) REQUIRES(!lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -070033
34 // Realloc never frees the input pointer, it is the caller's job to do this if necessary.
Mathieu Chartier90443472015-07-16 20:32:27 -070035 void* Realloc(Thread* self, void* ptr, size_t old_size, size_t new_size) REQUIRES(!lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070036
Mathieu Chartier54d220e2015-07-30 16:20:06 -070037 // Allocate an array of structs of type T.
Mathieu Chartierc7853442015-03-27 14:35:38 -070038 template<class T>
Mathieu Chartier90443472015-07-16 20:32:27 -070039 T* AllocArray(Thread* self, size_t elements) REQUIRES(!lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070040 return reinterpret_cast<T*>(Alloc(self, elements * sizeof(T)));
41 }
42
43 // Return the number of bytes used in the allocator.
Mathieu Chartier90443472015-07-16 20:32:27 -070044 size_t GetUsedMemory() const REQUIRES(!lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -070045
Mathieu Chartier90443472015-07-16 20:32:27 -070046 ArenaPool* GetArenaPool() REQUIRES(!lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -070047
48 // Return true if the linear alloc contrains an address.
Mathieu Chartier90443472015-07-16 20:32:27 -070049 bool Contains(void* ptr) const REQUIRES(!lock_);
Mathieu Chartierc7853442015-03-27 14:35:38 -070050
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010051 // Unsafe version of 'Contains' only to be used when the allocator is going
52 // to be deleted.
53 bool ContainsUnsafe(void* ptr) const NO_THREAD_SAFETY_ANALYSIS;
54
Mathieu Chartierc7853442015-03-27 14:35:38 -070055 private:
56 mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
57 ArenaAllocator allocator_ GUARDED_BY(lock_);
Mathieu Chartier3130cdf2015-05-03 15:20:23 -070058
59 DISALLOW_IMPLICIT_CONSTRUCTORS(LinearAlloc);
Mathieu Chartierc7853442015-03-27 14:35:38 -070060};
61
62} // namespace art
63
64#endif // ART_RUNTIME_LINEAR_ALLOC_H_