aboutsummaryrefslogtreecommitdiffstats
path: root/core/module-list.c
blob: 2cd2b1c88cc3eccc6887e5351135ca74ddbc05c8 (plain)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include "module-list.h"
#include "module-text.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>

struct PureList {
    unsigned fields;
    uint8_t text_fields[16];
    PureCell **items;
    size_t count, capacity;
    PureDraft *draft;     /* the stored copy, or NULL */
    size_t header;        /* draft bytes before the first item */
    uint64_t id, version; /* which list, and a number every change moves on */
    uint64_t shares;      /* copies made so far: an item made before the last one is shared */
    PureRetired *retired; /* where shared items go when the list lets them go */
    unsigned refs;        /* a shared copy's references */
    bool copy;            /* a shared copy: it owns its item array, not its items */
};

typedef enum { INSERTED, REMOVED, CHANGED, REPLACED } Change;
typedef struct {
    Change change;
    PureList *list;
    size_t at;
    unsigned field;
    PureCell *item; /* REMOVED: the removed item; REPLACED: the shared item a copy replaced */
    PureCell old;   /* CHANGED: the replaced cell */
} Entry;
typedef struct { PureCell *item; uint16_t text; uint8_t fields; } Retired;

static uint64_t lists_made;

/* An item's cells follow the list's share count when the item was made. One
 * made before the last share may be read by a shared copy, so it never
 * changes again (a change copies it) and is retired instead of freed. */
static PureCell *new_item(const PureList *list) {
    uint64_t *made = calloc(1, sizeof(*made) + (list->fields ? list->fields : 1) * sizeof(PureCell));
    if (!made) return NULL;
    *made = list->shares;
    return (PureCell *)(made + 1);
}

static bool shared(const PureList *list, const PureCell *item) { return ((const uint64_t *)item)[-1] < list->shares; }

static void free_cells(PureCell *item, unsigned fields, unsigned text) {
    for (unsigned f = 0; f < fields; ++f)
        if (text >> f & 1) free(item[f].text);
    free((uint64_t *)item - 1);
}

static unsigned text_mask(const PureList *list) {
    unsigned text = 0;
    for (unsigned f = 0; f < list->fields; ++f) text |= (unsigned)(list->text_fields[f] != 0) << f;
    return text;
}

static void free_item(const PureList *list, PureCell *item) {
    if (item) free_cells(item, list->fields, text_mask(list));
}

/* The list no longer has item: free it, or retire it while a copy may read it. */
static void let_go(PureList *list, PureCell *item) {
    if (!shared(list, item)) { free_item(list, item); return; }
    PureRetired *retired = list->retired;
    if (retired->count == retired->capacity) {
        size_t capacity = retired->capacity ? 2 * retired->capacity : 16;
        Retired *grown = realloc(retired->entries, capacity * sizeof(*grown));
        if (!grown) return; /* kept for good rather than freed while read */
        retired->entries = grown;
        retired->capacity = capacity;
    }
    ((Retired *)retired->entries)[retired->count++] = (Retired){item, (uint16_t)text_mask(list), (uint8_t)list->fields};
}

void pure_retired_free(PureRetired *retired) {
    Retired *entries = retired->entries;
    for (size_t i = 0; i < retired->count; ++i) free_cells(entries[i].item, entries[i].fields, entries[i].text);
    free(entries);
    *retired = (PureRetired){0};
}

PureList *pure_list_new(unsigned fields, const uint8_t *text_fields) {
    if (fields > 16) return NULL;
    PureList *list = calloc(1, sizeof(*list));
    if (!list) return NULL;
    list->fields = fields;
    memcpy(list->text_fields, text_fields, fields);
    list->id = __atomic_add_fetch(&lists_made, 1, __ATOMIC_RELAXED);
    return list;
}

void pure_list_free(PureList *list) {
    if (!list) return;
    if (list->copy) {
        if (__atomic_sub_fetch(&list->refs, 1, __ATOMIC_ACQ_REL)) return;
    } else
        for (size_t i = 0; i < list->count; ++i) let_go(list, list->items[i]);
    free(list->items);
    free(list);
}

size_t pure_list_count(const PureList *list) { return list->count; }

PureList *pure_list_share(PureList *list, PureRetired *retired) {
    PureList *copy = malloc(sizeof(*copy));
    PureCell **items = list->count ? malloc(list->count * sizeof(*items)) : NULL;
    if (!copy || (list->count && !items)) {
        free(copy);
        free(items);
        return NULL;
    }
    if (list->count) memcpy(items, list->items, list->count * sizeof(*items));
    *copy = (PureList){.fields = list->fields, .items = items, .count = list->count, .capacity = list->count,
                       .id = list->id, .version = list->version, .refs = 1, .copy = true};
    memcpy(copy->text_fields, list->text_fields, sizeof(copy->text_fields));
    list->retired = retired;
    ++list->shares;
    return copy;
}

void pure_list_hold(PureList *copy) { __atomic_add_fetch(&copy->refs, 1, __ATOMIC_RELAXED); }

bool pure_list_current(const PureList *copy, const PureList *list) {
    return copy->id == list->id && copy->version == list->version;
}

static bool log_change(PureUndo *undo, Entry entry) {
    if (undo->count == undo->capacity) {
        size_t capacity = undo->capacity ? 2 * undo->capacity : 16;
        Entry *entries = realloc(undo->entries, capacity * sizeof(*entries));
        if (!entries) return false;
        undo->entries = entries;
        undo->capacity = capacity;
    }
    ((Entry *)undo->entries)[undo->count++] = entry;
    return true;
}

static bool copy_cell(const PureList *list, unsigned field, PureCell from, PureCell *to) {
    *to = (PureCell){.num = list->text_fields[field] ? 0 : from.num};
    if (!list->text_fields[field] || !from.size) return true;
    to->text = malloc(from.size);
    if (!to->text) return false;
    memcpy(to->text, from.text, from.size);
    to->size = from.size;
    return true;
}

/* ---- The stored form: each field in order, a number as its binary64 bits
 * (little-endian), a text as a u32le length and its bytes. */

static size_t cell_bytes(const PureList *list, unsigned field, PureCell cell) {
    return list->text_fields[field] ? 4 + cell.size : 8;
}

static size_t item_bytes(const PureList *list, const PureCell *item) {
    size_t size = 0;
    for (unsigned f = 0; f < list->fields; ++f) size += cell_bytes(list, f, item[f]);
    return size;
}

/* Where an item starts in the draft. Linear in the items before it. */
static size_t item_offset(const PureList *list, size_t at) {
    size_t offset = list->header;
    for (size_t i = 0; i < at; ++i) offset += item_bytes(list, list->items[i]);
    return offset;
}

static unsigned char *encode_cell(const PureList *list, unsigned field, PureCell cell, unsigned char *out) {
    uint64_t bits = cell.size;
    unsigned width = 4;
    if (!list->text_fields[field]) { memcpy(&bits, &cell.num, sizeof(bits)); width = 8; }
    for (unsigned i = 0; i < width; ++i) *out++ = (unsigned char)(bits >> (8 * i));
    if (list->text_fields[field] && cell.size) { memcpy(out, cell.text, cell.size); out += cell.size; }
    return out;
}

/* Replace remove bytes of the draft, within bytes into the item at `at` (or
 * after the last item), with bytes; nothing to do without a draft. */
static PureResult mirror(PureList *list, size_t at, size_t within, size_t remove, const unsigned char *bytes,
                         size_t size) {
    if (!list->draft) return PURE_OK;
    return pure_draft_splice(list->draft, item_offset(list, at) + within, remove, (PureSpan){bytes, size});
}

void pure_list_mirror(PureList *list, PureDraft *draft, size_t header) {
    list->draft = draft;
    list->header = header;
}

PURE_COLD PureResult pure_list_write(PureList *list) {
    size_t size = 0;
    for (size_t i = 0; i < list->count; ++i) size += item_bytes(list, list->items[i]);
    unsigned char *bytes = size ? malloc(size) : NULL, *at = bytes;
    if (size && !bytes) return PURE_ALLOCATION;
    for (size_t i = 0; i < list->count; ++i)
        for (unsigned f = 0; f < list->fields; ++f) at = encode_cell(list, f, list->items[i][f], at);
    PureResult result = mirror(list, 0, 0, 0, bytes, size);
    free(bytes);
    return result;
}

PURE_COLD PureResult pure_list_read(PureList *list, const unsigned char *bytes, size_t size) {
    PureCell cells[16];
    for (size_t at = 0; at < size;) {
        for (unsigned f = 0; f < list->fields; ++f) {
            size_t width = list->text_fields[f] ? 4 : 8;
            if (size - at < width) return PURE_STORE_ERROR;
            uint64_t bits = 0;
            for (size_t i = 0; i < width; ++i) bits |= (uint64_t)bytes[at + i] << (8 * i);
            at += width;
            if (!list->text_fields[f]) {
                cells[f] = (PureCell){0};
                memcpy(&cells[f].num, &bits, sizeof(bits));
                if (!isfinite(cells[f].num)) return PURE_STORE_ERROR;
                continue;
            }
            if (bits > size - at || !pure_utf8_valid((PureSpan){bytes + at, (size_t)bits}))
                return PURE_STORE_ERROR;
            cells[f] = (PureCell){0, (unsigned char *)bytes + at, (size_t)bits};
            at += (size_t)bits;
        }
        PureResult result = pure_list_insert(list, list->count, cells, NULL);
        if (result != PURE_OK) return result;
    }
    return PURE_OK;
}

static void place(PureList *list, size_t at, PureCell *item) {
    memmove(list->items + at + 1, list->items + at, (list->count - at) * sizeof(*list->items));
    list->items[at] = item;
    ++list->count;
    ++list->version;
}

static PureCell *take(PureList *list, size_t at) {
    PureCell *item = list->items[at];
    memmove(list->items + at, list->items + at + 1, (list->count - at - 1) * sizeof(*list->items));
    --list->count;
    ++list->version;
    return item;
}

PureResult pure_list_insert(PureList *list, size_t at, const PureCell *cells, PureUndo *undo) {
    if (at > list->count || list->count == PURE_LIST_ITEMS) return PURE_LIMIT;
    if (list->count == list->capacity) {
        size_t capacity = list->capacity ? 2 * list->capacity : 8;
        PureCell **items = realloc(list->items, capacity * sizeof(*items));
        if (!items) return PURE_ALLOCATION;
        list->items = items;
        list->capacity = capacity;
    }
    PureCell *item = new_item(list);
    if (!item) return PURE_ALLOCATION;
    for (unsigned f = 0; f < list->fields; ++f)
        if (!copy_cell(list, f, cells[f], &item[f])) { free_item(list, item); return PURE_ALLOCATION; }
    size_t size = list->draft ? item_bytes(list, item) : 0;
    unsigned char *bytes = size ? malloc(size) : NULL, *end = bytes;
    if (size && !bytes) { free_item(list, item); return PURE_ALLOCATION; }
    for (unsigned f = 0; bytes && f < list->fields; ++f) end = encode_cell(list, f, item[f], end);
    if (undo && !log_change(undo, (Entry){.change = INSERTED, .list = list, .at = at})) {
        free(bytes);
        free_item(list, item);
        return PURE_ALLOCATION;
    }
    PureResult result = mirror(list, at, 0, 0, bytes, size);
    free(bytes);
    if (result != PURE_OK) {
        if (undo) --undo->count;
        free_item(list, item);
        return result;
    }
    place(list, at, item);
    return PURE_OK;
}

PureResult pure_list_remove(PureList *list, size_t at, PureUndo *undo) {
    if (at >= list->count) return PURE_NOT_FOUND;
    if (!log_change(undo, (Entry){.change = REMOVED, .list = list, .at = at, .item = list->items[at]}))
        return PURE_ALLOCATION;
    PureResult result = mirror(list, at, 0, item_bytes(list, list->items[at]), NULL, 0);
    if (result != PURE_OK) { --undo->count; return result; }
    take(list, at);
    return PURE_OK;
}

/* A shared item's copy with every field but one (left empty). */
static PureCell *copy_item(const PureList *list, const PureCell *item, unsigned except) {
    PureCell *fresh = new_item(list);
    for (unsigned f = 0; fresh && f < list->fields; ++f)
        if (f != except && !copy_cell(list, f, item[f], &fresh[f])) {
            free_item(list, fresh);
            fresh = NULL;
        }
    return fresh;
}

PureResult pure_list_set(PureList *list, size_t at, unsigned field, PureCell value, PureUndo *undo) {
    if (at >= list->count) return PURE_NOT_FOUND;
    PureCell copy, *item = list->items[at];
    if (!copy_cell(list, field, value, &copy)) return PURE_ALLOCATION;
    PureCell *fresh = shared(list, item) ? copy_item(list, item, field) : item;
    size_t size = list->draft ? cell_bytes(list, field, copy) : 0;
    unsigned char *bytes = size ? malloc(size) : NULL;
    Entry entry = fresh == item ? (Entry){.change = CHANGED, .list = list, .at = at, .field = field, .old = item[field]} :
                                  (Entry){.change = REPLACED, .list = list, .at = at, .item = item};
    if (!fresh || (size && !bytes) || !log_change(undo, entry)) {
        free(bytes);
        free(copy.text);
        if (fresh != item) free_item(list, fresh);
        return PURE_ALLOCATION;
    }
    if (bytes) encode_cell(list, field, copy, bytes);
    size_t within = 0;
    for (unsigned f = 0; f < field; ++f) within += cell_bytes(list, f, item[f]);
    PureResult result = mirror(list, at, within, cell_bytes(list, field, item[field]), bytes, size);
    free(bytes);
    if (result != PURE_OK) {
        --undo->count;
        free(copy.text);
        if (fresh != item) free_item(list, fresh);
        return result;
    }
    fresh[field] = copy;
    list->items[at] = fresh;
    ++list->version;
    return PURE_OK;
}

PureCell pure_list_get(const PureList *list, size_t at, unsigned field) {
    return list->items[at][field];
}

/* How two cells of a field order: numbers by value (-0 and +0 are one key),
 * texts by their bytes. */
static int order(const PureList *list, unsigned field, PureCell a, PureCell b) {
    if (!list->text_fields[field]) return (a.num > b.num) - (a.num < b.num);
    size_t shorter = a.size < b.size ? a.size : b.size;
    int bytes = shorter ? memcmp(a.text, b.text, shorter) : 0;
    return bytes ? (bytes > 0) - (bytes < 0) : (a.size > b.size) - (a.size < b.size);
}

size_t pure_list_seek(const PureList *list, unsigned field, PureCell key, bool *found) {
    size_t low = 0, high = list->count;
    while (low < high) {
        size_t middle = low + (high - low) / 2;
        if (order(list, field, list->items[middle][field], key) < 0) low = middle + 1;
        else high = middle;
    }
    *found = low < list->count && !order(list, field, list->items[low][field], key);
    return low;
}

/* Positions sorted by their items' field, stably (spare holds count). */
PURE_COLD static void merge_sort(const PureList *list, unsigned field, size_t *at, size_t *spare, size_t count) {
    if (count < 2) return;
    size_t half = count / 2, i = 0, j = half, k = 0;
    merge_sort(list, field, at, spare, half);
    merge_sort(list, field, at + half, spare, count - half);
    while (i < half && j < count)
        spare[k++] = order(list, field, list->items[at[j]][field], list->items[at[i]][field]) < 0 ? at[j++] : at[i++];
    while (i < half) spare[k++] = at[i++];
    while (j < count) spare[k++] = at[j++];
    memcpy(at, spare, count * sizeof(*at));
}

PURE_COLD PureResult pure_list_sorted(const PureList *list, unsigned field, PureList **out, size_t *moved) {
    *out = NULL;
    bool ordered = true;
    for (size_t i = 1; i < list->count && ordered; ++i)
        ordered = order(list, field, list->items[i - 1][field], list->items[i][field]) < 0;
    if (ordered) return PURE_OK;
    size_t *at = malloc(2 * list->count * sizeof(*at));
    if (!at) return PURE_ALLOCATION;
    for (size_t i = 0; i < list->count; ++i) at[i] = i;
    merge_sort(list, field, at, at + list->count, list->count);
    PureResult result = PURE_OK;
    for (size_t i = 1; i < list->count && result == PURE_OK; ++i)
        if (!order(list, field, list->items[at[i - 1]][field], list->items[at[i]][field])) result = PURE_CONFLICT;
    PureList *sorted = result == PURE_OK ? pure_list_new(list->fields, list->text_fields) : NULL;
    if (result == PURE_OK && !sorted) result = PURE_ALLOCATION;
    for (size_t i = 0; result == PURE_OK && i < list->count; ++i) {
        result = pure_list_insert(sorted, i, list->items[at[i]], NULL);
        moved[at[i]] = i;
    }
    free(at);
    if (result != PURE_OK) {
        pure_list_free(sorted);
        return result;
    }
    *out = sorted;
    return PURE_OK;
}

void pure_undo_rollback(PureUndo *undo, size_t mark) {
    Entry *entries = undo->entries;
    for (size_t i = undo->count; i-- > mark;) {
        Entry *e = &entries[i];
        if (e->change == INSERTED) free_item(e->list, take(e->list, e->at));
        else if (e->change == REMOVED) place(e->list, e->at, e->item); /* capacity is still there */
        else if (e->change == REPLACED) {
            free_item(e->list, e->list->items[e->at]);
            e->list->items[e->at] = e->item;
            ++e->list->version;
        } else {
            if (e->list->text_fields[e->field]) free(e->list->items[e->at][e->field].text);
            e->list->items[e->at][e->field] = e->old;
            ++e->list->version;
        }
    }
    undo->count = mark;
}

void pure_undo_commit(PureUndo *undo) {
    Entry *entries = undo->entries;
    for (size_t i = 0; i < undo->count; ++i) {
        Entry *e = &entries[i];
        if (e->change == REMOVED || e->change == REPLACED) let_go(e->list, e->item);
        else if (e->change == CHANGED && e->list->text_fields[e->field]) free(e->old.text);
    }
    undo->count = 0;
}

void pure_undo_free(PureUndo *undo) {
    free(undo->entries);
    *undo = (PureUndo){0};
}