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 licence given in the file "Licence"
6 * distributed in the NASM archive.
16 #define SYNC_MAX 4096 /* max # of sync points */
19 * This lot manages the current set of sync points by means of a
20 * heap (priority queue) structure.
32 * I'd like to allocate an array of size SYNC_MAX, then write
33 * `synx--' which would allow numbering the array from one
34 * instead of zero without wasting memory. Sadly I don't trust
35 * this to work in 16-bit Large model, so it's staying the way
36 * it is. Btw, we don't care about freeing this array, since it
37 * has to last for the duration of the program and will then be
38 * auto-freed on exit. And I'm lazy ;-)
40 * Speaking of 16-bit Large model, that's also the reason I'm
41 * not declaring this array statically - by doing it
42 * dynamically I avoid problems with the total size of DGROUP
45 synx
= malloc((SYNC_MAX
+ 1) * sizeof(*synx
));
47 fprintf(stderr
, "ndisasm: not enough memory for sync array\n");
53 void add_sync(uint32_t pos
, uint32_t length
)
57 if (nsynx
== SYNC_MAX
)
58 return; /* can't do anything - overflow */
61 synx
[nsynx
].pos
= pos
;
62 synx
[nsynx
].length
= length
;
64 for (i
= nsynx
; i
> 1; i
/= 2) {
65 if (synx
[i
/ 2].pos
> synx
[i
].pos
) {
67 t
= synx
[i
/ 2]; /* structure copy */
68 synx
[i
/ 2] = synx
[i
]; /* structure copy again */
69 synx
[i
] = t
; /* another structure copy */
74 uint32_t next_sync(uint32_t position
, uint32_t *length
)
76 while (nsynx
> 0 && synx
[1].pos
+ synx
[1].length
<= position
) {
79 t
= synx
[nsynx
]; /* structure copy */
80 synx
[nsynx
] = synx
[1]; /* structure copy */
81 synx
[1] = t
; /* ditto */
86 while (i
* 2 <= nsynx
) {
88 if (synx
[j
].pos
< synx
[i
].pos
&&
89 (j
+ 1 > nsynx
|| synx
[j
+ 1].pos
> synx
[j
].pos
)) {
90 t
= synx
[j
]; /* structure copy */
91 synx
[j
] = synx
[i
]; /* lots of these... */
92 synx
[i
] = t
; /* ...aren't there? */
94 } else if (j
+ 1 <= nsynx
&& synx
[j
+ 1].pos
< synx
[i
].pos
) {
95 t
= synx
[j
+ 1]; /* structure copy */
96 synx
[j
+ 1] = synx
[i
]; /* structure <yawn> copy */
97 synx
[i
] = t
; /* structure copy <zzzz....> */
106 *length
= synx
[1].length
;