summaryrefslogtreecommitdiff
path: root/hifi/xaf/hifi-dpf/core/xf-msg.c
blob: 4253e1e3facc7912061445b77f151600620da496 (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
/*******************************************************************************
* 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-msg.c
 *
 * Message/message pool handling
 *
 ******************************************************************************/

#define MODULE_TAG                      MSG

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

#include "xf.h"

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

/* ...allocate message pool */
int xf_msg_pool_init(xf_msg_pool_t *pool, u32 n, u32 core)
{   
    u32     i;
    
    /* ...allocate shared memory from global pool */
    XF_CHK_ERR(pool->p = xf_mem_alloc(XF_MM(sizeof(*pool->p) * n), XF_PROXY_ALIGNMENT, core, 1), -ENOMEM);
    
    /* ...place all messages into single-liked list */
    for (pool->head = &pool->p[i = 0]; i < n - 1; i++)
    {
        /* ...set message pointer to next message in the pool */
        xf_msg_pool_item(pool, i)->next = xf_msg_pool_item(pool, i + 1);
    }
    
    /* ...set tail of the list */
    xf_msg_pool_item(pool, i)->next = NULL;

    /* ...save pool size */
    pool->n = n;
    
    return 0;
}

/* ...destroy memory pool */
void xf_msg_pool_destroy(xf_msg_pool_t *pool, u32 core)
{
    /* ...release pool memory (from shared local-IPC memory) */
    xf_mem_free(pool->p, XF_MM(sizeof(*pool->p) * pool->n), core, 1);
}

/* ...allocate message from a pool (no concurrent access from other cores) */
xf_message_t * xf_msg_pool_get(xf_msg_pool_t *pool)
{
    __xf_message_t  *_m;
    
    /* ...pop message from the head of the pool */
    XF_CHK_ERR(_m = pool->head, NULL);
    
    /* ...advance list head */
    pool->head = (__xf_message_t *)(((xf_message_t *) _m)->next);

    /* ...debug - wipe out message "next" pointer */
    ((xf_message_t *) _m)->next = NULL;
    
    /* ...return properly aligned message pointer */
    return (xf_message_t *) _m;
}

/* ...return message back to the pool (no concurrent access from other cores) */
void xf_msg_pool_put(xf_msg_pool_t *pool, xf_message_t *m)
{
    __xf_message_t  *_m = (__xf_message_t *) m;
    
    /* ...make sure the message is properly aligned object */
    BUG(!XF_IS_ALIGNED(_m), _x("Corrupted message pointer: %p"), _m);
    
    /* ...make sure it is returned to the same pool (need a length for that - tbd) */
    BUG(!xf_msg_from_pool(pool, m) < 0, _x("Bad pool/message: %p/%p"), pool->p, _m);

    /* ...place message into the head */
    m->next = (xf_message_t *) pool->head;
    
    /* ...advance pool head */
    pool->head = _m;
}