summaryrefslogtreecommitdiff
path: root/hifi/xaf/hifi-dpf/include/xf-mm.h
blob: 712ea14a941ec0c1e2145f6c1fd1bcc1125fab34 (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
/*******************************************************************************
* 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-mm.h
 *
 * Generic dynamic memory manager (based on rb-tree index)
 *
 *******************************************************************************/

#ifndef __XF_H
#error "xf-mem.h mustn't be included directly"
#endif

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

/* ...red-black trees library */
#include "lib/rbtree.h"

/*******************************************************************************
 * Cache-line aligned types
 ******************************************************************************/

/* ...proper cache-line alignment */
#define XF_ALIGNED(size)                            \
    (((size) + XF_PROXY_ALIGNMENT - 1) & ~(XF_PROXY_ALIGNMENT - 1))

/* ...cache-line aligned type identifier */
#define XF_ALIGNED_TYPE(type)                       \
    __xf_aligned_##type

/* ...definition of cache-line aligned type */
#define XF_ALIGNED_TYPEDEF(type, name)              \
typedef union                                       \
{                                                   \
    /* ...item of original type */                  \
    type        __obj;                              \
                                                    \
    /* ...padding to cache-line */                  \
    u8          __pad[XF_ALIGNED(sizeof(type))];    \
                                                    \
}   XF_ALIGNED_TYPE(type), name __xf_shmem__

/* ...accessor to original type */
#define XF_ALIGNED_OBJ(p)                           \
    (&(p)->__obj)

#define XF_IS_ALIGNED(p)                            \
    (((u32)(p) & (XF_PROXY_ALIGNMENT - 1)) == 0)

/*******************************************************************************
 * Memory pool description
 ******************************************************************************/

/* ...memory allocator data */
typedef struct xf_mm_pool
{
    /* ...free blocks map sorted by block length */
    rb_tree_t       l_map;
    
    /* ...free blocks map sorted by address of the block */
    rb_tree_t       a_map;

    /* ...address of memory pool (32-bytes aligned at least); need that? - tbd */
    void           *addr;

    /* ...length of the pool (multiple of descriptor size); need that? - tbd */
    u32             size;    
    
}   xf_mm_pool_t;

/* ...descriptor of free memory block */
typedef struct xf_mm_block
{
    /* ...rb-tree node in a block-length map */
    rb_node_t       l_node;
    
    /* ...rb-tree node in a block-address map */
    rb_node_t       a_node;
    
}   xf_mm_block_t;

/* ...properly aligned allocation unit */
typedef u8 xf_mm_item[xf_next_power_of_two(sizeof(xf_mm_block_t))];

/* ...macro to assure proper alignment of dynamically allocated data */
#define XF_MM(size)         (((size) + sizeof(xf_mm_item) - 1) & ~(sizeof(xf_mm_item) - 1))

/* ...check if memory is properly aligned */
#define XF_MM_ALIGNED(size) (!((size) & (sizeof(xf_mm_item) - 1)))

/* ...alignement definition */
#define __xf_mm__  __attribute__((__aligned__(sizeof(xf_mm_item))))
    
/*******************************************************************************
 * Dynamically allocated buffer
 ******************************************************************************/

/* ...memory allocation metadata */
typedef struct xf_mm_buffer
{
    /* ...allocation address */
    void           *addr;
    
    /* ...length */
    u32             size;
    
}   __xf_mm__ xf_mm_buffer_t;

/*******************************************************************************
 * API functions
 ******************************************************************************/

/* ...pool initialization */
extern int      xf_mm_init(xf_mm_pool_t *pool, void *addr, u32 size);

/* ...block allocation */
extern void *   xf_mm_alloc(xf_mm_pool_t *pool, u32 size);

/* ...block deallocation */
extern void     xf_mm_free(xf_mm_pool_t *pool, void *addr, u32 size);