]> git.sur5r.net Git - cc65/blob - src/ar65/objdata.c
Added assertions
[cc65] / src / ar65 / objdata.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 objdata.c                                 */
4 /*                                                                           */
5 /*              Handling object file data for the ar65 archiver              */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 52                                             */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <string.h>
37
38 /* common */
39 #include "check.h"
40 #include "xmalloc.h"
41
42 /* ar65 */
43 #include "error.h"
44 #include "objdata.h"
45
46
47
48 /*****************************************************************************/
49 /*                                   Data                                    */
50 /*****************************************************************************/
51
52
53
54 /* Object data list management */
55 unsigned        ObjCount = 0;   /* Count of object files in the list */
56 ObjData*        ObjRoot  = 0;   /* List of object files */
57 ObjData*        ObjLast  = 0;   /* Last entry in list */
58 ObjData**       ObjPool  = 0;   /* Object files as array */
59
60
61
62 /*****************************************************************************/
63 /*                                   Code                                    */
64 /*****************************************************************************/
65
66
67
68 ObjData* NewObjData (void)
69 /* Allocate a new structure on the heap, insert it into the list, return it */
70 {
71     /* Allocate memory */
72     ObjData* O = xmalloc (sizeof (ObjData));
73
74     /* Initialize the data */
75     O->Next        = 0;
76     O->Name        = 0;
77     O->Index       = ~0;
78     O->Flags       = 0;
79     O->MTime       = 0;
80     O->Start       = 0;
81     O->Size        = 0;
82     O->StringCount = 0;
83     O->Strings     = 0;
84     O->ImportSize  = 0;
85     O->Imports     = 0;
86     O->ExportSize  = 0;
87     O->Exports     = 0;
88
89     /* Link it into the list */
90     if (ObjLast) {
91         ObjLast->Next = O;
92         ObjLast       = O;
93     } else {
94         /* First entry */
95         ObjRoot = ObjLast = O;
96     }
97
98     /* One object file more now */
99     ++ObjCount;
100
101     /* Return the new entry */
102     return O;
103 }
104
105
106
107 void FreeObjData (ObjData* O)
108 /* Free a complete struct */
109 {
110     unsigned I;
111
112     xfree (O->Name);
113     xfree (O->Imports);
114     xfree (O->Exports);
115     for (I = 0; I < O->StringCount; ++I) {
116         xfree (O->Strings[I]);
117     }
118     xfree (O->Strings);
119     xfree (O);
120 }
121
122
123
124 ObjData* FindObjData (const char* Module)
125 /* Search for the module with the given name and return it. Return NULL if the
126  * module is not in the list.
127  */
128 {
129     /* Hmm. Maybe we should hash the module names? */
130     ObjData* O = ObjRoot;
131     while (O) {
132         if (strcmp (O->Name, Module) == 0) {
133             return O;
134         }
135         O = O->Next;
136     }
137     return 0;
138 }
139
140
141
142 void DelObjData (const char* Module)
143 /* Delete the object module from the list */
144 {
145     ObjData* O = ObjRoot;
146     ObjData* Last = 0;
147     while (O) {
148         if (strcmp (O->Name, Module) == 0) {
149             /* Found the module, remove it from the list */
150             if (Last == 0) {
151                 /* This was the first entry in the list */
152                 ObjRoot = O->Next;
153             } else {
154                 Last->Next = O->Next;
155             }
156             if (ObjLast == O) {
157                 /* O was the last object in the list */
158                 ObjLast = Last;
159             }
160             --ObjCount;
161
162             /* Free the entry */
163             FreeObjData (O);
164
165             /* Done */
166             return;
167         }
168         Last = O;
169         O = O->Next;
170     }
171
172     /* Not found! */
173     Warning ("Module `%s' not found in library", Module);
174 }
175
176
177
178 void MakeObjPool (void)
179 /* Allocate memory, index the entries and make the ObjPool valid */
180 {
181     ObjData* O;
182     unsigned Index;
183
184     /* Allocate memory for the pool */
185     ObjPool = xmalloc (ObjCount * sizeof (ObjData*));
186
187     /* Setup the pointers and index the objects */
188     Index = 0;
189     O = ObjRoot;
190     while (O) {
191
192         /* Safety */
193         CHECK (Index < ObjCount);
194
195         /* Set the Index */
196         O->Index = Index;
197
198         /* Set the pool pointer */
199         ObjPool [Index] = O;
200
201         /* Next object */
202         ++Index;
203         O = O->Next;
204     }
205 }
206
207
208
209 const char* GetObjName (unsigned Index)
210 /* Get the name of a module by index */
211 {
212     PRECONDITION (ObjPool != 0 && Index < ObjCount && ObjPool [Index] != 0);
213     return ObjPool [Index]->Name;
214 }
215
216
217
218 const char* GetObjString (const ObjData* O, unsigned Index)
219 /* Get a string from the string pool of an object file */
220 {
221     if (Index >= O->StringCount) {
222         Error ("Invalid string index (%u) in module `%s'",
223                Index, GetObjName (O->Index));
224     }
225     return O->Strings[Index];
226 }
227
228
229