Dynamic memory allocation and fragmentation Seminar on Network and Operating Systems Group II.
Dynamic Memory Allocation & Fragmentation in C & C++
-
Upload
senthamizhan5759 -
Category
Documents
-
view
2.675 -
download
12
description
Transcript of Dynamic Memory Allocation & Fragmentation in C & C++
Dynamic Memory Allocation & Fragmentation
in C & C++
Dynamic Memory Allocation & Fragmentation
in C & C++
Colin [email protected]
2www.mentor.com/embedded
2www.mentor.com/embedded
AgendaAgenda
▪ Introduction to memory usage▪ Dynamic memory in C▪ Dynamic memory in C++▪ Issues and problems▪ Memory with an RTOS▪ Real time memory solutions▪ Conclusions
3www.mentor.com/embedded
3www.mentor.com/embedded
AgendaAgenda
▪ Introduction to memory usage▪ Dynamic memory in C▪ Dynamic memory in C++▪ Issues and problems▪ Memory with an RTOS▪ Real time memory solutions▪ Conclusions
4www.mentor.com/embedded
4www.mentor.com/embedded
C/C++ Memory SpacesC/C++ Memory Spaces
▪ Static memory▪ variables outside of
functions▪ static internal variables
▪ keyword static▪ program sections [FGA]
RA
M a
ddre
ss
Static storage
Variables
5www.mentor.com/embedded
5www.mentor.com/embedded
C/C++ Memory SpacesC/C++ Memory Spaces
▪ Static memory▪ variables outside of
functions▪ static internal variables
▪ keyword static▪ program sections [FGA]
▪ Automatic variables▪ stack▪ register▪ keyword auto
RA
M a
ddre
ss
Static storage
Dynamic storage
Variables
Stack
6www.mentor.com/embedded
6www.mentor.com/embedded
C/C++ Memory SpacesC/C++ Memory Spaces
▪ Static memory▪ variables outside of
functions▪ static internal variables
▪ keyword static▪ program sections [FGA]
▪ Automatic variables▪ stack▪ register▪ keyword auto
▪ HeapR
AM
add
ress
Static storage
Dynamic storage
Heap
Variables
Stack
7www.mentor.com/embedded
7www.mentor.com/embedded
C/C++ Memory SpacesC/C++ Memory Spaces
▪ With an operating system, there are typically multiple stacks R
AM
add
ress
Static storage
Dynamic storage
Stack
Heap
Variables
Stack
Stack
Stack
8www.mentor.com/embedded
8www.mentor.com/embedded
AgendaAgenda
▪ Introduction to memory usage▪ Dynamic memory in C▪ Dynamic memory in C++▪ Issues and problems▪ Memory with an RTOS▪ Real time memory solutions▪ Conclusions
9www.mentor.com/embedded
9www.mentor.com/embedded
Dynamic Memory in CDynamic Memory in C
▪ Management of heap space▪ Key functions
▪ malloc()▪ free()
void *malloc(size_t size); void free(void *pointer);
10www.mentor.com/embedded
10www.mentor.com/embedded
Dynamic Memory in CDynamic 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;
11www.mentor.com/embedded
11www.mentor.com/embedded
Dynamic Memory in CDynamic Memory in C
Heap
Size info
Data space
Pointer from malloc()
12www.mentor.com/embedded
12www.mentor.com/embedded
Dynamic Memory in CDynamic Memory in C
▪ Variant functions▪ calloc()
▪ simpler parameters▪ initialized to zero
▪ realloc()▪ copies as necessary
void *calloc(size_t nelements, size_t elementSize);void *realloc(void *pointer, size_t size);
13www.mentor.com/embedded
13www.mentor.com/embedded
AgendaAgenda
▪ Introduction to memory usage▪ Dynamic memory in C▪ Dynamic memory in C++▪ Issues and problems▪ Memory with an RTOS▪ Real time memory solutions▪ Conclusions
14www.mentor.com/embedded
14www.mentor.com/embedded
Dynamic Memory in C++Dynamic Memory in C++
▪ Same principle as C▪ Library functions available▪ New operators more
flexible and less error prone:
▪ new▪ delete
▪ care needed to use correct deallocator
▪ No reallocator▪ Also STL
p_var = new typename;p_var = new type(initializer);p_array = new type [size];
delete p_var;delete[] p_array;
15www.mentor.com/embedded
15www.mentor.com/embedded
Dynamic Memory in C++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;
16www.mentor.com/embedded
16www.mentor.com/embedded
AgendaAgenda
▪ Introduction to memory usage▪ Dynamic memory in C▪ Dynamic memory in C++▪ Issues and problems▪ Memory with an RTOS▪ Real time memory solutions▪ Conclusions
17www.mentor.com/embedded
17www.mentor.com/embedded
Issues and ProblemsIssues and Problems
▪ Dynamic behavior is troublesome in real time embedded systems
▪ Issues:▪ resource exhaustion▪ non-determinism
▪ Examples:▪ dynamic memory▪ RTOS objects
18www.mentor.com/embedded
18www.mentor.com/embedded
Issues and ProblemsIssues 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
19www.mentor.com/embedded
19www.mentor.com/embedded
Issues and ProblemsIssues 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 …
20www.mentor.com/embedded
20www.mentor.com/embedded
Heap [10K]
Memory FragmentationMemory Fragmentation
#define K (1024)...char *p1;
p1 = malloc(3*K);
3K
[7K free]
p1
21www.mentor.com/embedded
21www.mentor.com/embedded
Heap [10K]
Memory FragmentationMemory Fragmentation
#define K (1024)...char *p1, *p2;
p1 = malloc(3*K);p2 = malloc(4*K);...
Heap [10K]
3K
4K
[3K free]
p1
p2
22www.mentor.com/embedded
22www.mentor.com/embedded
Heap [10K]
Memory FragmentationMemory Fragmentation
#define K (1024)...char *p1, *p2;
p1 = malloc(3*K);p2 = malloc(4*K);...
free(p1);
Heap [10K]
[3K free]
4K
[3K free]
p2
23www.mentor.com/embedded
23www.mentor.com/embedded
Heap [10K]
Memory FragmentationMemory Fragmentation
#define K (1024)...char *p1, *p2;
p1 = malloc(3*K);p2 = malloc(4*K);...
free(p1);
p1 = malloc(4*K); //fails!
Heap [10K]
[3K free]
4K
[3K free]
p2
24www.mentor.com/embedded
24www.mentor.com/embedded
Memory FragmentationMemory Fragmentation
▪ Defragmentation not possible with direct pointers
▪ other code using memory address would be broken
▪ Garbage collection performed in other languages
▪ e.g. Java, BASIC▪ no direct pointers▪ not real time
25www.mentor.com/embedded
25www.mentor.com/embedded
AgendaAgenda
▪ Introduction to memory usage▪ Dynamic memory in C▪ Dynamic memory in C++▪ Issues and problems▪ Memory with an RTOS▪ Real time memory solutions▪ Conclusions
26www.mentor.com/embedded
26www.mentor.com/embedded
Memory with an RTOSMemory with an RTOS
▪ All OSes provide some memory management facilities
▪ compatible with real time requirements▪ deterministic
▪ May offer re-entrant malloc() facility▪ normally not deterministic
▪ Block/partition memory allocation
27www.mentor.com/embedded
27www.mentor.com/embedded
Block/partition Memory AllocationBlock/partition Memory Allocation
▪ Partition pool created either statically or dynamically
▪ Fixed size blocks of memory
▪ Specified number of blocks
▪ Resource availability information
▪ Failure modes limited▪ fragmentation not
possible▪ allocation failure
possible▪ task suspend option
▪ memory leaks possible
28www.mentor.com/embedded
28www.mentor.com/embedded
Block/partition Memory AllocationBlock/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)
status = NU_Create_Partition_Pool(&MyPool, “any name”,(VOID *) 0xB000, 2000, 40, NU_FIFO);
▪ descriptor MyPool▪ 40 byte partitions▪ 2000 byte memory area▪ located at 0xB000▪ task suspend in FIFO order
29www.mentor.com/embedded
29www.mentor.com/embedded
Block/partition Memory AllocationBlock/partition Memory Allocation
status = NU_Allocate_Partition(&MyPool, &ptr, NU_SUSPEND);
▪ allocate a partition from pool described by MyPool▪ ptr will point to allocated memory▪ task suspend on allocation failure
status = NU_Deallocate_Partition(ptr);
30www.mentor.com/embedded
30www.mentor.com/embedded
Memory Leak DetectionMemory Leak Detection
31www.mentor.com/embedded
31www.mentor.com/embedded
AgendaAgenda
▪ Introduction to memory usage▪ Dynamic memory in C▪ Dynamic memory in C++▪ Issues and problems▪ Memory with an RTOS▪ Real time memory solutions▪ Conclusions
32www.mentor.com/embedded
32www.mentor.com/embedded
StacksStacks
▪ Sizing is a challenge▪ almost impossible to
assess statically▪ best measured
dynamically▪ fill with known value▪ use debugger
trace/coverage
guard word
stack
SP guard word▪ Overflow/underflow
▪ can add protection▪ use guard words or
MMU
33www.mentor.com/embedded
33www.mentor.com/embedded
Dynamic MemoryDynamic 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
34www.mentor.com/embedded
34www.mentor.com/embedded
AgendaAgenda
▪ Introduction to memory usage▪ Dynamic memory in C▪ Dynamic memory in C++▪ Issues and problems▪ Memory with an RTOS▪ Real time memory solutions▪ Conclusions
35www.mentor.com/embedded
35www.mentor.com/embedded
ConclusionsConclusions
▪ Dynamic behavior, including memory allocation, is an issue for real time
▪ non-determinism▪ failure
▪ Fragmentation can be eliminated▪ Determinism possible▪ Failure modes can be contained