aboutsummaryrefslogtreecommitdiff
path: root/site/udo/twist/checkpointing.udo
blob: 39c45e84d5d5eb0ae5a5910117c6af4035fd84c4 (plain)
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
#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