added virtualmemlist - also tested using vec_test repo and its very fast

This commit is contained in:
Richard Thier 2024-09-03 17:16:00 +02:00
parent 0587129fc6
commit 5048b88106

61
VirtualMemList.hpp Normal file
View File

@ -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<typename T>
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