Skip to content

src/core/memory.h

Namespaces

Name
dakku
dakku namespace

Classes

Name
class dakku::MemoryArena
memory arena (unsynchronized)
class dakku::GlobalMemoryArena
global memory arena

Defines

Name
DAKKU_ALLOCA(type, count)
allocated memory from stack

Macros Documentation

define DAKKU_ALLOCA

#define DAKKU_ALLOCA(
    type,
    count
)
  (reinterpret_cast<(type) *>(alloca((count) * sizeof(type))))

allocated memory from stack

Parameters:

  • type type that needs to allocate
  • count the number of [type]s

Source code

#ifndef DAKKU_CORE_MEMORY_H_
#define DAKKU_CORE_MEMORY_H_
#include <core/logger.h>

#include <oneapi/tbb.h>
#include <oneapi/tbb/scalable_allocator.h>

#if __has_include(<malloc.h>)
#include <malloc.h>
#endif
#if __has_include(<alloca.h>)
#include <alloca.h>
#endif
#include <memory_resource>

namespace dakku {

#define DAKKU_ALLOCA(type, count) \
  (reinterpret_cast<(type) *>(alloca((count) * sizeof(type))))

class DAKKU_EXPORT_CORE MemoryArena {
 public:
  explicit MemoryArena() = default;
  explicit MemoryArena(std::pmr::memory_resource *buffer)
      : upStream(buffer), resource(&upStream) {}

  template <typename T, typename... Args>
  T *allocObject(Args &&...args) {
    return std::pmr::polymorphic_allocator<T>{&resource}.template new_object<T>(
        std::forward<Args>(args)...);
  }

  void release() { resource.release(); }

 private:
  oneapi::tbb::cache_aligned_resource upStream{
      oneapi::tbb::scalable_memory_resource()};
  std::pmr::unsynchronized_pool_resource resource{&upStream};
};

class DAKKU_EXPORT_CORE GlobalMemoryArena {
 public:
  static MemoryArena &instance();

 private:
  GlobalMemoryArena() = default;
  MemoryArena arena;
};
}  // namespace dakku
#endif

Updated on 2022-04-30 at 15:46:11 +0000