blob: 23d6b9f06bb1f2bb0f6dc39dec5cef160d0ac80b [file] [log] [blame]
buzbee862a7602013-04-05 10:58:54 -07001/*
2 * Copyright (C) 2013 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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_COMPILER_DEX_ARENA_ALLOCATOR_H_
18#define ART_COMPILER_DEX_ARENA_ALLOCATOR_H_
buzbee862a7602013-04-05 10:58:54 -070019
20#include <stdint.h>
21#include <stddef.h>
22#include "compiler_enums.h"
23
24namespace art {
25
26#define ARENA_DEFAULT_BLOCK_SIZE (256 * 1024)
buzbeea5abf702013-04-12 14:39:29 -070027#define ARENA_HIGH_WATER (16 * 1024)
buzbee862a7602013-04-05 10:58:54 -070028
29class ArenaAllocator {
30 public:
31
32 // Type of allocation for memory tuning.
33 enum ArenaAllocKind {
34 kAllocMisc,
35 kAllocBB,
36 kAllocLIR,
37 kAllocMIR,
38 kAllocDFInfo,
39 kAllocGrowableArray,
40 kAllocGrowableBitMap,
41 kAllocDalvikToSSAMap,
42 kAllocDebugInfo,
43 kAllocSuccessor,
44 kAllocRegAlloc,
45 kAllocData,
46 kAllocPredecessors,
47 kNumAllocKinds
48 };
49
50 ArenaAllocator(size_t default_size = ARENA_DEFAULT_BLOCK_SIZE);
Ian Rogerse7a5b7d2013-04-18 20:09:02 -070051 ~ArenaAllocator();
buzbee862a7602013-04-05 10:58:54 -070052 void* NewMem(size_t size, bool zero, ArenaAllocKind kind);
buzbee862a7602013-04-05 10:58:54 -070053 size_t BytesAllocated() {
54 return malloc_bytes_;
55 }
56
57 void DumpMemStats(std::ostream& os) const;
58
59 private:
60
61 // Variable-length allocation block.
62 struct ArenaMemBlock {
63 size_t block_size;
64 size_t bytes_allocated;
65 ArenaMemBlock *next;
66 char ptr[0];
67 };
68
buzbeea5abf702013-04-12 14:39:29 -070069 ArenaMemBlock* EmptyArenaBlock();
buzbee862a7602013-04-05 10:58:54 -070070
71 size_t default_size_; // Smallest size of new allocation block.
72 size_t block_size_; // Amount of allocatable bytes on a default block.
73 ArenaMemBlock* arena_head_; // Head of linked list of allocation blocks.
buzbeea5abf702013-04-12 14:39:29 -070074 ArenaMemBlock* current_block_; // NOTE: code assumes there's always at least 1 block.
buzbee862a7602013-04-05 10:58:54 -070075 int num_arena_blocks_;
buzbeea5abf702013-04-12 14:39:29 -070076 uint32_t malloc_bytes_; // Number of actual bytes malloc'd
77 uint32_t alloc_stats_[kNumAllocKinds]; // Bytes used by various allocation kinds.
78 uint32_t lost_bytes_; // Lost memory at end of too-small region
79 uint32_t num_allocations_;
buzbee862a7602013-04-05 10:58:54 -070080
81}; // ArenaAllocator
82
83
84struct MemStats {
85 public:
86 void Dump(std::ostream& os) const {
87 arena_.DumpMemStats(os);
88 }
89 MemStats(const ArenaAllocator &arena) : arena_(arena){};
90 private:
91 const ArenaAllocator &arena_;
92}; // MemStats
93
94} // namespace art
95
Brian Carlstromfc0e3212013-07-17 14:40:12 -070096#endif // ART_COMPILER_DEX_ARENA_ALLOCATOR_H_