~pbatch/patchwerk

patchwerk/pointer.w -rw-r--r-- 6.3 KiB
9c265356 — paul plan9 additions from Sigrid 3 months ago
                                                                                
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
@** Pointer.
\mkref{pointer}
A pointer in Patchwerk is a wrapper around a generic |void *|
pointer in C. It is used to store chunks of data, such as tables to be used
in table-lookup oscillators. Memory allocation is handled by the pointer type.
Pointers are linked together in a linked-list, and are called a pointer list.

@<Top@>+=
@<Pointer Top@>


@* Pointer Data and Type Declarations.

@ The pointer is implemented as an opaque pointer type called |pw_pointer|.

@ A callback interface is used to implement wrappers for memory allocation
and freeing.
@<Type Declarations@> +=
typedef void (*pw_pointer_function)(pw_pointer *p);

@ The opaque pointer has a private struct definition, also called |pw_pointer|.
@<Pointer Top@>+=
struct pw_pointer {
    @<Pointer Struct Data@>
};

@ Most of the time, pw\_pointers are not directly accessible to the user.
Instead, they indirectly used via top-level patch functions.
The |pw_pointer| type struct definition begins with a reference to the
top-level patch it belongs to.
@<Pointer Struct Data@> +=
pw_patch *patch;

@ Naturally, a pointer wrapper needs to have a |void *| pointer somewhere.
This is included, alongside an optional |type| variable for a weakly
enforced type-checking system.
@<Pointer Struct Data@> +=
int type;
void *ud;

@ The |pw_pointer_function| callback prototype is used for freeing memory at
the end of performance.
@<Pointer Struct Data@>+=
pw_pointer_function free;

@ The |pw_pointer| types can be strung together in a linked list, allowing for
many points of data to be automatically freed.
@<Pointer Struct Data@>+=
pw_pointer *next;

@ A collection of |pw_pointer| values in contained in a list called
a |pw_pointerlist|.

@<Type Declarations@>+=
typedef struct {
@<Pointer List Struct Data@>
} pw_pointerlist;

@ The |pw_pointerlist| implements a singly linked list.
It has a |root| value and a |last| value. Values are appended to the end of
a pointer list.
@<Pointer List Struct Data@>+=
pw_pointer *root;
pw_pointer *last;

@ The |pw_pointerlist| implementation keeps track of how many
items there are in the list. This is used for list iteration.
@<Pointer List Struct Data@>+=
unsigned int size;

@* Pointer Functions. The following functions below describe operations on
individual |pw_pointer| types.

@ A |pw_pointer| is created with the function |pw_pointer_create|.

@ @<Header@> +=
int pw_pointer_create(
    pw_patch *patch,
    pw_pointer **pointer,
    pw_pointer_function free,
    void *ud
);

@ @<Pointer Top@>+=

int pw_pointer_create(
    pw_patch *patch,
    pw_pointer **pointer,
    pw_pointer_function free,
    void *ud
)
{
    pw_pointer *pptr;
    int rc;

    rc = pw_memory_alloc(patch, sizeof(pw_pointer), (void **)&pptr);

    if(rc != PW_OK) return rc;

    *pointer = pptr;
    pptr->patch = patch;
    pptr->next = NULL;
    pptr->type = 0;
    pptr->ud = ud;
    pptr->free = free;
    return PW_OK;
}

@ A |pw_pointer| is destroyed with the function |pw_pointer_destroy|. This
will not only free the pointer itself, but the generic |void*| pointer
contained inside of it.
@<Header@>+=
void pw_pointer_destroy(pw_pointer **pointer);

@ @<Pointer Top@> +=
void pw_pointer_destroy(pw_pointer **pointer)
{
    (*pointer)->free(*pointer);
    pw_memory_free((*pointer)->patch, (void **)pointer);
}

@ The function |pw_pointer_data| will return the user data contained inside
of |pw_pointer|.
@<Header@>+=
void *pw_pointer_data(pw_pointer *pointer);

@ @<Pointer Top@>+=
void *pw_pointer_data(pw_pointer *pointer)
{
    return pointer->ud;
}

@ The function |pw_pointer_set_type| and |pw_pointer_get_type| will set and
get the optional type id, which is set to 0 by default.
@<Header@>+=
void pw_pointer_set_type(pw_pointer *pointer, int id);
int pw_pointer_get_type(pw_pointer *pointer);

@ @<Pointer Top@>+=
void pw_pointer_set_type(pw_pointer *pointer, int id)
{
    pointer->type = id;
}

int pw_pointer_get_id(pw_pointer *pointer)
{
    return pointer->type;
}

@ The function |pw_pointer_get_next| returns the value of the next pointer in
the linked list.

@<Header@>+=
pw_pointer * pw_pointer_get_next(pw_pointer *p);

@ @<Pointer Top@>+=
pw_pointer* pw_pointer_get_next(pw_pointer *p)
{
    return p->next;
}

@ The function |pw_pointer_set_next| sets the next value of the pointer.
@<Header@>+=
void pw_pointer_set_next(pw_pointer *p, pw_pointer *next);

@ @<Pointer Top@>+=
void pw_pointer_set_next(pw_pointer *p, pw_pointer *next)
{
    p->next = next;
}

@* Pointer List Functions. The following functions below describe operations on
pointer lists, otherwise known as |pw_pointerlist|.

@ A pointer list in initialized with the function |pw_pointerlist_init|.

@<Header@> +=
void pw_pointerlist_init(pw_pointerlist *plist);

@ @<Pointer Top@>+=
void pw_pointerlist_init(pw_pointerlist *plist)
{
    plist->root = NULL;
    plist->last = NULL;
    plist->size = 0;
}

@ The function |pw_pointerlist_top| returns the top of the list.
@<Header@>+=
pw_pointer * pw_pointerlist_top(pw_pointerlist *plist);

@ @<Pointer Top@>+=
pw_pointer * pw_pointerlist_top(pw_pointerlist *plist)
{
    return plist->root;
}

@ The function |pw_pointerlist_append| appends a pointer to the end of
the list. It is assumed that the pointer value has already been allocated
with |pw_pointer_create|.

@<Header@>+=
void pw_pointerlist_append(pw_pointerlist *plist, pw_pointer *p);

@ The function |pw_pointerlist_append| appends a value to the end of a
linked list. It has an edge case for when the list is zero. When it is zero,
the value is assigned to be |root| and |last|. Otherwise, the value is tacked
on the the |last| next value, and then the |last| value is updated. The size
of the list is the incremented.

@<Pointer Top@>+=
void pw_pointerlist_append(pw_pointerlist *plist, pw_pointer *p)
{
    if(plist->size == 0) {
        plist->root = p;
    } else {
        pw_pointer_set_next(plist->last, p);
    }

    plist->last = p;
    plist->size++;
}

@ The function |pw_pointerlist_free| frees memory for all pointers contained
inside of the pointer list.

@<Header@>+=
void pw_pointerlist_free(pw_pointerlist *plist);

@ @<Pointer Top@>+=
void pw_pointerlist_free(pw_pointerlist *plist)
{
    unsigned int i;
    pw_pointer *next;
    pw_pointer *val;

    val = pw_pointerlist_top(plist);
    for(i = 0; i < plist->size; i++) {
        next = pw_pointer_get_next(val);
        pw_pointer_destroy(&val);
        val = next;
    }
}