1 /*
2  * Copyright (c) 2018-2024, 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 #include "psa/error.h"
12 #include "compiler_ext_defs.h"
13 #include "lists.h"
14 
15 /*
16  * Pool Instance:
17  *  [ Pool Instance ] + N * [ Pool Chunks ]
18  */
19 struct tfm_pool_chunk_t {
20     struct tfm_pool_chunk_t *next;        /* Chunk list                     */
21     uint32_t magic;                       /* Magic value indicating allocation state */
22     uint8_t data[];                       /* Data indicator                 */
23 };
24 
25 struct tfm_pool_instance_t {
26     struct tfm_pool_chunk_t *next;        /* Point to the first free node   */
27     size_t chunksz;                       /* Chunks size of pool member     */
28     size_t pool_sz;                       /* Pool size in bytes             */
29     uint8_t chunks[];                     /* Data indicator                 */
30 };
31 
32 /*
33  * This will declares a static memory pool variable with chunk memory.
34  * Parameters:
35  *  name        -   Variable name, will be used when register
36  *  chunksz     -   chunk size in bytes
37  *  num         -   Number of chunks
38  */
39 #define TFM_POOL_DECLARE(name, chunksz, num)                                \
40     static uint8_t name##_pool_buf[((chunksz) +                             \
41                                    sizeof(struct tfm_pool_chunk_t)) * (num) \
42                                    + sizeof(struct tfm_pool_instance_t)]    \
43                                    __aligned(4);                            \
44     static struct tfm_pool_instance_t *name =                               \
45                             (struct tfm_pool_instance_t *)name##_pool_buf
46 
47 /* Get the head size of memory pool */
48 #define POOL_HEAD_SIZE (sizeof(struct tfm_pool_instance_t) +                \
49                         sizeof(struct tfm_pool_chunk_t))
50 
51 /* Get the whole size of memory pool */
52 #define POOL_BUFFER_SIZE(name)          sizeof(name##_pool_buf)
53 
54 /**
55  * \brief Register a memory pool.
56  *
57  * \param[in] pool              Pointer to memory pool declared by
58  *                              \ref TFM_POOL_DECLARE
59  * \param[in] poolsz             Size of the pool buffer.
60  * \param[in] chunksz           Size of chunks.
61  * \param[in] num               Number of chunks.
62  *
63  * \retval PSA_SUCCESS          Success.
64  * \retval SPM_ERROR_BAD_PARAMETERS Parameters error.
65  */
66 psa_status_t tfm_pool_init(struct tfm_pool_instance_t *pool, size_t poolsz,
67                            size_t chunksz, size_t num);
68 
69 /**
70  * \brief Allocate a memory from pool.
71  *
72  * \param[in] pool              pool pointer decleared by \ref TFM_POOL_DECLARE
73  *
74  * \retval buffer pointer       Success.
75  * \retval NULL                 Failed.
76  */
77 void *tfm_pool_alloc(struct tfm_pool_instance_t *pool);
78 
79 /**
80  * \brief Free the allocated memory.
81  *
82  * \param[in] pool              pool pointer decleared by \ref TFM_POOL_DECLARE
83  *
84  * \param[in] ptr               Buffer pointer want to free.
85  */
86 void tfm_pool_free(struct tfm_pool_instance_t *pool, void *ptr);
87 
88 /**
89  * \brief Checks whether a pointer points to a valid allocated chunk of data in
90  *        the pool.
91  *
92  * \param[in] pool              Pointer to memory pool declared by
93  *                              \ref TFM_POOL_DECLARE.
94  * \param[in] data              The pointer to check.
95  *
96  * \retval true                 Data is a chunk data in the pool.
97  * \retval false                Data is not a chunk data in the pool.
98  */
99 bool is_valid_chunk_data_in_pool(struct tfm_pool_instance_t *pool,
100                                  uint8_t *data);
101 
102 #endif /* __TFM_POOLS_H__ */
103