18
18
/**
19
19
* \file nsdynmemLIB.h
20
20
* \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.
22
24
*/
25
+
23
26
#ifndef NSDYNMEMLIB_H_
24
27
#define NSDYNMEMLIB_H_
25
28
#ifdef __cplusplus
@@ -28,6 +31,12 @@ extern "C" {
28
31
29
32
#include "ns_types.h"
30
33
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
+
31
40
/*!
32
41
* \enum heap_fail_t
33
42
* \brief Dynamically heap system failure call back event types.
@@ -47,22 +56,25 @@ typedef enum {
47
56
*/
48
57
typedef struct mem_stat_t {
49
58
/*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. */
54
63
uint32_t heap_alloc_total_bytes ; /**< Total Heap allocated bytes. */
55
64
uint32_t heap_alloc_fail_cnt ; /**< Counter for Heap allocation fail. */
56
65
} mem_stat_t ;
57
66
67
+
68
+ typedef struct ns_mem_book ns_mem_book_t ;
69
+
58
70
/**
59
71
* \brief Init and set Dynamical heap pointer and length.
60
72
*
61
73
* \param heap_ptr Pointer to dynamically heap buffer
62
74
* \param heap_size size of the heap buffer
63
75
* \return None
64
76
*/
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 );
66
78
67
79
68
80
/**
@@ -84,7 +96,7 @@ extern void ns_dyn_mem_free(void *heap_ptr);
84
96
* \return 0, Allocate Fail
85
97
* \return >0, Pointer to allocated data sector.
86
98
*/
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 );
88
100
/**
89
101
* \brief Allocate long period data.
90
102
*
@@ -95,7 +107,7 @@ extern void *ns_dyn_mem_temporary_alloc(int16_t alloc_size);
95
107
* \return 0, Allocate Fail
96
108
* \return >0, Pointer to allocated data sector.
97
109
*/
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 );
99
111
100
112
/**
101
113
* \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);
110
122
*/
111
123
extern const mem_stat_t * ns_dyn_mem_get_mem_stat (void );
112
124
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
+
113
184
#ifdef __cplusplus
114
185
}
115
186
#endif
0 commit comments