summaryrefslogtreecommitdiff
path: root/third_party/cherrypool/chry_pool.c
blob: b7d562dc15f9f82a8d2abbfe8dab7809d6710d28 (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
#include "chry_pool.h"

int chry_pool_create(struct chry_pool *pool, uint32_t *ringbuf, void *item, uint32_t item_size, uint32_t item_count)
{
    uintptr_t addr;

    chry_ringbuffer_init(&pool->rb, ringbuf, sizeof(uintptr_t) * item_count);

    for (uint32_t i = 0; i < item_count; i++) {
        addr = ((uintptr_t)item + i * item_size);
        chry_ringbuffer_write(&pool->rb, &addr, sizeof(uintptr_t));
    }
    pool->mq = usb_osal_mq_create(item_count);
    if (pool->mq == NULL) {
        return -1;
    }
    return 0;
}

uintptr_t *chry_pool_item_alloc(struct chry_pool *pool)
{
    uintptr_t *addr;
    bool ret;

    ret = chry_ringbuffer_read(&pool->rb, (uintptr_t *)&addr, sizeof(uintptr_t));
    if (ret == false) {
        return NULL;
    } else {
        return addr;
    }
}

int chry_pool_item_free(struct chry_pool *pool, uintptr_t *item)
{
    uintptr_t addr;

    addr = (uintptr_t)item;
    return chry_ringbuffer_write(&pool->rb, &addr, sizeof(uintptr_t));
}

int chry_pool_item_send(struct chry_pool *pool, uintptr_t *frame)
{
    return usb_osal_mq_send(pool->mq, (uintptr_t)frame);
}

int chry_pool_item_recv(struct chry_pool *pool, uintptr_t **frame, uint32_t timeout)
{
    return usb_osal_mq_recv(pool->mq, (uintptr_t *)frame, timeout);
}