From 32dccec98e4c7d7e4ce902e19ba8d1b29b823758 Mon Sep 17 00:00:00 2001 From: Jordan Augé Date: Wed, 23 Sep 2020 17:50:52 +0200 Subject: [HICN-570] Message buffer (incl. CS and PIT changes) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I4c508e4b04dee3acbfc3da1d26e1770cb826f22b Signed-off-by: Jordan Augé --- hicn-light/src/hicn/core/msgbuf_pool.c | 84 ++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 hicn-light/src/hicn/core/msgbuf_pool.c (limited to 'hicn-light/src/hicn/core/msgbuf_pool.c') diff --git a/hicn-light/src/hicn/core/msgbuf_pool.c b/hicn-light/src/hicn/core/msgbuf_pool.c new file mode 100644 index 000000000..597123a7a --- /dev/null +++ b/hicn-light/src/hicn/core/msgbuf_pool.c @@ -0,0 +1,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; +} -- cgit 1.2.3-korg