From: Marcelo Juchem Date: Mon, 8 Apr 2013 21:08:18 +0000 (-0700) Subject: Making StlAllocator usable when rebinding. X-Git-Tag: v0.22.0~1010 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=36b366265da55fc26201870d1c5c2477ccc12600;p=folly.git Making StlAllocator usable when rebinding. Summary: currently, StlAllocator can't be used when you want an untyped allocator that can be rebound later since it doesn't carry the SimpleAllocator pointer with it. This diff fixes that. Test Plan: unit test added Reviewed By: jon.coens@fb.com FB internal diff: D766559 --- diff --git a/folly/Memory.h b/folly/Memory.h index 00534177..e4db242b 100644 --- a/folly/Memory.h +++ b/folly/Memory.h @@ -78,9 +78,28 @@ template class StlAllocator { typedef void value_type; typedef void* pointer; typedef const void* const_pointer; + + StlAllocator() : alloc_(nullptr) { } + explicit StlAllocator(Alloc* alloc) : alloc_(alloc) { } + + Alloc* alloc() const { + return alloc_; + } + template struct rebind { typedef StlAllocator other; }; + + bool operator!=(const StlAllocator& other) const { + return alloc_ != other.alloc_; + } + + bool operator==(const StlAllocator& other) const { + return alloc_ == other.alloc_; + } + + private: + Alloc* alloc_; }; template diff --git a/folly/test/MemoryTest.cpp b/folly/test/MemoryTest.cpp index 82f1a50b..c9409d22 100644 --- a/folly/test/MemoryTest.cpp +++ b/folly/test/MemoryTest.cpp @@ -43,6 +43,21 @@ TEST(as_stl_allocator, sanity_check) { >::value)); } +TEST(StlAllocator, void_allocator) { + typedef StlAllocator void_allocator; + SysArena arena; + void_allocator valloc(&arena); + + typedef void_allocator::rebind::other int_allocator; + int_allocator ialloc(valloc); + + auto i = std::allocate_shared(ialloc, 10); + ASSERT_NE(nullptr, i.get()); + EXPECT_EQ(10, *i); + i.reset(); + ASSERT_EQ(nullptr, i.get()); +} + int main(int argc, char **argv) { FLAGS_logtostderr = true; google::InitGoogleLogging(argv[0]);