u6a/src/vm_pool.c

65 lines
2.1 KiB
C
Raw Permalink Normal View History

2020-01-30 10:11:10 +00:00
/*
* vm_pool.c - Unlambda VM object pool
*
* Copyright (C) 2020 CismonX <admin@cismon.net>
*
2020-10-10 19:50:45 +00:00
* This file is part of U6a.
*
* U6a is free software: you can redistribute it and/or modify
2020-01-30 10:11:10 +00:00
* 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.
*
2020-10-10 19:50:45 +00:00
* U6a is distributed in the hope that it will be useful,
2020-01-30 10:11:10 +00:00
* 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
2020-10-10 19:50:45 +00:00
* along with U6a. If not, see <https://www.gnu.org/licenses/>.
2020-01-30 10:11:10 +00:00
*/
2020-06-15 12:22:50 +00:00
#include "vm_pool.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>
bool
2020-06-20 11:32:34 +00:00
u6a_vm_pool_init(struct u6a_vm_pool_ctx* ctx, uint32_t pool_len, uint32_t ins_len, jmp_buf* jmp_ctx, const char* err_stage) {
2020-06-15 12:22:50 +00:00
const uint32_t pool_size = sizeof(struct u6a_vm_pool) + pool_len * sizeof(struct u6a_vm_pool_elem);
ctx->active_pool = malloc(pool_size);
if (UNLIKELY(ctx->active_pool == NULL)) {
u6a_err_bad_alloc(err_stage, pool_size);
2020-01-30 10:11:10 +00:00
return false;
}
2020-06-15 12:22:50 +00:00
const uint32_t holes_size = sizeof(struct u6a_vm_pool_elem_ptrs) + pool_len * sizeof(struct u6a_vm_pool_elem*);
ctx->holes = malloc(holes_size);
if (UNLIKELY(ctx->holes == NULL)) {
u6a_err_bad_alloc(err_stage, holes_size);
free(ctx->active_pool);
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-06-15 12:22:50 +00:00
ctx->fstack = malloc(free_stack_size);
if (UNLIKELY(ctx->fstack == NULL)) {
u6a_err_bad_alloc(err_stage, free_stack_size);
free(ctx->active_pool);
free(ctx->holes);
2020-01-30 10:11:10 +00:00
return false;
}
2020-06-15 12:22:50 +00:00
ctx->active_pool->pos = UINT32_MAX;
ctx->holes->pos = UINT32_MAX;
ctx->pool_len = pool_len;
2020-06-20 11:32:34 +00:00
ctx->jmp_ctx = jmp_ctx;
2020-06-15 12:22:50 +00:00
ctx->err_stage = err_stage;
2020-01-30 10:11:10 +00:00
return true;
}
void
2020-06-15 12:22:50 +00:00
u6a_vm_pool_destroy(struct u6a_vm_pool_ctx* ctx) {
free(ctx->active_pool);
free(ctx->holes);
free(ctx->fstack);
2020-01-30 10:11:10 +00:00
}