NimBLE-Arduino 2.1.2
Loading...
Searching...
No Matches
os_mempool.h
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
28#ifndef _OS_MEMPOOL_H_
29#define _OS_MEMPOOL_H_
30
31#include <stdbool.h>
32#include "os.h"
33#include "queue.h"
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
46 SLIST_ENTRY(os_memblock) mb_next;
47};
48
49/* XXX: Change this structure so that we keep the first address in the pool? */
50/* XXX: add memory debug structure and associated code */
51/* XXX: Change how I coded the SLIST_HEAD here. It should be named:
52 SLIST_HEAD(,os_memblock) mp_head; */
53
57struct os_mempool {
59 uint32_t mp_block_size;
61 uint16_t mp_num_blocks;
63 uint16_t mp_num_free;
65 uint16_t mp_min_free;
67 uint8_t mp_flags;
70 STAILQ_ENTRY(os_mempool) mp_list;
71 SLIST_HEAD(,os_memblock);
73 const char *name;
74};
75
80#define OS_MEMPOOL_F_EXT 0x01
81
82struct os_mempool_ext;
83
102typedef os_error_t os_mempool_put_fn(struct os_mempool_ext *ome, void *data,
103 void *arg);
104
105struct os_mempool_ext {
106 struct os_mempool mpe_mp;
107
108 /* Callback that is executed immediately when a block is freed. */
109 os_mempool_put_fn *mpe_put_cb;
110 void *mpe_put_arg;
111};
112
113#define OS_MEMPOOL_INFO_NAME_LEN (32)
114
129 char omi_name[OS_MEMPOOL_INFO_NAME_LEN];
130};
131
143 struct os_mempool_info *);
144
145/*
146 * To calculate size of the memory buffer needed for the pool. NOTE: This size
147 * is NOT in bytes! The size is the number of os_membuf_t elements required for
148 * the memory pool.
149 */
150#if MYNEWT_VAL(OS_MEMPOOL_GUARD)
151/*
152 * Leave extra 4 bytes of guard area at the end.
153 */
154#define OS_MEMPOOL_BLOCK_SZ(sz) ((sz) + sizeof(os_membuf_t))
155#else
156#define OS_MEMPOOL_BLOCK_SZ(sz) (sz)
157#endif
158#if (OS_ALIGNMENT == 4)
159typedef uint32_t os_membuf_t;
160#elif (OS_ALIGNMENT == 8)
161typedef uint64_t os_membuf_t;
162#elif (OS_ALIGNMENT == 16)
163typedef __uint128_t os_membuf_t;
164#else
165#error "Unhandled `OS_ALIGNMENT` for `os_membuf_t`"
166#endif /* OS_ALIGNMENT == * */
167#define OS_MEMPOOL_SIZE(n,blksize) ((((blksize) + ((OS_ALIGNMENT)-1)) / (OS_ALIGNMENT)) * (n))
168
170#define OS_MEMPOOL_BYTES(n,blksize) \
171 (sizeof (os_membuf_t) * OS_MEMPOOL_SIZE((n), (blksize)))
172
173#if SOC_ESP_NIMBLE_CONTROLLER && CONFIG_BT_CONTROLLER_ENABLED
185os_error_t r_os_mempool_init(struct os_mempool *mp, uint16_t blocks,
186 uint32_t block_size, void *membuf, const char *name);
187#define os_mempool_init r_os_mempool_init
201os_error_t r_os_mempool_ext_init(struct os_mempool_ext *mpe, uint16_t blocks,
202 uint32_t block_size, void *membuf, const char *name);
203#define os_mempool_ext_init r_os_mempool_ext_init
213os_error_t r_os_mempool_unregister(struct os_mempool *mp);
214#define os_mempool_unregister r_os_mempool_unregister
215
216
224os_error_t r_os_mempool_clear(struct os_mempool *mp);
225#define os_mempool_clear r_os_mempool_clear
226
227
238bool r_os_mempool_is_sane(const struct os_mempool *mp);
239#define os_mempool_is_sane r_os_mempool_is_sane
240
241
251int r_os_memblock_from(const struct os_mempool *mp, const void *block_addr);
252#define os_memblock_from r_os_memblock_from
253
254
262void *r_os_memblock_get(struct os_mempool *mp);
263#define os_memblock_get r_os_memblock_get
274os_error_t r_os_memblock_put_from_cb(struct os_mempool *mp, void *block_addr);
275#define os_memblock_put_from_cb r_os_memblock_put_from_cb
276
277
286os_error_t r_os_memblock_put(struct os_mempool *mp, void *block_addr);
287#define os_memblock_put r_os_memblock_put
288
289#else
301os_error_t os_mempool_init(struct os_mempool *mp, uint16_t blocks,
302 uint32_t block_size, void *membuf, const char *name);
303
317os_error_t os_mempool_ext_init(struct os_mempool_ext *mpe, uint16_t blocks,
318 uint32_t block_size, void *membuf, const char *name);
319
329os_error_t os_mempool_unregister(struct os_mempool *mp);
330
338os_error_t os_mempool_clear(struct os_mempool *mp);
339
347os_error_t os_mempool_ext_clear(struct os_mempool_ext *mpe);
348
359bool os_mempool_is_sane(const struct os_mempool *mp);
360
370int os_memblock_from(const struct os_mempool *mp, const void *block_addr);
371
379void *os_memblock_get(struct os_mempool *mp);
380
391os_error_t os_memblock_put_from_cb(struct os_mempool *mp, void *block_addr);
392
401os_error_t os_memblock_put(struct os_mempool *mp, void *block_addr);
402#endif
403
404#ifdef __cplusplus
405}
406#endif
407
408#endif /* _OS_MEMPOOL_H_ */
409
410
void * os_memblock_get(struct os_mempool *mp)
Definition os_mempool.c:341
os_error_t os_mempool_unregister(struct os_mempool *mp)
Definition os_mempool.c:208
os_error_t os_mempool_ext_init(struct os_mempool_ext *mpe, uint16_t blocks, uint32_t block_size, void *membuf, const char *name)
Definition os_mempool.c:190
bool os_mempool_is_sane(const struct os_mempool *mp)
Definition os_mempool.c:297
os_error_t os_mempool_init(struct os_mempool *mp, uint16_t blocks, uint32_t block_size, void *membuf, const char *name)
Definition os_mempool.c:183
struct os_mempool * os_mempool_info_get_next(struct os_mempool *, struct os_mempool_info *)
Definition os_mempool.c:458
os_error_t os_mempool_clear(struct os_mempool *mp)
Definition os_mempool.c:246
os_error_t os_mempool_ext_clear(struct os_mempool_ext *mpe)
Definition os_mempool.c:287
os_error_t os_mempool_put_fn(struct os_mempool_ext *ome, void *data, void *arg)
Definition os_mempool.h:102
os_error_t os_memblock_put_from_cb(struct os_mempool *mp, void *block_addr)
Definition os_mempool.c:380
int os_memblock_from(const struct os_mempool *mp, const void *block_addr)
Definition os_mempool.c:314
os_error_t os_memblock_put(struct os_mempool *mp, void *block_addr)
Definition os_mempool.c:410
Definition os_mempool.h:45
Definition os_mempool.h:119
int omi_min_free
Definition os_mempool.h:127
int omi_num_blocks
Definition os_mempool.h:123
char omi_name[OS_MEMPOOL_INFO_NAME_LEN]
Definition os_mempool.h:129
int omi_num_free
Definition os_mempool.h:125
int omi_block_size
Definition os_mempool.h:121
Definition os_mempool.h:57
uint8_t mp_flags
Definition os_mempool.h:67
uint16_t mp_num_blocks
Definition os_mempool.h:61
uint16_t mp_num_free
Definition os_mempool.h:63
uint32_t mp_block_size
Definition os_mempool.h:59
uint32_t mp_membuf_addr
Definition os_mempool.h:69
const char * name
Definition os_mempool.h:73
uint16_t mp_min_free
Definition os_mempool.h:65