SPM: Refine pool logics

The pool logic is needed by both SPM and partitions so that it needs
to be changed as shared sources. Refine pool logics before changing.

- Do not use 0 size array in structure definition. Also, remove the
  workaround structure member around this 0 size array definition.
- Need to provide pool instance while calling the free function. This
  saves recording one pool instance in the chunk data structure.

Change-Id: I4e83770a5a7b07d1d46aec85b04d98f40a6f922b
Signed-off-by: Ken Liu <Ken.Liu@arm.com>
diff --git a/secure_fw/partitions/psa_proxy/psa_proxy.c b/secure_fw/partitions/psa_proxy/psa_proxy.c
index 7864ef6..49347b6 100644
--- a/secure_fw/partitions/psa_proxy/psa_proxy.c
+++ b/secure_fw/partitions/psa_proxy/psa_proxy.c
@@ -40,7 +40,7 @@
 
 static inline void deallocate_forward_handle(psa_handle_t *h)
 {
-    tfm_pool_free(h);
+    tfm_pool_free(forward_handle_pool, h);
 }
 
 static psa_status_t forward_psa_call_to_secure_enclave(const psa_msg_t *msg)
diff --git a/secure_fw/spm/cmsis_psa/spm_ipc.c b/secure_fw/spm/cmsis_psa/spm_ipc.c
index d2067c1..b1ec03f 100644
--- a/secure_fw/spm/cmsis_psa/spm_ipc.c
+++ b/secure_fw/spm/cmsis_psa/spm_ipc.c
@@ -189,7 +189,7 @@
     BI_LIST_REMOVE_NODE(&conn_handle->list);
 
     /* Back handle buffer to pool */
-    tfm_pool_free(conn_handle);
+    tfm_pool_free(conn_handle_pool, conn_handle);
     return SPM_SUCCESS;
 }
 
diff --git a/secure_fw/spm/cmsis_psa/tfm_pools.c b/secure_fw/spm/cmsis_psa/tfm_pools.c
index 835674f..f298340 100644
--- a/secure_fw/spm/cmsis_psa/tfm_pools.c
+++ b/secure_fw/spm/cmsis_psa/tfm_pools.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -43,7 +43,6 @@
 
     pchunk = (struct tfm_pool_chunk_t *)pool->chunks;
     for (i = 0; i < num; i++) {
-        pchunk->pool = pool;
         BI_LIST_INSERT_BEFORE(&pool->chunks_list, &pchunk->list);
         pchunk = (struct tfm_pool_chunk_t *)&pchunk->data[chunksz];
     }
@@ -77,13 +76,11 @@
     return &pchunk->data;
 }
 
-void tfm_pool_free(void *ptr)
+void tfm_pool_free(struct tfm_pool_instance_t *pool, void *ptr)
 {
     struct tfm_pool_chunk_t *pchunk;
-    struct tfm_pool_instance_t *pool;
 
     pchunk = TFM_GET_CONTAINER_PTR(ptr, struct tfm_pool_chunk_t, data);
-    pool = (struct tfm_pool_instance_t *)pchunk->pool;
     BI_LIST_INSERT_BEFORE(&pool->chunks_list, &pchunk->list);
 }
 
diff --git a/secure_fw/spm/cmsis_psa/tfm_pools.h b/secure_fw/spm/cmsis_psa/tfm_pools.h
index e9c19dc..6f2f30d 100644
--- a/secure_fw/spm/cmsis_psa/tfm_pools.h
+++ b/secure_fw/spm/cmsis_psa/tfm_pools.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -8,42 +8,22 @@
 #define __TFM_POOLS_H__
 
 #include <stdbool.h>
-
 #include "lists.h"
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/*
- * Resource pool - few known size resources allocation/free is required,
- * so pool is more applicable than heap.
- */
-
 /*
  * Pool Instance:
  *  [ Pool Instance ] + N * [ Pool Chunks ]
  */
 struct tfm_pool_chunk_t {
     struct bi_list_node_t list;         /* Chunk list                     */
-    void *pool;                         /* Point to the parent pool       */
-    uint8_t data[0];                    /* Data indicator                 */
-};
-
-/*
- * tfm_pool_chunk_t minus the zero length "data" member,
- * required for standards compliant C
- */
-struct tfm_pool_chunk_s_t {
-    struct bi_list_node_t list;         /* Chunk list                     */
-    void *pool;                         /* Point to the parent pool       */
+    uint8_t data[];                     /* Data indicator                 */
 };
 
 struct tfm_pool_instance_t {
     size_t chunksz;                     /* Chunks size of pool member     */
     size_t chunk_count;                 /* A number of chunks in the pool */
     struct bi_list_node_t chunks_list;  /* Chunk list head in pool        */
-    struct tfm_pool_chunk_s_t chunks[0]; /* Data indicator                */
+    uint8_t chunks[];                   /* Data indicator                 */
 };
 
 /*
@@ -57,7 +37,7 @@
     static uint8_t name##_pool_buf[((chunksz) +                             \
                                    sizeof(struct tfm_pool_chunk_t)) * (num) \
                                    + sizeof(struct tfm_pool_instance_t)]    \
-                                   __attribute__((aligned(4)));            \
+                                   __attribute__((aligned(4)));             \
     static struct tfm_pool_instance_t *name =                               \
                             (struct tfm_pool_instance_t *)name##_pool_buf
 
@@ -96,9 +76,11 @@
 /**
  * \brief Free the allocated memory.
  *
+ * \param[in] pool              pool pointer decleared by \ref TFM_POOL_DECLARE
+ *
  * \param[in] ptr               Buffer pointer want to free.
  */
-void tfm_pool_free(void *ptr);
+void tfm_pool_free(struct tfm_pool_instance_t *pool, void *ptr);
 
 /**
  * \brief Checks whether a pointer points to a chunk data in the pool.
@@ -113,8 +95,4 @@
 bool is_valid_chunk_data_in_pool(struct tfm_pool_instance_t *pool,
                                  uint8_t *data);
 
-#ifdef __cplusplus
-}
-#endif
-
 #endif /* __TFM_POOLS_H__ */