ds0 DYNAMIC STORAGE ALLOCATION Hanan Samet Compute Science Depatment and Cente fo Automation Reseach and Institute fo Advanced Compute Studies Univesity of Mayland College Pak, Mayland 07 e-mail: hjs@umiacs.umd.edu Copyight 1997 Hanan Samet These notes may not be epoduced by any means (mechanical o electonic o any othe) without the expess witten pemission of Hanan Samet
DYNAMIC STORAGE ALLOCATION 6 v 5 g z 81 b ds1 Explicit allocation and deallocation ( feeing o libeating ) of blocks of contiguous stoage locations Issues: 1. how to keep tack of available space and its patitioning usually keep a linked list of available blocks a. elements location of stat of block size size of block pointe to next block in list b. how to ode (i.e., sot ) list size by location (i.e., inceasing ode) by size no ode. how to find a block of b consecutive locations if list soted by location, find fist one with s b (fist fit) a. equies a seach b. but good if want to mege adjacent empty blocks into lage ones upon stoage deallocation if list soted by size, find smallest one with s b (best fit) Ex: fist fit is supeio to best fit equest available aeas fist fit available aeas best fit stat 100,100 100,100 1000 00,100 100,00 1100 00,100 00,00 50 50,100 STUCK! Requests in ode of inceasing size: fist fit is bette Requests in ode of deceasing size: best fit is bette Can give example whee best fit is bette than fist fit
FRAGMENTATION ds Fagmentation esults when too many small blocks ae geneated Solutions: 1. can avoid by choosing a constant k and selecting block a of size s to satisfy the equest fo a block of size b if s b < k eliminates small blocks speeds up seach in fist-fit method as list of blocks is smalle. can avoid inspecting blocks that ae too small in fist-fit by pefoming seach in a cicula manne so that it esumes whee the last block was found. can also avoid by using compaction upon deallocation
LIBERATION ds 1. Want to etun stoage to the AVAIL list as soon as possible implies that can coalesce elements of AVAIL list into lage blocks. Contast with methods based on gabage collection which allocate stoage continuously until exhausting the AVAIL list followed by a pass fo stoage eclamation and compaction. Combining gabage collection with compaction stoage locations must be moved need to execise cae when moving pointe data pesence of elocation egistes obviates some of the poblems, since the pointes could be offset addesses
LIBERATION WITH COALESCING g z 81 b ds Ex: assume a soted AVAIL list by memoy locations i.e., LINK(p) Ω LINK(p)>p coalesce fom below fee next on AVAIL libeated coalesce fom above next on AVAIL libeated coalesce fom below and above next on AVAIL libeated Poblem: each time the algoithm is invoked to libeate block pointed at by p, we must seach though appoximately half the list to locate q such that LINK(q)>p
LIBERATION ALGORITHM ds5 Assume N consecutive wods stating at P0 ae being libeated Algoithm: 1. seach though AVAIL until finding a node Q such that link(q) = P > P0. if P0+N = P then begin /* coalesce fom above */ size(p0) size(p)+n; link(p0) link(p); end else begin link(p0) P; size(p0) N; end;. if Q+size(Q) = P0 then begin /* coalesce fom below */ size(q) size(q)+size(p); /* N was aleady accounted fo in step (above) */ link(q) link(p0); end else link(q) P0;
LIBERATION USING DOUBLY-LINKED LISTS g z 81 b ds6 Data stuctue block inuse=+ inuse size }SIZE { wods inuse fee block size link link inuse= inuse=+ INUSE and SIZE fields 1. easy to locate immediately adjacent blocks to detemine if coalescing is possible. obviate need to sot list of available blocks (AVAIL) in inceasing memoy size. moe complex if sot AVAIL by block size as need to update Doubly-linked AVAIL enables easy emoval of coalesced blocks Ex: inuse fee inuse size inuse= coalesce fom below pev next + libeated - + + coalesce fom above pev + libeated + next coalesce fom below and above pev next pev next + libeated +
BUDDY SYSTEM ds7 Restict block size to be a powe of 1. all blocks of size k stat at location x whee x mod k = 0. given a block stating at location x such that x mod k = 0 BUDDY k (x) = x + k if x mod k+1 = 0 BUDDY k (x) = x k if x mod k+1 = k Ex: BUDDY (10100) = 10000. only buddies can be meged. ty to coalesce buddies when stoage is deallocated k diffeent available block lists one fo each block size When equest a block of size k and none is available: 1. split smallest block j > k into a pai of blocks of size j 1. place block on appopiate AVAIL list and ty again Data stuctue 1. doubly-linked list (not cicula) FREE of available blocks indexed by k links stoed in actual blocks FREE[k] points to fist available block of size k. each block contains INUSE bit SIZE NEXT and PREV links fo FREE list Can get geate vaiety in block sizes using Fibonacci sequence of block sizes so b i = b i 1 +b i and now atio of successive block sizes is / instead of 1/
EXAMPLE OF BUDDY ALGORITHM 5 v g z 81 b ds8 M = 15 1 1 1 11 10 9 8 7 6 5 1 0 I S P N 0 0 0 Ω Ω Ω Ω Ω 0 0 0 0 Ω Ω Ω Ω Ω 0 1 1 1 8 Ω Ω 0 1 1 1 Ω Ω 0 1 0 Ω 10 Ω Ω 0 1 1 1 0 16 Ω 1 Ω Ω k FREE[k] 0 Ω 1 Ω 10 Ω 1 Ω 8 Ω 0 Ω initially, one block of size 16 stating at location 0 is available allocate a block of size allocate blocks of size,, in ode fee the block at location fee the block at location 0 mege block at 0 with its buddy at no futhe meging is possible as the buddy at is in use
BUDDY ALGORITHM NOTES ds9 Assume stoage uns fom locations 0 to m 1 To eseve a block of size k : 1. find smallest j fo which FREE[j] Ω (assume this block stats at location n). emove the block at location n fom FREE[j]. while j>k do begin j j 1; add block at location n+ j to FREE[j]; end; To libeate a block of size k stating at location n: while k m and NOT(INUSE(BUDDY k (n))) do begin emove BUDDY k (n) fom FREE[k]; k k+1; if BUDDY k (n)<n then n BUDDY k (n); end; INUSE flag only needs to be set in fist wod of each block 1. all emaining elements (wods) have thei buddies within the same block. no one outside the block will look fo buddies within the block
OVERFLOW 5 v g z 81 b ds10 At times, have moe stoage allocation equests than available memoy Can pefom gabage collection with compaction but will soon un out of memoy again Altenatively, emove blocks to seconday stoage: 1. keep a doubly-linked list of blocks in use, soted accoding to fequency of use wheneve a block is accessed, move it to font of list like a self-oganizing file Ex: stat C A B C D accessing C causes it to move to the font. cicula list of blocks and a ecently-used bit indicating if the block was accessed since the last time blocks wee emoved to seconday stoage to emove a block, mach down the list looking fo a 0 and eset all 1s that wee encounteed to 0 cuculating pointe ensues that a block eset to 0 will not be checked again fo emoval until all othe blocks have been checked Ex: stat stat 1 1 1 0 1 0 A 0 B 0 1 C D E 0 0 block D is the fist to be emoved access block A block B is emoved next stat