EtcPal  0.4.1
ETC Platform Abstraction Layer (EtcPal)
View other versions:
mempool.h
1 /******************************************************************************
2  * Copyright 2022 ETC Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************
16  * This file is a part of EtcPal. For more information, go to:
17  * https://github.com/ETCLabs/EtcPal
18  ******************************************************************************/
19 
20 /* etcpal/mempool.h: Fixed-size memory pools. */
21 
22 #ifndef ETCPAL_MEMPOOL_H_
23 #define ETCPAL_MEMPOOL_H_
24 
25 #include <stddef.h>
26 #include "etcpal/error.h"
27 
83 #ifdef __cplusplus
84 extern "C" {
85 #endif
86 
89 typedef struct EtcPalMempool EtcPalMempool;
90 
94 struct EtcPalMempool
95 {
96  EtcPalMempool* next;
97 };
98 
106 typedef struct EtcPalMempoolDesc
107 {
108  const size_t elem_size;
109  const size_t pool_size;
110  EtcPalMempool* freelist;
111  EtcPalMempool* const list;
112  size_t current_used;
113  void* const pool;
114 } EtcPalMempoolDesc;
115 
126 #define ETCPAL_MEMPOOL_DECLARE(name) extern EtcPalMempoolDesc name##_pool_desc;
127 
139 #define ETCPAL_MEMPOOL_DEFINE(name, type, size) \
140  type name##_pool[size]; \
141  struct EtcPalMempool name##_pool_list[size]; \
142  struct EtcPalMempoolDesc name##_pool_desc = {sizeof(type), /* elem_size */ \
143  size, /* pool_size */ \
144  NULL, /* freelist */ \
145  name##_pool_list, /* list */ \
146  0, /* current_used */ \
147  name##_pool /* pool */}
148 
160 #define ETCPAL_MEMPOOL_DEFINE_ARRAY(name, type, array_size, pool_size) \
161  type name##_pool[array_size][pool_size]; \
162  struct EtcPalMempool name##_pool_list[pool_size]; \
163  struct EtcPalMempoolDesc name##_pool_desc = {sizeof(type[array_size]), /* elem_size */ \
164  pool_size, /* pool_size */ \
165  NULL, /* freelist */ \
166  name##_pool_list, /* list */ \
167  0, /* current_used */ \
168  name##_pool /* pool */}
169 
181 #define etcpal_mempool_init(name) etcpal_mempool_init_priv(&name##_pool_desc)
182 
188 #define etcpal_mempool_alloc(name) etcpal_mempool_alloc_priv(&name##_pool_desc)
189 
200 #define etcpal_mempool_free(name, mem) etcpal_mempool_free_priv(&name##_pool_desc, mem)
201 
210 #define etcpal_mempool_size(name) (name##_pool_desc.pool_size)
211 
217 #define etcpal_mempool_used(name) etcpal_mempool_used_priv(&name##_pool_desc)
218 
221 etcpal_error_t etcpal_mempool_init_priv(EtcPalMempoolDesc* desc);
222 void* etcpal_mempool_alloc_priv(EtcPalMempoolDesc* desc);
223 void etcpal_mempool_free_priv(EtcPalMempoolDesc* desc, void* elem);
224 size_t etcpal_mempool_used_priv(EtcPalMempoolDesc* desc);
225 
228 #ifdef __cplusplus
229 }
230 #endif
231 
236 #endif /* ETCPAL_MEMPOOL_H_ */
etcpal_error_t
A set of error codes that can be returned by library functions.
Definition: error.h:49