turbolist/VirtualMemList.hpp

67 lines
1.5 KiB
C++
Raw Permalink Normal View History

#ifndef VIRTUAL_MEM_LIST_H
#define VIRTUAL_MEM_LIST_H
2024-10-01 17:51:13 +02:00
#include "arena.h/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);
2024-10-01 17:51:13 +02:00
base = ((T*) aralloc(&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];
}
VML_INLINE void push_back(T elem) noexcept {
2024-09-03 20:16:32 +02:00
// Smallest solution:
// base[end++] = elem;
// Non-working, but logically better solution:
2024-10-01 17:51:13 +02:00
// T *value = (T*) aralloc(&a, sizeof(T), sizeof(T), 1);
2024-09-03 20:16:32 +02:00
// *value = elem;
// Longer solution (more correct - but I let it waste a bit of memory)
2024-10-01 17:51:13 +02:00
T *value = (T*) aralloc(&a, sizeof(T), sizeof(T), 1);
2024-09-03 20:16:32 +02:00
base[end++] = elem;
}
VML_INLINE void pop_back() noexcept {
--end;
}
VML_INLINE uint32_t size() noexcept {
return end;
}
};
#endif // VIRTUAL_MEM_LIST_H