update
[rrq/rrqforth.git] / temp.asm
1 ;;; Managing a memory area for temporary "objects".
2 ;;;
3 ;;; Object space is allocated cyclically, starting subsequent to prior
4 ;;; allocation and cycling back when requested object size exceeds
5 ;;; the space edge. The "inital" part, which starts as empty, is
6 ;;; considered "held" space that is not reused when cycling back, and
7 ;;; apart from allowing for this, the space management is left open.
8
9
10         WORD p_objectspace,'TEMPSPACE',dovariable
11         ;; Holds size and address of the object space.
12         dq 1073741824 ; [0] Total object space size (request size)
13         dq 0 ; [8] Object space base address.
14
15         WORD p_tempused,'TEMPUSED',dovariable
16         dq 0 ; [16] Currently used.
17
18         WORD p_tempheld,'TEMPHELD',dovariable
19         ;; ( -- a )
20         ;; Marks the barrier for "held space"
21         dq 0 ; [24] Currently held.
22
23         WORD p_temp,'TEMP',fasm
24         ;; ( size -- addr )
25         ;; Allocate an object of given size
26         pushr rsi
27         cmp qword [p_objectspace_DFA+8],0
28         jg p_objecthole_initialized
29         ;; initialize object space
30         push qword [p_objectspace_DFA]
31         DOFORTH p_malloc
32         pop qword [p_objectspace_DFA+8]
33 p_objecthole_initialized:
34         mov rax,qword [p_tempheld_DFA]
35         cmp rax,qword [p_tempused_DFA]
36         jl p_objecthole_recycle
37         mov rax,qword [rsp]
38         add rax,qword [p_tempused_DFA]
39         cmp rax,qword [p_objectspace_DFA]
40         jl p_objecthole_from_tail
41         mov rax,qword [p_tempheld_DFA] ; cycling back to here
42 p_objecthole_recycle:
43         mov qword [p_tempused_DFA],rax
44         add rax,qword [rsp]
45 p_objecthole_from_tail:
46         ;; rax = new usage count
47         mov rbx,qword [p_tempused_DFA]
48         add rbx,qword [p_objectspace_DFA+8]
49         mov qword [rsp],rbx
50         mov qword [p_tempused_DFA],rax
51         popr rsi
52         next
53
54         WORD p_str2temp,'STR>TEMP',fasm
55         ;; ( char* n -- char* n )
56         ;; Capture a given [n:char*] string as a new temp object with
57         ;; leading size cell.
58         pushr rsi
59         mov rax,qword [rsp]
60         add rax,8
61         push rax
62         DOFORTH p_temp          ; ( -- char* n  addr )
63         pop rax
64         pop rbx
65         mov qword[rax],rbx
66         add rax,8
67         pop rcx
68         push rax
69         push rbx
70         push rcx
71         push rax
72         push rbx                ; ( -- addr n char* addr n )
73         DOFORTH p_strncpy
74         popr rsi
75         next