aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-light/src/hicn/core/msgbuf_pool.c
diff options
context:
space:
mode:
Diffstat (limited to 'hicn-light/src/hicn/core/msgbuf_pool.c')
-rw-r--r--hicn-light/src/hicn/core/msgbuf_pool.c84
1 files changed, 84 insertions, 0 deletions
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;
+}