From 5048b88106258b327d30136ff8a3457448c412e6 Mon Sep 17 00:00:00 2001 From: Richard Thier Date: Tue, 3 Sep 2024 17:16:00 +0200 Subject: [PATCH] added virtualmemlist - also tested using vec_test repo and its very fast --- VirtualMemList.hpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 VirtualMemList.hpp diff --git a/VirtualMemList.hpp b/VirtualMemList.hpp new file mode 100644 index 0000000..39a8d9e --- /dev/null +++ b/VirtualMemList.hpp @@ -0,0 +1,61 @@ +#ifndef VIRTUAL_MEM_LIST_H +#define VIRTUAL_MEM_LIST_H +#include "arena.h" + +#ifndef VML_NOINLINE +#define VML_NOINLINE __attribute__((noinline)) +#endif /* VML_NOINLINE */ +#ifndef VML_INLINE +#define VML_INLINE __attribute__((always_inline)) +#endif /* VML_INLINE */ + +#ifndef VML_LIKELY +#define VML_LIKELY(x) __builtin_expect(!!(x), 1) +#endif /* VML_LIKELY */ +#ifndef VML_UNLIKELY +#define VML_UNLIKELY(x) __builtin_expect(!!(x), 0) +#endif /* VML_UNLIKELY */ + +#ifndef VML_GROWTH_RATE +#define VML_GROWTH_RATE 2 +#endif /* VML_GROWTH_RATE */ + +template +class VirtualMemList { + arena a; + T *base; + uint32_t end; + +public: + VML_INLINE VirtualMemList(uint32_t initial_size = 0) noexcept { + a = newarena((ptrdiff_t)1 << 33); + base = ((T*) alloc(&a, sizeof(T), sizeof(T), 1)) + 1; + end = initial_size; + } + + VML_INLINE ~VirtualMemList() noexcept { + // TODO: arena free currently not implemented + } + + VML_INLINE T& operator[](uint32_t i) const noexcept { + return base[i]; + } + + /** Vector compatibility: Use insert() if you want the inserted thing as reference too */ + VML_INLINE void push_back(T elem) noexcept { + // Smaller solution: base[end++] = elem; + T *value = (T*) alloc(&a, sizeof(T), sizeof(T), 1); + *value = elem; + } + + /** Vector compatibility: Use pop() if you want the popped thing out as copy too */ + VML_INLINE void pop_back() noexcept { + --end; + } + + VML_INLINE uint32_t size() noexcept { + return end; + } +}; + +#endif // VIRTUAL_MEM_LIST_H