summaryrefslogtreecommitdiff
path: root/hifi/xaf/hifi-dpf/core/xf-mem.c
blob: ce5d8a6ad5b964566ee9829cc130fd13d4813b03 (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
/*******************************************************************************
* Copyright (C) 2018 Cadence Design Systems, Inc.
* 
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to use this Software with Cadence processor cores only and 
* not with any other processors and platforms, subject to
* the following conditions:
* 
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

******************************************************************************/

/*******************************************************************************
 * xf-mem.c
 *
 * Dynamic memory allocator implementation (based on rb-tree index)
 *
 ******************************************************************************/

#define MODULE_TAG                      MM

/*******************************************************************************
 * Includes
 ******************************************************************************/

#include "xf.h"

/*******************************************************************************
 * Tracing configuration
 ******************************************************************************/

TRACE_TAG(INIT, 1);

/*******************************************************************************
 * Internal helpers
 ******************************************************************************/

/* ...initialize block */
static inline xf_mm_block_t * xf_mm_block_init(void *addr, u32 size)
{
    xf_mm_block_t  *b = (xf_mm_block_t *)addr;
    
    /* ...use 31 available bits of node color to keep aligned size */
    return b->l_node.color = size, b;
}

/* ...check if the length of the block is less than given */
static inline int xf_mm_block_length_less(xf_mm_block_t *b, u32 size)
{
    /* ...we don't really care about LSB of color */
    return (b->l_node.color < size);
}

/* ...return exact block length */
static inline u32 xf_mm_block_length(xf_mm_block_t *b)
{
    /* ...wipe out least-significant bit from node color */
    return (b->l_node.color & ~1);
}

/* ...increase block length */
static inline u32 xf_mm_block_length_add(xf_mm_block_t *b, u32 size)
{
    /* ...return exact block length after increase */
    return ((b->l_node.color += size) & ~1);
}

/* ...decrease block length */
static inline u32 xf_mm_block_length_sub(xf_mm_block_t *b, u32 size)
{
    /* ...return exact block length after decrease */
    return ((b->l_node.color -= size) & ~1);
}

/*******************************************************************************
 * Internal functions
 ******************************************************************************/

/* ...find best-match node given requested size */
static inline  xf_mm_block_t * xf_mm_find_by_size(xf_mm_pool_t *pool, u32 size)
{
    rb_tree_t *tree = &pool->l_map;
    rb_idx_t   p_idx, t_idx;
    
    /* ...find first block having length greater than requested */
    for (p_idx = rb_root(tree); p_idx != rb_null(tree); p_idx = rb_right(tree, p_idx))
    {
        xf_mm_block_t  *b = container_of(p_idx, xf_mm_block_t, l_node);
        
        /* ...break upon finding first matching candidate */
        if (!xf_mm_block_length_less(b, size))
            break;
    }

    /* ...bail out if haven't found a block with sufficient size */
    if (p_idx == rb_null(tree))
        return NULL;

    /* ...try to find better match in left subtree */
    for (t_idx = rb_left(tree, p_idx); t_idx != rb_null(tree); )
    {
        xf_mm_block_t  *b = container_of(t_idx, xf_mm_block_t, l_node);

        /* ...check the size of the block */
        if (!xf_mm_block_length_less(b, size))
        {
            /* ...update best match */
            p_idx = t_idx;

            /* ...and check if we have anything better in left sbtree */
            t_idx = rb_left(tree, t_idx);
        }
        else
        {
            /* ...move towards higher block sizes in that subtree */
            t_idx = rb_right(tree, t_idx);
        }
    }

    /* ...p_idx is our best choice */
    return container_of(p_idx, xf_mm_block_t, l_node);
}

/* ...find the neighbours of the block basing on its address */
static void xf_mm_find_by_addr(xf_mm_pool_t *pool, void *addr, xf_mm_block_t **n)
{
    rb_tree_t  *tree = &pool->a_map;
    rb_idx_t    p_idx, l_idx, r_idx;

    /* ...it is not possible to have exact match in this map */
    for (p_idx = rb_root(tree), l_idx = r_idx = NULL; p_idx != rb_null(tree); )
    {
        /* ...only "is less than" comparison is valid (as "a_node" pointer is biased) */
        if ((u32)p_idx < (u32)addr)
        {
            /* ...update lower neighbour */
            l_idx = p_idx;
            
            /* ...and move towards higher addresses */
            p_idx = rb_right(tree, p_idx);
        }
        else
        {
            /* ...update higher neighbour */
            r_idx = p_idx;
            
            /* ...and move towards lower addresses */
            p_idx = rb_left(tree, p_idx);
        }
    }

    /* ...translate nodes into blocks */
    n[0] = (l_idx ? container_of(l_idx, xf_mm_block_t, a_node) : NULL);
    n[1] = (r_idx ? container_of(r_idx, xf_mm_block_t, a_node) : NULL);
}

/* ...insert the block into L-map */
static void xf_mm_insert_size(xf_mm_pool_t *pool, xf_mm_block_t *b, u32 size)
{
    rb_tree_t  *tree = &pool->l_map;
    rb_idx_t    p_idx, t_idx;
    
    /* ...find the parent node for the next block */
    for (p_idx = rb_root(tree); p_idx != rb_null(tree); p_idx = t_idx)
    {
        /* ...check for the size */
        if (xf_mm_block_length_less(container_of(p_idx, xf_mm_block_t, l_node), size))
        {
            /* ...move towards higher addresses */
            if ((t_idx = rb_right(tree, p_idx)) == rb_null(tree))
            {
                /* ...node becomes a right child of parent p */
                rb_set_right(tree, p_idx, &b->l_node);
                break;
            }
        }
        else
        {
            /* ...move towards lower addresses (ok if exact size match is found) */
            if ((t_idx = rb_left(tree, p_idx)) == rb_null(tree))
            {
                /* ...node becomes a left child of parent p */
                rb_set_left(tree, p_idx, &b->l_node);
                break;
            }
        }
    }

    /* ...insert node into tree */
    rb_insert(tree, &b->l_node, p_idx);
}

/* ...insert the block into A-map */
static void xf_mm_insert_addr(xf_mm_pool_t *pool, xf_mm_block_t *b)
{
    rb_tree_t  *tree = &pool->a_map;
    rb_idx_t    p_idx, t_idx;
    
    /* ...find the parent node for the next block */
    for (p_idx = rb_root(tree); p_idx != rb_null(tree); p_idx = t_idx)
    {
        /* ...check for the address (only "is less than" comparison is valid) */
        if ((u32)p_idx < (u32)b)
        {
            /* ...move towards higher addresses */
            if ((t_idx = rb_right(tree, p_idx)) == rb_null(tree))
            {
                /* ...node becomes a right child of parent p */
                rb_set_right(tree, p_idx, &b->a_node);
                break;
            }
        }
        else
        {
            /* ...move towards lower addresses (by design there cannot be exact match) */
            if ((t_idx = rb_left(tree, p_idx)) == rb_null(tree))
            {
                /* ...node becomes a left child of parent p */
                rb_set_left(tree, p_idx, &b->a_node);
                break;
            }
        }
    }

    /* ...insert node into tree */
    rb_insert(tree, &b->a_node, p_idx);
}

/*******************************************************************************
 * Entry points
 ******************************************************************************/

/* ...block allocation */
void * xf_mm_alloc(xf_mm_pool_t *pool, u32 size)
{
    xf_mm_block_t  *b;
    
    /* ...find best-fit free block */
    XF_CHK_ERR(b = xf_mm_find_by_size(pool, size), NULL);
    
    /* ...remove the block from the L-map */
    rb_delete(&pool->l_map, &b->l_node);
    
    /* ...check if the size is exactly the same as requested */
    if ((size = xf_mm_block_length_sub(b, size)) == 0)
    {
        /* ...the block needs to be removed from the A-map as well */
        rb_delete(&pool->a_map, &b->a_node);

        /* ...entire block goes to user */
        return (void *) b;
    }
    else
    {
        /* ...insert the block into L-map */
        xf_mm_insert_size(pool, b, size);
        
        /* ...A-map remains intact; tail of the block goes to user */
        return (void *) b + size;
    }
}

/* ...block deallocation */
void xf_mm_free(xf_mm_pool_t *pool, void *addr, u32 size)
{
    xf_mm_block_t  *b = xf_mm_block_init(addr, size);
    xf_mm_block_t  *n[2];
    
    /* ...find block neighbours in A-map */
    xf_mm_find_by_addr(pool, addr, n);

    /* ...check if we can merge block to left neighbour */
    if (n[0])
    {
        if ((void *)n[0] + xf_mm_block_length(n[0]) == addr)
        {
            /* ...merge free block with left neighbour; delete it from L-map */
            rb_delete(&pool->l_map, &n[0]->l_node);
        
            /* ...adjust block length (block remains in A-map) */
            addr = (void *)(b = n[0]), size = xf_mm_block_length_add(b, size);
        }
        else
        {
            /* ...mark there is no left-merge */
            n[0] = NULL;
        }
    }
    
    /* ...check if we can merge block to right neighbour */
    if (n[1])
    {
        if ((void *)n[1] == addr + size)
        {
            /* ...merge free block with right neighbour; delete it from L-map */
            rb_delete(&pool->l_map, &n[1]->l_node);
        
            /* ...adjust block length */
            size = xf_mm_block_length_add(b, xf_mm_block_length(n[1]));

            /* ...check if left merge took place as well */
            if (n[0])
            {
                /* ...left neighbour covers now all three blocks; drop record from A-map */
                rb_delete(&pool->a_map, &n[1]->a_node);
            }
            else
            {
                /* ...fixup tree pointers (equivalent to remove/reinsert the same key) */
                rb_replace(&pool->a_map, &n[1]->a_node, &b->a_node);
            }
        }
        else
        {
            n[1] = NULL;
        }
    }

    /* ...if any merge has occured, A-map is updated */
    if (n[0] == NULL && n[1] == NULL)
    {
        /* ...add new block into A-map */
        xf_mm_insert_addr(pool, b);
    }
    
    /* ...add (new or adjusted) block into L-map */
    xf_mm_insert_size(pool, b, size);
}

/* ...initialize memory allocator */
int xf_mm_init(xf_mm_pool_t *pool, void *addr, u32 size)
{
    /* ...check pool alignment validity */
    XF_CHK_ERR(((u32)addr & (sizeof(xf_mm_block_t) - 1)) == 0, -EINVAL);

    /* ...check pool size validity */
    XF_CHK_ERR(((size) & (sizeof(xf_mm_block_t) - 1)) == 0, -EINVAL);
    
    /* ...set pool parameters (need that stuff at all? - tbd) */    
    pool->addr = addr, pool->size = size;

    /* ...initialize rb-trees */
    rb_init(&pool->l_map), rb_init(&pool->a_map);

    /* ..."free" the entire block */
    xf_mm_free(pool, addr, size);

    TRACE(INIT, _b("memory allocator initialized: [%p..%p)"), addr, addr + size);
    
    return 0;
}