Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fileioc: improve garbage collect handling #270

Merged
merged 15 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 79 additions & 4 deletions src/fileioc/fileioc.asm
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ library 'FILEIOC', 5
;-------------------------------------------------------------------------------
export ti_ArchiveHasRoom

;-------------------------------------------------------------------------------
; v6 functions
;-------------------------------------------------------------------------------
export ti_SetPostGCHandler
export ti_SetPreGCHandler


;-------------------------------------------------------------------------------
vat_ptr0 := $d0244e
vat_ptr1 := $d0257b
Expand Down Expand Up @@ -461,7 +468,7 @@ ti_SetArchiveStatus:
.set_archived:
push bc
pop af
call z, _Arc_Unarc
call z, util_Arc_Unarc
jr .relocate_var
.set_not_archived:
push bc
Expand Down Expand Up @@ -1206,7 +1213,7 @@ ti_Rename:
inc de
call _Mov8b
call _PushOP1 ; save old name
ld hl, _Arc_Unarc
ld hl, util_Arc_Unarc
ld (.smc_archive), hl
pop hl ; new name
ld de, OP1 + 1
Expand All @@ -1224,7 +1231,7 @@ ti_Rename:
ld hl, $f8 ; $f8 = ret
ld (.smc_archive), hl
call _PushOP1
call _Arc_Unarc
call util_Arc_Unarc
call _PopOP1
jr .locate_program
.in_archive:
Expand Down Expand Up @@ -1257,7 +1264,7 @@ ti_Rename:
ldir
.is_zero:
call _PopOP1
call _Arc_Unarc
call util_Arc_Unarc
.smc_archive := $-3
call _PopOP1
call _ChkFindSym
Expand Down Expand Up @@ -1393,6 +1400,51 @@ ti_ArchiveHasRoom:
dec a
ret

;-------------------------------------------------------------------------------
ti_SetPostGCHandler:
;Set handler for setting up the screen after a garbage collect
; args:
; sp + 3 : pointer to handler. Set to 0 to use default handler (xlibc palette)
beckadamtheinventor marked this conversation as resolved.
Show resolved Hide resolved
; return:
; None
pop de
ex (sp),hl
push de
add hl,de
or a,a
sbc hl,de
jr nz,.notdefault
ld hl,util_post_gc_default_handler
.notdefault:
ld (util_post_gc_handler),hl
ex hl,de
jp (hl)
util_post_gc_default_handler:=$F8
beckadamtheinventor marked this conversation as resolved.
Show resolved Hide resolved

;-------------------------------------------------------------------------------
ti_SetPreGCHandler:
;Set handler for setting up the screen after a garbage collect
; args:
; sp + 3 : pointer to handler. Set to 0 to use default handler (xlibc palette)
beckadamtheinventor marked this conversation as resolved.
Show resolved Hide resolved
; return:
; None
pop de
ex (sp),hl
push de
add hl,de
or a,a
sbc hl,de
jr nz,.notdefault
ld hl,util_pre_gc_default_handler
.notdefault:
ld (util_pre_gc_handler),hl
ex hl,de
jp (hl)
util_pre_gc_default_handler:
xor a,a
ret


;-------------------------------------------------------------------------------
; internal library routines
;-------------------------------------------------------------------------------
Expand Down Expand Up @@ -1573,6 +1625,29 @@ util_set_offset:
ld (hl), bc
ret

util_Arc_Unarc: ;properly handle garbage collects :P
call _ChkFindSym
push hl
call _ChkInRAM
pop hl
jr nz,.arc_unarc ;if the file is already in archive, we won't trigger a gc
call _LoadDEInd_s
ld hl,12
add hl,de
call _FindFreeArcSpot ;check if we will trigger a gc
jr nz,.arc_unarc ;gc will not be triggered
call util_pre_gc_default_handler
util_pre_gc_handler:=$-3
or a,a
ret nz ;exit if the handler returns a non-zero value
call _Arc_Unarc
jp util_post_gc_default_handler
util_post_gc_handler:=$-3
.arc_unarc:
jp _Arc_Unarc



;-------------------------------------------------------------------------------
; Internal library data
;-------------------------------------------------------------------------------
Expand Down
32 changes: 32 additions & 0 deletions src/fileioc/fileioc.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,38 @@ uint8_t ti_RclVar(const uint8_t var_type, const char *var_name, void **data_stru
*/
bool ti_ArchiveHasRoom(uint24_t num_bytes);


/**
* Set routine to run after a garbage collect.
* @param routine Routine to run following a garbage collect. NULL sets it to do nothing.
* @note If your program uses graphx, pass gfx_Begin to setup graphics after the garbage collect finishes.
* */
void ti_SetPostGCHandler(void (*routine)(void));

/**
* Set routine to run before a garbage collect.
* @param routine Routine to run preceeding a garbage collect. NULL sets it to do nothing. If this routine returns true, the variable will not be archived.
beckadamtheinventor marked this conversation as resolved.
Show resolved Hide resolved
* @note Useful for cleanup. If your program uses graphx, run gfx_End inside the passed function to set OS graphics mode to avoid corrupted graphics during the garbage collect.
* @code
* bool pre_gc_handler(void){
* gfx_End();
* return 0;
beckadamtheinventor marked this conversation as resolved.
Show resolved Hide resolved
* }
* int main(void){
* gfx_Begin();
* ti_CloseAll();
* ti_SetPostGCHandler(gfx_Begin);
* ti_SetPreGCHandler(pre_gc_handler);
* //any garbage collect triggered in this program will now be preceeded by pre_gc_handler, and afterwards by gfx_Begin.
* //...
* ti_CloseAll();
* gfx_End();
* }
* @endcode
* */
void ti_SetPreGCHandler(bool (*routine)(void));


/**
* Allocates space for a real variable
* @returns Pointer to variable
Expand Down