X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FArena-inl.h;h=805e8616acc190ccb41bcabfce03359304c93cab;hb=ed8c80a0e0988e4ce687f51ca832a00e4a6b7930;hp=ad839ae4c1198b190e6894000b4bfd726d5cdb69;hpb=27494a20393fa45072e7d526d358835f3abe312a;p=folly.git diff --git a/folly/Arena-inl.h b/folly/Arena-inl.h index ad839ae4..805e8616 100644 --- a/folly/Arena-inl.h +++ b/folly/Arena-inl.h @@ -1,5 +1,5 @@ /* - * Copyright 2012 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,7 +31,6 @@ Arena::Block::allocate(Alloc& alloc, size_t size, bool allowSlack) { } void* mem = alloc.allocate(allocSize); - assert(isAligned(mem)); return std::make_pair(new (mem) Block(), allocSize - sizeof(Block)); } @@ -45,6 +44,13 @@ template void* Arena::allocateSlow(size_t size) { std::pair p; char* start; + + size_t allocSize = std::max(size, minBlockSize()) + sizeof(Block); + if (sizeLimit_ != kNoSizeLimit && + allocSize > sizeLimit_ - totalAllocatedSize_) { + throw std::bad_alloc(); + } + if (size > minBlockSize()) { // Allocate a large block for this chunk only, put it at the back of the // list so it doesn't get used for small allocations; don't change ptr_ @@ -63,6 +69,7 @@ void* Arena::allocateSlow(size_t size) { } assert(p.second >= size); + totalAllocatedSize_ += p.second + sizeof(Block); return start; } @@ -71,6 +78,8 @@ void Arena::merge(Arena&& other) { blocks_.splice_after(blocks_.before_begin(), other.blocks_); other.blocks_.clear(); other.ptr_ = other.end_ = nullptr; + totalAllocatedSize_ += other.totalAllocatedSize_; + other.totalAllocatedSize_ = 0; } template