Skip to content

Commit 83ad892

Browse files
committed
os/mm: Rename reserved field to memory_state in mm_allocnode_s and mm_freenode_s
Rename reserved field to memory_state in `mm_allocnode_s` and `mm_freenode_s` structures to track memory usage state. Add definitions for memory state values to enhance memory management debugging. Signed-off-by: seokhun-eom <seokhun.eom@samsung.com>
1 parent 09d9747 commit 83ad892

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

os/include/tinyara/mm/mm.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ typedef void *mmaddress_t; /* 32 bit address space */
227227

228228
#ifdef CONFIG_DEBUG_MM_HEAPINFO
229229
#define SIZEOF_MM_MALLOC_DEBUG_INFO (sizeof(mmaddress_t) + sizeof(pid_t) + sizeof(uint16_t))
230+
231+
/* Memory state values */
232+
#define MM_MEMORY_STATE_UNUSED 0 /* Initial state */
233+
#define MM_MEMORY_STATE_USED 1 /* Memory is referenced */
234+
#define MM_MEMORY_STATE_LEAK 2 /* Potential memory leak */
235+
#define MM_MEMORY_STATE_BROKEN 3 /* Heap corruption detected */
230236
#else
231237
#define SIZEOF_MM_MALLOC_DEBUG_INFO 0
232238
#endif
@@ -247,7 +253,7 @@ struct mm_allocnode_s {
247253
#ifdef CONFIG_DEBUG_MM_HEAPINFO
248254
mmaddress_t alloc_call_addr; /* malloc call address */
249255
pid_t pid; /* PID info */
250-
uint16_t reserved; /* Reserved for future use. */
256+
uint16_t memory_state; /* Memory state for leak detection. */
251257
#endif
252258
mmsize_t size; /* Size of this chunk */
253259

@@ -268,15 +274,15 @@ struct mm_freenode_s {
268274
#ifdef CONFIG_DEBUG_MM_HEAPINFO
269275
mmaddress_t alloc_call_addr; /* malloc call address */
270276
pid_t pid; /* PID info */
271-
uint16_t reserved; /* Reserved for future use. */
277+
uint16_t memory_state; /* Memory state for leak detection. */
272278
#endif
273279
mmsize_t size; /* Size of this chunk */
274280
FAR struct mm_freenode_s *flink; /* Supports a doubly linked list */
275281
FAR struct mm_freenode_s *blink;
276282
#ifdef CONFIG_DEBUG_MM_FREEINFO
277283
mmaddress_t free_call_addr; /* free call address */
278284
pid_t free_call_pid; /* free call PID */
279-
uint16_t reserved2; /* Reserved for future use and padding for 4-byte alignment */
285+
uint16_t reserved; /* Reserved for future use and padding for 4-byte alignment */
280286
#endif
281287
};
282288

@@ -301,7 +307,7 @@ struct mm_delaynode_s {
301307
#ifdef CONFIG_DEBUG_MM_FREEINFO
302308
mmaddress_t free_call_addr;
303309
pid_t free_call_pid;
304-
uint16_t reserved2;
310+
uint16_t reserved;
305311
#endif
306312
};
307313

os/kernel/debug/mem_leak_checker.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
/****************************************************************************
3838
* Pre-processor Definitions
3939
****************************************************************************/
40-
#define MEM_USED 0
41-
#define MEM_LEAK 1
42-
#define MEM_BROKEN 2
40+
#define MEM_USED MM_MEMORY_STATE_USED
41+
#define MEM_LEAK MM_MEMORY_STATE_LEAK
42+
#define MEM_BROKEN MM_MEMORY_STATE_BROKEN
4343
#define CMN_BIN_IDX 0
4444

4545
#define MEM_ACCESS_UNIT 0x04
@@ -111,10 +111,10 @@ static bool search_hash(unsigned long value)
111111

112112
while (cur != NULL) {
113113
if ((unsigned long)cur->node == value) {
114-
if (cur->node->reserved == MEM_USED) {
114+
if (cur->node->memory_state == MEM_USED) {
115115
return false;
116116
}
117-
cur->node->reserved = MEM_USED;
117+
cur->node->memory_state = MEM_USED;
118118
return true;
119119
}
120120
if (cur->next == NULL) {
@@ -197,7 +197,7 @@ static void fill_hash_table(struct mm_heap_s *heap, int *leak_cnt, int *broken_c
197197

198198
/* Check broken link */
199199
if (node_size != MM_PREV_NODE_SIZE(node)) {
200-
node->reserved = MEM_BROKEN;
200+
node->memory_state = MEM_BROKEN;
201201
(*broken_cnt)++;
202202
continue;
203203
}
@@ -210,7 +210,7 @@ static void fill_hash_table(struct mm_heap_s *heap, int *leak_cnt, int *broken_c
210210
if ((node->preceding & MM_ALLOC_BIT) != 0) {
211211
g_node_info[*leak_cnt].node = node;
212212
g_node_info[*leak_cnt].next = NULL;
213-
node->reserved = MEM_LEAK;
213+
node->memory_state = MEM_LEAK;
214214
add_hash(*leak_cnt);
215215
(*leak_cnt)++;
216216
}
@@ -344,7 +344,7 @@ static void print_info(struct mm_heap_s *heap, int leak_cnt, int broken_cnt)
344344
{
345345
for (node = heap->mm_heapstart[region]; node < heap->mm_heapend[region]; node = (struct mm_allocnode_s *)((char *)node + node->size)) {
346346
ASSERT(node->size);
347-
if (node->reserved == MEM_LEAK) {
347+
if (node->memory_state == MEM_LEAK) {
348348
/* alloc_call_addr can be from kernel, app or common binary.
349349
* based on the text addresses printed, user needs to check the
350350
* corresponding binaries accordingly
@@ -358,7 +358,7 @@ static void print_info(struct mm_heap_s *heap, int leak_cnt, int broken_cnt)
358358
pid = (-1) * pid;
359359
}
360360
printf("LEAK | %10p | %8d | %10p | %d\n", (void *)((char *)node + SIZEOF_MM_ALLOCNODE), node->size - SIZEOF_MM_ALLOCNODE, owner_addr, pid);
361-
} else if (node->reserved == MEM_BROKEN) {
361+
} else if (node->memory_state == MEM_BROKEN) {
362362
printf("BROKEN | %p\n", node);
363363
}
364364
}

os/mm/mm_heap/mm_heapinfo_utils.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ void heapinfo_update_node(FAR struct mm_allocnode_s *node, mmaddress_t caller_re
125125
{
126126
DEBUGASSERT(node);
127127
node->alloc_call_addr = caller_retaddr;
128-
node->reserved = 0;
128+
node->memory_state = MM_MEMORY_STATE_UNUSED;
129129
node->pid = getpid();
130130
}
131131

0 commit comments

Comments
 (0)