1 /*****************************************************************************/
5 /* A span of data within a segment */
9 /* (C) 2011, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
47 /*****************************************************************************/
49 /*****************************************************************************/
53 Span* NewSpan (struct Segment* Seg, unsigned long Offs, unsigned long Size)
54 /* Create and return a new span */
57 Span* S = xmalloc (sizeof (*S));
59 /* Initialize the fields */
64 /* Return the result */
70 Span* ReadSpan (FILE* F, ObjData* O)
71 /* Read a Span from a file and return it */
73 /* Read the section id and translate it to a section pointer */
74 Section* Sec = GetObjSection (O, ReadVar (F));
76 /* Read the offset and relocate it */
77 unsigned long Offs = ReadVar (F) + Sec->Offs;
79 /* Create and return a new Span */
80 return NewSpan (Sec->Seg, Offs, ReadVar (F));
85 void ReadSpans (Collection* Spans, FILE* F, ObjData* O)
86 /* Read a list of Spans from a file and return it */
88 /* First is number of Spans */
89 unsigned Count = ReadVar (F);
91 /* Preallocate enough entries in the collection */
92 CollGrow (Spans, Count);
94 /* Read the spans and add them */
96 CollAppend (Spans, ReadSpan (F, O));
102 void FreeSpan (Span* S)
103 /* Free a span structure */
105 /* Just free the structure */
111 void AddSpan (Collection* Spans, struct Segment* Seg, unsigned long Offs,
113 /* Either add a new span to the ones already in the given collection, or - if
114 * possible - merge it with adjacent ones that already exist.
119 /* We don't have many spans in a collection, so we do a linear search here.
120 * The collection is kept sorted which eases our work here.
122 for (I = 0; I < CollCount (Spans); ++I) {
124 /* Get the next span */
125 Span* S = CollAtUnchecked (Spans, I);
127 /* Must be same segment, otherwise we cannot merge */
132 /* Check if we can merge it */
133 if (Offs < S->Offs) {
135 /* Got the insert position */
136 if (Offs + Size == S->Offs) {
141 /* Insert a new entry */
142 CollInsert (Spans, NewSpan (Seg, Offs, Size), I);
148 } else if (S->Offs + S->Size == Offs) {
150 /* This is the regular case. Merge the two. */
158 /* We must append an entry */
159 CollAppend (Spans, NewSpan (Seg, Offs, Size));