TF-M Reference Manual  1.2.0
TrustedFirmware-M
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
tfm_pools.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 #ifndef __TFM_POOLS_H__
8 #define __TFM_POOLS_H__
9 
10 #include <stdbool.h>
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 /*
17  * Resource pool - few known size resources allocation/free is required,
18  * so pool is more applicable than heap.
19  */
20 
21 /*
22  * Pool Instance:
23  * [ Pool Instance ] + N * [ Pool Chunks ]
24  */
26  struct tfm_list_node_t list; /* Chunk list */
27  void *pool; /* Point to the parent pool */
28  uint8_t data[0]; /* Data indicator */
29 };
30 
31 /*
32  * tfm_pool_chunk_t minus the zero length "data" member,
33  * required for standards compliant C
34  */
36  struct tfm_list_node_t list; /* Chunk list */
37  void *pool; /* Point to the parent pool */
38 };
39 
41  size_t chunksz; /* Chunks size of pool member */
42  size_t chunk_count; /* A number of chunks in the pool */
43  struct tfm_list_node_t chunks_list; /* Chunk list head in pool */
44  struct tfm_pool_chunk_s_t chunks[0]; /* Data indicator */
45 };
46 
47 /*
48  * This will declares a static memory pool variable with chunk memory.
49  * Parameters:
50  * name - Variable name, will be used when register
51  * chunksz - chunk size in bytes
52  * num - Number of chunks
53  */
54 #define TFM_POOL_DECLARE(name, chunksz, num) \
55  static uint8_t name##_pool_buf[((chunksz) + \
56  sizeof(struct tfm_pool_chunk_t)) * (num) \
57  + sizeof(struct tfm_pool_instance_t)] \
58  __attribute__((aligned(4))); \
59  static struct tfm_pool_instance_t *name = \
60  (struct tfm_pool_instance_t *)name##_pool_buf
61 
62 /* Get the head size of memory pool */
63 #define POOL_HEAD_SIZE (sizeof(struct tfm_pool_instance_t) + \
64  sizeof(struct tfm_pool_chunk_t))
65 
66 /* Get the whole size of memory pool */
67 #define POOL_BUFFER_SIZE(name) sizeof(name##_pool_buf)
68 
81 int32_t tfm_pool_init(struct tfm_pool_instance_t *pool, size_t poolsz,
82  size_t chunksz, size_t num);
83 
93 
99 void tfm_pool_free(void *ptr);
100 
112  uint8_t *data);
113 
114 #ifdef __cplusplus
115 }
116 #endif
117 
118 #endif /* __TFM_POOLS_H__ */
void tfm_pool_free(void *ptr)
Free the allocated memory.
Definition: tfm_pools.c:80
struct tfm_list_node_t chunks_list
Definition: tfm_pools.h:43
int32_t tfm_pool_init(struct tfm_pool_instance_t *pool, size_t poolsz, size_t chunksz, size_t num)
Register a memory pool.
Definition: tfm_pools.c:22
uint8_t data[0]
Definition: tfm_pools.h:28
void * tfm_pool_alloc(struct tfm_pool_instance_t *pool)
Allocate a memory from pool.
Definition: tfm_pools.c:58
struct tfm_list_node_t list
Definition: tfm_pools.h:26
struct tfm_list_node_t list
Definition: tfm_pools.h:36
bool is_valid_chunk_data_in_pool(struct tfm_pool_instance_t *pool, uint8_t *data)
Checks whether a pointer points to a chunk data in the pool.
Definition: tfm_pools.c:90
struct tfm_pool_chunk_s_t chunks[0]
Definition: tfm_pools.h:44