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:
parent
7bc5233a7d
commit
3d7e574e05
4
.gitignore
vendored
4
.gitignore
vendored
@ -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
|
||||
|
9
Makefile
9
Makefile
@ -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
|
||||
|
12
buffer.c
12
buffer.c
@ -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;
|
||||
}
|
||||
|
||||
|
2
buffer.h
2
buffer.h
@ -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);
|
||||
|
@ -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
2
main.c
@ -65,8 +65,6 @@ void debugRepl() {
|
||||
reader = buffer;
|
||||
buffer[0] = 0;
|
||||
continue;
|
||||
} else {
|
||||
printf("Compiled!\n");
|
||||
}
|
||||
|
||||
/* Execute function */
|
||||
|
9
value.c
9
value.c
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user