X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FArena.h;h=5ea7552fcf252bc416019c81eb279175fac5b55d;hb=59c7c8eafcd986b169fecaaff870d2d29267d029;hp=eef6fab73b04d02c25fc00e10dc114be335da2f7;hpb=050c1c3d52e4df860911235ac894291b2c089e59;p=folly.git diff --git a/folly/Arena.h b/folly/Arena.h index eef6fab7..5ea7552f 100644 --- a/folly/Arena.h +++ b/folly/Arena.h @@ -1,5 +1,5 @@ /* - * Copyright 2012 Facebook, Inc. + * Copyright 2014 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,12 +18,15 @@ #define FOLLY_ARENA_H_ #include -#include #include +#include +#include #include +#include "folly/Conv.h" #include "folly/Likely.h" #include "folly/Malloc.h" +#include "folly/Memory.h" namespace folly { @@ -52,24 +55,34 @@ namespace folly { * guaranteed to be rounded up to a multiple of the maximum alignment * required on your system; the returned value must be also. * - * An implementation that uses malloc() / free() is defined below, see - * SysAlloc / SysArena. + * An implementation that uses malloc() / free() is defined below, see SysArena. */ template struct ArenaAllocatorTraits; template class Arena { public: explicit Arena(const Alloc& alloc, - size_t minBlockSize = kDefaultMinBlockSize) - : allocAndSize_(alloc, minBlockSize), - ptr_(nullptr), - end_(nullptr) { + size_t minBlockSize = kDefaultMinBlockSize, + size_t sizeLimit = kNoSizeLimit, + size_t maxAlign = kDefaultMaxAlign) + : allocAndSize_(alloc, minBlockSize) + , ptr_(nullptr) + , end_(nullptr) + , totalAllocatedSize_(0) + , bytesUsed_(0) + , sizeLimit_(sizeLimit) + , maxAlign_(maxAlign) { + if ((maxAlign_ & (maxAlign_ - 1)) || maxAlign_ > alignof(Block)) { + throw std::invalid_argument( + folly::to("Invalid maxAlign: ", maxAlign_)); + } } ~Arena(); void* allocate(size_t size) { size = roundUp(size); + bytesUsed_ += size; if (LIKELY(end_ - ptr_ >= size)) { // Fast path: there's enough room in the current block @@ -92,6 +105,19 @@ class Arena { // Transfer ownership of all memory allocated from "other" to "this". void merge(Arena&& other); + // Gets the total memory used by the arena + size_t totalSize() const { + return totalAllocatedSize_ + sizeof(Arena); + } + + // Gets the total number of "used" bytes, i.e. bytes that the arena users + // allocated via the calls to `allocate`. Doesn't include fragmentation, e.g. + // if block size is 4KB and you allocate 2 objects of 3KB in size, + // `bytesUsed()` will be 6KB, while `totalSize()` will be 8KB+. + size_t bytesUsed() const { + return bytesUsed_; + } + private: // not copyable Arena(const Arena&) = delete; @@ -129,19 +155,20 @@ class Arena { public: static constexpr size_t kDefaultMinBlockSize = 4096 - sizeof(Block); + static constexpr size_t kNoSizeLimit = 0; + static constexpr size_t kDefaultMaxAlign = alignof(Block); private: - static constexpr size_t maxAlign = alignof(Block); - static constexpr bool isAligned(uintptr_t address) { - return (address & (maxAlign - 1)) == 0; + bool isAligned(uintptr_t address) const { + return (address & (maxAlign_ - 1)) == 0; } - static bool isAligned(void* p) { + bool isAligned(void* p) const { return isAligned(reinterpret_cast(p)); } // Round up size so it's properly aligned - static constexpr size_t roundUp(size_t size) { - return (size + maxAlign - 1) & ~(maxAlign - 1); + size_t roundUp(size_t size) const { + return (size + maxAlign_ - 1) & ~(maxAlign_ - 1); } // cache_last makes the list keep a pointer to the last element, so we @@ -174,8 +201,15 @@ class Arena { BlockList blocks_; char* ptr_; char* end_; + size_t totalAllocatedSize_; + size_t bytesUsed_; + const size_t sizeLimit_; + const size_t maxAlign_; }; +template +struct IsArenaAllocator> : std::true_type { }; + /** * By default, don't pad the given size. */ @@ -186,21 +220,6 @@ struct ArenaAllocatorTraits { } }; -/** - * Arena-compatible allocator that calls malloc() and free(); see - * goodMallocSize() in Malloc.h for goodSize(). - */ -class SysAlloc { - public: - void* allocate(size_t size) { - return checkedMalloc(size); - } - - void deallocate(void* p) { - free(p); - } -}; - template <> struct ArenaAllocatorTraits { static size_t goodSize(const SysAlloc& alloc, size_t size) { @@ -213,11 +232,16 @@ struct ArenaAllocatorTraits { */ class SysArena : public Arena { public: - explicit SysArena(size_t minBlockSize = kDefaultMinBlockSize) - : Arena(SysAlloc(), minBlockSize) { + explicit SysArena(size_t minBlockSize = kDefaultMinBlockSize, + size_t sizeLimit = kNoSizeLimit, + size_t maxAlign = kDefaultMaxAlign) + : Arena(SysAlloc(), minBlockSize, sizeLimit, maxAlign) { } }; +template <> +struct IsArenaAllocator : std::true_type { }; + } // namespace folly #include "folly/Arena-inl.h"