1818/**
1919 * \file nsdynmemLIB.h
2020 * \brief Dynamical Memory API for library model
21- *
21+ * nsdynmemlib provides access to one default heap, along with the ability to use extra user heaps.
22+ * ns_dyn_mem_alloc/free always access the default heap initialised by ns_dyn_mem_init.
23+ * ns_mem_alloc/free access a user heap initialised by ns_mem_init. User heaps are identified by a book-keeping pointer.
2224 */
25+
2326#ifndef NSDYNMEMLIB_H_
2427#define NSDYNMEMLIB_H_
2528#ifdef __cplusplus
@@ -28,6 +31,12 @@ extern "C" {
2831
2932#include "ns_types.h"
3033
34+ // Added to maintain backward compatibility with older implementation of ns_dyn_mem APIs
35+ #define NSDYNMEMLIB_API_VERSION 2
36+
37+ typedef uint16_t ns_mem_block_size_t ; //external interface unsigned heap block size type
38+ typedef uint16_t ns_mem_heap_size_t ; //total heap size type.
39+
3140/*!
3241 * \enum heap_fail_t
3342 * \brief Dynamically heap system failure call back event types.
@@ -47,22 +56,25 @@ typedef enum {
4756 */
4857typedef struct mem_stat_t {
4958 /*Heap stats*/
50- int16_t heap_sector_size ; /**< Heap total Sector len. */
51- int16_t heap_sector_alloc_cnt ; /**< Reserved Heap sector cnt. */
52- int16_t heap_sector_allocated_bytes ; /**< Reserved Heap data in bytes. */
53- int16_t heap_sector_allocated_bytes_max ; /**< Reserved Heap data in bytes max value. */
59+ ns_mem_heap_size_t heap_sector_size ; /**< Heap total Sector len. */
60+ ns_mem_heap_size_t heap_sector_alloc_cnt ; /**< Reserved Heap sector cnt. */
61+ ns_mem_heap_size_t heap_sector_allocated_bytes ; /**< Reserved Heap data in bytes. */
62+ ns_mem_heap_size_t heap_sector_allocated_bytes_max ; /**< Reserved Heap data in bytes max value. */
5463 uint32_t heap_alloc_total_bytes ; /**< Total Heap allocated bytes. */
5564 uint32_t heap_alloc_fail_cnt ; /**< Counter for Heap allocation fail. */
5665} mem_stat_t ;
5766
67+
68+ typedef struct ns_mem_book ns_mem_book_t ;
69+
5870/**
5971 * \brief Init and set Dynamical heap pointer and length.
6072 *
6173 * \param heap_ptr Pointer to dynamically heap buffer
6274 * \param heap_size size of the heap buffer
6375 * \return None
6476 */
65- extern void ns_dyn_mem_init (uint8_t * heap , uint16_t h_size , void (* passed_fptr )(heap_fail_t ), mem_stat_t * info_ptr );
77+ extern void ns_dyn_mem_init (void * heap , ns_mem_heap_size_t h_size , void (* passed_fptr )(heap_fail_t ), mem_stat_t * info_ptr );
6678
6779
6880/**
@@ -84,7 +96,7 @@ extern void ns_dyn_mem_free(void *heap_ptr);
8496 * \return 0, Allocate Fail
8597 * \return >0, Pointer to allocated data sector.
8698 */
87- extern void * ns_dyn_mem_temporary_alloc (int16_t alloc_size );
99+ extern void * ns_dyn_mem_temporary_alloc (ns_mem_block_size_t alloc_size );
88100/**
89101 * \brief Allocate long period data.
90102 *
@@ -95,7 +107,7 @@ extern void *ns_dyn_mem_temporary_alloc(int16_t alloc_size);
95107 * \return 0, Allocate Fail
96108 * \return >0, Pointer to allocated data sector.
97109 */
98- extern void * ns_dyn_mem_alloc (int16_t alloc_size );
110+ extern void * ns_dyn_mem_alloc (ns_mem_block_size_t alloc_size );
99111
100112/**
101113 * \brief Get pointer to the current mem_stat_t set via ns_dyn_mem_init.
@@ -110,6 +122,65 @@ extern void *ns_dyn_mem_alloc(int16_t alloc_size);
110122 */
111123extern const mem_stat_t * ns_dyn_mem_get_mem_stat (void );
112124
125+ /**
126+ * \brief Init and set Dynamical heap pointer and length.
127+ *
128+ * \param heap_ptr Pointer to dynamically heap buffer
129+ * \param heap_size size of the heap buffer
130+ * \return !=0, Pointer to ns_mem_book_t.
131+ */
132+ extern ns_mem_book_t * ns_mem_init (void * heap , ns_mem_heap_size_t h_size , void (* passed_fptr )(heap_fail_t ), mem_stat_t * info_ptr );
133+
134+ /**
135+ * \brief Free allocated memory.
136+ *
137+ * \param book Address of book keeping structure
138+ * \param heap_ptr Pointer to allocated memory
139+ *
140+ * \return 0, Free OK
141+ * \return <0, Free Fail
142+ */
143+ extern void ns_mem_free (ns_mem_book_t * book , void * heap_ptr );
144+ /**
145+ * \brief Allocate temporary data.
146+ *
147+ * Space allocate started from beginning of the heap sector
148+ *
149+ * \param book Address of book keeping structure
150+ * \param alloc_size Allocated data size
151+ *
152+ * \return 0, Allocate Fail
153+ * \return >0, Pointer to allocated data sector.
154+ */
155+ extern void * ns_mem_temporary_alloc (ns_mem_book_t * book , ns_mem_block_size_t alloc_size );
156+ /**
157+ * \brief Allocate long period data.
158+ *
159+ * Space allocate started from end of the heap sector
160+ *
161+ * \param book Address of book keeping structure
162+ * \param alloc_size Allocated data size
163+ *
164+ * \return 0, Allocate Fail
165+ * \return >0, Pointer to allocated data sector.
166+ */
167+ extern void * ns_mem_alloc (ns_mem_book_t * book , ns_mem_block_size_t alloc_size );
168+
169+ /**
170+ * \brief Get pointer to the current mem_stat_t set via ns_mem_init.
171+ *
172+ * Get pointer to the statistics information, if one is set during the
173+ * initialization. This may be useful for statistics collection purposes.
174+ *
175+ * Note: the caller may not modify the returned structure.
176+ *
177+ * \param book Address of book keeping structure
178+ *
179+ * \return NULL, no mem_stat_t was given on initialization
180+ * \return !=0, Pointer to mem_stat_t.
181+ */
182+ extern const mem_stat_t * ns_mem_get_mem_stat (ns_mem_book_t * book );
183+
113184#ifdef __cplusplus
114185}
115186#endif
0 commit comments