summaryrefslogtreecommitdiff
path: root/cesar/lib/src/blk.c
blob: 069bde958aae3a76e3191352fc251cff29cd2597 (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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    lib/src/blk.c
 * \brief   512 byte memory blocks.
 * \ingroup lib
 *
 * \todo This is a temporary implementation using malloc and no optimisations.
 */
#include "common/std.h"

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

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

#define REFCNT(blk) ((int *) ((u8 *) (blk) - BLK_REFCNT_SIZE))
#define TO_BLK(data) ((blk_t *) ((u8 *) (data) + BLK_SIZE + BLK_REFCNT_SIZE))

/** Structure to accumulate the several actions type on blocks. */
struct blocks_t
{
    /** Incremented when a new blocks has been allocated. */
    uint allocated;
    /** Incremented when a block has been freed. */
    uint freed;
    /** Incremented when a new reference on a block has been added. */
    uint referenced;
    /** Incremented when a release action on a block has been done. */
    uint released;
};
typedef struct blocks_t blocks_t;

blocks_t blocks;

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 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;

blk_t *
blk_alloc_desc_ (void_FL)
{
    u8 *data = malloc (BLK_SIZE + BLK_REFCNT_SIZE + BLK_DESC_SIZE);
    if (!data)
        dbg_fatal ("exhausted virtual memory");
    blk_t *blk = TO_BLK (data);
    dbg_invalid_ptr (blk->next);
    blk->data = data;
    *REFCNT(blk) = 1;
    blocks.allocated++;
    blocks.referenced++;
    restrack_create (NULL, blk, _fl_, 1);
    return blk;
}

blk_t *
blk_alloc_desc_range_ (uint n, blk_t **last __FL)
{
    blk_t *first, *b;
    dbg_assert (n);
    dbg_assert_ptr (last);
    first = b = blk_alloc_desc_ (_fl);
    for (n--; n; n--)
    {
        b->next = blk_alloc_desc_ (_fl);
        b = b->next;
    }
    *last = b;
    return first;
}

static void
blk_free_desc_ (blk_t *blk __FL)
{
    dbg_assert_ptr (blk);
    dbg_assert (TO_BLK (blk->data) == blk);
    restrack_destroy (NULL, blk, _fl_, 0);
    blocks.freed++;
    free (blk->data);
}

void
blk_addref_desc_ (blk_t *blk __FL)
{
    dbg_assert_ptr (blk);
    dbg_assert (TO_BLK (blk->data) == blk);
    arch_atomic_add (REFCNT (blk), 1);
    restrack_update (NULL, blk, _fl_, 1);
    blocks.referenced++;
}

void
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
blk_addref_desc_range_nb_ (blk_t *first, uint n __FL)
{
    dbg_assert (n);
    blk_t *b;
    uint i;
    for (b = first, i = n; i; b = b->next, i--)
    {
        blk_addref_desc_ (b __fl);
    }
}

void
blk_release_desc_ (blk_t *blk __FL)
{
    dbg_assert_ptr (blk);
    dbg_assert (TO_BLK (blk->data) == blk);
    dbg_assert (((blk_obj_t *) blk)->magic != BLK_OBJ_MAGIC);
    dbg_assert (REFCNT (blk) != 0);
    restrack_update (NULL, blk, _fl_, -1);
    blocks.released++;
    if (arch_atomic_add (REFCNT (blk), -1) == 0)
    {
        blk_free_desc_ (blk __fl);
    }
}

void
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
blk_release_desc_range_nb_ (blk_t *first, uint n __FL)
{
    dbg_assert (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
blk_addref_ (void *data __FL)
{
    dbg_assert_ptr (data);
    blk_t *blk = TO_BLK (data);
    dbg_assert (((blk_obj_t *) blk)->magic == BLK_OBJ_MAGIC);
    blk_addref_desc_ (blk __fl);
}

void
blk_release_ (void *data __FL)
{
    dbg_assert_ptr (data);
    blk_obj_t *obj = (blk_obj_t *) TO_BLK (data);
    dbg_assert_ptr (obj);
    dbg_assert (obj->magic == BLK_OBJ_MAGIC);
    restrack_update (NULL, obj, _fl_, -1);
    blocks.released++;
    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 ()
        && blocks.allocated == blocks.freed
        && blocks.referenced == blocks.released;
}

void
blk_print_memory (void)
{
    fprintf (stderr, "[MEM STATE] Allocated : %d\t Freed : %d\t Referenced : %d\t Released : %d\n",
                    blocks.allocated, blocks.freed,
                    blocks.referenced, blocks.released
                   );
}