NASM 0.95
[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
13 #include "sync.h"
14
15 #define SYNC_MAX 4096                  /* max # of sync points */
16
17 /*
18  * This lot manages the current set of sync points by means of a
19  * heap (priority queue) structure.
20  */
21
22 static struct Sync {
23     unsigned long pos;
24     unsigned long length;
25 } *synx;
26 static int nsynx;
27
28 void init_sync(void) {
29     /*
30      * I'd like to allocate an array of size SYNC_MAX, then write
31      * `synx--' which would allow numbering the array from one
32      * instead of zero without wasting memory. Sadly I don't trust
33      * this to work in 16-bit Large model, so it's staying the way
34      * it is. Btw, we don't care about freeing this array, since it
35      * has to last for the duration of the program and will then be
36      * auto-freed on exit. And I'm lazy ;-)
37      * 
38      * Speaking of 16-bit Large model, that's also the reason I'm
39      * not declaring this array statically - by doing it
40      * dynamically I avoid problems with the total size of DGROUP
41      * in Borland C.
42      */
43     synx = malloc((SYNC_MAX+1) * sizeof(*synx));
44     if (!synx) {
45         fprintf(stderr, "ndisasm: not enough memory for sync array\n");
46         exit(1);
47     }
48     nsynx = 0;
49 }
50
51 void add_sync(unsigned long pos, unsigned long length) {
52     int i;
53
54     if (nsynx == SYNC_MAX)
55         return;                        /* can't do anything - overflow */
56
57     nsynx++;
58     synx[nsynx].pos = pos;
59     synx[nsynx].length = length;
60
61     for (i = nsynx; i > 1; i /= 2) {
62         if (synx[i/2].pos > synx[i].pos) {
63             struct Sync t;
64             t = synx[i/2];             /* structure copy */
65             synx[i/2] = synx[i];       /* structure copy again */
66             synx[i] = t;               /* another structure copy */
67         }
68     }
69 }
70
71 unsigned long next_sync(unsigned long position, unsigned long *length) {
72     while (nsynx > 0 && synx[1].pos + synx[1].length <= position) {
73         int i, j;
74         struct Sync t;
75         t = synx[nsynx];               /* structure copy */
76         synx[nsynx] = synx[1];         /* structure copy */
77         synx[1] = t;                   /* ditto */
78
79         nsynx--;
80
81         i = 1;
82         while (i*2 <= nsynx) {
83             j = i*2;
84             if (synx[j].pos < synx[i].pos &&
85                 (j+1 > nsynx || synx[j+1].pos > synx[j].pos)) {
86                 t = synx[j];           /* structure copy */
87                 synx[j] = synx[i];     /* lots of these... */
88                 synx[i] = t;           /* ...aren't there? */
89                 i = j;
90             } else if (j+1 <= nsynx && synx[j+1].pos < synx[i].pos) {
91                 t = synx[j+1];         /* structure copy */
92                 synx[j+1] = synx[i];   /* structure <yawn> copy */
93                 synx[i] = t;           /* structure copy <zzzz....> */
94                 i = j+1;
95             } else
96                 break;
97         }
98     }
99
100     if (nsynx > 0) {
101         if (length)
102             *length = synx[1].length;
103         return synx[1].pos;
104     } else {
105         if (length)
106             *length = 0L;
107         return ULONG_MAX;
108     }
109 }