-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod_objutil.c
More file actions
156 lines (135 loc) · 3.22 KB
/
mod_objutil.c
File metadata and controls
156 lines (135 loc) · 3.22 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "es.h"
#include "prim.h"
#include <sys/stat.h>
/* from objects.c */
/* these aren't normally used but are needed for this mod */
typedef struct {
char *name;
int (*deallocate)(Object *); /* called before an object is freed */
int (*refdeps)(Object *);
char *(*stringify)(Object *);
int (*onfork)(Object *);
Result (*objectify)(char *);
} Typedef;
extern size_t typessz;
extern Typedef **typedefs;
extern size_t objectssz;
extern Object **objects;
PRIM(objects_dumptypes) {
List *result = nil; Root r_result;
size_t i = 0;
gcref(&r_result, (void **)&result);
for(i = 0; i < typessz; i++) {
if(typedefs[i] == nil)
continue;
result = mklist(mkstr(str("type %d: %s", (int32_t)i, typedefs[i]->name)), result);
}
result = reverse(result);
gcrderef(&r_result);
return result;
}
PRIM(printtypes) {
size_t i;
int nocallbacks = 1;
for(i = 0; i < typessz; i++, nocallbacks = 1) {
if(typedefs[i] == nil)
continue;
dprintf(2, "type %lu: %s: ", i, typedefs[i]->name);
if(typedefs[i]->deallocate) {
dprintf(2, "deallocate ");
nocallbacks = 0;
}
if(typedefs[i]->refdeps) {
dprintf(2, "refdeps ");
nocallbacks = 0;
}
if(typedefs[i]->stringify) {
dprintf(2, "stringify ");
nocallbacks = 0;
}
if(typedefs[i]->onfork) {
dprintf(2, "onfork ");
nocallbacks = 0;
}
if(typedefs[i]->objectify) {
dprintf(2, "objectify");
nocallbacks = 0;
}
if(nocallbacks)
dprintf(2, "no callbacks");
dprintf(2, "\n");
}
return nil;
}
PRIM(printobjects) {
size_t i;
Typedef *objtype;
int nocallbacks = 1;
for(i = 0; i < objectssz; i++, objtype = nil) {
if(objects[i] == nil)
continue;
objtype = typedefs[objects[i]->type];
dprintf(2, "object %lu: type=%s(%d), id=%d, sysflags=%x, size=%lu, refs=%lu\n", i,
objtype->name, objects[i]->type, objects[i]->id, objects[i]->sysflags,
objects[i]->size, objects[i]->refcount);
dprintf(2, " ");
if(objtype->deallocate) {
dprintf(2, "deallocate ");
nocallbacks = 0;
}
if(objtype->refdeps) {
dprintf(2, "refdeps ");
nocallbacks = 0;
}
if(objtype->stringify) {
dprintf(2, "stringify ");
nocallbacks = 0;
}
if(objtype->onfork) {
dprintf(2, "onfork ");
nocallbacks = 0;
}
if(objtype->objectify) {
dprintf(2, "objectify");
nocallbacks = 0;
}
if(nocallbacks)
dprintf(2, "no callbacks");
dprintf(2, "\n");
}
return nil;
}
PRIM(printobjectstats) {
size_t nobjs = 0;
size_t nobjssz = 0;
size_t nobjssz_total = 0;
size_t ntypes = 0;
size_t i;
for(i = 0; i < typessz; i++) {
if(typedefs[i])
ntypes++;
}
for(i = 0; i < objectssz; i++) {
if(objects[i]) {
nobjs++;
nobjssz += objects[i]->size;
}
}
nobjssz_total = nobjssz + (nobjs * sizeof(Object));
dprintf(2, "%lu objects in %lu slots using %lu bytes (%lu total)\n", nobjs, objectssz, nobjssz,
nobjssz_total);
dprintf(2, "%lu types in %lu slots\n", ntypes, typessz);
return nil;
}
PRIM(objectify) {
Object *obj = nil;
if(list == nil)
fail("$&objectify", "missing argument");
if((obj = getobject(list->term)) == nil)
fail("$&objectify", "not an object");
return mklist(mkobject(obj), nil);
}
MODULE(mod_objutil, nil, nil,
DX(objects_dumptypes), DX(printtypes), DX(printobjects),
DX(printobjectstats), DX(objectify),
);