Chapter 12 Memory Management. The dynamic memory allocation can be achieved by using malloc() and free() function Using malloc() and free(0 in an embedded.

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

Dynamic memory allocation
R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Module R2 Overview. Process queues As processes enter the system and transition from state to state, they are stored queues. There may be many different.
Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Introduction of Memory Allocation. Memory Allocation There are two types of memory allocations possible in c. Compile-time or Static allocation Run-time.
Memory Allocation. Memory A memory or store is required in a computer to store programs (or information or data). Data used by the variables in a program.
Memory Management Chapter 7. Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated efficiently to pack as.
Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable supply of ready processes to.
Chapter 7 Memory Management
Embedded System Presentation Nguyễn Trần Quang Nguyễn Công Vũ 1µC/OS-II.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
Informática II Prof. Dr. Gustavo Patiño MJ
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Memory Management Chapter 4. Memory hierarchy Programmers want a lot of fast, non- volatile memory But, here is what we have:
Memory Management Chapter 7. Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated efficiently to pack as.
Memory Management Chapter 7 B.Ramamurthy. Memory Management Subdividing memory to accommodate multiple processes Memory needs to allocated efficiently.
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
Memory Management Chapter 5.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Lecture 8: Memory Mangement Operating System I Spring 2008.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Real-Time Operating Systems Suzanne Rivoire November 20, 2002
Real-Time Concepts for Embedded Systems Author: Qing Li with Caroline Yao ISBN: CMPBooks.
SOC Consortium Course Material SoC Design Laboratory Lab 8 Real-time OS - 2 Speaker: Yung-Chih Chen Advisor: Prof. Chun-Yao Wang November 17, 2003 Department.
國立台灣大學 資訊工程學系 Introduction to uC/OS-II
Variables and Objects, pointers and addresses: Chapter 3, Slide 1 variables and data objects are data containers with names the value of the variable is.
Lecture 9 RTOS: Inter-process communication
Chapter 4 Memory Management.
Memory Management Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University.
2-1 The critical section –A piece of code which cannot be interrupted during execution Cases of critical sections –Modifying a block of memory shared by.
Real Time Operating Systems Memory Management & Device Control.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
MicroC/OS-II S O T R.  MicroC/OS-II (commonly termed as µC/OS- II or uC/OS-II), is the acronym for Micro-Controller Operating Systems Version 2.  It.
Basic Memory Management 1. Readings r Silbershatz et al: chapters
uC/OS-II Real-Time Systems Concepts Kernel Structure Task Management
Memory Management Program must be brought (from disk) into memory and placed within a process for it to be run Main memory and registers are only storage.
Lab 3 Department of Computer Science and Information Engineering National Taiwan University Lab3 – μC/OS 2015/10/13/ 13 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Memory Management Chapter 5 Advanced Operating System.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
SOC Consortium Course Material SoC Design Laboratory Lab 8 Real-time OS - 1 Speaker: Yung-Chih Chen Advisor: Prof. Chun-Yao Wang November 17, 2003 Department.
Chapter 7: Semaphore Management Seulki Lee 1. Semaphore?  A protocol mechanism offered by most multitasking kernels  Control access to a shared resource.
Real Time Operating Systems Johan Dams
Chapter 13: Porting μC/OS-II 1. Outline Requirements Hardware Software Tasks of Porting µC/OS-II OS_CPU_C.H OS_CPU_C.C OS_CPU_A.ASM Testing a port 2.
Chapter 5 Time Management use clock tick to provide a periodic interrupt to keep track of time delays and timeouts The frequency of the clock tick depends.
Memory Management 1. Functions OSMemCreate() Create a partition OSMemGet() Obtaining a memory block OSMemPut() Returning a memory block OSMemQuery() Obtaining.
Memory Management Chapter 7.
Chapter 17 Free-Space Management
Memory Management Chapter 7.
Chapter 4: Task Management
Day 03 Introduction to C.
Lesson One – Creating a thread
Day 03 Introduction to C.
Akos Ledeczi EECE 6354, Fall 2017 Vanderbilt University
Introduction to uC/OS-II
Review & Lab assignments
Dynamic Memory A whole heap of fun….
Comp 208 Computers in Engineering Yi Lin Fall, 2005
Pointers, Dynamic Data, and Reference Types
Run-time environments
Presentation transcript:

Chapter 12 Memory Management

The dynamic memory allocation can be achieved by using malloc() and free() function Using malloc() and free(0 in an embedded real-time system is dangerous –Eventually may incur fragmentation –Execution time of malloc() and free(0) are nondeterministic uC/OS-II provides fixed-sized memory blocks from partition made of a contiguous memory –Allocation and deallocation of these memory blocks is done in constant time and is deterministic Memory service: OSMemCreate(0, OSMemGet(), OSMemPut(), OSMemQuery() Figure 7.1 memory partition Figure 7.2 multiple memory partitions

Memory control block data structure The memory partitions is maintained by memory control block typedef struct { void *OSMemAddr; //point to the beginning of memory block void *OSMemFreeList; //point to MCB or memory block INT32U OSMemBlkSize; INT32U OSMemNBlks; //total memory blocks INT32U OSMemNFree; //current available memory blocks } OS_MEM; List of free memory control blocks

12.01 Creating a partition, OSMemCreate() Four arguments are required –Beginning address of the memory partition –The number of blocks to be allocated from this partition –The size (in bytes) of each block –A pointer to a variable that contains an error code OS_MEM *CommTxBuf; INT8U CommTxPart[100][32]; void main (void) { INT8U err; OSInit();. CommTxBuf = OSMemCreate(CommTxPart, 100, 32, &err);. OSStart(); }

OS_MEM *OSMemCreate (void *addr, INT32U nblks, INT32U blksize, INT8U *err) { OS_MEM *pmem; INT8U *pblk; void **plink; INT32U i; if (nblks < 2) { //each memory partition must contain at least two memory blocks (1) *err = OS_MEM_INVALID_BLKS; return ((OS_MEM *)0); } if (blksize < sizeof(void *)) { //each memory block must be able to hold the size of a pointer (2) *err = OS_MEM_INVALID_SIZE; return ((OS_MEM *)0); } OS_ENTER_CRITICAL(); pmem = OSMemFreeList; //obtain a MCB (3) if (OSMemFreeList != (OS_MEM *)0) { OSMemFreeList = (OS_MEM *)OSMemFreeList->OSMemFreeList; } OS_EXIT_CRITICAL(); if (pmem == (OS_MEM *)0) { (4) *err = OS_MEM_INVALID_PART; return ((OS_MEM *)0); } OSMemCrate(): return MCB address or 0

plink = (void **)addr; (5) pblk = (INT8U *)addr + blksize; for (i = 0; i < (nblks - 1); i++) { *plink = (void *)pblk; plink = (void **)pblk; pblk = pblk + blksize; } *plink = (void *)0; OS_ENTER_CRITICAL(); pmem->OSMemAddr = addr; (6) pmem->OSMemFreeList = addr; pmem->OSMemNFree = nblks; pmem->OSMemNBlks = nblks; pmem->OSMemBlkSize = blksize; OS_EXIT_CRITICAL(); *err = OS_NO_ERR; return (pmem); (7) }

Figure 12.4 The data structure after OSMemCreate() successful

12.02 Obtaining a Memory block, OSMemGet() The APs need to understand how size of memory block is required –Need to provide the associated pointer of memory control block –When you are done using the block, you must return it to the proper memory partition void *OSMemGet (OS_MEM *pmem, INT8U *err) (1) { void *pblk; OS_ENTER_CRITICAL(); if (pmem->OSMemNFree > 0) { (2) pblk = pmem->OSMemFreeList; (3) pmem->OSMemFreeList = *(void **)pblk; (4) pmem->OSMemNFree--; (5) OS_EXIT_CRITICAL(); *err = OS_NO_ERR; return (pblk); (6) } else { OS_EXIT_CRITICAL(); *err = OS_MEM_NO_FREE_BLKS; return ((void *)0); }

Returning a Memory Block, OSMemPut() If you return a wrong memory partition, the system may be crash or waste memory space INT8U OSMemPut (OS_MEM *pmem, void *pblk) (1) { OS_ENTER_CRITICAL(); if (pmem->OSMemNFree >= pmem->OSMemNBlks) { (2) OS_EXIT_CRITICAL(); return (OS_MEM_FULL); } *(void **)pblk = pmem->OSMemFreeList; (3) pmem->OSMemFreeList = pblk; pmem->OSMemNFree++; (4) OS_EXIT_CRITICAL(); return (OS_NO_ERR); }

Obtaining Status of a Memory Partition, OSMemQuery() typedef struct { void *OSAddr; /* Points to beginning address of the memory partition */ void *OSFreeList; /* Points to beginning of the free list of memory blocks */ INT32U OSBlkSize; /* Size (in bytes) of each memory block */ INT32U OSNBlks; /* Total number of blocks in the partition */ INT32U OSNFree; /* Number of memory blocks free */ INT32U OSNUsed; /* Number of memory blocks used */ } OS_MEM_DATA; INT8U OSMemQuery (OS_MEM *pmem, OS_MEM_DATA *pdata) { OS_ENTER_CRITICAL(); pdata->OSAddr = pmem->OSMemAddr; (1) pdata->OSFreeList = pmem->OSMemFreeList; pdata->OSBlkSize = pmem->OSMemBlkSize; pdata->OSNBlks = pmem->OSMemNBlks; pdata->OSNFree = pmem->OSMemNFree; OS_EXIT_CRITICAL(); pdata->OSNUsed = pdata->OSNBlks - pdata->OSNFree; (2) return (OS_NO_ERR); }

Using Memory Partitions

Scanning analog inputs and reporting errors AnalogInputTask() { for (;;) { for (all analog inputs to read) { Read analog input; (1) if (analog input exceed threshold) { Get memory block; (2) Get current system time (in clock ticks); (3) Store the following items in the memory block: (4) System time (i.e. a time stamp); The channel that exceeded the threshold; An error code; The severity of the error; Etc. Post the error message to error queue; (5) (A pointer to the memory block containing the data) } Delay task until it’s time to sample analog inputs again; } ErrorHandlerTask() { for (;;) { Wait for message from error queue; (6) (Gets a pointer to a memory block containing information about the error reported) Read the message and take action based on error reported; (7) Return the memory block to the memory partition; (8) }

Waiting for memory blocks from a partition OS_EVENT *SemaphorePtr; (1) OS_MEM *PartitionPtr; INT8U Partition[100][32]; OS_STK TaskStk[1000]; void main (void) { INT8U err; OSInit(); (2). SemaphorePtr = OSSemCreate(100); (3) PartitionPtr = OSMemCreate(Partition, 100, 32, &err); (4). OSTaskCreate(Task, (void *)0, &TaskStk[999], &err); (5). OSStart(); (6) } void Task (void *pdata) { INT8U err; INT8U *pblock; for (;;) { OSSemPend(SemaphorePtr, 0, &err); (7) pblock = OSMemGet(PartitionPtr, &err); (8).. /* Use the memory block */. OSMemPut(PartitionPtr, pblock); (9) OSSemPost(SemaphorePtr); (10) }