summaryrefslogtreecommitdiff
path: root/infra/packet_manager/packet_pool.c
blob: 45c09cec14fe41add55329f479aec4a67c0f167e (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
#include <assert.h>
#include <stdlib.h>

#include "packet_pool.h"
#include "packet_internal.h"

struct packet_pool
{
    uint64_t capacity;
    uint64_t used;
    uint64_t free;
    struct packet_queue free_list;
};

struct packet_pool *packet_pool_new(uint64_t capacity)
{
    struct packet_pool *pool = (struct packet_pool *)calloc(1, sizeof(struct packet_pool));
    if (pool == NULL)
    {
        return NULL;
    }
    pool->capacity = capacity;
    pool->used = 0;
    pool->free = 0;
    TAILQ_INIT(&pool->free_list);

    for (uint64_t i = 0; i < capacity; i++)
    {
        struct packet *pkt = (struct packet *)calloc(1, sizeof(struct packet));
        if (pkt == NULL)
        {
            goto error_out;
        }
        TAILQ_INSERT_TAIL(&pool->free_list, pkt, pool_tqe);
        pool->free++;
    }

    return pool;

error_out:
    packet_pool_free(pool);
    return NULL;
}

void packet_pool_free(struct packet_pool *pool)
{
    struct packet *pkt;
    if (pool)
    {
        while ((pkt = TAILQ_FIRST(&pool->free_list)))
        {
            TAILQ_REMOVE(&pool->free_list, pkt, pool_tqe);
            free(pkt);
            pool->free--;
        }

        assert(pool->used == 0);
        assert(pool->free == 0);

        free(pool);
        pool = NULL;
    }
}

struct packet *packet_pool_acquire_packet(struct packet_pool *pool)
{
    struct packet *pkt = TAILQ_FIRST(&pool->free_list);
    if (pkt == NULL)
    {
        pkt = (struct packet *)calloc(1, sizeof(struct packet));
        if (pkt == NULL)
        {
            return NULL;
        }
    }
    else
    {
        TAILQ_REMOVE(&pool->free_list, pkt, pool_tqe);
        pool->free--;
    }

    pool->used++;

    return pkt;
}

void packet_pool_release_packet(struct packet_pool *pool, struct packet *pkt)
{
    if (pool == NULL || pkt == NULL)
    {
        return;
    }

    pool->used--;

    if (pool->free < pool->capacity)
    {
        TAILQ_INSERT_TAIL(&pool->free_list, pkt, pool_tqe);
        pool->free++;
    }
    else
    {
        free(pkt);
    }
}

uint64_t packet_pool_get_used_num(const struct packet_pool *pool)
{
    return pool->used;
}

uint64_t packet_pool_get_free_num(const struct packet_pool *pool)
{
    return pool->free;
}