1 /* sync.c the Netwide Disassembler synchronisation processing module
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the license given in the file "LICENSE"
6 * distributed in the NASM archive.
19 #define SYNC_MAX 4096 /* max # of sync points (initial) */
22 * This lot manages the current set of sync points by means of a
23 * heap (priority queue) structure.
30 static int max_synx
, nsynx
;
34 max_synx
= SYNC_MAX
-1;
35 synx
= nasm_malloc(SYNC_MAX
* sizeof(*synx
));
39 void add_sync(uint32_t pos
, uint32_t length
)
43 if (nsynx
>= max_synx
) {
44 max_synx
= (max_synx
<< 1)+1;
45 synx
= nasm_realloc(synx
, (max_synx
+1) * sizeof(*synx
));
49 synx
[nsynx
].pos
= pos
;
50 synx
[nsynx
].length
= length
;
52 for (i
= nsynx
; i
> 1; i
/= 2) {
53 if (synx
[i
/ 2].pos
> synx
[i
].pos
) {
55 t
= synx
[i
/ 2]; /* structure copy */
56 synx
[i
/ 2] = synx
[i
]; /* structure copy again */
57 synx
[i
] = t
; /* another structure copy */
62 uint32_t next_sync(uint32_t position
, uint32_t *length
)
64 while (nsynx
> 0 && synx
[1].pos
+ synx
[1].length
<= position
) {
67 t
= synx
[nsynx
]; /* structure copy */
68 synx
[nsynx
] = synx
[1]; /* structure copy */
69 synx
[1] = t
; /* ditto */
74 while (i
* 2 <= nsynx
) {
76 if (synx
[j
].pos
< synx
[i
].pos
&&
77 (j
+ 1 > nsynx
|| synx
[j
+ 1].pos
> synx
[j
].pos
)) {
78 t
= synx
[j
]; /* structure copy */
79 synx
[j
] = synx
[i
]; /* lots of these... */
80 synx
[i
] = t
; /* ...aren't there? */
82 } else if (j
+ 1 <= nsynx
&& synx
[j
+ 1].pos
< synx
[i
].pos
) {
83 t
= synx
[j
+ 1]; /* structure copy */
84 synx
[j
+ 1] = synx
[i
]; /* structure <yawn> copy */
85 synx
[i
] = t
; /* structure copy <zzzz....> */
94 *length
= synx
[1].length
;