1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-24 09:17:17 +00:00

Fix some memory leaks and buffer overrun bugs after profiling

debugRepl with valgrind.
This commit is contained in:
Calvin Rose 2017-02-09 15:56:45 -05:00
parent 7bc5233a7d
commit 3d7e574e05
8 changed files with 24 additions and 20 deletions

4
.gitignore vendored
View File

@ -1,3 +1,5 @@
# Target
./interp
# Created by https://www.gitignore.io/api/c # Created by https://www.gitignore.io/api/c
@ -54,4 +56,4 @@ Module.symvers
Mkfile.old Mkfile.old
dkms.conf dkms.conf
# End of https://www.gitignore.io/api/c # End of https://www.gitignore.io/api/c

View File

@ -1,8 +1,8 @@
# TIL # TIL
CFLAGS=-std=c99 -Wall -Wextra -m32 -g CFLAGS=-std=c99 -Wall -Wextra -g
TARGET=interp TARGET=./interp
PREFIX=/usr/local PREFIX=/usr/local
# C sources # C sources
@ -30,4 +30,7 @@ run: $(TARGET)
debug: $(TARGET) debug: $(TARGET)
gdb $(TARGET) gdb $(TARGET)
.PHONY: clean install run debug valgrind: $(TARGET)
valgrind $(TARGET)
.PHONY: clean install run debug valgrind

View File

@ -4,18 +4,12 @@
#include "value.h" #include "value.h"
#include "vstring.h" #include "vstring.h"
void BufferInit(GC * gc, Buffer * buffer, uint32_t capacity) { Buffer * BufferNew(GC * gc, uint32_t capacity) {
uint8_t * data; Buffer * buffer = GCAlloc(gc, sizeof(Buffer));
data = GCAlloc(gc, sizeof(uint8_t) * capacity); uint8_t * data = GCAlloc(gc, sizeof(uint8_t) * capacity);
buffer->data = data; buffer->data = data;
buffer->count = 0; buffer->count = 0;
buffer->capacity = capacity; buffer->capacity = capacity;
}
Buffer * BufferNew(GC * gc, uint32_t capacity) {
Buffer * buffer;
buffer = GCAlloc(gc, sizeof(Buffer));
BufferInit(gc, buffer, capacity);
return buffer; return buffer;
} }

View File

@ -3,8 +3,6 @@
#include "datatypes.h" #include "datatypes.h"
void BufferInit(GC * gc, Buffer * buffer, uint32_t capacity);
Buffer * BufferNew(GC * gc, uint32_t capacity); Buffer * BufferNew(GC * gc, uint32_t capacity);
void BufferEnsure(GC * gc, Buffer * buffer, uint32_t capacity); void BufferEnsure(GC * gc, Buffer * buffer, uint32_t capacity);

View File

@ -310,7 +310,8 @@ static uint16_t CompilerAddLiteral(Compiler * c, Scope * scope, Value x) {
Value valIndex; Value valIndex;
valIndex.type = TYPE_NUMBER; valIndex.type = TYPE_NUMBER;
literalIndex = scope->literalsArray->count; literalIndex = scope->literalsArray->count;
DictPut(gc, scope->literals, &valIndex, &x); valIndex.data.number = literalIndex;
DictPut(gc, scope->literals, &x, &valIndex);
ArrayPush(gc, scope->literalsArray, x); ArrayPush(gc, scope->literalsArray, x);
} }
return literalIndex; return literalIndex;
@ -1154,6 +1155,7 @@ Func * CompilerCompile(Compiler * c, Value form) {
Func * func = GCAlloc(gc, sizeof(Func)); Func * func = GCAlloc(gc, sizeof(Func));
env->values = GCAlloc(gc, sizeof(Value) * envSize); env->values = GCAlloc(gc, sizeof(Value) * envSize);
memcpy(env->values, c->env->data, envSize * sizeof(Value)); memcpy(env->values, c->env->data, envSize * sizeof(Value));
env->stackOffset = envSize;
env->thread = NULL; env->thread = NULL;
func->parent = NULL; func->parent = NULL;
func->def = def; func->def = def;

2
main.c
View File

@ -65,8 +65,6 @@ void debugRepl() {
reader = buffer; reader = buffer;
buffer[0] = 0; buffer[0] = 0;
continue; continue;
} else {
printf("Compiled!\n");
} }
/* Execute function */ /* Execute function */

View File

@ -264,7 +264,14 @@ uint32_t ValueHash(Value * x) {
case TYPE_FUNCENV: case TYPE_FUNCENV:
case TYPE_THREAD: case TYPE_THREAD:
/* Cast the pointer */ /* Cast the pointer */
hash = (uint32_t) x->data.string; {
union {
void * pointer;
uint32_t hash;
} u;
u.pointer = x->data.pointer;
hash = u.hash;
}
break; break;
} }
return hash; return hash;

2
vm.c
View File

@ -576,7 +576,7 @@ int VMStart(VM * vm) {
/* Initialize the VM */ /* Initialize the VM */
void VMInit(VM * vm) { void VMInit(VM * vm) {
GCInit(&vm->gc, 0); GCInit(&vm->gc, 100000000);
vm->gc.handleOutOfMemory = VMHandleOutOfMemory; vm->gc.handleOutOfMemory = VMHandleOutOfMemory;
vm->tempRoot.type = TYPE_NIL; vm->tempRoot.type = TYPE_NIL;
vm->base = NULL; vm->base = NULL;