blob: c3222d0485d1fd1800069e97064be475add1b908 [file] [log] [blame]
Igor Murashkin6918bf12015-09-27 19:19:06 -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#ifndef ART_RUNTIME_LAMBDA_LEAKING_ALLOCATOR_H_
17#define ART_RUNTIME_LAMBDA_LEAKING_ALLOCATOR_H_
18
19#include <utility> // std::forward
20
21namespace art {
22class Thread; // forward declaration
23
24namespace lambda {
25
26// Temporary class to centralize all the leaking allocations.
27// Allocations made through this class are never freed, but it is a placeholder
28// that means that the calling code needs to be rewritten to properly:
29//
30// (a) Have a lifetime scoped to some other entity.
31// (b) Not be allocated over and over again if it was already allocated once (immutable data).
32//
33// TODO: do all of the above a/b for each callsite, and delete this class.
34class LeakingAllocator {
35 public:
36 // Allocate byte_size bytes worth of memory. Never freed.
37 static void* AllocateMemory(Thread* self, size_t byte_size);
38
39 // Make a new instance of T, flexibly sized, in-place at newly allocated memory. Never freed.
40 template <typename T, typename... Args>
41 static T* MakeFlexibleInstance(Thread* self, size_t byte_size, Args&&... args) {
42 return new (AllocateMemory(self, byte_size)) T(std::forward<Args>(args)...);
43 }
44
45 // Make a new instance of T in-place at newly allocated memory. Never freed.
46 template <typename T, typename... Args>
47 static T* MakeInstance(Thread* self, Args&&... args) {
48 return new (AllocateMemory(self, sizeof(T))) T(std::forward<Args>(args)...);
49 }
50};
51
52} // namespace lambda
53} // namespace art
54
55#endif // ART_RUNTIME_LAMBDA_LEAKING_ALLOCATOR_H_