aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-light/src/hicn/core/msgbuf_pool.c
blob: 597123a7acb6df485e05a89b89be21a5c408b373 (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
/*
 * Copyright (c) 2020 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file msgbuf_pool.c
 * @brief Implementation of hICN packet pool.
 */

#include "../base/pool.h"
#include "msgbuf_pool.h"

#define PACKET_POOL_DEFAULT_INIT_SIZE 1024

msgbuf_pool_t *
_msgbuf_pool_create(size_t init_size, size_t max_size)
{
    msgbuf_pool_t * msgbuf_pool = malloc(sizeof(msgbuf_pool_t));

    if (init_size == 0)
        init_size = PACKET_POOL_DEFAULT_INIT_SIZE;

    pool_init(msgbuf_pool->buffers, init_size, 0);

    return msgbuf_pool;
}

void
msgbuf_pool_free(msgbuf_pool_t * msgbuf_pool)
{
    pool_free(msgbuf_pool->buffers);
    free(msgbuf_pool);
}

int
msgbuf_pool_get(msgbuf_pool_t * msgbuf_pool, msgbuf_t * msgbuf)
{
    pool_get(msgbuf_pool->buffers, msgbuf);
    return 0;
}

void
msgbuf_pool_put(msgbuf_pool_t * msgbuf_pool, msgbuf_t * msgbuf)
{
    pool_put(msgbuf_pool->buffers, msgbuf);
}

int
msgbuf_pool_getn(msgbuf_pool_t * msgbuf_pool, msgbuf_t ** msgbuf, size_t n)
{
    for (unsigned i = 0; i < n; i++) {
        if (!msgbuf_pool_get(msgbuf_pool, msgbuf[i])) {
            for (unsigned j = 0; j < i; j++) {
                msgbuf_pool_put(msgbuf_pool, msgbuf[j]);
                return 0;
            }
            break;
        }
    }
    return -1;
}

off_t
msgbuf_pool_get_id(msgbuf_pool_t * msgbuf_pool, msgbuf_t * msgbuf)
{
    return msgbuf - msgbuf_pool->buffers;
}

msgbuf_t *
msgbuf_pool_at(const msgbuf_pool_t * msgbuf_pool, off_t id)
{
    return msgbuf_pool->buffers + id;
}