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.
17 #define SYNC_MAX 4096 /* max # of sync points (initial) */
20 * This lot manages the current set of sync points by means of a
21 * heap (priority queue) structure.
28 static int max_synx, nsynx;
32 max_synx = SYNC_MAX-1;
33 synx = nasm_malloc(SYNC_MAX * sizeof(*synx));
37 void add_sync(uint32_t pos, uint32_t length)
41 if (nsynx >= max_synx) {
42 max_synx = (max_synx << 1)+1;
43 synx = nasm_realloc(synx, (max_synx+1) * sizeof(*synx));
47 synx[nsynx].pos = pos;
48 synx[nsynx].length = length;
50 for (i = nsynx; i > 1; i /= 2) {
51 if (synx[i / 2].pos > synx[i].pos) {
53 t = synx[i / 2]; /* structure copy */
54 synx[i / 2] = synx[i]; /* structure copy again */
55 synx[i] = t; /* another structure copy */
60 uint32_t next_sync(uint32_t position, uint32_t *length)
62 while (nsynx > 0 && synx[1].pos + synx[1].length <= position) {
65 t = synx[nsynx]; /* structure copy */
66 synx[nsynx] = synx[1]; /* structure copy */
67 synx[1] = t; /* ditto */
72 while (i * 2 <= nsynx) {
74 if (synx[j].pos < synx[i].pos &&
75 (j + 1 > nsynx || synx[j + 1].pos > synx[j].pos)) {
76 t = synx[j]; /* structure copy */
77 synx[j] = synx[i]; /* lots of these... */
78 synx[i] = t; /* ...aren't there? */
80 } else if (j + 1 <= nsynx && synx[j + 1].pos < synx[i].pos) {
81 t = synx[j + 1]; /* structure copy */
82 synx[j + 1] = synx[i]; /* structure <yawn> copy */
83 synx[i] = t; /* structure copy <zzzz....> */
92 *length = synx[1].length;