summaryrefslogtreecommitdiff
path: root/cesar/lib/src/blk.c
blob: da596756dea0c28428817ffed78561f5e2841bda (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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    lib/src/blk.c
 * \brief   512 byte memory blocks.
 * \ingroup lib
 *
 * Implementation
 * --------------
 *
 * Almost everything ought to be aligned on its size.  Therefore, descriptors
 * can not be located near data blocks, and reference counters can not be
 * located near the descriptors.  Instead, the following organisation is used:
 *
 *   | padding | reference counters | descriptors | data blocks | padding |
 *
 * There might be padding at allocation region end because of data blocks
 * alignment requirement.  There might also be padding at allocation region
 * begin because everything is packed to the region end (there is no padding
 * between reference counters, descriptors and data blocks).
 *
 * At initialisation, every blocks are chained in a simple list.  When there
 * is a allocation need, the requested blocks are extracted from this list.
 *
 * Freed blocks are inserted back in the list.  This is a LIFO list in order
 * to minimise page allocation on simulation environment (try to reuse the
 * same pages instead of dirtying new pages).
 *
 * No special optimisation will be done on descriptor-less allocation as this
 * is a rarely useful case in production code.
 */
#include "common/std.h"

#include "lib/blk.h"
#include "lib/restrack.h"
#include "hal/arch/arch.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config/blk.h"

/** A block descriptor reference counter is 4 bytes. */
#define BLK_REFCNT_SIZE 4

/** Memory used by a block and its dependencies. */
#define BLK_TOTAL_SIZE (BLK_SIZE + BLK_DESC_SIZE + BLK_REFCNT_SIZE)

/** Find reference counter from descriptor. */
#define REFCNT(desc) \
    ((int *) ((uint) (desc) / (BLK_DESC_SIZE / BLK_REFCNT_SIZE) \
              + blk_global.desc_to_refcnt_offset))

/** Find descriptor from data. */
#define DATA_TO_DESC(data) \
    ((blk_t *) ((uint) (data) / (BLK_SIZE / BLK_DESC_SIZE) \
                + blk_global.data_to_desc_offset))

/** Budget for blk allocation without interrupt lock release. */
#define BLK_BUDGET 64u

/** Block allocator context. */
struct blk_global_t
{
    /** Size of the memory for blocks allocation.
     * Relies on automatic initialization to be set to zero. */
    uint blk_mem_size;
    /** Offset to compute reference counter address from descriptor
     * address. */
    uint desc_to_refcnt_offset;
    /** Offset to compute descriptor address from data address. */
    uint data_to_desc_offset;
    /** Automatic initialisation depends on this to be initialised to zero by
     * the compiler. */
    bool inited;
    /** Free list head. */
    blk_t *free_list;
    /** Number of free blocks. */
    uint free_nb;
    /** Total number of blocks. */
    uint total_nb;
    /** Allocation region start. */
    void *start;
    /** Allocation region stop. */
    void *stop;
};
typedef struct blk_global_t blk_global_t;

/** Global context. */
blk_global_t ARCH_DLRAM_BSS blk_global;

enum {
    /** Magic code used for destructor-less allocations.
     * 'o', 'b', 'j', <odd number>.  This magic should be odd to not collide
     * with a pointer. */
    BLK_OBJ_MAGIC = 0x6f626aa5
};

/** Block descriptor with two extra words. */
struct blk_full_t
{
    /** Pointer to next descriptor. */
    struct blk_full_t *next;
    /** Pointer to data. */
    u8 *data;
    /** Two extra words. */
    u32 w[2];
};
typedef struct blk_full_t blk_full_t;

/** Block descriptor for descriptor-less allocations. */
struct blk_obj_t
{
    /** Magic code in place of next pointer. */
    u32 magic;
    /** Pointer to data. */
    u8 *data;
    /** Destructor function pointer or NULL. */
    blk_destructor_t destructor;
    /** Reserved for future usage. */
    u32 reserved;
};
typedef struct blk_obj_t blk_obj_t;

void
blk_init (uint blk_mem_size)
{
    blk_global.blk_mem_size = blk_mem_size;
}

/**
 * Real Init.
 */
static void
blk_do_init (void)
{
    /* If the blk memory size is unknown, use the value of CONFIG_BLK_NB. */
    if (blk_global.blk_mem_size == 0)
        blk_global.blk_mem_size = BLK_TOTAL_SIZE * (CONFIG_BLK_NB + 1);
    /* Allocate memory. */
    u8 *alloc = malloc (blk_global.blk_mem_size);
    if (!alloc)
        dbg_fatal ("exhausted virtual memory");
    uint base = (uint) ARCH_CPU_TO_DMA (alloc);
    uint start = base, stop = base + blk_global.blk_mem_size;
    /* Align block, descriptors and reference counters. */
    stop = stop & ~(BLK_SIZE - 1);
    uint nb_blks = (stop - start) / BLK_TOTAL_SIZE;
    start = stop - nb_blks * BLK_TOTAL_SIZE;
    blk_global.start = (void *) start;
    blk_global.stop = (void *) stop;
    /* Determine memory repartition. */
    base = start;
    dbg_assert (base % BLK_REFCNT_SIZE == 0);
    int *refcnts = (void *) base;
    base = base + BLK_REFCNT_SIZE * nb_blks;
    dbg_assert (base % BLK_DESC_SIZE == 0);
    blk_full_t *blks = (void *) base;
    base = base + BLK_DESC_SIZE * nb_blks;
    dbg_assert (base % BLK_SIZE == 0);
    u8 *datas = (void *) base;
    /* Initialise offsets. */
    blk_global.desc_to_refcnt_offset =
        (uint) refcnts - (uint) blks / (BLK_DESC_SIZE / BLK_REFCNT_SIZE);
    blk_global.data_to_desc_offset =
        (uint) blks - (uint) datas / (BLK_SIZE / BLK_DESC_SIZE);
    /* Prepare free list. */
    uint i;
    blk_global.free_list = (blk_t *) blks;
    for (i = 0; i < nb_blks; i++)
    {
        *refcnts = 1;
        blks->next = blks + 1;
        blks->data = datas;
        refcnts++;
        blks++;
        datas += 512;
    }
    blks[-1].next = NULL;
    dbg_assert ((uint) datas == (uint) blk_global.stop);
    /* Prepare counters. */
    blk_global.free_nb = nb_blks;
    blk_global.total_nb = nb_blks;
    /* Initialised. */
    blk_global.inited = true;
}

blk_t * ARCH_ILRAM
blk_alloc_desc_ (void_FL)
{
    /* Initialise block allocator. */
    if (!blk_global.inited)
        blk_do_init ();
    /* Get a block from free list. */
    uint flags = arch_isr_lock ();
    blk_t *blk = blk_global.free_list;
    if (!blk)
        dbg_fatal ("exhausted block memory");
    blk_global.free_list = blk->next;
    blk_global.free_nb--;
    arch_isr_unlock (flags);
    /* Prepare block. */
    dbg_invalid_ptr (blk->next);
    /* Book keeping. */
    restrack_create (NULL, blk, _fl_, 1);
    /* Done. */
    return blk;
}

blk_t * ARCH_ILRAM
blk_alloc_desc_range_ (uint n, blk_t **last __FL)
{
    blk_t *first, *b, **pfirst;
    uint take, left;
    uint i;
    dbg_blame (n);
    dbg_blame_ptr (last);
    /* Initialise block allocator. */
    if (!blk_global.inited)
        blk_do_init ();
    /* Get blocks from free list. */
    left = n;
    pfirst = &first;
    do
    {
        take = MIN (left, BLK_BUDGET);
        uint flags = arch_isr_lock ();
        *pfirst = blk_global.free_list;
        for (b = *pfirst, i = take - 1; i && b; i--)
            b = b->next;
        if (!b)
            dbg_fatal ("exhausted block memory");
        blk_global.free_list = b->next;
        blk_global.free_nb -= take;
        left -= take;
        pfirst = &b->next;
        arch_isr_unlock (flags);
    } while (left);
    /* Prepare last block. */
    dbg_invalid_ptr (b->next);
    /* Book keeping. */
#if CONFIG_RESTRACK
    {
        blk_t *p;
        for (p = first; p != b; p = p->next)
            restrack_create (NULL, p, _fl_, 1);
        restrack_create (NULL, p, _fl_, 1);
    }
#endif
    /* Done. */
    *last = b;
    return first;
}

static void ARCH_ILRAM
blk_free_desc_ (blk_t *blk __FL)
{
    dbg_blame_ptr (blk);
    dbg_blame (DATA_TO_DESC (blk->data) == blk);
    /* Prepare next allocation. */
    *REFCNT (blk) = 1;
    /* Book keeping. */
    restrack_destroy (NULL, blk, _fl_, 0);
    /* Insert block in free list. */
    uint flags = arch_isr_lock ();
    blk->next = blk_global.free_list;
    blk_global.free_list = blk;
    blk_global.free_nb++;
    arch_isr_unlock (flags);
}

void ARCH_ILRAM
blk_addref_desc_ (blk_t *blk __FL)
{
    dbg_blame_ptr (blk);
    dbg_blame (DATA_TO_DESC (blk->data) == blk);
    /* Update reference counter. */
    arch_atomic_add (REFCNT (blk), 1);
    /* Book keeping. */
    restrack_update (NULL, blk, _fl_, 1);
}

void ARCH_ILRAM
blk_addref_desc_range_ (blk_t *first, blk_t *last __FL)
{
    blk_t *b;
    for (b = first; b != last; b = b->next)
    {
        blk_addref_desc_ (b __fl);
    }
    blk_addref_desc_ (b __fl);
}

void ARCH_ILRAM
blk_addref_desc_range_nb_ (blk_t *first, uint n __FL)
{
    dbg_blame (n);
    blk_t *b;
    uint i;
    for (b = first, i = n; i; b = b->next, i--)
    {
        blk_addref_desc_ (b __fl);
    }
}

void ARCH_ILRAM
blk_release_desc_ (blk_t *blk __FL)
{
    dbg_blame_ptr (blk);
    dbg_blame (DATA_TO_DESC (blk->data) == blk);
    dbg_blame (((blk_obj_t *) blk)->magic != BLK_OBJ_MAGIC);
    dbg_blame (REFCNT (blk) != 0);
    /* Book keeping. */
    restrack_update (NULL, blk, _fl_, -1);
    /* Update reference counter. */
    if (arch_atomic_add (REFCNT (blk), -1) == 0)
    {
        blk_free_desc_ (blk __fl);
    }
}

void ARCH_ILRAM
blk_release_desc_range_ (blk_t *first, blk_t *last __FL)
{
    blk_t *b, *bn;
    for (b = first; b != last; b = bn)
    {
        bn = b->next;
        blk_release_desc_ (b __fl);
    }
    blk_release_desc_ (b __fl);
}

void ARCH_ILRAM
blk_release_desc_range_nb_ (blk_t *first, uint n __FL)
{
    dbg_blame (n);
    blk_t *b, *bn;
    uint i;
    for (b = first, i = n; i; b = bn, i--)
    {
        bn = b->next;
        blk_release_desc_ (b __fl);
    }
}

void *
blk_alloc_ (void_FL)
{
    return blk_new_ (NULL __fl);
}

void *
blk_new_ (blk_destructor_t destructor __FL)
{
    blk_t *blk = blk_alloc_desc_ (_fl);
    blk_obj_t *obj = (blk_obj_t *) blk;
    obj->magic = BLK_OBJ_MAGIC;
    obj->destructor = destructor;
    return blk->data;
}

void *
blk_alloc_zero_ (void_FL)
{
    void *data = blk_alloc_ (_fl);
    memset (data, 0, BLK_SIZE);
    return data;
}

void ARCH_ILRAM
blk_addref_ (void *data __FL)
{
    dbg_blame_ptr (data);
    blk_t *blk = DATA_TO_DESC (data);
    dbg_blame (((blk_obj_t *) blk)->magic == BLK_OBJ_MAGIC);
    blk_addref_desc_ (blk __fl);
}

void ARCH_ILRAM
blk_release_ (void *data __FL)
{
    dbg_blame_ptr (data);
    blk_obj_t *obj = (blk_obj_t *) DATA_TO_DESC (data);
    dbg_blame_ptr (obj);
    dbg_blame (obj->magic == BLK_OBJ_MAGIC);
    restrack_update (NULL, obj, _fl_, -1);
    if (arch_atomic_add (REFCNT (obj), -1) == 0)
    {
        if (obj->destructor)
            obj->destructor (data);
        blk_free_desc_ ((blk_t *) obj __fl);
    }
}

bool
blk_check_memory (void)
{
    return restrack_check ()
        && blk_global.free_nb == blk_global.total_nb;
}

void
blk_print_memory (void)
{
    fprintf (stderr, "blk: total: %d, free: %d\n",
             blk_global.total_nb, blk_global.free_nb);
}

uint
blk_slack (void)
{
    if (blk_global.free_nb > CONFIG_BLK_SLACK)
        return blk_global.free_nb - CONFIG_BLK_SLACK;
    else
        return 0;
}