2 Commits

Autor SHA1 Mensagem Data
  Ludovic 'Archivist' Lagouardette a83c08aed7 Fixed garbage collection mistakes 1 semana atrás
  Ludovic 'Archivist' Lagouardette d2d8e73e1e Fixed cosmetic incongruities 1 semana atrás
3 arquivos alterados com 75 adições e 20 exclusões
Visão dividida
  1. +33
    -15
      lib.c
  2. +6
    -2
      main.c
  3. +36
    -3
      test/garbage_shenanigans.c

+ 33
- 15
lib.c Ver arquivo

@ -40,12 +40,18 @@
#define MAX_MAIN_SIZE 256
#endif
#ifndef INK_STEP_BATCH_COUNT
#define INK_STEP_BATCH_COUNT 1
#endif
struct label {
int active;
int dest;
char* name;
};
static void noop(void) {}
#ifdef NOSTDLIB
static size_t strlen(const char* c) {
@ -318,7 +324,7 @@ int ink_destroy(struct context* ctx) {
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; ++j) {
for (j = 0; j < t->elements_top; ++j) {
if (t->elements[j].in_use) {
t->collect(ctx, t->elements[j].data);
ctx->free(ctx, t->elements[j].data);
@ -337,8 +343,8 @@ int ink_destroy(struct context* ctx) {
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);
k">if(ctx->routines[i].function_stack) ctx->free(ctx, ctx->routines[i].function_stack);
k">if(ctx->routines[i].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]);
@ -436,19 +442,19 @@ static char* ink_itoa(struct context* _, int cpy) {
#ifndef NOSTDLIB
static void* ink_malloc(struct context* _, size_t sz) {
_=_;
(void)(_);
return malloc(sz);
}
static void* ink_realloc(struct context* _, void* ptr, size_t sz) {
_=_;
(void)(_);
return realloc(ptr, sz);
}
static void ink_free(struct context* _, void* ptr) {
_=_;
(void)(_);
free(ptr);
}
static int ink_putchar(struct context* _, int c) {
n">_=_;
p">(void)(_);
return putchar(c);
}
@ -498,7 +504,7 @@ static int ink_consume_one(int* end, struct context* pContext, char* r, int is_s
return 0;
}
#endif
is_str = is_str;
(void)(is_str);
if(*end == 0) {
return 0;
}
@ -986,7 +992,7 @@ static int ink_parse(struct context* pContext, struct elem* executable_buffer, i
function_buffer_top += 1;
break;
}
next_token: i=i;
next_token: noop();
}
#ifndef NOEXTRACHECKS
if(mode == MODE_FUNCTION || mode == MODE_DO) {
@ -1452,7 +1458,11 @@ void ink_gc(struct context* ctx) {
for (i = 0; i < ctx->types_top; ++i) {
for (j = 0; j < ctx->types[i].elements_top; ++j) {
/* Only mark from things that are active and detected as in use */
if (ctx->types[i].elements[j].in_use && ctx->types[i].elements[j].is_protected && ctx->types[i].elements[j].uses) {
if(ctx->types[i].elements[j].in_use && ctx->types[i].elements[j].is_protected && !ctx->types[i].elements[j].uses) {
ctx->types[i].elements[j].uses = 1;
marked = 1;
}
if (ctx->types[i].elements[j].in_use && ctx->types[i].elements[j].uses) {
struct ink_collection_list c;
c = ctx->types[i].gc(ctx, ctx->types[i].elements[j].data);
for (k = 0; k < c.count; ++k) {
@ -2081,13 +2091,17 @@ static void dump_stack(struct context* ctx) {
if(idx != NULL) ctx->free(ctx, idx);
value = type = idx = NULL;
}
return;
}
static void collect_noop() {}
static void collect_noop(struct context* ctx, void* array) {
(void)(ctx);
(void)(array);
}
static struct ink_collection_list gc_noop() {
struct ink_collection_list c;
static struct ink_collection_list gc_noop(struct context* ctx, void* array) {
struct ink_collection_list c;
(void)(ctx);
(void)(array);
c.elements = NULL;
c.count = 0;
return c;
@ -2235,7 +2249,7 @@ static void push_delimited_array(struct context* ctx) {
/* Don't copy the delimiter */
idx -= 1;
ary->elements = malloc(sizeof(struct elem) * idx);
ary->elements = ctx->malloc(ctx, sizeof(struct elem) * idx);
#ifndef NOEXTRACHECKS
if(ary->elements == NULL) {
currentRoutine->panic = -541;
@ -2507,3 +2521,7 @@ int ink_std_library(struct context* ctx) {
return v;
}
#ifdef INK_DUMMY_MAIN
int main() {}
#endif

+ 6
- 2
main.c Ver arquivo

@ -2,8 +2,12 @@
#include <stdio.h>
#include <time.h>
#ifndef INK_SH_READ_BUFF
#define INK_SH_READ_BUFF 32768
#endif // INK_SH_READ_BUFF
int main(int argc, char** argv) {
char read_buffer[2048];
char read_buffer[n">INK_SH_READ_BUFF];
struct timespec start_time, end_time;
clock_t begin, end;
double time_spent;
@ -16,7 +20,7 @@ int main(int argc, char** argv) {
FILE* file;
size_t cnt;
file = fopen(*argv, "r");
cnt = fread(read_buffer, 1, mi">2047, file);
cnt = fread(read_buffer, 1, n">INK_SH_READ_BUFF - 1, file);
if(cnt == 0) {
fprintf(stderr, "Can't read file !! -> %s\n", *argv);
}

+ 36
- 3
test/garbage_shenanigans.c Ver arquivo

@ -1,9 +1,41 @@
#include "ink.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
size_t allocated = 0;
size_t deallocated = 0;
static void* ink_malloc(struct context* _, size_t sz) {
(void)(_);
allocated+=sz;
void* ptr = malloc(sz + sizeof(size_t));
*(size_t*)ptr = sz;
return (size_t*)ptr + 1;
}
static void ink_free(struct context* _, void* ptr) {
(void)(_);
deallocated += *((size_t*)ptr-1);
free(((size_t*)ptr)-1);
}
static void* ink_realloc(struct context* _, void* ptr, size_t sz) {
(void)(_);
char* new_ptr = ink_malloc(_, sz);
size_t old_sz = *(-1 + (size_t*)ptr);
memcpy(new_ptr, ptr, old_sz);
ink_free(_, ptr);
return new_ptr;
}
static int ink_putchar(struct context* _, int c) {
(void)(_);
return putchar(c);
}
int main(int argc, char** argv) {
struct context* ctx;
ctx = ink_make_default_context();
ctx = ink_make_context(ink_malloc, ink_realloc, ink_free, ink_putchar);
ink_std_library(ctx);
ink_compile(
ctx,
"fn encrypt do\n"
@ -118,6 +150,7 @@ int main(int argc, char** argv) {
counter += increment;
}
}
ink_destroy(ctx);
assert(allocated == deallocated);
//return ctx->routines[coro].panic != INK_ROUTINE_SUCCESS && ctx->routines[coro].panic != INK_ROUTINE_CAN_REUSE;
}

Carregando…
Cancelar
Salvar