allocator.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. //
  10. // Abstract interface for allocating memory in blocks. This memory is freed
  11. // when the allocator object is destroyed. See the Arena class for more info.
  12. #pragma once
  13. #include <cerrno>
  14. #include <cstddef>
  15. #include "rocksdb/write_buffer_manager.h"
  16. namespace ROCKSDB_NAMESPACE {
  17. class Logger;
  18. class Allocator {
  19. public:
  20. virtual ~Allocator() {}
  21. virtual char* Allocate(size_t bytes) = 0;
  22. virtual char* AllocateAligned(size_t bytes, size_t huge_page_size = 0,
  23. Logger* logger = nullptr) = 0;
  24. virtual size_t BlockSize() const = 0;
  25. };
  26. class AllocTracker {
  27. public:
  28. explicit AllocTracker(WriteBufferManager* write_buffer_manager);
  29. // No copying allowed
  30. AllocTracker(const AllocTracker&) = delete;
  31. void operator=(const AllocTracker&) = delete;
  32. ~AllocTracker();
  33. void Allocate(size_t bytes);
  34. // Call when we're finished allocating memory so we can free it from
  35. // the write buffer's limit.
  36. void DoneAllocating();
  37. void FreeMem();
  38. bool is_freed() const { return write_buffer_manager_ == nullptr || freed_; }
  39. private:
  40. WriteBufferManager* write_buffer_manager_;
  41. std::atomic<size_t> bytes_allocated_;
  42. bool done_allocating_;
  43. bool freed_;
  44. };
  45. } // namespace ROCKSDB_NAMESPACE