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