提供基本的ttl测试用例
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

67 rindas
2.0 KiB

  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #include "util/arena.h"
  5. namespace leveldb {
  6. static const int kBlockSize = 4096;
  7. Arena::Arena() : memory_usage_(0) {
  8. alloc_ptr_ = nullptr; // First allocation will allocate a block
  9. alloc_bytes_remaining_ = 0;
  10. }
  11. Arena::~Arena() {
  12. for (size_t i = 0; i < blocks_.size(); i++) {
  13. delete[] blocks_[i];
  14. }
  15. }
  16. char* Arena::AllocateFallback(size_t bytes) {
  17. if (bytes > kBlockSize / 4) {
  18. // Object is more than a quarter of our block size. Allocate it separately
  19. // to avoid wasting too much space in leftover bytes.
  20. char* result = AllocateNewBlock(bytes);
  21. return result;
  22. }
  23. // We waste the remaining space in the current block.
  24. alloc_ptr_ = AllocateNewBlock(kBlockSize);
  25. alloc_bytes_remaining_ = kBlockSize;
  26. char* result = alloc_ptr_;
  27. alloc_ptr_ += bytes;
  28. alloc_bytes_remaining_ -= bytes;
  29. return result;
  30. }
  31. char* Arena::AllocateAligned(size_t bytes) {
  32. const int align = (sizeof(void*) > 8) ? sizeof(void*) : 8;
  33. assert((align & (align-1)) == 0); // Pointer size should be a power of 2
  34. size_t current_mod = reinterpret_cast<uintptr_t>(alloc_ptr_) & (align-1);
  35. size_t slop = (current_mod == 0 ? 0 : align - current_mod);
  36. size_t needed = bytes + slop;
  37. char* result;
  38. if (needed <= alloc_bytes_remaining_) {
  39. result = alloc_ptr_ + slop;
  40. alloc_ptr_ += needed;
  41. alloc_bytes_remaining_ -= needed;
  42. } else {
  43. // AllocateFallback always returned aligned memory
  44. result = AllocateFallback(bytes);
  45. }
  46. assert((reinterpret_cast<uintptr_t>(result) & (align-1)) == 0);
  47. return result;
  48. }
  49. char* Arena::AllocateNewBlock(size_t block_bytes) {
  50. char* result = new char[block_bytes];
  51. blocks_.push_back(result);
  52. memory_usage_.fetch_add(block_bytes + sizeof(char*),
  53. std::memory_order_relaxed);
  54. return result;
  55. }
  56. } // namespace leveldb