blob: 04c716da38166e985f3da9dafbfa2c47ddc32357 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
#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)
{
if(a == nil)
return;
a->refs--;
if(a->refs == 0){
/* print("Freeing array: %S (%p)\n", pparray(a), a); */
if(a->type == AtypeArray)
for(int i = 0; i < a->size; i++)
freearray(a->arraydata[i]);
free(a->shape);
free(a);
alloccounts--;
}else if(a->refs > 0){
/* print("Not freeing (refs=%d): %S (%p)\n", a->refs, pparray(a), a); */
}else{
print("NEGATIVE REF COUNT! %p\n", a);
exits(nil);
}
}
Array *
allocarray(arrayDataType t, int rank, int size)
{
Array *a = emalloc(sizeof(Array));
a->rank = rank;
a->type = t;
a->size = size;
a->stranded = 0;
a->shape = emalloc(sizeof(int) * rank);
a->rawdata = emalloc(datasizes[t] * size);
a->type = t;
a->refs = 1;
alloccounts++;
return a;
}
void
incref(Array *a)
{
/* print("INCREF %d->%d %S\n", a->refs, a->refs+1, pparray(a)); */
a->refs++;
if(a->type == AtypeArray)
for(int i = 0; i < a->size; i++)
incref(a->arraydata[i]);
}
|