summaryrefslogtreecommitdiff
path: root/src/memory/arena.cpp
blob: 8962d8dbadf732bdab77107a6de234b5b8478205 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include "arena.hpp"

#include <cstdint>
#include <optional>

#include "esp_heap_caps.h"
#include "freertos/queue.h"
#include "span.hpp"

namespace memory {

Arena::Arena(std::size_t block_size,
             std::size_t num_blocks,
             uint32_t alloc_flags)
    : block_size_(block_size) {
  pool_ = static_cast<std::byte*>(
      heap_caps_malloc(block_size * num_blocks, alloc_flags));
  free_blocks_ = xQueueCreate(num_blocks, sizeof(void*));
  for (int i = 0; i < num_blocks; i++) {
    std::byte* block = pool_ + (i * block_size);
    xQueueSend(free_blocks_, &block, 0);
  }
}

Arena::~Arena() {
  // We shouldn't have any blocks in use when destroying an arena.
  assert(uxQueueSpacesAvailable(free_blocks_) == 0);
  vQueueDelete(free_blocks_);
  free(pool_);
}

auto Arena::Acquire() -> std::optional<ArenaPtr> {
  std::byte* block;
  bool result = xQueueReceive(free_blocks_, &block, 0);
  if (result) {
    ArenaPtr ptr{this, block, block_size_, 0};
    return ptr;
  } else {
    return {};
  }
}

auto Arena::Return(ArenaPtr ptr) -> void {
  assert(ptr.owner == this);
  xQueueSend(free_blocks_, &ptr.start, 0);
}

auto Arena::BlocksFree() -> std::size_t {
  return uxQueueMessagesWaiting(free_blocks_);
}

auto ArenaRef::Acquire(Arena* a) -> std::optional<ArenaRef> {
  auto ptr = a->Acquire();
  if (ptr) {
    ArenaRef ref{*ptr};
    return ref;
  }
  return {};
}

ArenaRef::ArenaRef(ArenaPtr p) : ptr(p) {}

ArenaRef::ArenaRef(ArenaRef&& other) : ptr(other.Release()) {}

auto ArenaRef::Release() -> ArenaPtr {
  auto ret = ptr;
  ptr.owner = nullptr;
  ptr.start = nullptr;
  ptr.size = 0;
  ptr.used_size = 0;
  return ret;
}

ArenaRef::~ArenaRef() {
  if (ptr.owner != nullptr) {
    ptr.owner->Return(ptr);
  }
}

}  // namespace memory