sync.c: Style nitfix
[nasm.git] / sync.c
blobd5949a50ce9c98a7a9981e74f0241b1fc3f1cfdf
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * sync.c the Netwide Disassembler synchronisation processing module
38 #include "compiler.h"
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <limits.h>
43 #include <inttypes.h>
45 #include "nasmlib.h"
46 #include "sync.h"
48 #define SYNC_MAX 4096 /* max # of sync points (initial) */
51 * This lot manages the current set of sync points by means of a
52 * heap (priority queue) structure.
55 static struct Sync {
56 uint32_t pos;
57 uint32_t length;
58 } *synx;
60 static int max_synx, nsynx;
62 static inline void swap_sync(uint32_t dst, uint32_t src)
64 struct Sync t = synx[dst];
65 synx[dst] = synx[src];
66 synx[src] = t;
69 void init_sync(void)
71 max_synx = SYNC_MAX - 1;
72 synx = nasm_malloc(SYNC_MAX * sizeof(*synx));
73 nsynx = 0;
76 void add_sync(uint32_t pos, uint32_t length)
78 int i;
80 if (nsynx >= max_synx) {
81 max_synx = (max_synx << 1) + 1;
82 synx = nasm_realloc(synx, (max_synx + 1) * sizeof(*synx));
85 nsynx++;
86 synx[nsynx].pos = pos;
87 synx[nsynx].length = length;
89 for (i = nsynx; i > 1; i /= 2) {
90 if (synx[i / 2].pos > synx[i].pos)
91 swap_sync(i / 2, i);
95 uint32_t next_sync(uint32_t position, uint32_t *length)
97 while (nsynx > 0 && synx[1].pos + synx[1].length <= position) {
98 int i, j;
100 swap_sync(nsynx, 1);
101 nsynx--;
103 i = 1;
104 while (i * 2 <= nsynx) {
105 j = i * 2;
106 if (synx[j].pos < synx[i].pos &&
107 (j + 1 > nsynx || synx[j + 1].pos > synx[j].pos)) {
108 swap_sync(j, i);
109 i = j;
110 } else if (j + 1 <= nsynx && synx[j + 1].pos < synx[i].pos) {
111 swap_sync(j + 1, i);
112 i = j + 1;
113 } else
114 break;
118 if (nsynx > 0) {
119 if (length)
120 *length = synx[1].length;
121 return synx[1].pos;
122 } else {
123 if (length)
124 *length = 0L;
125 return 0;