Skip to content

Commit f7145e9

Browse files
add ti_ArchiveHasRoomVar
Signed-off-by: mateoconlechuga <matthewwaltzis@gmail.com>
1 parent 77b3e95 commit f7145e9

File tree

2 files changed

+81
-39
lines changed

2 files changed

+81
-39
lines changed

src/fileioc/fileioc.asm

Lines changed: 72 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ library FILEIOC, 7
6262
; v6 functions
6363
;-------------------------------------------------------------------------------
6464
export ti_SetGCBehavior
65+
;-------------------------------------------------------------------------------
66+
; v7 functions
67+
;-------------------------------------------------------------------------------
68+
export ti_ArchiveHasRoomVar
6569

6670
;-------------------------------------------------------------------------------
6771
; v7 functions
@@ -355,11 +359,11 @@ ti_Open:
355359
ld a,(hl)
356360
cp a,'+'
357361
jr nz,.no_append
358-
.archive_var:
359-
call ti.PushOP1
362+
.unarchive_var:
360363
call ti.ChkFindSym
364+
jr c, .not_found
361365
call ti.ChkInRam
362-
jr z, .in_ram
366+
jr z, .save_ptrs
363367
or a, a
364368
sbc hl, hl
365369
ex de, hl
@@ -368,16 +372,9 @@ ti_Open:
368372
ld d, (hl)
369373
ex de, hl
370374
call ti.EnoughMem
371-
push af
372-
call ti.PopOP1
373-
pop af
374375
jp c, util_ret_null_pop_ix
375-
call ti.PushOP1
376-
call ti.Arc_Unarc
377-
call ti.PopOP1
378-
jr .archive_var
379-
.in_ram:
380-
call ti.PopOP1
376+
call util_unarchive
377+
jr .unarchive_var
381378
.no_append:
382379
call ti.ChkFindSym
383380
jr c, .not_found
@@ -457,26 +454,15 @@ ti_SetArchiveStatus:
457454
djnz .copy_name
458455
xor a, a
459456
ld (de), a
460-
call ti.PushOP1
461-
ld iy, ti.flags
462-
call ti.ChkFindSym
463-
call ti.ChkInRam
464-
push af
465-
pop bc
466457
pop af
467458
or a, a
468-
jr z, .set_not_archived
459+
jr z, .set_unarchived
469460
.set_archived:
470-
push bc
471-
pop af
472-
call z, util_Arc_Unarc
461+
call util_archive
473462
jr .relocate_var
474-
.set_not_archived:
475-
push bc
476-
pop af
477-
call nz, ti.Arc_Unarc
463+
.set_unarchived:
464+
call util_unarchive
478465
.relocate_var:
479-
call ti.PopOP1
480466
call ti.ChkFindSym
481467
jp c, util_ret_neg_one
482468
call ti.ChkInRam
@@ -1207,7 +1193,7 @@ ti_Rename:
12071193
inc de
12081194
call ti.Mov8b
12091195
call ti.PushOP1 ; save old name
1210-
ld hl, util_Arc_Unarc
1196+
ld hl, util_archive
12111197
ld (.smc_archive), hl
12121198
pop hl ; new name
12131199
ld de, ti.OP1 + 1
@@ -1222,10 +1208,10 @@ ti_Rename:
12221208
jr c, .return_2
12231209
call ti.ChkInRam
12241210
jr nz, .in_archive
1225-
ld hl, util_no_op ; no-op routine instead of assuming $F8 points to a ret instruction lol
1211+
ld hl, util_no_op ; no-op routine
12261212
ld (.smc_archive), hl
12271213
call ti.PushOP1
1228-
call util_Arc_Unarc
1214+
call util_archive
12291215
call ti.PopOP1
12301216
jr .locate_program
12311217
.in_archive:
@@ -1258,7 +1244,7 @@ ti_Rename:
12581244
ldir
12591245
.is_zero:
12601246
call ti.PopOP1
1261-
call util_Arc_Unarc
1247+
call 0
12621248
.smc_archive := $-3
12631249
call ti.PopOP1
12641250
call ti.ChkFindSym
@@ -1396,6 +1382,44 @@ ti_ArchiveHasRoom:
13961382
xor a,a
13971383
ret
13981384

1385+
;-------------------------------------------------------------------------------
1386+
ti_ArchiveHasRoomVar:
1387+
; checks if there is room in the archive without triggering a garbage collect.
1388+
; args:
1389+
; sp + 3 : handle to variable
1390+
; return:
1391+
; true if there is room, false if not
1392+
pop de
1393+
pop bc
1394+
push bc
1395+
push de
1396+
call util_is_slot_open
1397+
jr nz,.fail
1398+
call util_get_vat_ptr
1399+
ld hl,(hl)
1400+
ld bc,-6
1401+
add hl,bc
1402+
ld c,(hl) ; get var size
1403+
call util_get_data_ptr
1404+
ld hl,(hl)
1405+
.entry_sym:
1406+
ld a,c
1407+
ex de,hl
1408+
ld hl,(hl)
1409+
ld bc,12
1410+
add hl,bc
1411+
ld c,a
1412+
add.s hl,bc
1413+
jr c,.fail
1414+
ld c,l
1415+
ld b,h
1416+
call ti.FindFreeArcSpot
1417+
ld a,1
1418+
ret nz
1419+
.fail:
1420+
xor a,a
1421+
ret
1422+
13991423
;-------------------------------------------------------------------------------
14001424
ti_SetGCBehavior:
14011425
;Set routines to run before and after a garbage collect would be triggered.
@@ -1621,19 +1645,28 @@ util_set_offset:
16211645
ld (hl), bc
16221646
ret
16231647

1624-
util_Arc_Unarc: ; properly handle garbage collects
1648+
util_archive: ; properly handle garbage collects
1649+
ld iy, ti.flags
1650+
call ti.ChkFindSym
16251651
call ti.ChkInRam
1626-
jp nz,ti.Arc_Unarc ; if the file is already in archive, we won't trigger a gc
1627-
ex hl,de
1628-
call ti.LoadDEInd_s
1629-
ex hl,de
1630-
call ti_ArchiveHasRoom.entry
1631-
jp nz,ti.Arc_Unarc ; gc will not be triggered
1652+
ret nz
1653+
call ti_ArchiveHasRoomVar.entry_sym
1654+
jp nz,ti.Arc_Unarc
1655+
call ti.PushOP1
16321656
call util_pre_gc_default_handler
16331657
util_pre_gc_handler := $-3
16341658
call ti.Arc_Unarc
1635-
jp util_post_gc_default_handler
1659+
call util_post_gc_default_handler
16361660
util_post_gc_handler := $-3
1661+
jp ti.PopOP1
1662+
1663+
util_unarchive:
1664+
ld iy, ti.flags
1665+
call ti.PushOP1
1666+
call ti.ChkFindSym
1667+
call ti.ChkInRam
1668+
call nz,ti.Arc_Unarc
1669+
jp ti.PopOP1
16371670

16381671
;-------------------------------------------------------------------------------
16391672
; Internal library data

src/fileioc/fileioc.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,15 @@ uint8_t ti_RclVar(uint8_t type, const char *name, void **data);
515515
*/
516516
bool ti_ArchiveHasRoom(uint24_t num_bytes);
517517

518+
/**
519+
* Checks to see if there is room in the archive for storing an AppVar/variable,
520+
* without needing to execute a Garbage Collect.
521+
*
522+
* @param[in] handle AppVar/variable handle.
523+
* @returns `true` if archiving the variable will trigger a Garbage Collect.
524+
*/
525+
bool ti_ArchiveHasRoomVar(uint8_t handle);
526+
518527
/**
519528
* Set routines to run before and after a garbage collect would be triggered.
520529
* A garbage collect is used to free up space in archive memory by reorganizing

0 commit comments

Comments
 (0)