diff options
Diffstat (limited to 'compiler/dex/arena_allocator.h')
-rw-r--r-- | compiler/dex/arena_allocator.h | 154 |
1 files changed, 108 insertions, 46 deletions
diff --git a/compiler/dex/arena_allocator.h b/compiler/dex/arena_allocator.h index e8e2c027d0..dda52a2ed0 100644 --- a/compiler/dex/arena_allocator.h +++ b/compiler/dex/arena_allocator.h @@ -19,64 +19,126 @@ #include <stdint.h> #include <stddef.h> + +#include "base/mutex.h" #include "compiler_enums.h" +#include "mem_map.h" namespace art { -#define ARENA_DEFAULT_BLOCK_SIZE (256 * 1024) -#define ARENA_HIGH_WATER (16 * 1024) +class Arena; +class ArenaPool; +class ArenaAllocator; + +class Arena { + public: + static constexpr size_t kDefaultSize = 128 * KB; + explicit Arena(size_t size = kDefaultSize); + ~Arena(); + void Reset(); + uint8_t* Begin() { + return memory_; + } + + uint8_t* End() { + return memory_ + size_; + } + + size_t Size() const { + return size_; + } + + size_t RemainingSpace() const { + return Size() - bytes_allocated_; + } + + private: + size_t bytes_allocated_; + uint8_t* memory_; + size_t size_; + MemMap* map_; + Arena* next_; + friend class ArenaPool; + friend class ArenaAllocator; + DISALLOW_COPY_AND_ASSIGN(Arena); +}; + +class ArenaPool { + public: + ArenaPool(); + ~ArenaPool(); + Arena* AllocArena(size_t size); + void FreeArena(Arena* arena); + + private: + Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; + Arena* free_arenas_ GUARDED_BY(lock_); + DISALLOW_COPY_AND_ASSIGN(ArenaPool); +}; class ArenaAllocator { - public: - // Type of allocation for memory tuning. - enum ArenaAllocKind { - kAllocMisc, - kAllocBB, - kAllocLIR, - kAllocMIR, - kAllocDFInfo, - kAllocGrowableArray, - kAllocGrowableBitMap, - kAllocDalvikToSSAMap, - kAllocDebugInfo, - kAllocSuccessor, - kAllocRegAlloc, - kAllocData, - kAllocPredecessors, - kNumAllocKinds - }; - - explicit ArenaAllocator(size_t default_size = ARENA_DEFAULT_BLOCK_SIZE); + public: + // Type of allocation for memory tuning. + enum ArenaAllocKind { + kAllocMisc, + kAllocBB, + kAllocLIR, + kAllocMIR, + kAllocDFInfo, + kAllocGrowableArray, + kAllocGrowableBitMap, + kAllocDalvikToSSAMap, + kAllocDebugInfo, + kAllocSuccessor, + kAllocRegAlloc, + kAllocData, + kAllocPredecessors, + kNumAllocKinds + }; + + static constexpr bool kCountAllocations = false; + + explicit ArenaAllocator(ArenaPool* pool); ~ArenaAllocator(); - void* NewMem(size_t size, bool zero, ArenaAllocKind kind); - size_t BytesAllocated() { - return malloc_bytes_; + + // Returns zeroed memory. + void* Alloc(size_t bytes, ArenaAllocKind kind) ALWAYS_INLINE { + bytes = (bytes + 3) & ~3; + if (UNLIKELY(ptr_ + bytes > end_)) { + // Obtain a new block. + ObtainNewArenaForAllocation(bytes); + if (UNLIKELY(ptr_ == nullptr)) { + return nullptr; + } + } + if (kCountAllocations) { + alloc_stats_[kind] += bytes; + ++num_allocations_; + } + uint8_t* ret = ptr_; + ptr_ += bytes; + return ret; } + void ObtainNewArenaForAllocation(size_t allocation_size); + size_t BytesAllocated() const; void DumpMemStats(std::ostream& os) const; - private: - // Variable-length allocation block. - struct ArenaMemBlock { - size_t block_size; - size_t bytes_allocated; - ArenaMemBlock *next; - char ptr[0]; - }; - - ArenaMemBlock* EmptyArenaBlock(); - - size_t default_size_; // Smallest size of new allocation block. - size_t block_size_; // Amount of allocatable bytes on a default block. - ArenaMemBlock* arena_head_; // Head of linked list of allocation blocks. - ArenaMemBlock* current_block_; // NOTE: code assumes there's always at least 1 block. - int num_arena_blocks_; - uint32_t malloc_bytes_; // Number of actual bytes malloc'd - uint32_t alloc_stats_[kNumAllocKinds]; // Bytes used by various allocation kinds. - uint32_t lost_bytes_; // Lost memory at end of too-small region - uint32_t num_allocations_; -}; // ArenaAllocator + private: + void UpdateBytesAllocated(); + + ArenaPool* pool_; + uint8_t* begin_; + uint8_t* end_; + uint8_t* ptr_; + Arena* arena_head_; + // Statistics. + size_t num_allocations_; + size_t alloc_stats_[kNumAllocKinds]; // Bytes used by various allocation kinds. + + DISALLOW_COPY_AND_ASSIGN(ArenaAllocator); +}; // ArenaAllocator struct MemStats { public: |