buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | #ifndef ART_SRC_COMPILER_DEX_COMPILER_ARENA_ALLOCATOR_H_ |
| 18 | #define ART_SRC_COMPILER_DEX_COMPILER_ARENA_ALLOCATOR_H_ |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <stddef.h> |
| 22 | #include "compiler_enums.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | #define ARENA_DEFAULT_BLOCK_SIZE (256 * 1024) |
| 27 | |
| 28 | class ArenaAllocator { |
| 29 | public: |
| 30 | |
| 31 | // 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 | |
| 49 | ArenaAllocator(size_t default_size = ARENA_DEFAULT_BLOCK_SIZE); |
| 50 | void* NewMem(size_t size, bool zero, ArenaAllocKind kind); |
| 51 | void ArenaReset(); |
| 52 | size_t BytesAllocated() { |
| 53 | return malloc_bytes_; |
| 54 | } |
| 55 | |
| 56 | void DumpMemStats(std::ostream& os) const; |
| 57 | |
| 58 | private: |
| 59 | |
| 60 | // Variable-length allocation block. |
| 61 | struct ArenaMemBlock { |
| 62 | size_t block_size; |
| 63 | size_t bytes_allocated; |
| 64 | ArenaMemBlock *next; |
| 65 | char ptr[0]; |
| 66 | }; |
| 67 | |
| 68 | ArenaMemBlock* EmptyArena(); |
| 69 | |
| 70 | size_t default_size_; // Smallest size of new allocation block. |
| 71 | size_t block_size_; // Amount of allocatable bytes on a default block. |
| 72 | ArenaMemBlock* arena_head_; // Head of linked list of allocation blocks. |
| 73 | ArenaMemBlock* current_arena_; // NOTE: code assumes there's always at least 1 block. |
| 74 | int num_arena_blocks_; |
| 75 | uint32_t malloc_bytes_; // Number of actual bytes malloc'd |
| 76 | uint32_t alloc_stats_[kNumAllocKinds]; // Bytes used by various allocation kinds. |
| 77 | |
| 78 | }; // ArenaAllocator |
| 79 | |
| 80 | |
| 81 | struct MemStats { |
| 82 | public: |
| 83 | void Dump(std::ostream& os) const { |
| 84 | arena_.DumpMemStats(os); |
| 85 | } |
| 86 | MemStats(const ArenaAllocator &arena) : arena_(arena){}; |
| 87 | private: |
| 88 | const ArenaAllocator &arena_; |
| 89 | }; // MemStats |
| 90 | |
| 91 | } // namespace art |
| 92 | |
| 93 | #endif // ART_SRC_COMPILER_DEX_COMPILER_ARENA_ALLOCATOR_H_ |