2 ; Ullrich von Bassewitz, 19.03.2000
4 ; Free a block on the heap.
6 ; void __fastcall__ free (void* block);
9 ; C implementation was:
11 ; void free (void* block)
12 ; /* Release an allocated memory block. The function will accept NULL pointers
13 ; * (and do nothing in this case).
18 ; struct freeblock* f;
21 ; /* Allow NULL arguments */
26 ; /* Get a pointer to the real memory block, then get the size */
27 ; b = (unsigned*) block;
30 ; /* Check if the block is at the top of the heap */
31 ; if (((int) b) + size == (int) _hptr) {
33 ; /* Decrease _hptr to release the block */
34 ; _hptr = (unsigned*) (((int) _hptr) - size);
36 ; /* Check if the last block in the freelist is now at heap top. If so,
37 ; * remove this block from the freelist.
40 ; if (((int) f) + f->size == (int) _hptr) {
41 ; /* Remove the last block */
42 ; _hptr = (unsigned*) (((int) _hptr) - f->size);
43 ; if (_hlast = f->prev) {
44 ; /* Block before is now last block */
47 ; /* The freelist is empty now */
55 ; /* Not at heap top, enter the block into the free list */
62 .importzp ptr1, ptr2, ptr3, ptr4
63 .export _free, heapadd
69 ;-----------------------------------------------------------------------------
73 stx ptr2+1 ; Save block
75 ; Is the argument NULL?
77 ora ptr2+1 ; Is the argument NULL?
80 ; Decrement the given pointer by the admin space amount, so it points to the
81 ; real block allocated. The size of the block is stored in the admin space.
82 ; Remember the block size in ptr1.
89 @L1: ldy #freeblock_size+1
90 lda (ptr2),y ; High byte of size
96 ; Check if the block is on top of the heap
103 bne heapadd ; Add to free list
107 ; The pointer is located at the heap top. Lower the heap top pointer to
115 ; Check if the last block in the freelist is now at heap top. If so, remove
116 ; this block from the freelist.
121 beq @L9 ; Jump if free list empty
123 sta ptr1+1 ; Pointer to last block now in ptr1
126 lda (ptr1),y ; Low byte of block size
129 iny ; High byte of block size
134 bne @L9 ; Jump if last block not on top of heap
136 bne @L9 ; Jump if last block not on top of heap
138 ; Remove the last block
145 ; Correct the next pointer of the now last block
147 ldy #freeblock_prev+1 ; Offset of ->prev field
149 sta ptr2+1 ; Remember f->prev in ptr2
153 sta ptr2 ; Remember f->prev in ptr2
155 ora __heaplast+1 ; -> prev == 0?
156 bne @L8 ; Jump if free list not empty
158 ; Free list is now empty (A = 0)
167 ; Block before is now last block. ptr2 points to f->prev.
170 dey ; Points to high byte of ->next
172 dey ; Low byte of f->prev->next
176 ; The block is not on top of the heap. Add it to the free list. This was
177 ; formerly a separate function called __hadd that was implemented in C as
180 ; void _hadd (void* mem, size_t size)
181 ; /* Add an arbitrary memory block to the heap. This function is used by
182 ; * free(), but it does also allow usage of otherwise unused memory
183 ; * blocks as heap space. The given block is entered in the free list
184 ; * without any checks, so beware!
187 ; struct freeblock* f;
188 ; struct freeblock* left;
189 ; struct freeblock* right;
191 ; if (size >= sizeof (struct freeblock)) {
193 ; /* Set the admin data */
194 ; f = (struct freeblock*) mem;
197 ; /* Check if the freelist is empty */
198 ; if (_hfirst == 0) {
200 ; /* The freelist is empty until now, insert the block */
208 ; /* We have to search the free list. As we are doing so, we check
209 ; * if it is possible to combine this block with another already
210 ; * existing block. Beware: The block may be the "missing link"
211 ; * between *two* other blocks.
215 ; while (right && f > right) {
217 ; right = right->next;
221 ; /* Ok, the current block must be inserted between left and right (but
222 ; * beware: one of the two may be zero!). Also check for the condition
223 ; * that we have to merge two or three blocks.
226 ; /* Check if we must merge the block with the right one */
227 ; if (((unsigned) f) + size == (unsigned) right) {
228 ; /* Merge with the right block */
229 ; f->size += right->size;
230 ; if (f->next = right->next) {
233 ; /* This is now the last block */
237 ; /* No merge, just set the link */
243 ; /* Special case: This is the new freelist end */
247 ; /* Check if we must merge the block with the left one */
248 ; if ((unsigned) f == ((unsigned) left) + left->size) {
249 ; /* Merge with the left block */
250 ; left->size += f->size;
251 ; if (left->next = f->next) {
252 ; left->next->prev = left;
254 ; /* This is now the last block */
258 ; /* No merge, just set the link */
264 ; /* Special case: This is the new freelist start */
272 ; Check if the free list is empty, storing _hfirst into ptr3 for later
282 ; The free list is empty, so this is the first and only block. A contains
283 ; zero if we come here.
285 ldy #freeblock_next-1
286 @L2: iny ; f->next = f->prev = 0;
288 cpy #freeblock_prev+1 ; Done?
294 stx __heapfirst+1 ; _heapfirst = f;
296 stx __heaplast+1 ; _heaplast = f;
300 ; We have to search the free list. As we are doing so, check if it is possible
301 ; to combine this block with another, already existing block. Beware: The
302 ; block may be the "missing link" between two blocks.
303 ; ptr3 contains _hfirst (the start value of the search) when execution reaches
304 ; this point, Y contains size+1. We do also know that _heapfirst (and therefore
305 ; ptr3) is not zero on entry.
310 sta ptr4+1 ; left = 0;
311 ldy #freeblock_next+1
314 @Loop: lda ptr3+1 ; High byte of right
319 @L1: bcs CheckRightMerge
321 @L2: stx ptr4 ; left = right;
325 lda (ptr3),y ; right = right->next;
327 iny ; Points to next+1
334 ; If we come here, the right pointer is zero, so we don't need to check for
335 ; a merge. The new block is the new freelist end.
336 ; A is zero when we come here, Y points to next+1
338 sta (ptr2),y ; Clear high byte of f->next
340 sta (ptr2),y ; Clear low byte of f->next
342 lda ptr2 ; _heaplast = f;
347 ; Since we have checked the case that the freelist is empty before, if the
348 ; right pointer is NULL, the left *cannot* be NULL here. So skip the
349 ; pointer check and jump right to the left block merge
353 ; The given block must be inserted between left and right, and right is not
368 ; Merge with the right block. Do f->size += right->size;
374 iny ; Points to size+1
379 ; Set f->next = right->next and remember f->next in ptr1 (we don't need the
380 ; size stored there any longer)
383 lda (ptr3),y ; Low byte of right->next
384 sta (ptr2),y ; Store to low byte of f->next
386 iny ; Points to next+1
387 lda (ptr3),y ; High byte of right->next
388 sta (ptr2),y ; Store to high byte of f->next
391 beq @L1 ; Jump if f->next zero
396 lda ptr2 ; Low byte of f
397 sta (ptr1),y ; Low byte of f->next->prev
398 iny ; Points to prev+1
399 lda ptr2+1 ; High byte of f
400 sta (ptr1),y ; High byte of f->next->prev
401 jmp CheckLeftMerge ; Done
403 ; f->next is zero, this is now the last block
405 @L1: lda ptr2 ; _heaplast = f;
411 ; No right merge, just set the link.
414 ldy #freeblock_next ; f->next = right;
417 iny ; Points to next+1
422 lda ptr2 ; right->prev = f;
424 iny ; Points to prev+1
428 ; Check if the left pointer is zero
431 lda ptr4 ; left == NULL?
433 bne CheckLeftMerge2 ; Jump if there is a left block
435 ; We don't have a left block, so f is actually the new freelist start
438 sta (ptr2),y ; f->prev = 0;
442 lda ptr2 ; _heapfirst = f;
449 ; Check if the left block is adjacent to the following one
452 ldy #freeblock_size ; Calculate left + left->size
453 lda (ptr4),y ; Low byte of left->size
456 iny ; Points to size+1
457 lda (ptr4),y ; High byte of left->size
463 bne NoLeftMerge ; Jump if blocks not adjacent
465 ; Merge with the left block. Do left->size += f->size;
471 iny ; Points to size+1
476 ; Set left->next = f->next and remember left->next in ptr1.
479 lda (ptr2),y ; Low byte of f->next
482 iny ; Points to next+1
483 lda (ptr2),y ; High byte of f->next
486 ora ptr1 ; left->next == NULL?
489 ; Do left->next->prev = left
492 lda ptr4 ; Low byte of left
495 lda ptr4+1 ; High byte of left
499 ; This is now the last block, do _heaplast = left
507 ; No merge of the left block, just set the link. Y points to size+1 if
508 ; we come here. Do left->next = f.
512 lda ptr2 ; Low byte of left
515 lda ptr2+1 ; High byte of left