From 9fbf91db06a6d4f4b5cd8bb45389a731bb86bf22 Mon Sep 17 00:00:00 2001 From: Richard Date: Sun, 13 Apr 2025 18:48:02 +0100 Subject: initial --- site/udo/twist/checkpointing.udo | 127 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100755 site/udo/twist/checkpointing.udo (limited to 'site/udo/twist/checkpointing.udo') diff --git a/site/udo/twist/checkpointing.udo b/site/udo/twist/checkpointing.udo new file mode 100755 index 0000000..39c45e8 --- /dev/null +++ b/site/udo/twist/checkpointing.udo @@ -0,0 +1,127 @@ +#ifndef UDO_TWIST_CHECKPOINTING +#define UDO_TWIST_CHECKPOINTING ## + +gitwst_maxundolevels = 32 +gitwst_checkpoints[][] init imaxinstances, gitwst_maxundolevels +gitwst_checkpointstate[] init imaxinstances +gitwst_checkpointencodemult = 10000 + + +opcode twst_checkpoint_encode, i, ii + ifnL, ifnR xin + iencoded = (ifnL * gitwst_checkpointencodemult) + (ifnR / gitwst_checkpointencodemult) + xout iencoded +endop + +opcode twst_checkpoint_decode, ii, i + iencoded xin + ifnL = int(iencoded) / gitwst_checkpointencodemult + ifnR = frac(iencoded) * gitwst_checkpointencodemult + xout ifnL, ifnR +endop + +opcode twst_checkpoint_clear, 0, j + instanceindex xin + instanceindex = (instanceindex == -1) ? gitwst_instanceindex : instanceindex + icheckpointnumber = gitwst_checkpointstate[instanceindex] + while (icheckpointnumber >= 0) do + ifnL, ifnR twst_checkpoint_decode gitwst_checkpoints[instanceindex][icheckpointnumber] + if (ifnL > 0 && ftexists(ifnL) == 1) then + ftfree ifnL, 0 + endif + if (ifnR > 0 && ftexists(ifnR) == 1) then + ftfree ifnR, 0 + endif + icheckpointnumber -= 1 + od + gitwst_checkpointstate[instanceindex] = 0 +endop + +opcode twst_checkpoint, 0, j + instanceindex xin + imaxundo chnget "twst_maxundo" + imaxundo = (imaxundo == -1) ? gitwst_maxundolevels : imaxundo + if (imaxundo == 0 || imaxundo > gitwst_maxundolevels) then + goto complete + endif + + instanceindex = (instanceindex == -1) ? gitwst_instanceindex : instanceindex + icheckpointnumber = gitwst_checkpointstate[instanceindex] + if (icheckpointnumber >= imaxundo) then + ifnL, ifnR twst_checkpoint_decode gitwst_checkpoints[instanceindex][0] + if (ifnL > 0 && ftexists(ifnL) == 1) then + ftfree ifnL, 0 + endif + if (ifnR > 0 && ftexists(ifnR) == 1) then + ftfree ifnR, 0 + endif + index = 1 + itemp[] getrow gitwst_checkpoints, instanceindex + while (index <= imaxundo) do + gitwst_checkpoints[instanceindex][index - 1] = itemp[index] + index += 1 + od + gitwst_checkpointstate[instanceindex] = icheckpointnumber ;- 1 + else + gitwst_checkpointstate[instanceindex] = icheckpointnumber + 1 + endif + + ifnL = gitwst_bufferL[instanceindex] + ifnR = gitwst_bufferR[instanceindex] + ilen = ftlen(ifnL) + + ifnCheckpointL ftgen 0, 0, ilen, 2, 0 + tableicopy ifnCheckpointL, ifnL + if (ifnR > 0) then + ifnCheckpointR ftgen 0, 0, ilen, 2, 0 + tableicopy ifnCheckpointR, ifnR + else + ifnCheckpointR = 0 + endif + + iencoded twst_checkpoint_encode ifnCheckpointL, ifnCheckpointR + gitwst_checkpoints[instanceindex][icheckpointnumber] = iencoded + +complete: +endop + +opcode twst_undo, i, jp + instanceindex, iapplyundo xin + instanceindex = (instanceindex == -1) ? gitwst_instanceindex : instanceindex + + icheckpointnumber = gitwst_checkpointstate[instanceindex] + + icheckpointnumber -= 1 + if (icheckpointnumber < 0) then + istatus = -1 + goto complete + endif + + gitwst_checkpointstate[instanceindex] = icheckpointnumber + ifnL, ifnR twst_checkpoint_decode gitwst_checkpoints[instanceindex][icheckpointnumber] + + if (iapplyundo == 1) then ; apply or just step back and forget + if (ifnL > 0 && ftexists(ifnL) = 1) then + ftfree gitwst_bufferL[instanceindex], 0 + gitwst_bufferL[instanceindex] = ifnL + endif + if (ifnR > 0 && ftexists(ifnR) == 1) then + ftfree gitwst_bufferR[instanceindex], 0 + gitwst_bufferR[instanceindex] = ifnR + endif + else + if (ifnL > 0 && ftexists(ifnL) = 1) then + ftfree ifnL, 0 + endif + if (ifnL > 0 && ftexists(ifnR) = 1) then + ftfree ifnR, 0 + endif + endif + + istatus = 1 + +complete: + xout istatus +endop + +#end -- cgit v1.2.3