Simulated Pointers.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Dynamic Memory Management
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
CSC321: Programming Languages 11-1 Programming Languages Tucker and Noonan Chapter 11: Memory Management 11.1 The Heap 11.2 Implementation of Dynamic Arrays.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Memory Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
CS 1114: Data Structures – Implementation: part 1 Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
Linked lists and memory allocation Prof. Noah Snavely CS1114
Reference Counters Associate a counter with each heap item Whenever a heap item is created, such as by a new or malloc instruction, initialize the counter.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
SNU IDB Lab. Ch7. Linear Lists – Simulated Pointers © copyright 2006 SNU IDB Lab.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
GARBAGE COLLECTION IN AN UNCOOPERATIVE ENVIRONMENT Hans-Juergen Boehm Computer Science Dept. Rice University, Houston Mark Wieser Xerox Corporation, Palo.
Memory Management -Memory allocation -Garbage collection.
Simulated Pointers Limitations Of C++ Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
Linked Lists A formal data structure. Linked Lists Collections of data items “lined up in a row” Inserts and deletes can be done anywhere in the list.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Eliminating External Fragmentation in a Non-Moving Garbage Collector for Java Author: Fridtjof Siebert, CASES 2000 Michael Sallas Object-Oriented Languages.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
CSE 220 – C Programming malloc, calloc, realloc.
Memory Management.
Constructors and Destructors
Garbage Collection What is garbage and how can we deal with it?
Data Types In Text: Chapter 6.
Lecture 6 of Computer Science II
Storage 18-May-18.
CHP - 9 File Structures.
Lectures linked lists Chapter 6 of textbook
Big-O notation Linked lists
Prepared by, Jesmin Akhter, Lecturer, IIT, JU
Data Structure Dr. Mohamed Khafagy.
UNIT-3 LINKED LIST.
Dynamic Memory Allocation
Storage Management.
Data Structures Interview / VIVA Questions and Answers
Concepts of programming languages
Main Memory Management
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Pointers and Dynamic Variables
Optimizing Malloc and Free
Simulated Pointers.
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Linked List (Part I) Data structure.
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Variables Title slide variables.
Constructors and Destructors
Linked Representation
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Chapter 12 Memory Management
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Data Structures & Algorithms
Data Structures and Algorithms Memory allocation and Dynamic Array
LINKED LIST Dr. T. Kokilavani Assistant Professor
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Memory Management Memory management is the art and the process of coordinating and controlling the use of memory in a computer system Why memory management?
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

Simulated Pointers

Limitations Of C++ Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization. No arithmetic. Can’t use C++ pointers to represent a data structure stored on a disk. Serialization and deserialization may be expensive and complex operations. In some applications it is desirable to perform arithmetic on pointers.

Simulated-Pointer Memory Layout c a e d b Data structure of memory is an array, and each array position has an element field (type T) and a next field (type int). In a simulated pointer representation an array of nodes is used. The linked data structure is built inside this array.

Node Representation template <class T> class simulatedNode { public: // constructors defined here protected: T element; int next; } In chainNode the data type of next is chainNode<T>*, whereas in simulatedNode the data type of next is int. Other than this, there is really no significant difference.

How It All Looks c a e d b c a e d b next 11 14 14 -1 8 element 1 2 3 In chain<T> firstNode is of type chainNode<T>*, here it is an int. Logically, there is no difference between a list represented using C++ pointers/references and one represented using simulated pointers (i.e., integer pointers). next 11 14 14 -1 8 element c a e d b 1 2 3 4 5 8 11 14 firstNode = 4

Still Drawn The Same firstNode a b c d e -1 Note that we use a next value of –1 to mean that there is no next node. The use of –1 in our simulated pointer representations is equivalent to the use of null in the linked representations that use C++ pointers/references.

Memory Management Linked system (C++ or simulated pointer) requires: a way to keep track of the memory that is not in use (i.e., a storage pool) way to allocate a node C++ has the method new way to free a node that is no longer in use C++ has the method delete Memory management is a crucial component of any programming system that permits dynamic allocation/deallocation of memory. Linked lists require dynamic allocation/deallocation of nodes, and so require memory management methods. The term `garbage’ refers to memory that is neither in use nor in the storage pool. Garbage collection is the process of identifying garbage and hauling it to the storage pool.

Garbage Collection The system determines which nodes/memory are not in use and returns these nodes (this memory) to the pool of free storage. This is done in two or three steps: Mark nodes that are in use. Compact free space (optional). Move free nodes to storage pool. This is called the Mark-and-Sweep method. The garbage collector may be invoked whenever a request for memory fails because the storage pool doesn’t have enough memory to satisfy the request. Alternatively, the collector may work in the background scouring memory for portions/nodes that are not in use and also not in the storage pool.

Marking Unmark all nodes (set all mark bits to false). c a e d b firstNode Unmark all nodes (set all mark bits to false). Start at each program variable that contains a reference, follow all pointers, mark nodes that are reached. Each node has a mark bit that is false when the node is unmarked and true when the node is marked.

Marking c c a a e e d d b e firstNode Start at firstNode and mark all nodes reachable from firstNode. Repeat for all pointer variables.

Compaction Free Memory c a e d b e d b firstNode Move all marked nodes (i.e., nodes in use) to one end of memory, updating all pointers as necessary. Compaction is useful when the user requests nodes of different size. Following compaction you have a single large chunk of free memory and so can handle requests for large nodes.

Put Free Memory In Storage Pool Scan memory for unmarked nodes (if no compaction done), otherwise put single free block (unless no free memory) into pool.

Advantages Of Garbage Collection Programmer doesn’t have to worry about freeing nodes as they become free. However, for garbage collection to be effective, we must set reference variables to null when the object being referenced is no longer needed.

Advantages Of Garbage Collection Applications may run faster when run on computers that have more memory. This could happen when the garbage collector is set up to run only when a request to allocate a node cannot be met using memory currently in the storage pool. In such an implementation of garbage collection, increasing a computer’s memory could reduce the number of times the garbage collector is invoked.

Disadvantage Of Garbage Collection Garbage collection time is linear in memory size (not in amount of free memory). Many different algorithms exist, tradeoffs So a lot effort could be expended to reclaim a small amount of unused memory.

Alternative To Garbage Collection Provide a method to free/deallocate a node. e.g., delete method of C++ Now free nodes are always in storage pool.

Advantage Of Alternative Time to free nodes is proportional to number of nodes being freed and not to total memory size. It is desirable to spend effort in proportion to the amount of useful work that is done.

Disadvantages Of Alternative User must write methods to free data structure nodes. Time is spent freeing nodes that may not be reused. Application run time does not improve with increase in memory size.

Storage Pool Organization When All Nodes Have Same Size b c d e -1 firstNode Storage pool is a chain of nodes that are not in use. Maintain a chain of free nodes Allocate from front of chain Add node that is freed to chain front

Simulated-Pointer Memory Management template <class T> class simulatedSpace { public: // constructor and other methods // defined here protected: int firstNode; simulatedNode<T> *node; }

Constructor template <class T> simulatedSpace (int numberOfNodes) { node = new simulatedNode<T> [numberOfNodes]; // create nodes and link into a chain for (int i = 0; i < numberOfNodes - 1; i++) node[i].next = i + 1; // last node of array and chain node[numberOfNodes - 1].next = -1; // first node of chain of free nodes firstNode = 0; }

Allocate A Node template <class T> int allocateNode(T& element, int next) {// Allocate a free node and set its fields. if (firstNode == -1) {// double number of nodes, code omitted } int i = firstNode; // allocate first node firstNode = node[i].next; node[i].element = element; node[i].next = next; return i;

Free A Node template <class T> void deallocateNode(int i) {// Free node i. // make i first node on free space list node[i].next = firstNode; firstNode = i; }

Simulated Pointers Can allocate a chain of nodes without having to relink. Can free a chain of nodes in O(1) time when first and last nodes of chain are known.

Simulated Pointers Don’t use unless you see a clear advantage to using simulated pointers over C++ pointers. Use C++11 smart pointers Reduce bugs, retain efficiency