buffer fixes

This commit is contained in:
ProgramSnail 2024-11-12 02:12:28 +03:00
parent 959c06cc65
commit f5c7843942
3 changed files with 24 additions and 15 deletions

View file

@ -7,6 +7,8 @@
#include "stdlib.h" #include "stdlib.h"
extern size_t __gc_stack_top, __gc_stack_bottom;
static inline void **s_top(struct State *s) { static inline void **s_top(struct State *s) {
return s->stack + STACK_SIZE - s->bf->global_area_size; return s->stack + STACK_SIZE - s->bf->global_area_size;
} }
@ -54,7 +56,8 @@ static inline void s_push(struct State *s, void *val) {
#endif #endif
--s->sp; --s->sp;
*s->sp = val; *s->sp = val;
// __gc_stack_top= (size_t)(s->sp - 1); __gc_stack_top = (size_t)(s->sp);
__gc_stack_top -= __gc_stack_top & 0xF;
} }
static inline void s_push_i(struct State *s, aint val) { static inline void s_push_i(struct State *s, aint val) {
@ -82,7 +85,8 @@ static inline void *s_pop(struct State *s) {
void *value = *s->sp; void *value = *s->sp;
*s->sp = NULL; *s->sp = NULL;
++s->sp; ++s->sp;
// __gc_stack_top = (size_t)(s->sp - 1); __gc_stack_top = (size_t)(s->sp);
__gc_stack_top -= __gc_stack_top & 0xF;
return value; return value;
} }
@ -226,7 +230,7 @@ static inline void **var_by_category(struct State *s, enum VarCategory category,
if (s->fp->closure == NULL) { if (s->fp->closure == NULL) {
s_failure(s, "can't read closure parameter not in closure"); s_failure(s, "can't read closure parameter not in closure");
} }
if (!UNBOXED(s->fp->closure)) { // TODO: check ?? if (UNBOXED(s->fp->closure)) {
s_failure(s, "not boxed value expected in closure index"); s_failure(s, "not boxed value expected in closure index");
} }
data *d = TO_DATA(s->fp->closure); data *d = TO_DATA(s->fp->closure);

View file

@ -15,7 +15,7 @@
// CLOJURE_T = CLOSURE_TAG, // CLOJURE_T = CLOSURE_TAG,
// }; // };
#define STACK_SIZE 100000 #define STACK_SIZE 128 * 1024
static const size_t MAX_ARRAY_SIZE = 0x11111110; static const size_t MAX_ARRAY_SIZE = 0x11111110;

View file

@ -19,8 +19,10 @@ char *ip_read_string(char **ip, bytefile *bf) {
return get_string(bf, ip_read_int(ip)); return get_string(bf, ip_read_int(ip));
} }
const size_t BUFFER_SIZE = 1000;
void run(bytefile *bf, int argc, char **argv) { void run(bytefile *bf, int argc, char **argv) {
void *buffer[10000]; // FIXME: TODO: TMP void *buffer[BUFFER_SIZE];
struct State s; struct State s;
init_state(bf, &s); init_state(bf, &s);
@ -118,20 +120,22 @@ void run(bytefile *bf, int argc, char **argv) {
if (args_count < 0) { if (args_count < 0) {
s_failure(&s, "args count should be >= 0"); s_failure(&s, "args count should be >= 0");
} }
// void **buffer = calloc(args_count + 1, sizeof(void *)); void **opr_buffer = args_count >= BUFFER_SIZE ? calloc(args_count + 1, sizeof(void *)) : buffer;
for (size_t i = 1; i <= args_count; ++i) { for (size_t i = 1; i <= args_count; ++i) {
buffer[args_count - i] = s_pop(&s); opr_buffer[args_count - i] = s_pop(&s);
} }
buffer[args_count] = (void *)LtagHash((char *)name); opr_buffer[args_count] = (void *)LtagHash((char *)name);
void *sexp = Bsexp((aint *)buffer, BOX(args_count + 1)); void *sexp = Bsexp((aint *)opr_buffer, BOX(args_count + 1));
push_extra_root(sexp); push_extra_root(sexp);
s_push(&s, sexp); s_push(&s, sexp);
pop_extra_root(sexp); pop_extra_root(sexp);
// free(buffer); if (args_count >= BUFFER_SIZE) {
free(opr_buffer);
}
break; break;
} }
@ -426,17 +430,18 @@ void run(bytefile *bf, int argc, char **argv) {
s_failure(&s, "elements count should be >= 0"); s_failure(&s, "elements count should be >= 0");
} }
// void **buffer = calloc(elem_count, sizeof(void *)); void **opr_buffer = elem_count > BUFFER_SIZE ? calloc(elem_count, sizeof(void *)) : buffer;
for (size_t i = 0; i < elem_count; ++i) { for (size_t i = 0; i < elem_count; ++i) {
buffer[elem_count - i - 1] = s_pop(&s); opr_buffer[elem_count - i - 1] = s_pop(&s);
} }
void *array = void *array =
Barray((aint *)buffer, BOX(elem_count)); // NOTE: not shure if elems should be added Barray((aint *)opr_buffer, BOX(elem_count)); // NOTE: not shure if elems should be added
// s_popn(&s, elem_count);
s_push(&s, array); s_push(&s, array);
// free(buffer); if (elem_count > BUFFER_SIZE) {
free(opr_buffer);
}
break; break;
} }