summaryrefslogtreecommitdiffstats
path: root/research/malloc.txt
blob: 633548622a8be70a79c331828d98e216bedfa018 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
A heap is a set of slots.  Contiguous sets of slots with the same status (free
or allocated) are grouped into blocks.

Example Heap Activity
=====================

	|
	|
	
	malloc
	 ___
	|   |
	|___|
	
	malloc
	 _______
	|   .   |
	|_______|
	
	malloc
	 ___________
	|   .   .   |
	|___________|
	
	free 1
	 ___ ___ ___
	|   | F |   |
	|___|___|___|
	
	malloc
	 ___________
	|   .   .   |
	|___________|
	
	malloc
	 _______________
	|   .   .   .   |
	|_______________|
	
	malloc
	 ___________________
	|   .   .   .   .   |
	|___________________|
	
	malloc
	 _______________________
	|   .   .   .   .   .   |
	|_______________________|
	
	malloc
	 ___________________________
	|   .   .   .   .   .   .   |
	|___________________________|
	
	malloc
	 _______________________________
	|   .   .   .   .   .   .   .   |
	|_______________________________|
	
	free 1
	 ___ ___ _______________________
	|   | F |   .   .   .   .   .   |
	|___|___|_______________________|
	
	free 2
	 ___ _______ ___________________
	|   | F .   |   .   .   .   .   |
	|___|_______|___________________|
	
	free 6
	 ___ _______ ___________ ___ ___
	|   | F .   |   .   .   | F |   |
	|___|_______|___________|___|___|
	
	free 4
	 ___ _______ ___ ___ ___ ___ ___
	|   | F .   |   | F |   | F |   |
	|___|_______|___|___|___|___|___|
	
	free 5
	 ___ _______ ___ ___________ ___
	|   | F .   |   | F .   .   |   |
	|___|_______|___|___________|___|
	
	malloc
	 _______ ___ ___ ___________ ___
	|   .   | F |   | F .   .   |   |
	|_______|___|___|___________|___|
	
	malloc
	 _______________ ___________ ___
	|   .   .   .   | F .   .   |   |
	|_______________|___________|___|
	
	malloc
	 ___________________ _______ ___
	|   .   .   .   .   | F .   |   |
	|___________________|_______|___|

Superpseudocode
===============

	malloc
		if no top
			set heap->top
			set heap[heap->top] size=1 prev=null, next=null
			return top

	malloc
		if no top
			set and return top
		else if no free head
			grow and return top
		else (free blocks exist)
			if free head size == 1
				move free head to next block
				return old block
			else
				move free block start to head+1, --size
				return old block
	free
		if ptr+1 free
			if heap[ptr+1]->prev + size = ptr
				merge prev with ptr+1
			else
				merge ptr with ptr+1
		elif ptr-1 free
			merge ptr-1 with ptr (just ++size)
		else
			mark ptr free

Pseudocode
==========

`__malloc(heap)`:

	if heap->top = null
		# Empty heap, grow
		heap->top = 0
		return 0
	elif heap->free_head = null
		# No free blocks, grow heap
		return ++heap->top
	else
		# Free blocks exist, reallocate
		if heap[heap->free_head]->size == 1
			# Take whole block, point to next block
			new = heap->free_head
			heap->free_head = heap[heap->free_head]->next
			heap[heap->free_head]->prev = null
			return new
		else
			# Shrink block
			new = heap->free_head
			++heap->free_head
			heap[heap->free_head]->size = heap[new]->size - 1
			heap[heap->free_head]->prev = null
			heap[heap->free_head]->next = heap[new]->next
			unset heap[new]->size
			unset heap[new]->prev
			unset heap[new]->next
			return new

`__free(heap, ptr)`:

	if ptr < heap->top and heap[ptr + 1]->size != null
		
	elif ptr > 0 and heap[ptr - 1]->size != null
		++heap[ptr - 1]->size
	else
		heap->ptr

	if ptr > 0 and heap[ptr - 1]->size != null
		++heap[ptr - 1]->size
	elif ptr < heap->top and heap[ptr + 1]->size != null
		heap[ptr]->size = heap[ptr + 1]->size + 1
		heap[ptr]->prev = heap[ptr + 1]->prev
		heap[ptr]->next = heap[ptr + 1]->next