Use Genode functions to allocate memory.

This commit is contained in:
Michael Müller
2022-07-07 12:39:57 +02:00
parent 3955cc82fe
commit 8fba63d09a
4 changed files with 17 additions and 6 deletions

View File

@@ -2,6 +2,8 @@
#include "alignment_helper.h"
#include <cstdint>
#include <cstdlib>
#include <base/heap.h>
#include <mx/system/environment.h>
namespace mx::memory {
/**
@@ -10,7 +12,12 @@ namespace mx::memory {
*/
class GlobalHeap
{
private:
static Genode::Heap _heap;
public:
static Genode::Heap &heap() { return _heap; }
/**
* Allocates the given size on the given NUMA node.
*
@@ -21,6 +28,7 @@ public:
static void *allocate(const std::uint8_t numa_node_id, const std::size_t size)
{
/* TODO: Use component's heap */
_heap.alloc(size);
}
/**
@@ -33,7 +41,7 @@ public:
static void *allocate_cache_line_aligned(const std::size_t size)
{
/* TODO: Use component's heap, as std::aligned_alloc might not be thread-safe */
return std::malloc(alignment_helper::next_multiple(size, 64UL));
return _heap.alloc(alignment_helper::next_multiple(size, 64UL));
}
/**
@@ -42,6 +50,8 @@ public:
* @param memory Pointer to memory.
* @param size Size of the allocated object.
*/
static void free(void *memory, const std::size_t size) { /* TODO: Free via Genode component's heap */ }
static void free(void *memory, const std::size_t size) { /* TODO: Free via Genode component's heap */
_heap.free(memory, size);
}
};
} // namespace mx::memory

View File

@@ -2,6 +2,7 @@
#include <cstdint>
#include <cstdlib>
#include "global_heap.h"
namespace mx::memory {
/**
@@ -41,12 +42,12 @@ public:
/**
* @return Allocated memory using systems malloc (but aligned).
*/
[[nodiscard]] void *allocate(const std::uint16_t /*core_id*/) override { return std::malloc(S); }
[[nodiscard]] void *allocate(const std::uint16_t /*core_id*/) override { return GlobalHeap::heap().alloc(S); }
/**
* Frees the given memory using systems free.
* @param address Memory to free.
*/
void free(const std::uint16_t /*core_id*/, void *address) noexcept override { std::free(address); }
void free(const std::uint16_t /*core_id*/, void *address) noexcept override { GlobalHeap::heap().free(address); }
};
} // namespace mx::memory

View File

@@ -7,7 +7,7 @@
using namespace mx::tasking::profiling;
ProfilingTask::ProfilingTask(mx::util::maybe_atomic<bool> &is_running, mx::tasking::Channel &channel)
: _is_running(is_running), _channel(channel), _timer(*new Timer::Connection(*system::Environment::env))
: _is_running(is_running), _channel(channel), _timer (*new (memory::GlobalHeap::heap())Timer::Connection(*system::Environment::env))
{
_idle_ranges.reserve(1 << 16);
}

View File

@@ -80,7 +80,7 @@ public:
// Create a new resource builder.
if (_resource_builder == nullptr || need_new_scheduler)
{
_resource_builder = new resource::Builder(*_scheduler, *_resource_allocator);
_resource_builder = new(memory::GlobalHeap::heap()) resource::Builder(*_scheduler, *_resource_allocator);
}
return true;