dynamic memory allocation & fragmentation in c & c++

36
Dynamic Memory Allocation & Fragmentation in C & C++ Dynamic Memory Allocation & Fragmentation in C & C++ Colin Walls [email protected]

Upload: senthamizhan5759

Post on 27-Apr-2015

2.688 views

Category:

Documents


12 download

DESCRIPTION

article from mentor graphics

TRANSCRIPT

Page 1: Dynamic Memory Allocation & Fragmentation in C & C++

Dynamic Memory Allocation & Fragmentation

in C & C++

Dynamic Memory Allocation & Fragmentation

in C & C++

Colin [email protected]

Page 2: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 3: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 4: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 5: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 6: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 7: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 8: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 9: Dynamic Memory Allocation & Fragmentation in C & C++

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);

Page 10: Dynamic Memory Allocation & Fragmentation in C & C++

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;

Page 11: Dynamic Memory Allocation & Fragmentation in C & C++

11www.mentor.com/embedded

11www.mentor.com/embedded

Dynamic Memory in CDynamic Memory in C

Heap

Size info

Data space

Pointer from malloc()

Page 12: Dynamic Memory Allocation & Fragmentation in C & C++

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);

Page 13: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 14: Dynamic Memory Allocation & Fragmentation in C & C++

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;

Page 15: Dynamic Memory Allocation & Fragmentation in C & C++

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;

Page 16: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 17: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 18: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 19: Dynamic Memory Allocation & Fragmentation in C & C++

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 …

Page 20: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 21: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 22: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 23: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 24: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 25: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 26: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 27: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 28: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 29: Dynamic Memory Allocation & Fragmentation in C & C++

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);

Page 30: Dynamic Memory Allocation & Fragmentation in C & C++

30www.mentor.com/embedded

30www.mentor.com/embedded

Memory Leak DetectionMemory Leak Detection

Page 31: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 32: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 33: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 34: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 35: Dynamic Memory Allocation & Fragmentation in C & C++

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

Page 36: Dynamic Memory Allocation & Fragmentation in C & C++

Questions?Questions?

Colin [email protected]

blogs.mentor.com/colinwalls