Remove limit on number of sync points
[platform/upstream/nasm.git] / sync.c
1 /* sync.c   the Netwide Disassembler synchronisation processing module
2  *
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.
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <limits.h>
12 #include <inttypes.h>
13
14 #include "nasmlib.h"
15 #include "sync.h"
16
17 #define SYNC_MAX 4096           /* max # of sync points (initial) */
18
19 /*
20  * This lot manages the current set of sync points by means of a
21  * heap (priority queue) structure.
22  */
23
24 static struct Sync {
25     uint32_t pos;
26     uint32_t length;
27 } *synx;
28 static int max_synx, nsynx;
29
30 void init_sync(void)
31 {
32     max_synx = SYNC_MAX-1;
33     synx = nasm_malloc(SYNC_MAX * sizeof(*synx));
34     nsynx = 0;
35 }
36
37 void add_sync(uint32_t pos, uint32_t length)
38 {
39     int i;
40
41     if (nsynx >= max_synx) {
42         max_synx = (max_synx << 1)+1;
43         synx = nasm_realloc(synx, (max_synx+1) * sizeof(*synx));
44     }
45
46     nsynx++;
47     synx[nsynx].pos = pos;
48     synx[nsynx].length = length;
49
50     for (i = nsynx; i > 1; i /= 2) {
51         if (synx[i / 2].pos > synx[i].pos) {
52             struct Sync t;
53             t = synx[i / 2];    /* structure copy */
54             synx[i / 2] = synx[i];      /* structure copy again */
55             synx[i] = t;        /* another structure copy */
56         }
57     }
58 }
59
60 uint32_t next_sync(uint32_t position, uint32_t *length)
61 {
62     while (nsynx > 0 && synx[1].pos + synx[1].length <= position) {
63         int i, j;
64         struct Sync t;
65         t = synx[nsynx];        /* structure copy */
66         synx[nsynx] = synx[1];  /* structure copy */
67         synx[1] = t;            /* ditto */
68
69         nsynx--;
70
71         i = 1;
72         while (i * 2 <= nsynx) {
73             j = i * 2;
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? */
79                 i = j;
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....> */
84                 i = j + 1;
85             } else
86                 break;
87         }
88     }
89
90     if (nsynx > 0) {
91         if (length)
92             *length = synx[1].length;
93         return synx[1].pos;
94     } else {
95         if (length)
96             *length = 0L;
97         return UINT32_MAX;
98     }
99 }