Dynamic Memory Allocation & Fragmentation in C & C++
Dynamic Memory Allocation & Fragmentation in C & C++
2
www.mentor.com/embedded
Agenda
3
www.mentor.com/embedded
C/C++ Memory Spaces
▪ Static memory Static storage
RAM address
▪ program sections [FGA]
4
www.mentor.com/embedded
C/C++ Memory Spaces
▪ Static memory Static storage
▪ keyword static
RAM address
▪ program sections [FGA]
▪ Automatic variables
▪ stack
▪ register
▪ keyword auto
Stack
5
www.mentor.com/embedded
C/C++ Memory Spaces
▪ Static memory Static storage
RAM address
▪ program sections [FGA]
▪ Automatic variables
▪ stack
▪ register
▪ keyword auto
▪ Heap Stack
6
www.mentor.com/embedded
C/C++ Memory Spaces
Static storage
Variables
Dynamic storage
Heap
▪ With an operating
RAM address
system, there are
typically multiple stacks
Stack
Stack
Stack
Stack
7
www.mentor.com/embedded
Agenda
8
www.mentor.com/embedded
Dynamic Memory in C
▪ Management of heap space
▪ Key functions
▪ malloc()
▪ free()
9
www.mentor.com/embedded
Dynamic Memory in C
int my_array[10];
my_array[3] = 99;
int *pointer;
pointer = malloc(10 * sizeof(int));
*(pointer+3) = 99;
pointer[3] = 99;
...
free(pointer);
pointer = NULL;
10
www.mentor.com/embedded
Dynamic Memory in C
Heap
Size info
Pointer from malloc()
Data space
11
www.mentor.com/embedded
Dynamic Memory in C
▪ Variant functions
▪ calloc()
▪ simpler parameters
▪ initialized to zero
▪ realloc()
▪ copies as necessary
12
www.mentor.com/embedded
Agenda
13
www.mentor.com/embedded
Dynamic Memory in C++
▪ Same principle as C
▪ Library functions available
▪ New operators more
flexible and less error p_var = new typename;
p_var = new type(initializer);
prone:
p_array = new type [size];
▪ new
▪ delete delete p_var;
▪ care needed to use correct delete[] p_array;
deallocator
▪ No reallocator
▪ Also STL
14
www.mentor.com/embedded
Dynamic Memory in C++
int my_array[10];
my_array[3] = 99;
int* pointer;
pointer = new int[10];
pointer[3] = 99;
...
delete[] pointer;
pointer = NULL;
15
www.mentor.com/embedded
Agenda
16
www.mentor.com/embedded
Issues and Problems
17
www.mentor.com/embedded
Issues and Problems
▪ Stack overflow/underflow
▪ push too much data
▪ pop when stack empty
▪ hard to locate bug
▪ errors may show up much later
▪ most likely in another task
18
www.mentor.com/embedded
Issues and Problems
▪ malloc() etc.
▪ reentrancy
▪ determinism of allocation time
▪ memory leaks
▪ mismatch of allocation and deallocation
▪ lead to performance degradation over time
▪ allocation failure
▪ what to do?
▪ not enough memory
▪ fragmentation …
19
www.mentor.com/embedded
Memory Fragmentation
Heap [10K]
#define K (1024) p1
... 3K
char *p1;
p1 = malloc(3*K);
[7K free]
20
www.mentor.com/embedded
Memory Fragmentation
Heap [10K]
#define K (1024) p1
... 3K
char *p1, *p2;
p2
p1 = malloc(3*K);
p2 = malloc(4*K); 4K
...
[3K free]
21
www.mentor.com/embedded
Memory Fragmentation
Heap [10K]
#define K (1024)
... [3K free]
char *p1, *p2;
p2
p1 = malloc(3*K);
p2 = malloc(4*K); 4K
...
free(p1);
[3K free]
22
www.mentor.com/embedded
Memory Fragmentation
Heap [10K]
#define K (1024)
... [3K free]
char *p1, *p2;
p2
p1 = malloc(3*K);
p2 = malloc(4*K); 4K
...
free(p1);
[3K free]
p1 = malloc(4*K); //fails!
23
www.mentor.com/embedded
Memory Fragmentation
24
www.mentor.com/embedded
Agenda
25
www.mentor.com/embedded
Memory with an RTOS
26
www.mentor.com/embedded
Block/partition Memory Allocation
27
www.mentor.com/embedded
Block/partition Memory Allocation
STATUS NU_Create_Partition_Pool(NU_PARTITION_POOL *pool,
CHAR *name, VOID *start_address, UNSIGNED pool_size,
UNSIGNED partition_size, OPTION suspend_type)
▪ descriptor MyPool
▪ 40 byte partitions
▪ 2000 byte memory area
▪ located at 0xB000
▪ task suspend in FIFO order
28
www.mentor.com/embedded
Block/partition Memory Allocation
status = NU_Deallocate_Partition(ptr);
29
www.mentor.com/embedded
Memory Leak Detection
30
www.mentor.com/embedded
Agenda
31
www.mentor.com/embedded
Stacks
▪ Sizing is a challenge
▪ almost impossible to guard word
assess statically
▪ best measured
dynamically
▪ fill with known value stack
▪ use debugger
trace/coverage
32
www.mentor.com/embedded
Dynamic Memory
▪ Define series of partition pools
▪ Geometric series of sizes
▪ e.g. 32, 64, 128, 256 bytes
▪ Write malloc() to select best fit
▪ can be deterministic
▪ Map free() directly on to deallocate API
▪ Failure modes:
▪ no fragmentation
▪ allocation failure causes task suspend
33
www.mentor.com/embedded
Agenda
34
www.mentor.com/embedded
Conclusions
35
www.mentor.com/embedded
Questions?
Colin Walls
colin_walls@mentor.com
blogs.mentor.com/colinwalls