1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 02:59:54 +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
@ -54,4 +56,4 @@ Module.symvers
Mkfile.old
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
CFLAGS=-std=c99 -Wall -Wextra -m32 -g
CFLAGS=-std=c99 -Wall -Wextra -g
TARGET=interp
TARGET=./interp
PREFIX=/usr/local
# C sources
@ -30,4 +30,7 @@ run: $(TARGET)
debug: $(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 "vstring.h"
void BufferInit(GC * gc, Buffer * buffer, uint32_t capacity) {
uint8_t * data;
data = GCAlloc(gc, sizeof(uint8_t) * capacity);
Buffer * BufferNew(GC * gc, uint32_t capacity) {
Buffer * buffer = GCAlloc(gc, sizeof(Buffer));
uint8_t * data = GCAlloc(gc, sizeof(uint8_t) * capacity);
buffer->data = data;
buffer->count = 0;
buffer->capacity = capacity;
}
Buffer * BufferNew(GC * gc, uint32_t capacity) {
Buffer * buffer;
buffer = GCAlloc(gc, sizeof(Buffer));
BufferInit(gc, buffer, capacity);
return buffer;
}

View File

@ -3,8 +3,6 @@
#include "datatypes.h"
void BufferInit(GC * gc, Buffer * buffer, uint32_t capacity);
Buffer * BufferNew(GC * gc, 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;
valIndex.type = TYPE_NUMBER;
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);
}
return literalIndex;
@ -1154,6 +1155,7 @@ Func * CompilerCompile(Compiler * c, Value form) {
Func * func = GCAlloc(gc, sizeof(Func));
env->values = GCAlloc(gc, sizeof(Value) * envSize);
memcpy(env->values, c->env->data, envSize * sizeof(Value));
env->stackOffset = envSize;
env->thread = NULL;
func->parent = NULL;
func->def = def;

2
main.c
View File

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

View File

@ -264,7 +264,14 @@ uint32_t ValueHash(Value * x) {
case TYPE_FUNCENV:
case TYPE_THREAD:
/* 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;
}
return hash;

2
vm.c
View File

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