blob: e8e2c027d0473472261a77338f1543f54954c76f [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:
buzbee862a7602013-04-05 10:58:54 -070031 // Type of allocation for memory tuning.
32 enum ArenaAllocKind {
33 kAllocMisc,
34 kAllocBB,
35 kAllocLIR,
36 kAllocMIR,
37 kAllocDFInfo,
38 kAllocGrowableArray,
39 kAllocGrowableBitMap,
40 kAllocDalvikToSSAMap,
41 kAllocDebugInfo,
42 kAllocSuccessor,
43 kAllocRegAlloc,
44 kAllocData,
45 kAllocPredecessors,
46 kNumAllocKinds
47 };
48
Brian Carlstrom93ba8932013-07-17 21:31:49 -070049 explicit ArenaAllocator(size_t default_size = ARENA_DEFAULT_BLOCK_SIZE);
Ian Rogerse7a5b7d2013-04-18 20:09:02 -070050 ~ArenaAllocator();
buzbee862a7602013-04-05 10:58:54 -070051 void* NewMem(size_t size, bool zero, ArenaAllocKind kind);
buzbee862a7602013-04-05 10:58:54 -070052 size_t BytesAllocated() {
53 return malloc_bytes_;
54 }
55
56 void DumpMemStats(std::ostream& os) const;
57
58 private:
buzbee862a7602013-04-05 10:58:54 -070059 // Variable-length allocation block.
60 struct ArenaMemBlock {
61 size_t block_size;
62 size_t bytes_allocated;
63 ArenaMemBlock *next;
64 char ptr[0];
65 };
66
buzbeea5abf702013-04-12 14:39:29 -070067 ArenaMemBlock* EmptyArenaBlock();
buzbee862a7602013-04-05 10:58:54 -070068
69 size_t default_size_; // Smallest size of new allocation block.
70 size_t block_size_; // Amount of allocatable bytes on a default block.
71 ArenaMemBlock* arena_head_; // Head of linked list of allocation blocks.
buzbeea5abf702013-04-12 14:39:29 -070072 ArenaMemBlock* current_block_; // NOTE: code assumes there's always at least 1 block.
buzbee862a7602013-04-05 10:58:54 -070073 int num_arena_blocks_;
buzbeea5abf702013-04-12 14:39:29 -070074 uint32_t malloc_bytes_; // Number of actual bytes malloc'd
75 uint32_t alloc_stats_[kNumAllocKinds]; // Bytes used by various allocation kinds.
76 uint32_t lost_bytes_; // Lost memory at end of too-small region
77 uint32_t num_allocations_;
buzbee862a7602013-04-05 10:58:54 -070078}; // ArenaAllocator
79
80
81struct MemStats {
82 public:
83 void Dump(std::ostream& os) const {
84 arena_.DumpMemStats(os);
85 }
Brian Carlstrom9b7085a2013-07-18 15:15:21 -070086 explicit MemStats(const ArenaAllocator &arena) : arena_(arena) {}
buzbee862a7602013-04-05 10:58:54 -070087 private:
88 const ArenaAllocator &arena_;
Brian Carlstrom7934ac22013-07-26 10:54:15 -070089}; // MemStats
buzbee862a7602013-04-05 10:58:54 -070090
91} // namespace art
92
Brian Carlstromfc0e3212013-07-17 14:40:12 -070093#endif // ART_COMPILER_DEX_ARENA_ALLOCATOR_H_