X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FArena.h;h=ce758e5b4014fc7a81e7b3a7dec33ba924fd5e10;hb=fd915b73606e09a5f46a1bca0a5d3643a1567014;hp=a8d8d7f806a2a1dae5e19e05cf1facd46940efee;hpb=5e3674ca805202eee22829ee936c11a38dabd6ab;p=folly.git diff --git a/folly/Arena.h b/folly/Arena.h index a8d8d7f8..ce758e5b 100644 --- a/folly/Arena.h +++ b/folly/Arena.h @@ -1,5 +1,5 @@ /* - * Copyright 2013 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/Likely.h" -#include "folly/Malloc.h" +#include +#include +#include +#include namespace folly { @@ -52,8 +55,7 @@ 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 @@ -61,13 +63,19 @@ class Arena { public: explicit Arena(const Alloc& alloc, size_t minBlockSize = kDefaultMinBlockSize, - size_t sizeLimit = 0) + size_t sizeLimit = kNoSizeLimit, + size_t maxAlign = kDefaultMaxAlign) : allocAndSize_(alloc, minBlockSize) , ptr_(nullptr) , end_(nullptr) , totalAllocatedSize_(0) , bytesUsed_(0) - , sizeLimit_(sizeLimit) { + , sizeLimit_(sizeLimit) + , maxAlign_(maxAlign) { + if ((maxAlign_ & (maxAlign_ - 1)) || maxAlign_ > alignof(Block)) { + throw std::invalid_argument( + folly::to("Invalid maxAlign: ", maxAlign_)); + } } ~Arena(); @@ -141,25 +149,27 @@ class Arena { private: Block() { } ~Block() { } - } __attribute__((aligned)); + } __attribute__((__aligned__)); // This should be alignas(std::max_align_t) but neither alignas nor // max_align_t are supported by gcc 4.6.2. public: static constexpr size_t kDefaultMinBlockSize = 4096 - sizeof(Block); + static constexpr size_t kNoSizeLimit = 0; + static constexpr size_t kDefaultMaxAlign = alignof(Block); + static constexpr size_t kBlockOverhead = sizeof(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 @@ -194,9 +204,13 @@ class Arena { char* end_; size_t totalAllocatedSize_; size_t bytesUsed_; - size_t sizeLimit_; + const size_t sizeLimit_; + const size_t maxAlign_; }; +template +struct IsArenaAllocator> : std::true_type { }; + /** * By default, don't pad the given size. */ @@ -207,21 +221,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) { @@ -234,15 +233,18 @@ struct ArenaAllocatorTraits { */ class SysArena : public Arena { public: - explicit SysArena( - size_t minBlockSize = kDefaultMinBlockSize, - size_t sizeLimit = 0) - : Arena(SysAlloc(), minBlockSize, sizeLimit) { + 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" +#include #endif /* FOLLY_ARENA_H_ */