67 lines
1.5 KiB
C++
67 lines
1.5 KiB
C++
#ifndef VIRTUAL_MEM_LIST_H
|
|
#define VIRTUAL_MEM_LIST_H
|
|
#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);
|
|
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 {
|
|
// Smallest solution:
|
|
// base[end++] = elem;
|
|
|
|
// Non-working, but logically better solution:
|
|
// T *value = (T*) aralloc(&a, sizeof(T), sizeof(T), 1);
|
|
// *value = elem;
|
|
|
|
// Longer solution (more correct - but I let it waste a bit of memory)
|
|
T *value = (T*) aralloc(&a, sizeof(T), sizeof(T), 1);
|
|
base[end++] = elem;
|
|
}
|
|
|
|
VML_INLINE void pop_back() noexcept {
|
|
--end;
|
|
}
|
|
|
|
VML_INLINE uint32_t size() noexcept {
|
|
return end;
|
|
}
|
|
};
|
|
|
|
#endif // VIRTUAL_MEM_LIST_H
|