]>
git.saurik.com Git - apple/boot.git/blob - i386/nasm/sync.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
24 /* sync.c the Netwide Disassembler synchronisation processing module
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.
38 #define SYNC_MAX 4096 /* max # of sync points */
41 * This lot manages the current set of sync points by means of a
42 * heap (priority queue) structure.
51 void init_sync(void) {
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 ;-)
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
66 synx
= malloc((SYNC_MAX
+1) * sizeof(*synx
));
68 fprintf(stderr
, "ndisasm: not enough memory for sync array\n");
74 void add_sync(unsigned long pos
, unsigned long length
) {
77 if (nsynx
== SYNC_MAX
)
78 return; /* can't do anything - overflow */
81 synx
[nsynx
].pos
= pos
;
82 synx
[nsynx
].length
= length
;
84 for (i
= nsynx
; i
> 1; i
/= 2) {
85 if (synx
[i
/2].pos
> synx
[i
].pos
) {
87 t
= synx
[i
/2]; /* structure copy */
88 synx
[i
/2] = synx
[i
]; /* structure copy again */
89 synx
[i
] = t
; /* another structure copy */
94 unsigned long next_sync(unsigned long position
, unsigned long *length
) {
95 while (nsynx
> 0 && synx
[1].pos
+ synx
[1].length
<= position
) {
98 t
= synx
[nsynx
]; /* structure copy */
99 synx
[nsynx
] = synx
[1]; /* structure copy */
100 synx
[1] = t
; /* ditto */
105 while (i
*2 <= nsynx
) {
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? */
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....> */
125 *length
= synx
[1].length
;