• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #pragma once
16 
17 #include <cstddef>
18 #include <span>
19 
20 #include "pw_allocator/block.h"
21 #include "pw_allocator/freelist.h"
22 
23 namespace pw::allocator {
24 
25 class FreeListHeap {
26  public:
27   template <size_t kNumBuckets>
28   friend class FreeListHeapBuffer;
29   struct HeapStats {
30     size_t total_bytes;
31     size_t bytes_allocated;
32     size_t cumulative_allocated;
33     size_t cumulative_freed;
34     size_t total_allocate_calls;
35     size_t total_free_calls;
36   };
37   FreeListHeap(std::span<std::byte> region, FreeList& freelist);
38 
39   void* Allocate(size_t size);
40   void Free(void* ptr);
41   void* Realloc(void* ptr, size_t size);
42   void* Calloc(size_t num, size_t size);
43 
44   void LogHeapStats();
45 
46  private:
BlockToSpan(Block * block)47   std::span<std::byte> BlockToSpan(Block* block) {
48     return std::span<std::byte>(block->UsableSpace(), block->InnerSize());
49   }
50 
51   void InvalidFreeCrash();
52 
53   std::span<std::byte> region_;
54   FreeList& freelist_;
55   HeapStats heap_stats_;
56 };
57 
58 template <size_t kNumBuckets = 6>
59 class FreeListHeapBuffer {
60  public:
61   static constexpr std::array<size_t, kNumBuckets> defaultBuckets{
62       16, 32, 64, 128, 256, 512};
63 
FreeListHeapBuffer(std::span<std::byte> region)64   FreeListHeapBuffer(std::span<std::byte> region)
65       : freelist_(defaultBuckets), heap_(region, freelist_) {}
66 
Allocate(size_t size)67   void* Allocate(size_t size) { return heap_.Allocate(size); }
Free(void * ptr)68   void Free(void* ptr) { heap_.Free(ptr); }
Realloc(void * ptr,size_t size)69   void* Realloc(void* ptr, size_t size) { return heap_.Realloc(ptr, size); }
Calloc(size_t num,size_t size)70   void* Calloc(size_t num, size_t size) { return heap_.Calloc(num, size); }
71 
heap_stats()72   const FreeListHeap::HeapStats& heap_stats() const {
73     return heap_.heap_stats_;
74   };
75 
LogHeapStats()76   void LogHeapStats() { heap_.LogHeapStats(); }
77 
78  private:
79   FreeListBuffer<kNumBuckets> freelist_;
80   FreeListHeap heap_;
81 };
82 
83 }  // namespace pw::allocator
84