Introduction to Systems Programming (CS 0449)

Slides:



Advertisements
Similar presentations
Part IV: Memory Management
Advertisements

Palm OS Frederick Penrose, Kevin Wong, and Hoan Do.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Chapter 4 : File Systems What is a file system?
The Linux Kernel: Memory Management
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Virtual Memory Introduction to Operating Systems: Module 9.
An Overview of Palm OS n Designed for special hardware –small screen ( 160 x 160 ) –less processing power than desktop PCs –quick turnaround expected –limited.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
CS 104 Introduction to Computer Science and Graphics Problems
Computer Organization and Architecture
Windows 2000 Memory Management Computing Department, Lancaster University, UK.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-7 Memory Management (1) Department of Computer Science and Software.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 8: Main Memory.
Palm OS Jeremy Etzkorn Paul Rutschky Adam Lee Amit Bhatia Tony Picarazzi.
Chapter 3.5 Memory and I/O Systems. 2 Memory Management Memory problems are one of the leading causes of bugs in programs (60-80%) MUCH worse in languages.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Programming of Mobile and Handheld Devices Lecture 9: Memory, databases and files on Palm OS Rob Pooley
Topic 2d High-Level languages and Systems Software
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
Chapter 4 Memory Management Virtual Memory.
CE Operating Systems Lecture 17 File systems – interface and implementation.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Virtual Memory Pranav Shah CS147 - Sin Min Lee. Concept of Virtual Memory Purpose of Virtual Memory - to use hard disk as an extension of RAM. Personal.
Memory Management Chapter 5 Advanced Operating System.
Chapter 10 Chapter 10 Implementing Subprograms. Implementing Subprograms  The subprogram call and return operations are together called subprogram linkage.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CS161 – Design and Architecture of Computer
Stack and Heap Memory Stack resident variables include:
Processes and threads.
Module 11: File Structure
Memory COMPUTER ARCHITECTURE
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
CS161 – Design and Architecture of Computer
CS522 Advanced database Systems
Chapter 9: Virtual Memory – Part I
Dynamic Memory Allocation
Run-time organization
Main Memory Management
Circular Buffers, Linked Lists
Object Oriented Programming COP3330 / CGS5409
CS Introduction to Operating Systems
Chapter 9: Virtual-Memory Management
Object Oriented Programming COP3330 / CGS5409
Background Program must be brought into memory and placed within a process for it to be run. Input queue – collection of processes on the disk that are.
File System B. Ramamurthy B.Ramamurthy 11/27/2018.
Pointers, Dynamic Data, and Reference Types
Week 9 – Lesson 1 Arrays – Character Strings
Main Memory Background Swapping Contiguous Allocation Paging
CS399 New Beginnings Jonathan Walpole.
Dynamic Memory Allocation
Memory Allocation CS 217.
Page Replacement Implementation Issues
Memory Management Overview
Virtual Memory Hardware
Lecture 3: Main Memory.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
COP 3330 Object-oriented Programming in C++
Buddy Allocation CS 161: Lecture 5 2/11/19.
COMP755 Advanced Operating Systems
Pointers, Dynamic Data, and Reference Types
Chapter 8 & 9 Main Memory and Virtual Memory
Lecture 20: Representing Data Elements
CSE 542: Operating Systems
Chapter 5 File Systems -Compiled for MCA, PU
Presentation transcript:

Introduction to Systems Programming (CS 0449) Palm OS Memory Management Memory Managements and Memory APIs Pointers and Handles (Ch.7—PDA Programming in C book & Ch.4, pg102, PalmOS Bible book). Palm OS string functions (Ch.8—PDA Programming in C book, & Ch.9, pg314, PalmOS Bible book).

Palm OS Memory Palm OS Version 4 and before.

Palm OS Architecture - Memory Management Palm OS built on 32-bit memory architecture. with data types: 8, 16, 32 bit-long. (Palm OS V4.1) Memory Addresses: 32-bit long OS Address Space = 4 GB = 2 32 (to store data + code) –This is a theoretical size! OS reserves 256 MB of address space for each card. A Card is a logical abstraction to describe memory area used to contain (ROM + RAM).

Palm OS Architecture – RAM (Dynamic + Storage) Dynamic RAM (Dynamic Heap) Storage RAM (Storage Heap) Desktop Computer Actual RAM (Physical Memory) File System (Hard Disk) Heap: a complete binary tree having a level order relationship among the nodes. max-heap: value-parent >= value-of-each-of-its-children min-heap: value-parent <= value-of-each-of-its-children 60 50 55 30 10 52

Palm OS Architecture – RAM (Dynamic + Storage) Heap in Palm OS: An area of contiguous memory that manages and controls smaller units of memory (like the idea of a heap structure) that are called chunks. Chunk: are in memory between 1-byte and less than 64-KB 1-byte < Chunk < 64-KB = 65,528 bytes All data in Palm OS are stored in chucks.

Palm OS Memory Management – RAM (Dynamic + Storage) Dynamic RAM: Used to implement Dynamic Heap! -Compares to actual RAM on desktop computer. -Provides space for temp storage of: System Global Variables. System Dynamic Allocation (TCP/IP stack, IrDA stack). Application Dynamic Allocation. Application Stack. Application Global Variables. Storage RAM: Used same way as file systems on desktop computer. Provides permanent storage for application & data Storage Heap!

Storage RAM in Palm OS- Memory Fragmentation Occurs as Storage Heaps fill with data If total free memory > new record-size there might not be enough contiguous space in any given heap to contain the record. Palm OS V 1.0, 2.0 Palm OS V.3.0 & later versions use a single large heap to prevent Fragmentation…

Palm OS V 1.0 & 2.0 64KB X 40KB 50KB System update 2.0.4 64KB 40KB Before Allocation X After Allocation None of the heaps has enough free space. Fails 64KB 40KB 50KB Before Allocation After Allocation System moves chunks from least occupied heap until enough space for new memory allocation System update 2.0.4 96KB 20KB 50KB Fragmentation is not a problem PalmOS V3.0

Palm OS Memory Management – [ ROM + RAM (Dynamic + Storage) ] Built-in Apps Default DB RAM Storage RAM Add-on Applications Preferences User data Dynamic RAM Shared by OS and active applications: Runtime storage -OS (Global variables + Dynamic allocations) -Active Applications (Globals, Stack, Dynamic allocations)

Palm OS Memory Management – Writing to RAM Problem: Writing accidentally through an invalid pointer? (a poorly written application) Writing to RAM in Linux OS: Each application has its own address space and user’s data are in files. Solution: A bad pointer write harms only the current application. Writing to (dynamic or worse to storage) RAM in Palm OS! Previous solution does not work! Because there is no separate address space and there is only one application running. Writing to dynamic RAM is not so bad, since you can always fix by resetting. Writing into storage RAM is bad! Will overwrite existing apps or data. Solution:  Hardware write protection on the storage area of RAM.

9- Palm OS Memory Management – Writing to Storage Memory Solution 1) 2) Write Direct Write Dynamic Memory Write Fails Due to write protection Storage Memory To a valid block of storage Heap Write OS Validate Call Turn off write protection and then turn it back on. Cost: on write  Slow! extra instructions! on read  Ok.

10- Amount of dynamic memory available Depends on Version of Palm OS Amount of RAM in device Palm OS Version 3.0 and earlier If RAM <= 512 KB  dynamic area = 32 KB -If RAM <= 1 MB  dynamic area = 64 KB -else = 96 KB Palm OS Version (3.1  3.3) System heap = 128 KB Palm OS V.3.5 If RAM < 2 MB  gives 64KB dynamic area. -If RAM = 2 MB  dynamic area = 128 KB. =4 MB  dynamic area = 256 KB.

Memory APIs - Allocation Application Programming Interface, a set of functions and data structures that give a developer access to certain features of an operating system or program. Memory Allocation (Dynamic RAM) MemHandle MemHandleNew(UInt32 size) Returns a relocatable memory chunk of desired size. Null on Err. Err MemHandleFree(MemHandle h) Free (deallocate) a relocatble memory chunk. It may be called with locked chunk. Don’t call it more than once, and don’t call it with NULL. Memory Allocation (Storage RAM) MemPtr MemPtrNew(UInt32 size) Returns a nonrelocatable memory chunk of desired size. Null on Err. void MemPtrFree(MemPtr p) It may be called to free locked relocatble chunk.

Memory APIs – Locking Memory Chunks There are APIs for locking and unlocking memory chunks. 1. MemPtr MemHandleLock(MemHandle h) -Lock the relocatable memory chunk and return a ptr to the locked block. -Err: if called on an already locked chunk with > 14 (max =14 times). “chunk overloacked” Err. 2. Err MemHandleUnlock(MemHandle h) -Unlock locked relocatable memory chunk. -Err: to call it if lock count=0 (not locked). “chunk underlocked” 3. Err MemPtrUnlock(MemPtr p) -Unlock locked relocatable memory chunk referenced by the pointer. ***It can be used if you no longer have access to the handle. 4. MemHandle MemPtrRecoverHandle(MemPtr p) -Returns the handle associated with the passed-in locked pointer. -Useful if misplaced handle.

Memory APIs –Memory Size Information There are APIs for determining the size of memory chunks and resizing the chunk. 1. UInt32 MemHandleSize(MemHandle h) -Returns the size allocated for the relocatable block. 2. UInt32 MemPtrSize(MemPtr p) -Returns the size allocated for the block pointed to by p. 3. Err MemHandleResize(MemHandle h, UInt32 newSize) -Resize the specified block to new size. -If block is locked  a resize to a smaller size will succeed, but to a larger size, it will it will fail (unless there is a contiguous free space). 4. Err MemPtrResize(MemPtr p, UInt32 newSize) -If block is locked  a resize to a smaller size will succeed, but to a larger size, it will fail (unless there is a contiguous free space).

Memory APIs – Heap Information There are APIs for finding out information about cards and heaps. 1. UInt16 MemNumCards(void) -Returns the number of cards on the device. 2. UInt16 MemNumHeaps(UInt16 cardNumber) -Returns the number of memory heaps on the specified card. 3. UInt16 MemHeapID(UInt16 cardNo, UInt16 heapIndex) -Returns for a given heap number, it ID number, which are assigned sequentially starting from 0. i.e. HeapID=0 on card0 is the dynamic heap. 4. Err MemHeapFreeBytes (UInt16 heapID, UInt32 *freeP, UInt32 *maxP) -Returns total free space and max contiguous free space. 5. UInt32 MemHeapSize(UInt16 heapID) -Returns the total size of the heap with its given ID. 6. UInt16 MemPtrHeapID(MemPtr p) -Returns the heap ID of the heap to which a pointer has been allocated.

Pointers & Handles

Getting Field Text // Getting the field pointer FieldType *field = (FieldType *) GetObjectPtr( MyFieldID ); UInt16 length; char *textPtr; // Getting the length of the field length = FldGetTextLength( field ); // Getting the field text (valid only until user edits field) // "textPtr" will be NULL if there is no text in the field textPtr = FldGetTextPtr( field );

Allocating memory // Allocate static chunk of memory char * textPtr = MemPtrNew( 32 ) ; // Deallocate static chunk of memory MemPtrFree ( textPtr ) ;

Pointers Revisited Pointer points to a static position in memory Problem: memory fragmentation A B C D E F G 1. Memory Free A Free C E F X 2. delete B,D,G, add X ? A C E F 3. X Free

Pointers vs. Handles Solution: let OS manage pointers Palm OS Pointer table Ptr to A Ptr to C Ptr to E Ptr to F Application Handle to A Handle to C Handle to E Handle to F Memory A Free C Free E F Allocate new handle Free X Free

Pointers vs. Handles Solution: let OS manage pointers Palm OS Application Handle to A Handle to C Handle to E Handle to F Handle to X Palm OS Pointer table Ptr to A Ptr to C Ptr to E Ptr to F Ptr to X A C E F Memory X Free

Handles //Handles are pointers to memory that may be moved by the OS. // Allocate moveable chunk of memory MemHandle textH = MemHandleNew( 32 ) ; //returns a handle (textH) to a moveable chunk. // Deallocate moveable chunk of memory MemHandleFree ( textH ) ;

Handles and Pointers: Size UInt32 length; // Get the size of the moveable chunk length = MemHandleSize( textH ); // Get the size of the static chunk length = MemPtrSize( textPtr );

Handles, Locking and Resizing //Because the OS may move freely a memory chunk connected to a handle, you must first lock the handle before you read/write data to the chunk. // Lock handle before accessing contents char * textPtr = MemHandleLock( textH ) ; // Make sure there is enough space MemHandleResize( textH, sizeof("Hello") + 1); // Use the memory buffer StrCopy( textPtr, "Hello" ); // Unlock the memory MemHandleUnlock ( textH ) ;

Use pointers (MemPtrNew()) when: Handles vs. Pointers When to use handles? When to use pointers? Which is better? Use pointers (MemPtrNew()) when: Memory is frequently updated (memory chunks locked for a long time are not desired), and/or Memory chunk locked for long periods of time (chunks not frequently updated) With Pointers, the memory chunks cannot be relocated (considered SRAM by the OS). Use handles (MemHandleNew()) when: Lock memory chunk for short periods of time Infrequent need to resize memory chunk/buffer With handles, the memory chunk associated with the handle can be relocated.

Palm OS String functions Built-in string functions, different names. PalmOS Standard library Description StrLen strlen Return length of string in characters StrCopy strcpy Copy source string to target buffer StrCat strcat Append one string to the other StrAToI atoi Convert string to integer StrIToA itoa Convert integer to string (as decimal) StrIToH Convert integer to string (as hexadecimal) StrCompare strcmp Compare two strings StrNCopy strncpy copy strings up to certain length StrNCompare strncmp compare strings up to certain length

Getting Form Object Pointer (Palm OS Bible, Chapter 8, page 268) // We need an object pointer before we can do anything with the // object, such as to read or change a Field text. // The procedure below returns object pointer from its resource ID MemPtr GetObjectPtr ( UInt16 objectID ) { FormType * form; form = FrmGetActiveForm ( ); return ( FrmGetObjectPtr ( form , FrmGetObjectIndex (form, objectID ) ) ); } ... // Usage Example FieldType *field = (FieldType *) GetObjectPtr( MyFieldID );

Getting Field Text (Palm OS Bible, Chapter 8, page 280) // Getting the field pointer FieldType *field = (FieldType *) GetObjectPtr( MyFieldID ); UInt16 length; char *textPtr; // Getting the length of the field length = FldGetTextLength( field ); // Getting the field text (valid only until user edits field) // "textPtr" will be NULL if there is no text in the field textPtr = FldGetTextPtr( field );

Modifying Field Text (Palm OS Bible, Chapter 8, page 281) // Parameters: the field pointer and the new text string void FldSetNewText( FieldType *field , char * newText) { MemHandle textH; // Text string handle char * str; // temporary string pointer textH = FldGetTextHandle( field ); // Get the field text handle if ( textH ) { // if there was handle, FldSetTextHandle ( field , NULL ); // then release and resize MemHandleResize (textH , StrLen( newText ) + 1 ); } else textH = MemHandleNew ( StrLen( newText ) +1 ); str = MemHandleLock ( textH ); // Get text pointer and lock the handle StrCopy ( str, newText ) ; // Copy the new string MemHandleUnlock ( textH ); // Unlock the text memory handle FldSetTextHandle ( field , textH ); // Set the new field value FldDrawField ( field ); // Redraw the field