Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Moved strings on-heap and macroified symbol types

Moved strings (char *) on heap as payload of U8VEC to close memory
leak.

Macroified symbol types and made symbol printer function to enable
easier scaning/printing of heap.
  • Loading branch information...
commit 5cd00f77036ece87f178bc15a777b3c4b369abf7 1 parent 60f37c1
@jbf authored
View
6 src/ast-walker/bootstrap.c
@@ -21,9 +21,9 @@ static symbol_entry_t nil_s = {(unsigned char *)"NIL", &nil_c};
static symbol_entry_t false_s = {(unsigned char *)"FALSE", &false_c};
static symbol_entry_t t_s = {(unsigned char *)"T", &t_c};
-static cell_t nil_c = {{.type = PAYLOAD_NIL}, {.symbol = &nil_s}};
-static cell_t false_c = {{.type = PAYLOAD_SYMBOL}, {.symbol = &false_s}};
-static cell_t t_c = {{.type = PAYLOAD_SYMBOL}, {.symbol = &t_s}};
+static cell_t nil_c = {{.type = SYMBOL}, {.symbol = &nil_s}};
+static cell_t false_c = {{.type = SYMBOL}, {.symbol = &false_s}};
+static cell_t t_c = {{.type = SYMBOL}, {.symbol = &t_s}};
extern jmp_buf __jmp_env;
extern cell_t *orig_sexpr;
View
16 src/ast-walker/cell.c
@@ -1,8 +1,9 @@
+#include <stdio.h>
+
#include "cell.h"
#include "function.h"
#include "primitives.h"
-
-#include <stdio.h>
+#include "util.h"
void pp_list(cell_t *cell);
void pp(cell_t *cell);
@@ -78,3 +79,14 @@ void pp_list(cell_t *cell) {
}
}
}
+
+const char *object_type_str(cell_t *cell) {
+#define DECLARE_OBJECT_TYPE(x, y) if ( x ## P (cell)) { return QUOTEME(x) ; } else
+#include "heap_objects_declare.h"
+#undef DECLARE_OBJECT_TYPE
+ { /* this continues the last else from macro expansion */
+ return "Unknwn object";
+ }
+
+ return "Unknown object";
+}
View
43 src/ast-walker/cell.h
@@ -5,35 +5,17 @@
#include <stdint.h>
-/*
- * binary position:
- * 9876543210
- * symbol payloads:
- *
- * cons cell xxxxxxxxx0 both car and cdr point to other cells
- * symbols 0000000011
- * nil-symbol 0000000111 only symbols have bit 1 (zero-indexed) set
- * string 0000000101
- * number 0000001101
- * PRIMITIVE 0000010101
- * FUNCTION 0000011101
- * u8[] 0000100101 tag word for u8[] alloced on heap
- */
-
-typedef enum {
- PAYLOAD_SYMBOL = 3,
- PAYLOAD_NIL = 7,
- PAYLOAD_STRING = 5,
- PAYLOAD_NUMBER = 13,
- PRIMITIVE = 21,
- FUNCTION = 29,
- U8VEC = 37,
-} cell_type_t;
-
struct environ_t;
struct function_t;
struct primitive_t;
+/* cell type enum */
+#define DECLARE_OBJECT_TYPE(x, y) x = y,
+typedef enum {
+#include "heap_objects_declare.h"
+} cell_type_t;
+#undef DECLARE_OBJECT_TYPE
+
typedef struct cell_t {
union {
cell_type_t type;
@@ -61,12 +43,14 @@ typedef struct u8_cell_t {
#define ATOMP(c) (((c)->slot1.type & 1) == 1)
extern cell_t *nil_cell;
#define NILP(c) ((c) == nil_cell)
-#define SYMBOLP(c) (((c)->slot1.type & 2) == 2)
-#define NUMBERP(c) ((c)->slot1.type == PAYLOAD_NUMBER)
-#define STRINGP(c) ((c)->slot1.type == PAYLOAD_STRING)
+
+/* declare content predicates */
+#define SYMBOLP(c) ((c)->slot1.type == SYMBOL)
+#define NUMBERP(c) ((c)->slot1.type == NUMBER)
+#define STRINGP(c) ((c)->slot1.type == STRING)
#define PRIMITIVEP(c) ((c)->slot1.type == PRIMITIVE)
#define FUNCTIONP(c) ((c)->slot1.type == FUNCTION)
-#define U8VECP(c) ((c)->-slot1.type == U8VEC)
+#define U8VECP(c) ((c)->slot1.type == U8VEC)
/* Accessors. */
#define CAR(c) ((c)->slot1.car)
@@ -91,4 +75,5 @@ void print_cell(cell_t *cell);
void pretty_print(cell_t *cell);
void pp(cell_t *cell);
+const char *object_type_str(cell_t *cell);
#endif /* _CELL_H */
View
4 src/ast-walker/environment.c
@@ -49,8 +49,8 @@ int add_to_environment(environ_t *env, cell_t *symbol, cell_t *value) {
ok = alloc_sym_value_pair(&sym, &val);
if (!ok) {
- free(sym);
- free(val);
+ free_malloced(sym);
+ free_malloced(val);
return EOOM;
}
View
20 src/ast-walker/heap_objects_declare.h
@@ -0,0 +1,20 @@
+/*
+ * binary position:
+ * 9876543210
+ * symbol payloads:
+ *
+ * cons cell xxxxxxxxx0 both car and cdr point to other cells
+ * symbols 0000000011 only symbols have bit 1 (zero-indexed) set
+ * string 0000000101
+ * number 0000001101
+ * PRIMITIVE 0000010101
+ * FUNCTION 0000011101
+ * u8[] 0000100101 tag word for u8[] alloced on heap
+ */
+
+DECLARE_OBJECT_TYPE(SYMBOL, 3)
+DECLARE_OBJECT_TYPE(STRING, 5)
+DECLARE_OBJECT_TYPE(NUMBER, 13)
+DECLARE_OBJECT_TYPE(PRIMITIVE, 21)
+DECLARE_OBJECT_TYPE(FUNCTION, 29)
+DECLARE_OBJECT_TYPE(U8VEC, 37)
View
4 src/ast-walker/liveness.c
@@ -59,10 +59,10 @@ void push_liveness(frame_t **top, frame_t *new_top) {
void pop_liveness(frame_t **top) {
frame_t *t;
- free((*top)->live_cells);
+ free_malloced((*top)->live_cells);
t = *top;
*top = (*top)->next_frame;
- free(t);
+ free_malloced(t);
}
void dealloc_frames(frame_t **top) {
View
21 src/ast-walker/memory.c
@@ -1,5 +1,3 @@
-#include "memory.h"
-
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
@@ -9,6 +7,8 @@
#include <stdlib.h>
#include <string.h>
+#include "memory.h"
+
#include "util.h"
#ifdef LIVENESS_DEBUG
@@ -152,3 +152,20 @@ void destroy_mem_sys__safe() {
perror(NULL);
}
}
+
+void scan_heap() {
+ char *start, *base, *end;
+
+ start = (char *)mem_sys_heap;
+ end = start + mem_sys_heap_size;
+
+ for (base = start; base < end; ) {
+ cell_t *obj = (cell_t *)base;
+
+ if (U8VECP(obj)) {
+ base = base + sizeof(cell_t) + U8LEN(obj);
+ } else {
+ base += sizeof(cell_t);
+ }
+ }
+}
View
18 src/ast-walker/primitives.c
@@ -114,7 +114,7 @@ int proper_list_length(cell_t *lst, int target_length) {
* Do not use on symbol.
*/
int list_of(cell_type_t type, cell_t *lst) {
- assert(type != PAYLOAD_SYMBOL);
+ assert(type != SYMBOL);
if (NULL == lst) return -1;
for (; lst != NULL && PAIRP(lst); lst = CDR(lst)) {
@@ -211,7 +211,7 @@ cell_t *prim_length(cell_t *rest, environ_t *env) {
length = proper_list_length(tmp, 0);
if (length >= 0) {
tmp = new(cell_t);
- tmp->slot1.type = PAYLOAD_NUMBER;
+ tmp->slot1.type = NUMBER;
tmp->slot2.i_val = length;
return tmp;
} else {
@@ -222,7 +222,7 @@ cell_t *prim_length(cell_t *rest, environ_t *env) {
cell_t *prim_plus(cell_t *rest, environ_t *env) {
cell_t *args = evargs(rest, env);
- int ok = list_of(PAYLOAD_NUMBER, args);
+ int ok = list_of(NUMBER, args);
if (ok < 1) {
return NULL; /* ERROR */
@@ -234,7 +234,7 @@ cell_t *prim_plus(cell_t *rest, environ_t *env) {
tmp += I_VAL(CAR(args));
}
- rc->slot1.type = PAYLOAD_NUMBER;
+ rc->slot1.type = NUMBER;
rc->slot2.i_val = tmp;
return rc;
}
@@ -242,7 +242,7 @@ cell_t *prim_plus(cell_t *rest, environ_t *env) {
cell_t *prim_mul(cell_t *rest, environ_t *env) {
cell_t *args = evargs(rest, env);
- int ok = list_of(PAYLOAD_NUMBER, args);
+ int ok = list_of(NUMBER, args);
if (ok < 1) {
return NULL; /* ERROR */
@@ -254,7 +254,7 @@ cell_t *prim_mul(cell_t *rest, environ_t *env) {
tmp *= I_VAL(CAR(args));
}
- rc->slot1.type = PAYLOAD_NUMBER;
+ rc->slot1.type = NUMBER;
rc->slot2.i_val = tmp;
return rc;
}
@@ -262,7 +262,7 @@ cell_t *prim_mul(cell_t *rest, environ_t *env) {
cell_t *prim_number_equals(cell_t *rest, environ_t *env) {
cell_t *args = evargs(rest, env);
- int ok = list_of(PAYLOAD_NUMBER, args);
+ int ok = list_of(NUMBER, args);
if (ok < 1) {
return NULL; /* ERROR */
@@ -281,7 +281,7 @@ cell_t *prim_number_equals(cell_t *rest, environ_t *env) {
cell_t *prim_minus(cell_t *rest, environ_t *env) {
cell_t *args = evargs(rest, env);
- int ok = list_of(PAYLOAD_NUMBER, args);
+ int ok = list_of(NUMBER, args);
if (ok < 1) {
return NULL; /* ERROR */
@@ -293,7 +293,7 @@ cell_t *prim_minus(cell_t *rest, environ_t *env) {
tmp -= I_VAL(CAR(args));
}
- rc->slot1.type = PAYLOAD_NUMBER;
+ rc->slot1.type = NUMBER;
rc->slot2.i_val = tmp;
return rc;
}
View
12 src/ast-walker/reader.c
@@ -41,7 +41,7 @@ cell_t *read_intern(STREAM *stream, symbol_table *symbol_table) {
if (NULL == cell) {
return NULL;
}
- cell->slot1.type = PAYLOAD_NUMBER;
+ cell->slot1.type = NUMBER;
cell->slot2.i_val = tok.i_val;
return cell;
case TOKEN_STRING:
@@ -53,9 +53,9 @@ cell_t *read_intern(STREAM *stream, symbol_table *symbol_table) {
slen = strlen((char *)tok.string_val)+1; /* Add '\0' sigh */
string = u8_new(slen);
strncpy((char*)U8DATA(string), (char*)tok.string_val, slen);
- cell->slot1.type = PAYLOAD_STRING;
+ cell->slot1.type = STRING;
cell->slot2.string = (cell_t *)string;
- free(tok.string_val);
+ free_token_payload(&tok);
return cell;
default:
return NULL;
@@ -137,7 +137,7 @@ cell_t *read_list_intern(STREAM *stream, symbol_table *symbol_table) {
return NULL;
}
current->slot1.car = temp;
- temp->slot1.type = PAYLOAD_NUMBER;
+ temp->slot1.type = NUMBER;
temp->slot2.i_val = tok.i_val;
break;
case TOKEN_STRING:
@@ -153,9 +153,9 @@ cell_t *read_list_intern(STREAM *stream, symbol_table *symbol_table) {
string = u8_new(slen);
strncpy((char*)U8DATA(string), (char*)tok.string_val, slen);
current->slot1.car = temp;
- temp->slot1.type = PAYLOAD_STRING;
+ temp->slot1.type = STRING;
temp->slot2.string = (cell_t *)string;
- free(tok.string_val);
+ free_token_payload(&tok);
break;
default:
if (first) { // if first, this is the second time in the loop,
View
8 src/ast-walker/symbol.c
@@ -42,13 +42,13 @@ cell_t *intern(unsigned char *sym, symbol_table *tab) {
new_cell = new(cell_t); // nothing extra live at call to new
if (NULL == new_tab || NULL == new_sym || NULL == new_cell) {
- free(new_tab);
- free(new_sym);
+ free_malloced(new_tab);
+ free_malloced(new_sym);
/* new_cell is on-heap, gc:d */
return 0; /* Out of memory. */
}
- new_cell->slot1.type = PAYLOAD_SYMBOL;
+ new_cell->slot1.type = SYMBOL;
new_cell->slot2.symbol = new_sym;
new_sym->symbol_name = sym; /* Keep old string. */
@@ -61,7 +61,7 @@ cell_t *intern(unsigned char *sym, symbol_table *tab) {
return new_cell;
} else {
- free(sym);
+ free_malloced(sym);
return existing;
}
}
View
5 src/ast-walker/token.c
@@ -8,6 +8,7 @@
#include <stdio.h>
#include "errors.h"
+#include "memory.h"
int read_plus_minus(STREAM *stream, int first, int index, unsigned char token_string[], token_t *tok);
int readstring(STREAM *stream, int index, unsigned char token_string[], token_t *tok);
@@ -23,7 +24,7 @@ int isstop_pushback(STREAM *stream, int c);
void free_token_payload(token_t *tok) {
if (tok->type == TOKEN_SYMBOL ||
tok->type == TOKEN_STRING) {
- free(tok->payload);
+ free_malloced(tok->payload);
}
}
@@ -133,7 +134,7 @@ int read_plus_minus(STREAM *stream,
if(isstop_pushback(stream, next) ||
next == EOF) {
unsigned char *str;
- str = malloc(2);
+ str = malloc_or_bail(2);
if (!str) {
return EOOM;
Please sign in to comment.
Something went wrong with that request. Please try again.