summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apl9.h6
-rw-r--r--eval.c4
-rw-r--r--lexer.c2
-rw-r--r--main.c9
-rw-r--r--memory.c28
-rw-r--r--print.c97
-rw-r--r--symbol.c4
7 files changed, 110 insertions, 40 deletions
diff --git a/apl9.h b/apl9.h
index 3317e3b..8eb570f 100644
--- a/apl9.h
+++ b/apl9.h
@@ -156,6 +156,9 @@ void freesymtab(Symtab *);
vlong globalIO(void);
/* memory.c */
+void *emalloc(ulong);
+void checkmem(char *);
+
Array *allocarray(int, int, int);
void freearray(Array *);
void incref(Array *);
@@ -193,6 +196,7 @@ Array *opOver(Datum *, Datum *, Array *, Array *);
/* Global variables */
extern int traceeval; /* eval.c */
+extern int debugmem; /* memory.c */
extern Rune *errormsg; /* eval.c */
extern int datasizes[]; /* array.c */
extern Rune primfuncnames[]; /* functions.c */
@@ -205,4 +209,4 @@ extern opmonad monadoperatordefs[]; /* operators.c */
extern opdyad dyadoperatordefs[]; /* operators.c */
extern Symtab *globalsymtab; /* symbol.c */
extern Symtab *currentsymtab; /* symbol.c */
-extern int alloccounts; /* memory.c */ \ No newline at end of file
+extern int alloccounts; /* memory.c */
diff --git a/eval.c b/eval.c
index b3efc05..6b12245 100644
--- a/eval.c
+++ b/eval.c
@@ -233,7 +233,7 @@ Datum
monadop(Datum left, Datum right)
{
traceprint("Applying left argument to operator\n");
- Datum *arg = malloc(sizeof(Datum));
+ Datum *arg = emalloc(sizeof(Datum));
*arg = left;
Datum result;
@@ -252,7 +252,7 @@ Datum
dyadop(Datum left, Datum right)
{
traceprint("Applying right argument to operator\n");
- Datum *arg = malloc(sizeof(Datum));
+ Datum *arg = emalloc(sizeof(Datum));
*arg = right;
Datum result;
diff --git a/lexer.c b/lexer.c
index bf2aaa4..4c9e8f2 100644
--- a/lexer.c
+++ b/lexer.c
@@ -11,7 +11,7 @@ lexline(Rune *line)
{
int offset = 0;
int len = runestrlen(line);
- Statement *stmt = malloc(sizeof(Statement));
+ Statement *stmt = emalloc(sizeof(Statement));
stmt->ntoks = 0;
stmt->toks = mallocz(sizeof(Datum) * MAX_LINE_TOKENS, 1);
stmt->next = nil;
diff --git a/main.c b/main.c
index ad84ad3..82c44f5 100644
--- a/main.c
+++ b/main.c
@@ -1,6 +1,7 @@
#include <u.h>
#include <libc.h>
#include <bio.h>
+#include <pool.h>
#include "apl9.h"
@@ -17,14 +18,20 @@ main(int argc, char *argv[])
globalsymtab = newsymtab();
currentsymtab = globalsymtab;
traceeval = 0;
+ debugmem = 0;
ARGBEGIN{
case 't':
traceeval = 1;
break;
+ case 'm':
+ debugmem = 1;
+ mainmem->flags |= POOL_NOREUSE;
+ break;
}ARGEND
while(!off){
+ checkmem("main loop");
Rune *input = prompt(L"\t");
Datum *result = evalline(input);
if(result == nil){
@@ -39,7 +46,7 @@ main(int argc, char *argv[])
freearray(result->array);
free(result);
}
- print("Unfreed allocations: %d\n", alloccounts);
+ print("Unfreed arrays: %d\n", alloccounts);
}
exits(nil);
}
diff --git a/memory.c b/memory.c
index fd0fec6..04c716d 100644
--- a/memory.c
+++ b/memory.c
@@ -1,10 +1,32 @@
#include <u.h>
#include <libc.h>
#include <bio.h>
+#include <pool.h>
#include "apl9.h"
int alloccounts = 0;
+int debugmem;
+
+void *
+emalloc(ulong size)
+{
+ void *res = malloc(size);
+ if(res == nil){
+ print("Out of memory! :(\n");
+ exits("emalloc");
+ }
+ return res;
+}
+
+void
+checkmem(char *where)
+{
+ if(debugmem){
+ print("Checking memory at: %s\n", where);
+ poolcheck(mainmem);
+ }
+}
void
freearray(Array *a)
@@ -32,13 +54,13 @@ freearray(Array *a)
Array *
allocarray(arrayDataType t, int rank, int size)
{
- Array *a = malloc(sizeof(Array));
+ Array *a = emalloc(sizeof(Array));
a->rank = rank;
a->type = t;
a->size = size;
a->stranded = 0;
- a->shape = malloc(sizeof(int) * rank);
- a->rawdata = malloc(datasizes[t] * size);
+ a->shape = emalloc(sizeof(int) * rank);
+ a->rawdata = emalloc(datasizes[t] * size);
a->type = t;
a->refs = 1;
alloccounts++;
diff --git a/print.c b/print.c
index 1ebdecb..bf1be0c 100644
--- a/print.c
+++ b/print.c
@@ -6,6 +6,7 @@
void strdims(Rune *, int *, int *);
Rune *printborder(Rune *, int *, int, int);
+Rune *strline(Rune *, int);
Rune *
ppdatum(Datum d)
@@ -61,7 +62,7 @@ ppdatums(Datum *ds, int n)
Rune *
pparray(Array *a)
{
- Rune **elemstrs = malloc(sizeof(Rune *) * a->size);
+ Rune **elemstrs = emalloc(sizeof(Rune *) * a->size);
int rowcount = 1;
if(a->rank > 0){
for(int i = 0; i < a->rank-1; i++)
@@ -82,45 +83,55 @@ pparray(Array *a)
int lastdim = a->rank ? a->shape[a->rank-1] : 1;
int *widths = mallocz(sizeof(int) * lastdim, 1);
+ int *heights = mallocz(sizeof(int) * rowcount, 1);
for(int i = 0; i < a->size; i++){
int w,h;
strdims(elemstrs[i], &w, &h);
if(w > widths[i%lastdim])
widths[i%lastdim] = w;
+ if(h > heights[i/lastdim])
+ heights[i/lastdim] = h;
}
- /* Should do height padding here as well */
- for(int i = 0; i < a->size; i++){
- if(i%lastdim == 0)
- rowstrs[i/lastdim] = runesmprint("%S", boxing ? L"│" : L"");
- Rune *tmp = rowstrs[i/lastdim];
- char *fmt = smprint("%%%s%dS", align, widths[i%lastdim]);
- Rune *elem = runesmprint(fmt, elemstrs[i]);
- Rune *spacing;
- if((i+1)%lastdim == 0)
- spacing = boxing ? L"│" : L"";
- else
- spacing = boxing ? L"│" : L" ";
- rowstrs[i/lastdim] = runesmprint("%S%S%S", tmp, elem, spacing);
- free(tmp);
- free(elem);
- free(fmt);
- free(elemstrs[i]);
+ Rune *tmp;
+ for(int row = 0; row < rowcount; row++){
+ rowstrs[row] = runestrdup(L"");
+ for(int y = 0; y < heights[row]; y++){
+ if(boxing){
+ tmp = rowstrs[row];
+ rowstrs[row] = runesmprint("%S│", tmp);
+ free(tmp);
+ }
+ for(int x = 0; x < lastdim; x++){
+ int i = row * lastdim + x;
+ tmp = rowstrs[row];
+ char *fmt = smprint("%%%s%dS", align, widths[x]);
+ Rune *line = strline(elemstrs[i], y);
+ Rune *padded = runesmprint(fmt, line);
+ Rune *spacing = boxing ? L"│" : (x == lastdim-1) ? L"" : L" ";
+ rowstrs[row] = runesmprint("%S%S%S", tmp, padded, spacing);
+ free(tmp);
+ free(fmt);
+ free(line);
+ free(padded);
+ if(y == heights[row]-1)
+ free(elemstrs[i]);
+ }
+ if(y < heights[row]-1){
+ tmp = rowstrs[row];
+ rowstrs[row] = runesmprint("%S\n", tmp);
+ free(tmp);
+ }
+ }
}
- free(elemstrs);
-
- if(rowstrs[0] == nil)
- rowstrs[0] = runestrdup(L"");
if(rowcount == 1 && !boxing)
return rowstrs[0];
Rune *res = runesmprint("");
- Rune *tmp;
for(int i = 0; i < rowcount; i++){
if(i == 0 && boxing)
res = printborder(res, widths, lastdim, 0);
-
int j = 1;
int blanks = 0;
for(int dim = 0; dim < a->rank - 1 && i+1 != a->size; dim++){
@@ -138,14 +149,11 @@ pparray(Array *a)
res = runesmprint("%S\n", tmp);
free(tmp);
}
-
if(blanks > 0 && i != 0 && i < rowcount-1 && boxing)
res = printborder(res, widths, lastdim, 0);
-
tmp = res;
res = runesmprint("%S%S\n", tmp, rowstrs[i]);
free(tmp);
-
if(i == rowcount-1 && boxing)
res = printborder(res, widths, lastdim, 2);
@@ -208,7 +216,7 @@ printborder(Rune *input, int *widths, int lastdim, int type)
int width = 1;
for(int i = 0; i < lastdim; i++)
width += widths[i]+1;
- Rune *border = malloc(sizeof(Rune) * width + 1);
+ Rune *border = emalloc(sizeof(Rune) * (width + 1));
border[width] = 0;
border[0] = borderchars[0];
@@ -224,7 +232,36 @@ printborder(Rune *input, int *widths, int lastdim, int type)
}
Rune *result = runesmprint("%S%S\n", input, border);
- //free(border);
- //free(input);
+ free(border);
+ free(input);
return result;
+}
+
+Rune *
+strline(Rune *str, int n)
+{
+ Rune *start = str;
+ Rune *end = str;
+ int l = 0;
+ while(l<n){
+ if(*start == '\n' || *start == 0)
+ l++;
+ if((l<n && *start != 0) || *start == '\n')
+ start++;
+ }
+ if(*start == 0)
+ return runestrdup(L"");
+
+ l = 0;
+ while(l<(n+1)){
+ if(*end == '\n' || *end == 0)
+ l++;
+ if(l<(n+1))
+ end++;
+ }
+ int len = end-start;
+ Rune *line = emalloc(sizeof(Rune) * (len + 1));
+ runestrncpy(line, start, len);
+ line[len] = 0;
+ return line;
} \ No newline at end of file
diff --git a/symbol.c b/symbol.c
index 294d9a4..8bcaa13 100644
--- a/symbol.c
+++ b/symbol.c
@@ -16,7 +16,7 @@ getsym(Symtab *tab, Rune *name)
tab->nsyms++;
tab->syms = realloc(tab->syms, sizeof(Symbol *) * tab->nsyms);
- tab->syms[tab->nsyms-1] = malloc(sizeof(Symbol));
+ tab->syms[tab->nsyms-1] = emalloc(sizeof(Symbol));
tab->syms[tab->nsyms-1]->name = runestrdup(name);
tab->syms[tab->nsyms-1]->undefined = 1;
return tab->syms[tab->nsyms-1];
@@ -25,7 +25,7 @@ getsym(Symtab *tab, Rune *name)
Symtab *
newsymtab(void)
{
- Symtab *tab = malloc(sizeof(Symtab));
+ Symtab *tab = emalloc(sizeof(Symtab));
tab->nsyms = 0;
tab->syms = nil;