作者: 韩晨旭 10225101440 李畅 10225102463
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.

68 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. #include <assert.h>
  6. namespace leveldb {
  7. static const int kBlockSize = 4096;
  8. Arena::Arena() : memory_usage_(0) {
  9. alloc_ptr_ = NULL; // First allocation will allocate a block
  10. alloc_bytes_remaining_ = 0;
  11. }
  12. Arena::~Arena() {
  13. for (size_t i = 0; i < blocks_.size(); i++) {
  14. delete[] blocks_[i];
  15. }
  16. }
  17. char* Arena::AllocateFallback(size_t bytes) {
  18. if (bytes > kBlockSize / 4) {
  19. // Object is more than a quarter of our block size. Allocate it separately
  20. // to avoid wasting too much space in leftover bytes.
  21. char* result = AllocateNewBlock(bytes);
  22. return result;
  23. }
  24. // We waste the remaining space in the current block.
  25. alloc_ptr_ = AllocateNewBlock(kBlockSize);
  26. alloc_bytes_remaining_ = kBlockSize;
  27. char* result = alloc_ptr_;
  28. alloc_ptr_ += bytes;
  29. alloc_bytes_remaining_ -= bytes;
  30. return result;
  31. }
  32. char* Arena::AllocateAligned(size_t bytes) {
  33. const int align = (sizeof(void*) > 8) ? sizeof(void*) : 8;
  34. assert((align & (align-1)) == 0); // Pointer size should be a power of 2
  35. size_t current_mod = reinterpret_cast<uintptr_t>(alloc_ptr_) & (align-1);
  36. size_t slop = (current_mod == 0 ? 0 : align - current_mod);
  37. size_t needed = bytes + slop;
  38. char* result;
  39. if (needed <= alloc_bytes_remaining_) {
  40. result = alloc_ptr_ + slop;
  41. alloc_ptr_ += needed;
  42. alloc_bytes_remaining_ -= needed;
  43. } else {
  44. // AllocateFallback always returned aligned memory
  45. result = AllocateFallback(bytes);
  46. }
  47. assert((reinterpret_cast<uintptr_t>(result) & (align-1)) == 0);
  48. return result;
  49. }
  50. char* Arena::AllocateNewBlock(size_t block_bytes) {
  51. char* result = new char[block_bytes];
  52. blocks_.push_back(result);
  53. memory_usage_.NoBarrier_Store(
  54. reinterpret_cast<void*>(MemoryUsage() + block_bytes + sizeof(char*)));
  55. return result;
  56. }
  57. } // namespace leveldb