]>
git.saurik.com Git - apple/boot.git/blob - i386/nasm/sync.c
5de2d822dfd4baaff88976d16dda6f09eaba474c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
23 * @APPLE_LICENSE_HEADER_END@
25 /* sync.c the Netwide Disassembler synchronisation processing module
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.
39 #define SYNC_MAX 4096 /* max # of sync points */
42 * This lot manages the current set of sync points by means of a
43 * heap (priority queue) structure.
52 void init_sync(void) {
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 ;-)
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
67 synx
= malloc((SYNC_MAX
+1) * sizeof(*synx
));
69 fprintf(stderr
, "ndisasm: not enough memory for sync array\n");
75 void add_sync(unsigned long pos
, unsigned long length
) {
78 if (nsynx
== SYNC_MAX
)
79 return; /* can't do anything - overflow */
82 synx
[nsynx
].pos
= pos
;
83 synx
[nsynx
].length
= length
;
85 for (i
= nsynx
; i
> 1; i
/= 2) {
86 if (synx
[i
/2].pos
> synx
[i
].pos
) {
88 t
= synx
[i
/2]; /* structure copy */
89 synx
[i
/2] = synx
[i
]; /* structure copy again */
90 synx
[i
] = t
; /* another structure copy */
95 unsigned long next_sync(unsigned long position
, unsigned long *length
) {
96 while (nsynx
> 0 && synx
[1].pos
+ synx
[1].length
<= position
) {
99 t
= synx
[nsynx
]; /* structure copy */
100 synx
[nsynx
] = synx
[1]; /* structure copy */
101 synx
[1] = t
; /* ditto */
106 while (i
*2 <= nsynx
) {
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? */
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....> */
126 *length
= synx
[1].length
;