Explorar el Código

Added context clearing

main
Ludovic 'Archivist' Lagouardette hace 4 meses
padre
commit
1e90035873
Se han modificado 4 ficheros con 51 adiciones y 1 borrados
  1. +2
    -0
      CMakeLists.txt
  2. +7
    -0
      include/ink.h
  3. +41
    -0
      lib.c
  4. +1
    -1
      main.c

+ 2
- 0
CMakeLists.txt Ver fichero

@ -53,3 +53,5 @@ endif()
# Benchmark is broken since the addition to coroutines
# add_executable(ink_bench bench.c)
# target_link_libraries(ink_bench PUBLIC ink)
# Valgrind testing should run `./test/test01.nk ./test/test03.nk ./test/test06.nk` as arguments

+ 7
- 0
include/ink.h Ver fichero

@ -251,6 +251,13 @@ void ink_make_context_inplace(struct context* location, void*(*malloc)(struct co
struct context* ink_make_default_context(void);
#endif
/**
* Destroys a context and frees the memory from it
* @param ctx The context to destroy
* @return the panic value of the context
*/
int ink_destroy(struct context* ctx);
/**
* Steps the current routine by one execution step
* @param pContext The context of the routine to advance

+ 41
- 0
lib.c Ver fichero

@ -307,6 +307,47 @@ void ink_pop(struct context* ctx) {
ctx->routines[ctx->routine_current].top--;
}
int ink_destroy(struct context* ctx) {
int panic = ctx->panic;
int i, j;
for (i = 0; i < ctx->types_top; ++i) {
struct ink_type* t = ctx->types + i;
if (t->element_size > 0 && t->elements_top > 0) {
for (j = 0; i < t->element_size; ++i) {
if (t->elements[j].in_use) {
t->collect(ctx, t->elements[j].data);
ctx->free(ctx, t->elements[j].data);
}
}
ctx->free(ctx, t->elements);
}
/* Type names are not freed, for they are not copied */
/* ctx->inner_free(ctx, t->name); */
}
for (i = 0; i < ctx->words_top; ++i) {
ctx->inner_free(ctx, ctx->words[i].name);
ctx->free(ctx, ctx->words[i].things);
}
for (i = 0; i < ctx->native_words_top; ++i) {
ctx->inner_free(ctx, ctx->native_words[i].name);
}
for (i = 0; i < ctx->routines_top; ++i) {
ctx->free(ctx, ctx->routines[i].function_stack);
ctx->free(ctx, ctx->routines[i].stack);
}
for (i = 0; i < ctx->lex_reserved_words_top; ++i) {
ctx->free(ctx, ctx->lex_reserved_words[i]);
}
ctx->inner_free(ctx, ctx->lex_reserved_words);
ctx->inner_free(ctx, ctx->native_words);
ctx->inner_free(ctx, ctx->routines);
ctx->free(ctx, ctx->words);
ctx->inner_free(ctx, ctx->types);
ctx->inner_free(ctx, ctx);
return panic;
}
struct context* ink_make_context(void*(*malloc)(struct context*, size_t), void*(*realloc)(struct context*, void*, size_t), void(*free)(struct context*, void*), int(*putchar)(struct context*, int)) {
struct context* ctx;
ctx = (struct context*)malloc(NULL, sizeof(struct context));

+ 1
- 1
main.c Ver fichero

@ -52,5 +52,5 @@ int main(int argc, char** argv) {
s_total += (end_time.tv_sec - start_time.tv_sec) + (end_time.tv_nsec - start_time.tv_nsec) / 1.0e9;
s_total = ctx->steps / s_total;
printf("\nExecuted in %u steps\nCollected %u times\nExecution freq: %lfHz\n", ctx->steps, ctx->collections, s_total);
return ">ctx->panic;
return f">ink_destroy(ctx);
}

Cargando…
Cancelar
Guardar