Compare commits
2 Commits
c88bd10b73
...
69750cabf9
Author | SHA1 | Date | |
---|---|---|---|
|
69750cabf9 | ||
|
2c1edf668a |
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
[submodule "arena.h"]
|
||||||
|
path = arena.h
|
||||||
|
url = ssh://gitea@188.157.159.51:8122/prenex/arena.h.git
|
@ -1,6 +1,6 @@
|
|||||||
#ifndef VIRTUAL_MEM_LIST_H
|
#ifndef VIRTUAL_MEM_LIST_H
|
||||||
#define VIRTUAL_MEM_LIST_H
|
#define VIRTUAL_MEM_LIST_H
|
||||||
#include "arena.h"
|
#include "arena.h/arena.h"
|
||||||
|
|
||||||
#ifndef VML_NOINLINE
|
#ifndef VML_NOINLINE
|
||||||
#define VML_NOINLINE __attribute__((noinline))
|
#define VML_NOINLINE __attribute__((noinline))
|
||||||
@ -29,7 +29,7 @@ class VirtualMemList {
|
|||||||
public:
|
public:
|
||||||
VML_INLINE VirtualMemList(uint32_t initial_size = 0) noexcept {
|
VML_INLINE VirtualMemList(uint32_t initial_size = 0) noexcept {
|
||||||
a = newarena((ptrdiff_t)1 << 33);
|
a = newarena((ptrdiff_t)1 << 33);
|
||||||
base = ((T*) alloc(&a, sizeof(T), sizeof(T), 1)) + 1;
|
base = ((T*) aralloc(&a, sizeof(T), sizeof(T), 1)) + 1;
|
||||||
end = initial_size;
|
end = initial_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,11 +46,11 @@ public:
|
|||||||
// base[end++] = elem;
|
// base[end++] = elem;
|
||||||
|
|
||||||
// Non-working, but logically better solution:
|
// Non-working, but logically better solution:
|
||||||
// T *value = (T*) alloc(&a, sizeof(T), sizeof(T), 1);
|
// T *value = (T*) aralloc(&a, sizeof(T), sizeof(T), 1);
|
||||||
// *value = elem;
|
// *value = elem;
|
||||||
|
|
||||||
// Longer solution (more correct - but I let it waste a bit of memory)
|
// Longer solution (more correct - but I let it waste a bit of memory)
|
||||||
T *value = (T*) alloc(&a, sizeof(T), sizeof(T), 1);
|
T *value = (T*) aralloc(&a, sizeof(T), sizeof(T), 1);
|
||||||
base[end++] = elem;
|
base[end++] = elem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
103
arena.h
103
arena.h
@ -1,103 +0,0 @@
|
|||||||
// Gradual-commit arena demonstration
|
|
||||||
// This is free and unencumbered software released into the public domain.
|
|
||||||
/* Usage:
|
|
||||||
*
|
|
||||||
* arena a = newarena((ptrdiff_t)1 << 33);
|
|
||||||
* if (!alloc(a, size, align, count)) {
|
|
||||||
* break;
|
|
||||||
* }
|
|
||||||
* total += size * count;
|
|
||||||
*/
|
|
||||||
#ifndef ARENA_H
|
|
||||||
#define ARENA_H
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
static void *os_reserve(ptrdiff_t);
|
|
||||||
static char os_commit(void *, ptrdiff_t);
|
|
||||||
|
|
||||||
#define ARENA_PAGESIZE ((ptrdiff_t)1<<26)
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
char *begin;
|
|
||||||
char *commit;
|
|
||||||
char *end;
|
|
||||||
} arena;
|
|
||||||
|
|
||||||
static arena newarena(ptrdiff_t cap)
|
|
||||||
{
|
|
||||||
arena a = {0};
|
|
||||||
cap += -cap & (ARENA_PAGESIZE - 1);
|
|
||||||
a.begin = a.commit = a.end = (char*) os_reserve(cap);
|
|
||||||
if (a.begin) {
|
|
||||||
a.end += cap;
|
|
||||||
}
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void *alloc(arena *a, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count)
|
|
||||||
{
|
|
||||||
ptrdiff_t padding = -(size_t)a->begin & (align - 1);
|
|
||||||
ptrdiff_t committed = a->commit - a->begin;
|
|
||||||
if (count > (committed-padding)/size) {
|
|
||||||
ptrdiff_t reserved = a->end - a->begin;
|
|
||||||
if (count > (reserved-padding)/size) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
ptrdiff_t needed = size*count + padding - committed;
|
|
||||||
needed += -needed & (ARENA_PAGESIZE - 1);
|
|
||||||
if (!os_commit(a->commit, needed)) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
a->commit += needed;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *ptr = a->begin + padding;
|
|
||||||
a->begin += padding + size*count;
|
|
||||||
|
|
||||||
// Change to this instead if you want zero-inited (but this gets slow with many arenas)
|
|
||||||
//return memset(ptr, 0, size*count);
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
// $ cc -g3 -nostartfiles -o arena.exe arena.c
|
|
||||||
// $ cl /Z7 arena.c /link /subsystem:console kernel32.lib libvcruntime.lib
|
|
||||||
#define W32(r) __declspec(dllimport) r __stdcall
|
|
||||||
W32(void) ExitProcess(int);
|
|
||||||
W32(void *) VirtualAlloc(void *, ptrdiff_t, int, int);
|
|
||||||
|
|
||||||
#define MEM_COMMIT 0x1000
|
|
||||||
#define MEM_RESERVE 0x2000
|
|
||||||
#define PAGE_NOACCESS 0x0001
|
|
||||||
#define PAGE_READWRITE 0x0004
|
|
||||||
|
|
||||||
static void *os_reserve(ptrdiff_t cap)
|
|
||||||
{
|
|
||||||
return VirtualAlloc(0, cap, MEM_RESERVE, PAGE_NOACCESS);
|
|
||||||
}
|
|
||||||
|
|
||||||
static char os_commit(void *ptr, ptrdiff_t len)
|
|
||||||
{
|
|
||||||
return VirtualAlloc(ptr, len, MEM_COMMIT, PAGE_READWRITE);
|
|
||||||
}
|
|
||||||
|
|
||||||
#else // POSIX
|
|
||||||
// $ cc -g3 -o arena arena.c
|
|
||||||
#include <sys/mman.h>
|
|
||||||
|
|
||||||
static void *os_reserve(ptrdiff_t cap)
|
|
||||||
{
|
|
||||||
void *r = mmap(0, cap, PROT_NONE, MAP_ANON|MAP_PRIVATE, -1, 0);
|
|
||||||
return r==MAP_FAILED ? 0 : r;
|
|
||||||
}
|
|
||||||
|
|
||||||
static char os_commit(void *ptr, ptrdiff_t len)
|
|
||||||
{
|
|
||||||
return !mprotect(ptr, len, PROT_READ|PROT_WRITE);
|
|
||||||
}
|
|
||||||
#endif // POSIX
|
|
||||||
|
|
||||||
#endif /* ARENA_H */
|
|
1
arena.h
Submodule
1
arena.h
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit 3037bf6bec96b0ebc231510d308da1daece276fd
|
26
main.cpp
26
main.cpp
@ -10,7 +10,7 @@
|
|||||||
#define TLT int
|
#define TLT int
|
||||||
#include "turbolist.h"
|
#include "turbolist.h"
|
||||||
|
|
||||||
#include"arena.h"
|
#include"arena.h/arena.h"
|
||||||
|
|
||||||
// #define PRINT_DBG
|
// #define PRINT_DBG
|
||||||
// #define N 65535
|
// #define N 65535
|
||||||
@ -133,7 +133,7 @@ int main() {
|
|||||||
// malloc-like
|
// malloc-like
|
||||||
[](size_t size) {
|
[](size_t size) {
|
||||||
static thread_local arena a = newarena((ptrdiff_t)1 << 33);
|
static thread_local arena a = newarena((ptrdiff_t)1 << 33);
|
||||||
return (void*) alloc(&a, sizeof(uint8_t), sizeof(uint8_t), size);
|
return (void*) aralloc(&a, sizeof(uint8_t), sizeof(uint8_t), size);
|
||||||
},
|
},
|
||||||
// free-like
|
// free-like
|
||||||
[](void *ptr) {},
|
[](void *ptr) {},
|
||||||
@ -174,29 +174,29 @@ int main() {
|
|||||||
arena a9 = newarena((ptrdiff_t)1 << 33);
|
arena a9 = newarena((ptrdiff_t)1 << 33);
|
||||||
*/
|
*/
|
||||||
// steps at first real elem, there should be N from here!
|
// steps at first real elem, there should be N from here!
|
||||||
int *arenalist = ((int*)alloc(&a, sizeof(int), sizeof(int), 1)) + 1;
|
int *arenalist = ((int *)aralloc(&a, sizeof(int), sizeof(int), 1)) + 1;
|
||||||
for(int i = 0; i < N; ++i) {
|
for(int i = 0; i < N; ++i) {
|
||||||
int *value = (int*)alloc(&a, sizeof(int), sizeof(int), 1);
|
int *value = (int *)aralloc(&a, sizeof(int), sizeof(int), 1);
|
||||||
*value = i;
|
*value = i;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
value = (int*)alloc(&a1, sizeof(int), sizeof(int), 1);
|
value = (int*)aralloc(&a1, sizeof(int), sizeof(int), 1);
|
||||||
*value = i;
|
*value = i;
|
||||||
value = (int*)alloc(&a2, sizeof(int), sizeof(int), 1);
|
value = (int*)aralloc(&a2, sizeof(int), sizeof(int), 1);
|
||||||
*value = i;
|
*value = i;
|
||||||
value = (int*)alloc(&a3, sizeof(int), sizeof(int), 1);
|
value = (int*)aralloc(&a3, sizeof(int), sizeof(int), 1);
|
||||||
*value = i;
|
*value = i;
|
||||||
value = (int*)alloc(&a4, sizeof(int), sizeof(int), 1);
|
value = (int*)aralloc(&a4, sizeof(int), sizeof(int), 1);
|
||||||
*value = i;
|
*value = i;
|
||||||
value = (int*)alloc(&a5, sizeof(int), sizeof(int), 1);
|
value = (int*)aralloc(&a5, sizeof(int), sizeof(int), 1);
|
||||||
*value = i;
|
*value = i;
|
||||||
value = (int*)alloc(&a6, sizeof(int), sizeof(int), 1);
|
value = (int*)aralloc(&a6, sizeof(int), sizeof(int), 1);
|
||||||
*value = i;
|
*value = i;
|
||||||
value = (int*)alloc(&a7, sizeof(int), sizeof(int), 1);
|
value = (int*)aralloc(&a7, sizeof(int), sizeof(int), 1);
|
||||||
*value = i;
|
*value = i;
|
||||||
value = (int*)alloc(&a8, sizeof(int), sizeof(int), 1);
|
value = (int*)aralloc(&a8, sizeof(int), sizeof(int), 1);
|
||||||
*value = i;
|
*value = i;
|
||||||
value = (int*)alloc(&a9, sizeof(int), sizeof(int), 1);
|
value = (int*)aralloc(&a9, sizeof(int), sizeof(int), 1);
|
||||||
*value = i;
|
*value = i;
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user