2020-01-30 10:11:10 +00:00
|
|
|
/*
|
|
|
|
* vm_pool.c - Unlambda VM object pool
|
|
|
|
*
|
|
|
|
* Copyright (C) 2020 CismonX <admin@cismon.net>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "vm_pool.h"
|
|
|
|
#include "vm_stack.h"
|
2020-02-05 16:59:11 +00:00
|
|
|
#include "logging.h"
|
2020-01-30 10:11:10 +00:00
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
struct vm_pool_elem {
|
|
|
|
struct u6a_vm_var_tuple values;
|
|
|
|
uint32_t refcnt;
|
|
|
|
uint32_t flags;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define POOL_ELEM_HOLDS_PTR ( 1 << 0 )
|
|
|
|
|
|
|
|
struct vm_pool {
|
|
|
|
uint32_t pos;
|
|
|
|
struct vm_pool_elem elems[];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct vm_pool_elem_ptrs {
|
|
|
|
uint32_t pos;
|
|
|
|
struct vm_pool_elem* elems[];
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct vm_pool* active_pool;
|
|
|
|
static struct vm_pool_elem_ptrs* holes;
|
|
|
|
static uint32_t pool_len;
|
|
|
|
static struct vm_pool_elem** fstack;
|
|
|
|
static uint32_t fstack_top;
|
|
|
|
|
2020-02-05 16:59:11 +00:00
|
|
|
const char* err_stage;
|
|
|
|
|
2020-01-30 10:11:10 +00:00
|
|
|
static inline struct vm_pool_elem*
|
|
|
|
vm_pool_elem_alloc() {
|
|
|
|
struct vm_pool_elem* new_elem;
|
|
|
|
if (holes->pos == UINT32_MAX) {
|
2020-02-16 16:36:49 +00:00
|
|
|
if (UNLIKELY(++active_pool->pos == pool_len)) {
|
2020-02-05 16:59:11 +00:00
|
|
|
u6a_err_vm_pool_oom(err_stage);
|
2020-01-30 10:11:10 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
new_elem = active_pool->elems + active_pool->pos;
|
|
|
|
} else {
|
|
|
|
new_elem = holes->elems[holes->pos--];
|
|
|
|
}
|
|
|
|
new_elem->refcnt = 1;
|
|
|
|
return new_elem;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct vm_pool_elem*
|
|
|
|
vm_pool_elem_dup(struct vm_pool_elem* elem) {
|
|
|
|
struct vm_pool_elem* new_elem = vm_pool_elem_alloc();
|
|
|
|
if (UNLIKELY(new_elem == NULL)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*new_elem = *elem;
|
|
|
|
return new_elem;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2020-02-08 16:51:31 +00:00
|
|
|
free_stack_push(struct u6a_vm_var_fn fn) {
|
|
|
|
if (fn.token.fn & U6A_VM_FN_REF) {
|
|
|
|
fstack[++fstack_top] = active_pool->elems + fn.ref;
|
2020-01-30 10:11:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct vm_pool_elem*
|
|
|
|
free_stack_pop() {
|
|
|
|
if (fstack_top == UINT32_MAX) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-02-04 18:47:45 +00:00
|
|
|
return fstack[fstack_top--];
|
2020-01-30 10:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-02-05 16:59:11 +00:00
|
|
|
u6a_vm_pool_init(uint32_t pool_len_, uint32_t ins_len, const char* err_stage_) {
|
2020-02-02 17:09:21 +00:00
|
|
|
const uint32_t pool_size = sizeof(struct vm_pool) + pool_len_ * sizeof(struct vm_pool_elem);
|
2020-01-30 10:11:10 +00:00
|
|
|
active_pool = malloc(pool_size);
|
|
|
|
if (UNLIKELY(active_pool == NULL)) {
|
2020-02-05 16:59:11 +00:00
|
|
|
u6a_err_bad_alloc(err_stage_, pool_size);
|
2020-01-30 10:11:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-02-02 17:09:21 +00:00
|
|
|
const uint32_t holes_size = sizeof(struct vm_pool_elem_ptrs) + pool_len_ * sizeof(struct vm_pool_elem*);
|
2020-01-30 10:11:10 +00:00
|
|
|
holes = malloc(holes_size);
|
2020-02-02 17:09:21 +00:00
|
|
|
if (UNLIKELY(holes == NULL)) {
|
2020-02-05 16:59:11 +00:00
|
|
|
u6a_err_bad_alloc(err_stage_, holes_size);
|
2020-02-02 17:09:21 +00:00
|
|
|
free(holes);
|
2020-01-30 10:11:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-02-02 17:09:21 +00:00
|
|
|
const uint32_t free_stack_size = ins_len * sizeof(struct vm_pool_elem*);
|
2020-01-30 10:11:10 +00:00
|
|
|
fstack = malloc(free_stack_size);
|
2020-02-02 17:09:21 +00:00
|
|
|
if (UNLIKELY(fstack == NULL)) {
|
2020-02-05 16:59:11 +00:00
|
|
|
u6a_err_bad_alloc(err_stage_, free_stack_size);
|
2020-01-30 10:11:10 +00:00
|
|
|
free(active_pool);
|
|
|
|
free(holes);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
active_pool->pos = UINT32_MAX;
|
|
|
|
holes->pos = UINT32_MAX;
|
|
|
|
pool_len = pool_len_;
|
2020-02-05 16:59:11 +00:00
|
|
|
err_stage = err_stage_;
|
2020-01-30 10:11:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
U6A_HOT uint32_t
|
|
|
|
u6a_vm_pool_alloc1(struct u6a_vm_var_fn v1) {
|
|
|
|
struct vm_pool_elem* elem = vm_pool_elem_alloc();
|
|
|
|
if (UNLIKELY(elem == NULL)) {
|
|
|
|
return UINT32_MAX;
|
|
|
|
}
|
2020-02-16 16:36:49 +00:00
|
|
|
elem->values = (struct u6a_vm_var_tuple) { .v1.fn = v1, .v2.ptr = NULL };
|
2020-01-30 10:11:10 +00:00
|
|
|
elem->flags = 0;
|
|
|
|
return elem - active_pool->elems;
|
|
|
|
}
|
|
|
|
|
|
|
|
U6A_HOT uint32_t
|
|
|
|
u6a_vm_pool_alloc2(struct u6a_vm_var_fn v1, struct u6a_vm_var_fn v2) {
|
|
|
|
struct vm_pool_elem* elem = vm_pool_elem_alloc();
|
|
|
|
if (UNLIKELY(elem == NULL)) {
|
|
|
|
return UINT32_MAX;
|
|
|
|
}
|
2020-02-02 17:09:21 +00:00
|
|
|
elem->values = (struct u6a_vm_var_tuple) { .v1.fn = v1, .v2.fn = v2 };
|
|
|
|
elem->flags = 0;
|
2020-01-30 10:11:10 +00:00
|
|
|
return elem - active_pool->elems;
|
|
|
|
}
|
|
|
|
|
|
|
|
U6A_HOT uint32_t
|
|
|
|
u6a_vm_pool_alloc2_ptr(void* v1, void* v2) {
|
|
|
|
struct vm_pool_elem* elem = vm_pool_elem_alloc();
|
|
|
|
if (UNLIKELY(elem == NULL)) {
|
|
|
|
return UINT32_MAX;
|
|
|
|
}
|
2020-02-02 17:09:21 +00:00
|
|
|
elem->values = (struct u6a_vm_var_tuple) { .v1.ptr = v1, .v2.ptr = v2 };
|
|
|
|
elem->flags = POOL_ELEM_HOLDS_PTR;
|
2020-01-30 10:11:10 +00:00
|
|
|
return elem - active_pool->elems;
|
|
|
|
}
|
|
|
|
|
|
|
|
U6A_HOT union u6a_vm_var
|
|
|
|
u6a_vm_pool_get1(uint32_t offset) {
|
2020-02-08 16:51:31 +00:00
|
|
|
return active_pool->elems[offset].values.v1;
|
2020-01-30 10:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U6A_HOT struct u6a_vm_var_tuple
|
|
|
|
u6a_vm_pool_get2(uint32_t offset) {
|
2020-02-08 16:51:31 +00:00
|
|
|
return active_pool->elems[offset].values;
|
2020-01-30 10:11:10 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 18:47:45 +00:00
|
|
|
U6A_HOT struct u6a_vm_var_tuple
|
|
|
|
u6a_vm_pool_get2_separate(uint32_t offset) {
|
|
|
|
struct vm_pool_elem* elem = active_pool->elems + offset;
|
|
|
|
struct u6a_vm_var_tuple values = elem->values;
|
|
|
|
if (elem->refcnt > 1) {
|
|
|
|
// Continuation having more than 1 reference should be separated before reinstatement
|
|
|
|
values.v1.ptr = u6a_vm_stack_dup(values.v1.ptr);
|
|
|
|
}
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
U6A_HOT void
|
|
|
|
u6a_vm_pool_addref(uint32_t offset) {
|
|
|
|
++active_pool->elems[offset].refcnt;
|
|
|
|
}
|
|
|
|
|
2020-01-30 10:11:10 +00:00
|
|
|
U6A_HOT void
|
|
|
|
u6a_vm_pool_free(uint32_t offset) {
|
2020-02-08 16:51:31 +00:00
|
|
|
struct vm_pool_elem* elem = active_pool->elems + offset;
|
2020-01-30 10:11:10 +00:00
|
|
|
fstack_top = UINT32_MAX;
|
|
|
|
do {
|
|
|
|
if (--elem->refcnt == 0) {
|
2020-02-02 17:09:21 +00:00
|
|
|
holes->elems[++holes->pos] = elem;
|
2020-01-30 10:11:10 +00:00
|
|
|
if (elem->flags & POOL_ELEM_HOLDS_PTR) {
|
|
|
|
// Continuation destroyed before used
|
|
|
|
u6a_vm_stack_discard(elem->values.v1.ptr);
|
|
|
|
} else {
|
2020-02-08 16:51:31 +00:00
|
|
|
free_stack_push(elem->values.v2.fn);
|
2020-02-16 16:36:49 +00:00
|
|
|
free_stack_push(elem->values.v1.fn);
|
2020-01-30 10:11:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-02 17:09:21 +00:00
|
|
|
} while ((elem = free_stack_pop()));
|
2020-01-30 10:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
u6a_vm_pool_destroy() {
|
|
|
|
free(active_pool);
|
|
|
|
free(holes);
|
|
|
|
free(fstack);
|
|
|
|
}
|