]>
Commit | Line | Data |
---|---|---|
14c7c974 A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
4f6e3300 A |
6 | * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights |
7 | * Reserved. This file contains Original Code and/or Modifications of | |
8 | * Original Code as defined in and that are subject to the Apple Public | |
9 | * Source License Version 1.1 (the "License"). You may not use this file | |
10 | * except in compliance with the License. Please obtain a copy of the | |
11 | * License at http://www.apple.com/publicsource and read it before using | |
12 | * this file. | |
14c7c974 A |
13 | * |
14 | * The Original Code and all software distributed under the License are | |
4f6e3300 | 15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
14c7c974 A |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
4f6e3300 A |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the |
19 | * License for the specific language governing rights and limitations | |
20 | * under the License. | |
14c7c974 A |
21 | * |
22 | * @APPLE_LICENSE_HEADER_END@ | |
23 | */ | |
24 | /* sync.c the Netwide Disassembler synchronisation processing module | |
25 | * | |
26 | * The Netwide Assembler is copyright (C) 1996 Simon Tatham and | |
27 | * Julian Hall. All rights reserved. The software is | |
28 | * redistributable under the licence given in the file "Licence" | |
29 | * distributed in the NASM archive. | |
30 | */ | |
31 | ||
32 | #include <stdio.h> | |
33 | #include <stdlib.h> | |
34 | #include <limits.h> | |
35 | ||
36 | #include "sync.h" | |
37 | ||
38 | #define SYNC_MAX 4096 /* max # of sync points */ | |
39 | ||
40 | /* | |
41 | * This lot manages the current set of sync points by means of a | |
42 | * heap (priority queue) structure. | |
43 | */ | |
44 | ||
45 | static struct Sync { | |
46 | unsigned long pos; | |
47 | unsigned long length; | |
48 | } *synx; | |
49 | static int nsynx; | |
50 | ||
51 | void init_sync(void) { | |
52 | /* | |
53 | * I'd like to allocate an array of size SYNC_MAX, then write | |
54 | * `synx--' which would allow numbering the array from one | |
55 | * instead of zero without wasting memory. Sadly I don't trust | |
56 | * this to work in 16-bit Large model, so it's staying the way | |
57 | * it is. Btw, we don't care about freeing this array, since it | |
58 | * has to last for the duration of the program and will then be | |
59 | * auto-freed on exit. And I'm lazy ;-) | |
60 | * | |
61 | * Speaking of 16-bit Large model, that's also the reason I'm | |
62 | * not declaring this array statically - by doing it | |
63 | * dynamically I avoid problems with the total size of DGROUP | |
64 | * in Borland C. | |
65 | */ | |
66 | synx = malloc((SYNC_MAX+1) * sizeof(*synx)); | |
67 | if (!synx) { | |
68 | fprintf(stderr, "ndisasm: not enough memory for sync array\n"); | |
69 | exit(1); | |
70 | } | |
71 | nsynx = 0; | |
72 | } | |
73 | ||
74 | void add_sync(unsigned long pos, unsigned long length) { | |
75 | int i; | |
76 | ||
77 | if (nsynx == SYNC_MAX) | |
78 | return; /* can't do anything - overflow */ | |
79 | ||
80 | nsynx++; | |
81 | synx[nsynx].pos = pos; | |
82 | synx[nsynx].length = length; | |
83 | ||
84 | for (i = nsynx; i > 1; i /= 2) { | |
85 | if (synx[i/2].pos > synx[i].pos) { | |
86 | struct Sync t; | |
87 | t = synx[i/2]; /* structure copy */ | |
88 | synx[i/2] = synx[i]; /* structure copy again */ | |
89 | synx[i] = t; /* another structure copy */ | |
90 | } | |
91 | } | |
92 | } | |
93 | ||
94 | unsigned long next_sync(unsigned long position, unsigned long *length) { | |
95 | while (nsynx > 0 && synx[1].pos + synx[1].length <= position) { | |
96 | int i, j; | |
97 | struct Sync t; | |
98 | t = synx[nsynx]; /* structure copy */ | |
99 | synx[nsynx] = synx[1]; /* structure copy */ | |
100 | synx[1] = t; /* ditto */ | |
101 | ||
102 | nsynx--; | |
103 | ||
104 | i = 1; | |
105 | while (i*2 <= nsynx) { | |
106 | j = i*2; | |
107 | if (synx[j].pos < synx[i].pos && | |
108 | (j+1 > nsynx || synx[j+1].pos > synx[j].pos)) { | |
109 | t = synx[j]; /* structure copy */ | |
110 | synx[j] = synx[i]; /* lots of these... */ | |
111 | synx[i] = t; /* ...aren't there? */ | |
112 | i = j; | |
113 | } else if (j+1 <= nsynx && synx[j+1].pos < synx[i].pos) { | |
114 | t = synx[j+1]; /* structure copy */ | |
115 | synx[j+1] = synx[i]; /* structure <yawn> copy */ | |
116 | synx[i] = t; /* structure copy <zzzz....> */ | |
117 | i = j+1; | |
118 | } else | |
119 | break; | |
120 | } | |
121 | } | |
122 | ||
123 | if (nsynx > 0) { | |
124 | if (length) | |
125 | *length = synx[1].length; | |
126 | return synx[1].pos; | |
127 | } else { | |
128 | if (length) | |
129 | *length = 0L; | |
130 | return ULONG_MAX; | |
131 | } | |
132 | } |