#include <folly/ThreadCachedArena.h>
+#include <memory>
+
namespace folly {
ThreadCachedArena::ThreadCachedArena(size_t minBlockSize, size_t maxAlign)
}
void ThreadCachedArena::zombify(SysArena&& arena) {
- std::lock_guard<std::mutex> lock(zombiesMutex_);
- zombies_.merge(std::move(arena));
+ zombies_->merge(std::move(arena));
+}
+
+size_t ThreadCachedArena::totalSize() const {
+ size_t result = sizeof(ThreadCachedArena);
+ for (const auto& arena : arena_.accessAllThreads()) {
+ result += arena.totalSize();
+ }
+ result += zombies_->totalSize() - sizeof(SysArena);
+ return result;
}
} // namespace folly
#ifndef FOLLY_THREADCACHEDARENA_H_
#define FOLLY_THREADCACHEDARENA_H_
-#include <utility>
-#include <mutex>
-#include <limits>
-#include <boost/intrusive/slist.hpp>
+#include <type_traits>
-#include <folly/Likely.h>
#include <folly/Arena.h>
+#include <folly/Likely.h>
+#include <folly/Synchronized.h>
#include <folly/ThreadLocal.h>
namespace folly {
// Deallocate? Never!
}
+ // Gets the total memory used by the arena
+ size_t totalSize() const;
+
private:
+ struct ThreadLocalPtrTag {};
+
ThreadCachedArena(const ThreadCachedArena&) = delete;
ThreadCachedArena(ThreadCachedArena&&) = delete;
ThreadCachedArena& operator=(const ThreadCachedArena&) = delete;
const size_t minBlockSize_;
const size_t maxAlign_;
- SysArena zombies_; // allocated from threads that are now dead
- std::mutex zombiesMutex_;
- ThreadLocalPtr<SysArena> arena_; // per-thread arena
+
+ ThreadLocalPtr<SysArena, ThreadLocalPtrTag> arena_; // Per-thread arena.
+
+ // Allocations from threads that are now dead.
+ Synchronized<SysArena> zombies_;
};
template <>
TEST(ThreadCachedArena, SingleThreaded) {
static const size_t requestedBlockSize = 64;
ThreadCachedArena arena(requestedBlockSize);
+ EXPECT_EQ(arena.totalSize(), sizeof(ThreadCachedArena));
+
ArenaTester tester(arena);
tester.allocate(100, 100 << 10);
tester.verify();
+
+ EXPECT_GT(arena.totalSize(), sizeof(ThreadCachedArena));
}
TEST(ThreadCachedArena, MultiThreaded) {