-
Notifications
You must be signed in to change notification settings - Fork 3
/
zlox_uheap.c
486 lines (437 loc) · 18.6 KB
/
zlox_uheap.c
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/* zlox_kheap.c Kernel heap functions, also provides
a placement malloc() for use before the heap is
initialised. */
#include "zlox_uheap.h"
#include "zlox_paging.h"
#include "zlox_monitor.h"
// Defined in zlox_task.c
extern ZLOX_TASK * current_task;
ZLOX_VOID * zlox_uheap_alloc(ZLOX_UINT32 size, ZLOX_UINT8 page_align, ZLOX_HEAP * heap);
ZLOX_VOID zlox_uheap_free(ZLOX_VOID *p, ZLOX_HEAP *heap);
ZLOX_VOID zlox_uheap_panic_assert(const ZLOX_CHAR *file, ZLOX_UINT32 line, const ZLOX_CHAR * desc)
{
// We encountered a massive problem and have to stop.
asm volatile("cli"); // Disable interrupts.
zlox_monitor_write("ASSERTION-FAILED(");
zlox_monitor_write((const ZLOX_CHAR *)desc);
zlox_monitor_write(") at ");
zlox_monitor_write((const ZLOX_CHAR *)file);
zlox_monitor_write(":");
zlox_monitor_write_dec(line);
zlox_monitor_write("\n");
zlox_exit(-1);
}
#define ZLOX_ASSERT_UHEAP(b) ((b) ? (void)0 : zlox_uheap_panic_assert(__FILE__, __LINE__, #b))
ZLOX_UINT32 zlox_uheap_malloc_int(ZLOX_UINT32 sz, ZLOX_SINT32 align, ZLOX_UINT32 *phys)
{
// 是否初始化了堆,如果初始化了,则使用堆来分配内存
if (current_task->heap != 0)
{
ZLOX_VOID *addr = zlox_uheap_alloc(sz, (ZLOX_UINT8)align, current_task->heap);
if (phys != 0)
{
// 从分页表里获取addr线性地址对应的实际的物理内存地址
ZLOX_PAGE *page = zlox_get_page((ZLOX_UINT32)addr, 0, current_task->page_directory);
*phys = (page->frame * 0x1000) + ((ZLOX_UINT32)addr & 0xFFF);
}
// 返回线性地址
return (ZLOX_UINT32)addr;
}
else
{
return 0;
}
}
// 给用户态程式使用的分配堆函数
ZLOX_UINT32 zlox_umalloc(ZLOX_UINT32 sz)
{
ZLOX_UINT32 ret = zlox_uheap_malloc_int(sz, 0, 0);
return ret;
}
// 给用户态程式使用的释放堆函数
ZLOX_VOID zlox_ufree(ZLOX_VOID *p)
{
if (current_task->heap != 0)
zlox_uheap_free(p, current_task->heap);
else
return;
}
// 当堆空间不足时,扩展堆的物理内存
static ZLOX_VOID zlox_uheap_expand(ZLOX_UINT32 new_size, ZLOX_HEAP * heap)
{
// Sanity check.
ZLOX_ASSERT_UHEAP(new_size > heap->end_address - heap->start_address);
// Get the nearest following page boundary.
// 扩展的尺寸必须是页对齐的
if ((new_size & 0x00000FFF) != 0)
{
new_size &= 0xFFFFF000;
new_size += 0x1000;
}
// Make sure we are not overreaching ourselves.
ZLOX_ASSERT_UHEAP(heap->start_address + new_size <= heap->max_address);
// This should always be on a page boundary.
ZLOX_UINT32 old_size = heap->end_address-heap->start_address;
ZLOX_UINT32 i = old_size;
ZLOX_BOOL need_flushTLB = ZLOX_FALSE;
while (i < new_size)
{
// 从frames位图里为扩展的线性地址分配物理内存
zlox_alloc_frame( zlox_get_page(heap->start_address+i, 1, current_task->page_directory),
(heap->supervisor)?1:0, (heap->readonly)?0:1);
i += 0x1000 /* page size */;
need_flushTLB = ZLOX_TRUE;
}
if(need_flushTLB)
zlox_page_Flush_TLB();
heap->end_address = heap->start_address+new_size;
}
// 根据需要回收一部分堆的物理内存,将回收的部分从页表和frames位图里去除
static ZLOX_UINT32 zlox_uheap_contract(ZLOX_UINT32 new_size, ZLOX_HEAP * heap)
{
// Sanity check.
ZLOX_ASSERT_UHEAP(new_size < heap->end_address-heap->start_address);
ZLOX_UINT32 new_size_orig = new_size;
// Get the nearest following page boundary.
if (new_size & 0x00000FFF)
{
new_size &= 0xFFFFF000;
new_size += 0x1000;
}
// Don't contract too far!
// 回收的尺寸受到ZLOX_UHEAP_MIN_SIZE的限制,防止回收过多的内存资源
if (new_size < ZLOX_UHEAP_MIN_SIZE)
{
if(ZLOX_UHEAP_MIN_SIZE - new_size >
sizeof(ZLOX_KHP_HEADER) + sizeof(ZLOX_KHP_FOOTER))
new_size = ZLOX_UHEAP_MIN_SIZE;
// 如果收缩堆空间后,剩余尺寸不能形成一个有效的hole,则放弃收缩,返回原来的尺寸
else
return (heap->end_address-heap->start_address);
}
ZLOX_UINT32 diff = new_size - new_size_orig;
if(diff == 0)
;
else if(diff > 0)
{
ZLOX_KHP_HEADER * header = (ZLOX_KHP_HEADER *)(heap->start_address + new_size_orig);
if(diff <= (sizeof(ZLOX_KHP_HEADER) + sizeof(ZLOX_KHP_FOOTER)))
{
new_size += 0x1000;
}
diff = new_size - new_size_orig;
if(diff >= header->size)
{
return (heap->end_address-heap->start_address);
}
}
ZLOX_UINT32 old_size = heap->end_address-heap->start_address;
ZLOX_UINT32 i = old_size - 0x1000;
ZLOX_BOOL need_flushTLB = ZLOX_FALSE;
while (new_size <= i)
{
// 通过zlox_free_frame将需要回收的页面从页表和frames位图里去除
zlox_free_frame(zlox_get_page(heap->start_address+i, 0, current_task->page_directory));
i -= 0x1000;
need_flushTLB = ZLOX_TRUE;
}
if(need_flushTLB)
zlox_page_Flush_TLB();
heap->end_address = heap->start_address + new_size;
return new_size;
}
// 从heap的index里搜索出满足size尺寸的最小的hole
static ZLOX_SINT32 zlox_uheap_find_smallest_hole(ZLOX_UINT32 size, ZLOX_UINT8 page_align, ZLOX_HEAP * heap)
{
// Find the smallest hole that will fit.
ZLOX_UINT32 iterator = 0;
while (iterator < heap->index.size)
{
ZLOX_KHP_HEADER *header = (ZLOX_KHP_HEADER *)zlox_lookup_ordered_array(iterator, &heap->index);
// If the user has requested the memory be page-aligned
if (page_align > 0)
{
// Page-align the starting point of this header.
ZLOX_UINT32 location = (ZLOX_UINT32)header;
ZLOX_SINT32 offset = 0;
ZLOX_SINT32 base_hole_size = (ZLOX_SINT32)(sizeof(ZLOX_KHP_HEADER) + sizeof(ZLOX_KHP_FOOTER));
if ( ((location + sizeof(ZLOX_KHP_HEADER)) & 0x00000FFF) != 0)
{
offset = 0x1000 /* page size */ - (location+sizeof(ZLOX_KHP_HEADER)) % 0x1000;
if(offset <= base_hole_size)
offset += 0x1000;
}
ZLOX_SINT32 hole_size = (ZLOX_SINT32)header->size - offset;
// Can we fit now?
// 去除对齐产生的offset偏移值后,如果剩余的hole尺寸大于等于所需的size,
// 且offset因页对齐被剥离出去后可以产生一个新的hole的话,就说明当前找到的hole符合要求
if ( (hole_size >= (ZLOX_SINT32)size) )
break;
}
else if (header->size >= size)
break;
iterator++;
}
// Why did the loop exit?
if (iterator == heap->index.size)
return -1; // We got to the end and didn't find anything.
else
return iterator;
}
// 用于对两个hole或block进行size(尺寸)比较的函数
static ZLOX_SINT8 zlox_uheap_header_less_than(ZLOX_VOID *a, ZLOX_VOID *b)
{
return (((ZLOX_KHP_HEADER *)a)->size < ((ZLOX_KHP_HEADER *)b)->size)?1:0;
}
ZLOX_HEAP * zlox_create_uheap(ZLOX_UINT32 start, ZLOX_UINT32 end_addr,
ZLOX_UINT32 max, ZLOX_UINT8 supervisor, ZLOX_UINT8 readonly)
{
ZLOX_HEAP *heap = (ZLOX_HEAP *)zlox_kmalloc(sizeof(ZLOX_HEAP));
// All our assumptions are made on startAddress and endAddress being page-aligned.
ZLOX_ASSERT_UHEAP(start%0x1000 == 0);
ZLOX_ASSERT_UHEAP(end_addr%0x1000 == 0);
zlox_pages_alloc(start, (end_addr - start));
heap->index = zlox_place_ordered_array((ZLOX_VOID *)start, ZLOX_UHEAP_INDEX_SIZE, &zlox_uheap_header_less_than);
start += sizeof(ZLOX_VPTR) * ZLOX_UHEAP_INDEX_SIZE;
// Make sure the start address is page-aligned.
if ((start & 0x00000FFF) != 0)
{
start &= 0xFFFFF000;
start += 0x1000;
}
// Write the start, end and max addresses into the heap structure.
heap->start_address = start;
heap->end_address = end_addr;
heap->max_address = max;
heap->supervisor = supervisor;
heap->readonly = readonly;
// We start off with one large hole in the index.
// 刚开始创建的堆,除了堆头部的index指针数组外,其余部分是一个大的hole,这样zlox_kmalloc函数就可以从该hole里分配到内存
ZLOX_KHP_HEADER *hole = (ZLOX_KHP_HEADER *)start;
hole->size = end_addr-start;
hole->magic = ZLOX_HEAP_MAGIC;
hole->is_hole = 1;
ZLOX_KHP_FOOTER *hole_footer = (ZLOX_KHP_FOOTER *)((ZLOX_UINT32)hole + hole->size - sizeof(ZLOX_KHP_FOOTER));
hole_footer->magic = ZLOX_HEAP_MAGIC;
hole_footer->header = hole;
zlox_insert_ordered_array((ZLOX_VPTR)hole, &heap->index);
return heap;
}
// 从heap堆里查找能够容纳size尺寸的hole,如果找到该hole,则将该hole变为block,如果hole分离出block后,
// 还有多余的空间,则多余的部分形成新的hole,如果找不到满足size要求的hole,则对heap进行expand(扩展物理内存)
ZLOX_VOID * zlox_uheap_alloc(ZLOX_UINT32 size, ZLOX_UINT8 page_align, ZLOX_HEAP * heap)
{
// Make sure we take the size of header/footer into account.
// 计算new_size时,除了要考虑size所需的内存尺寸外,还必须把头部,底部结构的尺寸也考虑进去
ZLOX_UINT32 new_size = size + sizeof(ZLOX_KHP_HEADER) + sizeof(ZLOX_KHP_FOOTER);
// Find the smallest hole that will fit.
ZLOX_SINT32 iterator = zlox_uheap_find_smallest_hole(new_size, page_align, heap);
if (iterator == -1) // If we didn't find a suitable hole
{
// Save some previous data.
ZLOX_UINT32 old_length = heap->end_address - heap->start_address;
ZLOX_UINT32 old_end_address = heap->end_address;
// We need to allocate some more space.
zlox_uheap_expand(old_length+new_size, heap);
ZLOX_UINT32 new_length = heap->end_address - heap->start_address;
ZLOX_KHP_FOOTER * old_end_footer = (ZLOX_KHP_FOOTER *)(old_end_address - sizeof(ZLOX_KHP_FOOTER));
ZLOX_ASSERT_UHEAP(old_end_footer->magic == ZLOX_HEAP_MAGIC);
// 在zlox_uheap_expand扩容了heap的堆空间后,新增空间的前面如果本身是一个hole的话,就将新增的空间合并到前面的hole里
if(old_end_footer->header->magic == ZLOX_HEAP_MAGIC &&
old_end_footer->header->is_hole == 1)
{
// The last header needs adjusting.
ZLOX_KHP_HEADER *header = old_end_footer->header;
header->size += new_length - old_length;
// Rewrite the footer.
ZLOX_KHP_FOOTER *footer = (ZLOX_KHP_FOOTER *) ( (ZLOX_UINT32)header + header->size - sizeof(ZLOX_KHP_FOOTER) );
footer->header = header;
footer->magic = ZLOX_HEAP_MAGIC;
}
// 如果前面不是一个hole,则将新增的空间变为一个单独的hole
else
{
ZLOX_KHP_HEADER *header = (ZLOX_KHP_HEADER *)old_end_address;
header->magic = ZLOX_HEAP_MAGIC;
header->size = new_length - old_length;
header->is_hole = 1;
ZLOX_KHP_FOOTER *footer = (ZLOX_KHP_FOOTER *) (old_end_address + header->size - sizeof(ZLOX_KHP_FOOTER));
footer->magic = ZLOX_HEAP_MAGIC;
footer->header = header;
zlox_insert_ordered_array((ZLOX_VPTR)header, &heap->index);
}
// We now have enough space. Recurse, and call the function again.
return zlox_uheap_alloc(size, page_align, heap);
}
ZLOX_KHP_HEADER *orig_hole_header = (ZLOX_KHP_HEADER *)zlox_lookup_ordered_array(iterator, &heap->index);
ZLOX_UINT32 orig_hole_pos = (ZLOX_UINT32)orig_hole_header;
ZLOX_UINT32 orig_hole_size = orig_hole_header->size;
// Here we work out if we should split the hole we found into two parts.
// Is the original hole size - requested hole size less than the overhead for adding a new hole?
// 判断找到的hole尺寸除了可以满足基本的new_size需求外,是否可以再分出一个hole,这里只有差值大于头与底部的大小,
// 才能形成一个新的hole,所以这里差值做了小于等于的判断,当小于等于时,说明不能形成一个新的hole,
// 则将hole整个分配出去
if (orig_hole_size-new_size <= sizeof(ZLOX_KHP_HEADER)+sizeof(ZLOX_KHP_FOOTER))
{
// Then just increase the requested size to the size of the hole we found.
// 如果hole满足new_size后,不能再分出一个hole了,则将整个hole都分配出去
size += orig_hole_size-new_size;
new_size = orig_hole_size;
}
// If we need to page-align the data, do it now and make a new hole in front of our block.
// 如果需要对齐,而hole的头部下面的指针没有页对齐,则进行页对齐操作,
// 且将页对齐后剩余出来的offset偏移值作为新hole的大小
if (page_align &&
((orig_hole_pos + sizeof(ZLOX_KHP_HEADER)) & 0x00000FFF)
)
{
ZLOX_SINT32 offset = 0x1000 /* page size */ - (orig_hole_pos + sizeof(ZLOX_KHP_HEADER)) % 0x1000;
if(offset <= (ZLOX_SINT32)(sizeof(ZLOX_KHP_HEADER)+sizeof(ZLOX_KHP_FOOTER)))
offset += 0x1000;
ZLOX_UINT32 new_location = orig_hole_pos + offset;
ZLOX_KHP_HEADER *hole_header = (ZLOX_KHP_HEADER *)orig_hole_pos;
hole_header->size = offset;
hole_header->magic = ZLOX_HEAP_MAGIC;
hole_header->is_hole = 1;
ZLOX_KHP_FOOTER *hole_footer = (ZLOX_KHP_FOOTER *) ( (ZLOX_UINT32)new_location - sizeof(ZLOX_KHP_FOOTER) );
hole_footer->magic = ZLOX_HEAP_MAGIC;
hole_footer->header = hole_header;
orig_hole_pos = new_location;
orig_hole_size = orig_hole_size - hole_header->size;
}
else
{
// Else we don't need this hole any more, delete it from the index.
// hole变为block后,需要将hole从index指针数组里移除
zlox_remove_ordered_array(iterator, &heap->index);
}
// Overwrite the original header...
ZLOX_KHP_HEADER *block_header = (ZLOX_KHP_HEADER *)orig_hole_pos;
block_header->magic = ZLOX_HEAP_MAGIC;
block_header->is_hole = 0;
// 如果找到的hole减去所需的new_size后,剩余尺寸可以容纳一个有效的hole的话,就将剩余的部分变为一个新的hole
if (orig_hole_size - new_size > sizeof(ZLOX_KHP_HEADER)+sizeof(ZLOX_KHP_FOOTER))
{
block_header->size = new_size;
// ...And the footer
ZLOX_KHP_FOOTER *block_footer = (ZLOX_KHP_FOOTER *) (orig_hole_pos + sizeof(ZLOX_KHP_HEADER) + size);
block_footer->magic = ZLOX_HEAP_MAGIC;
block_footer->header = block_header;
ZLOX_KHP_HEADER *hole_header = (ZLOX_KHP_HEADER *) (orig_hole_pos + sizeof(ZLOX_KHP_HEADER) + size + sizeof(ZLOX_KHP_FOOTER));
hole_header->magic = ZLOX_HEAP_MAGIC;
hole_header->is_hole = 1;
hole_header->size = orig_hole_size - new_size;
ZLOX_KHP_FOOTER *hole_footer = (ZLOX_KHP_FOOTER *) ( (ZLOX_UINT32)hole_header + hole_header->size - sizeof(ZLOX_KHP_FOOTER) );
hole_footer->magic = ZLOX_HEAP_MAGIC;
hole_footer->header = hole_header;
// Put the new hole in the index;
zlox_insert_ordered_array((ZLOX_VPTR)hole_header, &heap->index);
}
// 剩余部分不足以形成新的hole,则将找到的hole整个分配出去
else
{
block_header->size = orig_hole_size;
ZLOX_KHP_FOOTER *block_footer = (ZLOX_KHP_FOOTER *)(orig_hole_pos + block_header->size - sizeof(ZLOX_KHP_FOOTER));
block_footer->magic = ZLOX_HEAP_MAGIC;
block_footer->header = block_header;
}
// ...And we're done!
return (ZLOX_VOID *)((ZLOX_UINT32)block_header+sizeof(ZLOX_KHP_HEADER));
}
// 将p指针所在的block(已分配的堆内存)变为hole(未分配的堆内存),同时在变为hole之后,
// 如果该hole的左右相邻位置存在hole的话,则将这些hole合并为一个大的hole,
// 另外,如果free释放生成的hole刚好又位于堆的底部时,则将堆空间进行contract收缩操作
ZLOX_VOID zlox_uheap_free(ZLOX_VOID *p, ZLOX_HEAP *heap)
{
// Exit gracefully for null pointers.
if (p == 0)
return;
// Get the header and footer associated with this pointer.
ZLOX_KHP_HEADER *header = (ZLOX_KHP_HEADER *)((ZLOX_UINT32)p - sizeof(ZLOX_KHP_HEADER));
ZLOX_KHP_FOOTER *footer = (ZLOX_KHP_FOOTER *)((ZLOX_UINT32)header + header->size - sizeof(ZLOX_KHP_FOOTER));
// Sanity checks.
ZLOX_ASSERT_UHEAP(header->magic == ZLOX_HEAP_MAGIC);
ZLOX_ASSERT_UHEAP(footer->magic == ZLOX_HEAP_MAGIC);
// 如果本身就是一个hole,则直接返回
if(header->is_hole == 1)
return;
// Make us a hole.
header->is_hole = 1;
// Do we want to add this header into the 'free holes' index?
ZLOX_CHAR do_add = 1;
// Unify left
// If the thing immediately to the left of us is a footer...
// 合并左侧低地址方向的hole,如果左侧刚好有一个hole,则将当前释放的hole直接合并到左侧的hole里
ZLOX_KHP_FOOTER *test_footer = (ZLOX_KHP_FOOTER *)((ZLOX_UINT32)header - sizeof(ZLOX_KHP_FOOTER));
if((ZLOX_UINT32)test_footer > heap->start_address)
{
if (test_footer->magic == ZLOX_HEAP_MAGIC &&
test_footer->header->is_hole == 1)
{
ZLOX_UINT32 cache_size = header->size; // Cache our current size.
header = test_footer->header; // Rewrite our header with the new one.
footer->header = header; // Rewrite our footer to point to the new header.
header->size += cache_size; // Change the size.
do_add = 0; // Since this header is already in the index, we don't want to add it again.
}
}
// Unify right
// If the thing immediately to the right of us is a header...
// 合并右侧高地址方向的hole,如果右侧刚好有一个hole,则将右侧的hole合并到当前释放的hole里
ZLOX_KHP_HEADER *test_header = (ZLOX_KHP_HEADER*)((ZLOX_UINT32)footer + sizeof(ZLOX_KHP_FOOTER));
if((ZLOX_UINT32)test_header < heap->end_address)
{
if (test_header->magic == ZLOX_HEAP_MAGIC &&
test_header->is_hole == 1)
{
header->size += test_header->size; // Increase our size.
test_footer = (ZLOX_KHP_FOOTER *) ( (ZLOX_UINT32)test_header + // Rewrite it's footer to point to our header.
test_header->size - sizeof(ZLOX_KHP_FOOTER) );
footer = test_footer;
footer->header = header;
// Find and remove this header from the index.
ZLOX_UINT32 iterator = 0;
while ((iterator < heap->index.size) &&
(zlox_lookup_ordered_array(iterator, &heap->index) != (ZLOX_VPTR)test_header))
iterator++;
// Make sure we actually found the item.
ZLOX_ASSERT_UHEAP(iterator < heap->index.size);
// Remove it.
zlox_remove_ordered_array(iterator, &heap->index);
}
}
// If the footer location is the end address, we can contract.
// 如果hole位于堆的底部,则对堆进行contract收缩操作,以回收一部分物理内存资源
if ( (ZLOX_UINT32)footer+sizeof(ZLOX_KHP_FOOTER) == heap->end_address)
{
ZLOX_UINT32 old_length = heap->end_address-heap->start_address;
// 如果header指针被回收掉了的话,header就会是无效的指针,因此,先把header->size存储起来.
ZLOX_UINT32 orig_header_size = header->size;
ZLOX_UINT32 new_length = zlox_uheap_contract( (ZLOX_UINT32)header - heap->start_address, heap);
// Check how big we will be after resizing.
if (orig_header_size - (old_length-new_length) > 0)
{
// We will still exist, so resize us.
header->size -= old_length-new_length;
footer = (ZLOX_KHP_FOOTER *)((ZLOX_UINT32)header + header->size - sizeof(ZLOX_KHP_FOOTER));
footer->magic = ZLOX_HEAP_MAGIC;
footer->header = header;
}
else
{
// We will no longer exist :(. Remove us from the index.
ZLOX_UINT32 iterator = 0;
while ( (iterator < heap->index.size) &&
(zlox_lookup_ordered_array(iterator, &heap->index) != (ZLOX_VPTR)header))
iterator++;
// If we didn't find ourselves, we have nothing to remove.
if (iterator < heap->index.size)
zlox_remove_ordered_array(iterator, &heap->index);
do_add = 0;
}
}
// If required, add us to the index.
// 由于生成了新的hole,所以根据需要将该hole的头部的指针值设置到index对应的数组里
if (do_add == 1)
zlox_insert_ordered_array((ZLOX_VPTR)header, &heap->index);
}