Discussions on hw7 Memory Allocation Exercise 3 functions:

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

Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
Dynamic memory allocation
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.
Dynamic Memory Management
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
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.
Memory management.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 12 Outline 12.1Introduction.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
C Lab 3 C Arraylist Implementation. Goals ●Review ○ Referencing/Dereferencing ○ Free ●realloc and memmove ●ArrayList ●Debugging with GDB.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Discussion: Week 3/26. Structs: Used to hold associated data together Used to group together different types of variables under the same name struct Telephone{
Malloc Recitation Section K (Kevin Su) November 5 th, 2012.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
1 Inner Workings of Malloc and Free Professor Jennifer Rexford COS 217.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees.
Memory Allocation CS Introduction to Operating Systems.
1 Homework / Quiz Exam 2 – Solutions Posted – Questions? Continuing K&R Chapter 6 HW6 is on line – due class 22.
CGS 3460 Pointer n To hold the location of another variable n Declaration: la type and a name with an asterisk lE.g. int *ptr; n Assigning a value lptr.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Stack and Heap Memory Stack resident variables include:
ICS 145B -- L. Bic1 Project: Main Memory Management Textbook: pages ICS 145B L. Bic.
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #6C Pointers,
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
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.
UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.
Chapter 17 Free-Space Management Chien-Chung Shen CIS, UD
CS 241 Section Week #9 (11/05/09). Topics MP6 Overview Memory Management Virtual Memory Page Tables.
Homework Finishing K&R Chapter 5 today –Skipping sections for now –Not covering section 5.12 Starting K&R Chapter 6 next.
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
1 Homework / Exam Finishing K&R Chapter 5 today –Skipping sections for now –Not covering section 5.12 Starting K&R Chapter 6 next Continue HW5.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Simulated Pointers Limitations Of C++ Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
CSE 351 Dynamic Memory Allocation 1. Dynamic Memory Dynamic memory is memory that is “requested” at run- time Solves two fundamental dilemmas: How can.
Dynamic Memory Management Jennifer Rexford 1. 2 Goals of this Lecture Dynamic memory management techniques Garbage collection by the run-time system (Java)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CSE 220 – C Programming malloc, calloc, realloc.
Chapter 17 Free-Space Management
Lectures linked lists Chapter 6 of textbook
Introduction to Programming
Data Structure and Algorithms
Dynamic Memory Allocation
CSCI206 - Computer Organization & Programming
Discussion 6 HW4 and Libraries.
Lists.
The Memory Manager Project
Clear1 and Clear2 clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) {
Lists.
Circular Buffers, Linked Lists
CS 240 – Lecture 21 Alloc Implementation.
CS Introduction to Operating Systems
Memory Management Chapter 10 11/24/2018 Crowley OS Chap. 10.
Dynamic Memory Allocation
CS703 - Advanced Operating Systems
EECE.2160 ECE Application Programming
Review & Lab assignments
CS111 Computer Programming
Malloc Lab CSCI 380: Operating Systems
Homework Continue with K&R Chapter 5 Skipping sections for now
Chapter 10-1: Dynamic Memory Allocation
Dynamic Memory – A Review
Presentation transcript:

Discussions on hw7 Memory Allocation Exercise 3 functions: void initalloc(void); char * alloc(int n); void freef(char *p); You are asked to write the freef() function The alloctest.c program is provided to test the functions It teaches you how to write malloc and free Explain about malloc and free again Initalloc -> allocates the buffer for our job -> simulate malloc and free on it Alloc -> similar to malloc Freef -> similar to free. You should wright the freef function -> return the used space to our initial buffer If your program works well, the output should be same as mine

initalloc function Initializes a large free buffer struct blockl{ unsigned int tag:8; unsigned int size :24; struct blockl *nextp; struct blockl *prevp;}; Blockrp = allocbuf + ALLOCSIZE -TAGSIZE Cursorp = freep = allocbuf F TAGSIZE allocbuf ALLOCSIZE Struct blockr{ unsigned int tag:8; unsigned int size:24;}; Bit fields Size = allocsize -> fixed size You should do all the computations in char * domain and not the struct doman. For example if curorp is a pointer to a structure, first I should cast it to char * pointer, then I can do calculation by doing subtraction TAGSIZE = sizeof(blockr) MINFREESIZE= sizeof(blockl) + sizeof(blockr) USEDTAG = 0xaa FREETAG = 0x55

Function alloc(n) Allocate a buffer of allocsize = n (in multiple of 4 ) + 2*TAGSIZE bytes from the free buffer pool The tags for both ends of the allocated buffer are of type struct blockr. They are not kept using a linked list (no need for the char * elements) Function returns a pointer = holdp + TAGSIZE Linux in its free time does search in your memory and creates a free list (linked list) of all the free spaces in the memory. So when you ask for malloc it finds from the free list. So we don’t need to keep track of used blocks holdp pointer returned by alloc U U n bytes in multiple of 4 TAGSIZE TAGSIZE

Allocation Algorithm I) cursorp->size < allocsize=4*((n-1)/4 +1) +2*TAGSIZE return null if all free blocks are not big enough II) cursor->size > allocsize + MINFREESIZE return pointer =holdp + TAGSIZE p=cursorp=holdp blockrp = p +holdp->size -TAGSIZE U allocsize U The size of free block has been changed blockrp =p + allocsize-TAGSIZE cursorp allocsize F U new free size holdp = p blockrp = p-TAGSIZE

Enchaining Free Buffers Allocated buffers are not chained An example of chaining 4 free buffers Most recent freep cursorp Least recent BUF 4 BUF 3 BUF 2 BUF 1 nextp =3 prevp =1 nextp =2 nextp =1 nextp =4 prevp =4 prevp =3 prevp =2

Unchaining free buffers Most recent Least recent BUF 4 BUF 3 BUF 2 BUF 1 nextp =3 prevp =1 nextp =2 nextp =1 nextp =4 prevp =4 prevp =3 prevp =2 freep cursorp BUF 4 BUF 2 BUF 1 nextp =2 prevp =1 nextp =1 nextp =4 prevp =4 prevp =2

Multiple cases of freef Left block is not free Case 1: Left block is free freef -> chnge0xaa to 0x55 and add to pointers If 2 free blocks are adjacent to each other, I should make a big free block so use the coalesce function To check you are in which case: check the tag of the left block. Use the pointer and cast it to struct * and see the tag Char * p Ptr = P – sizeof(struct blockr) – sizeof(struct blockl) (struct blockr *) ptr -> tag F F U U F F Case 3: Both left and right blocks are free

Pseudocode for freef(char *p) p is the same pointer obtained from alloc() Change tag to FREETAG on blockl Change tag to FREETAG on blockr No need to update size on blockl and blockr because alloc() sets the sizes correctly Check if block on left is also free? case 1: yes, coalesce free block on left and p case 2: no, enchain buffer Check if block on right is also free? case 3: yes, unchain the block on right and coalesce p with block on right. return