Simplify things using collections. Some more generic overhaul.

git-svn-id: svn://svn.cc65.org/cc65/trunk@4940 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
uz 2011-01-28 11:54:35 +00:00
parent a9990fbcf3
commit 5d7001dc93
6 changed files with 100 additions and 155 deletions

View file

@ -6,8 +6,8 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 1998-2003 Ullrich von Bassewitz */ /* (C) 1998-2011, Ullrich von Bassewitz */
/* Römerstraße 52 */ /* Roemerstrasse 52 */
/* D-70794 Filderstadt */ /* D-70794 Filderstadt */
/* EMail: uz@cc65.org */ /* EMail: uz@cc65.org */
/* */ /* */
@ -56,7 +56,7 @@
typedef struct HashEntry HashEntry; typedef struct HashEntry HashEntry;
struct HashEntry { struct HashEntry {
HashEntry* Next; /* Next in list */ HashEntry* Next; /* Next in list */
unsigned Module; /* Module index */ const ObjData* Module; /* Pointer to object module */
char Name [1]; /* Name of identifier */ char Name [1]; /* Name of identifier */
}; };
@ -72,7 +72,7 @@ static HashEntry* HashTab [HASHTAB_SIZE];
static HashEntry* NewHashEntry (const char* Name, unsigned Module) static HashEntry* NewHashEntry (const char* Name, const ObjData* Module)
/* Create and return a new hash entry */ /* Create and return a new hash entry */
{ {
/* Get the length of the name */ /* Get the length of the name */
@ -91,7 +91,7 @@ static HashEntry* NewHashEntry (const char* Name, unsigned Module)
void ExpInsert (const char* Name, unsigned Module) void ExpInsert (const char* Name, const ObjData* Module)
/* Insert an exported identifier and check if it's already in the list */ /* Insert an exported identifier and check if it's already in the list */
{ {
HashEntry* L; HashEntry* L;
@ -114,7 +114,7 @@ void ExpInsert (const char* Name, unsigned Module)
/* Duplicate entry */ /* Duplicate entry */
Warning ("External symbol `%s' in module `%s' is duplicated in " Warning ("External symbol `%s' in module `%s' is duplicated in "
"module `%s'", "module `%s'",
Name, GetObjName (L->Module), GetObjName (Module)); Name, L->Name, Module->Name);
} }
if (L->Next == 0) { if (L->Next == 0) {
break; break;
@ -127,9 +127,9 @@ void ExpInsert (const char* Name, unsigned Module)
int ExpFind (const char* Name) const ObjData* ExpFind (const char* Name)
/* Check for an identifier in the list. Return -1 if not found, otherwise /* Check for an identifier in the list. Return NULL if not found, otherwise
* return the number of the module, that exports the identifer. * return a pointer to the module, that exports the identifer.
*/ */
{ {
/* Get a pointer to the list with the symbols hash value */ /* Get a pointer to the list with the symbols hash value */
@ -144,7 +144,7 @@ int ExpFind (const char* Name)
} }
/* Not found */ /* Not found */
return -1; return 0;
} }

View file

@ -6,10 +6,10 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 1998 Ullrich von Bassewitz */ /* (C) 1998-2011, Ullrich von Bassewitz */
/* Wacholderweg 14 */ /* Roemerstrasse 52 */
/* D-70597 Stuttgart */ /* D-70794 Filderstadt */
/* EMail: uz@musoftware.de */ /* EMail: uz@cc65.org */
/* */ /* */
/* */ /* */
/* This software is provided 'as-is', without any expressed or implied */ /* This software is provided 'as-is', without any expressed or implied */
@ -38,18 +38,28 @@
/*****************************************************************************/
/* Forwards */
/*****************************************************************************/
struct ObjData;
/*****************************************************************************/ /*****************************************************************************/
/* Code */ /* Code */
/*****************************************************************************/ /*****************************************************************************/
void ExpInsert (const char* Name, unsigned Module); void ExpInsert (const char* Name, const struct ObjData* Module);
/* Insert an exported identifier and check if it's already in the list */ /* Insert an exported identifier and check if it's already in the list */
int ExpFind (const char* Name); const struct ObjData* ExpFind (const char* Name);
/* Check for an identifier in the list. Return -1 if not found, otherwise /* Check for an identifier in the list. Return NULL if not found, otherwise
* return the number of the module, that exports the identifer. * return a pointer to the module, that exports the identifer.
*/ */

View file

@ -38,9 +38,7 @@
#include <errno.h> #include <errno.h>
/* common */ /* common */
#include "bitops.h"
#include "exprdefs.h" #include "exprdefs.h"
#include "filepos.h"
#include "libdefs.h" #include "libdefs.h"
#include "print.h" #include "print.h"
#include "symdefs.h" #include "symdefs.h"
@ -179,7 +177,7 @@ static void WriteHeader (void)
static void WriteIndexEntry (ObjData* O) static void WriteIndexEntry (const ObjData* O)
/* Write one index entry */ /* Write one index entry */
{ {
unsigned I; unsigned I;
@ -211,7 +209,7 @@ static void WriteIndexEntry (ObjData* O)
static void WriteIndex (void) static void WriteIndex (void)
/* Write the index of a library file */ /* Write the index of a library file */
{ {
ObjData* O; unsigned I;
/* Sync I/O in case the last operation was a read */ /* Sync I/O in case the last operation was a read */
fseek (NewLib, 0, SEEK_CUR); fseek (NewLib, 0, SEEK_CUR);
@ -220,13 +218,11 @@ static void WriteIndex (void)
Header.IndexOffs = ftell (NewLib); Header.IndexOffs = ftell (NewLib);
/* Write the object file count */ /* Write the object file count */
WriteVar (NewLib, ObjCount); WriteVar (NewLib, CollCount (&ObjPool));
/* Write the object files */ /* Write the object files */
O = ObjRoot; for (I = 0; I < CollCount (&ObjPool); ++I) {
while (O) { WriteIndexEntry (CollConstAt (&ObjPool, I));
WriteIndexEntry (O);
O = O->Next;
} }
} }
@ -438,7 +434,7 @@ static void LibCheckExports (ObjData* O)
/* Insert the name into the hash table */ /* Insert the name into the hash table */
Print (stdout, 1, " %s\n", Name); Print (stdout, 1, " %s\n", Name);
ExpInsert (Name, O->Index); ExpInsert (Name, O);
} }
} }
@ -456,17 +452,14 @@ void LibClose (void)
unsigned char Buf [4096]; unsigned char Buf [4096];
size_t Count; size_t Count;
/* Index the object files and make an array containing the objects */
MakeObjPool ();
/* Walk through the object file list, inserting exports into the /* Walk through the object file list, inserting exports into the
* export list checking for duplicates. Copy any data that is still * export list checking for duplicates. Copy any data that is still
* in the old library into the new one. * in the old library into the new one.
*/ */
for (I = 0; I < ObjCount; ++I) { for (I = 0; I < CollCount (&ObjPool); ++I) {
/* Get a pointer to the object */ /* Get a pointer to the object */
ObjData* O = ObjPool [I]; ObjData* O = CollAt (&ObjPool, I);
/* Check exports, make global export table */ /* Check exports, make global export table */
LibCheckExports (O); LibCheckExports (O);

View file

@ -6,10 +6,10 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 1998 Ullrich von Bassewitz */ /* (C) 1998-2011, Ullrich von Bassewitz */
/* Wacholderweg 14 */ /* Roemerstrasse 52 */
/* D-70597 Stuttgart */ /* D-70794 Filderstadt */
/* EMail: uz@musoftware.de */ /* EMail: uz@cc65.org */
/* */ /* */
/* */ /* */
/* This software is provided 'as-is', without any expressed or implied */ /* This software is provided 'as-is', without any expressed or implied */
@ -36,10 +36,11 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
/* ar65 */
#include "error.h" #include "error.h"
#include "objdata.h"
#include "library.h" #include "library.h"
#include "list.h" #include "list.h"
#include "objdata.h"
@ -52,7 +53,8 @@
void ListObjFiles (int argc, char* argv []) void ListObjFiles (int argc, char* argv [])
/* List modules in a library */ /* List modules in a library */
{ {
ObjData* O; unsigned I;
const ObjData* O;
/* Check the argument count */ /* Check the argument count */
if (argc <= 0) { if (argc <= 0) {
@ -66,10 +68,14 @@ void ListObjFiles (int argc, char* argv [])
LibOpen (argv [0], 1, 0); LibOpen (argv [0], 1, 0);
/* List the modules */ /* List the modules */
O = ObjRoot; for (I = 0; I < CollCount (&ObjPool); ++I) {
while (O) {
/* Get the entry */
O = CollConstAt (&ObjPool, I);
/* Print the name */
printf ("%s\n", O->Name); printf ("%s\n", O->Name);
O = O->Next;
} }
/* Create a new library file and close the old one */ /* Create a new library file and close the old one */

View file

@ -6,8 +6,8 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 1998-2003 Ullrich von Bassewitz */ /* (C) 1998-2011, Ullrich von Bassewitz */
/* merstrasse 52 */ /* Roemerstrasse 52 */
/* D-70794 Filderstadt */ /* D-70794 Filderstadt */
/* EMail: uz@cc65.org */ /* EMail: uz@cc65.org */
/* */ /* */
@ -51,11 +51,8 @@
/* Object data list management */ /* Collection with object files */
unsigned ObjCount = 0; /* Count of object files in the list */ Collection ObjPool = STATIC_COLLECTION_INITIALIZER;
ObjData* ObjRoot = 0; /* List of object files */
ObjData* ObjLast = 0; /* Last entry in list */
ObjData** ObjPool = 0; /* Object files as array */
@ -72,9 +69,7 @@ ObjData* NewObjData (void)
ObjData* O = xmalloc (sizeof (ObjData)); ObjData* O = xmalloc (sizeof (ObjData));
/* Initialize the data */ /* Initialize the data */
O->Next = 0;
O->Name = 0; O->Name = 0;
O->Index = ~0;
O->Flags = 0; O->Flags = 0;
O->MTime = 0; O->MTime = 0;
O->Start = 0; O->Start = 0;
@ -86,17 +81,8 @@ ObjData* NewObjData (void)
O->ExportSize = 0; O->ExportSize = 0;
O->Exports = 0; O->Exports = 0;
/* Link it into the list */ /* Add it to the list */
if (ObjLast) { CollAppend (&ObjPool, O);
ObjLast->Next = O;
ObjLast = O;
} else {
/* First entry */
ObjRoot = ObjLast = O;
}
/* One object file more now */
++ObjCount;
/* Return the new entry */ /* Return the new entry */
return O; return O;
@ -126,13 +112,18 @@ ObjData* FindObjData (const char* Module)
* module is not in the list. * module is not in the list.
*/ */
{ {
unsigned I;
/* Hmm. Maybe we should hash the module names? */ /* Hmm. Maybe we should hash the module names? */
ObjData* O = ObjRoot; for (I = 0; I < CollCount (&ObjPool); ++I) {
while (O) {
/* Get this object file */
ObjData* O = CollAtUnchecked (&ObjPool, I);
/* Did we find it? */
if (strcmp (O->Name, Module) == 0) { if (strcmp (O->Name, Module) == 0) {
return O; return O;
} }
O = O->Next;
} }
return 0; return 0;
} }
@ -142,31 +133,22 @@ ObjData* FindObjData (const char* Module)
void DelObjData (const char* Module) void DelObjData (const char* Module)
/* Delete the object module from the list */ /* Delete the object module from the list */
{ {
ObjData* O = ObjRoot; unsigned I;
ObjData* Last = 0; for (I = 0; I < CollCount (&ObjPool); ++I) {
while (O) {
/* Get this object file */
ObjData* O = CollAtUnchecked (&ObjPool, I);
/* Did we find it? */
if (strcmp (O->Name, Module) == 0) { if (strcmp (O->Name, Module) == 0) {
/* Found the module, remove it from the list */
if (Last == 0) {
/* This was the first entry in the list */
ObjRoot = O->Next;
} else {
Last->Next = O->Next;
}
if (ObjLast == O) {
/* O was the last object in the list */
ObjLast = Last;
}
--ObjCount;
/* Free the entry */ /* Free the entry */
CollDelete (&ObjPool, I);
FreeObjData (O); FreeObjData (O);
/* Done */ /* Done */
return; return;
} }
Last = O;
O = O->Next;
} }
/* Not found! */ /* Not found! */
@ -175,52 +157,12 @@ void DelObjData (const char* Module)
void MakeObjPool (void)
/* Allocate memory, index the entries and make the ObjPool valid */
{
ObjData* O;
unsigned Index;
/* Allocate memory for the pool */
ObjPool = xmalloc (ObjCount * sizeof (ObjData*));
/* Setup the pointers and index the objects */
Index = 0;
O = ObjRoot;
while (O) {
/* Safety */
CHECK (Index < ObjCount);
/* Set the Index */
O->Index = Index;
/* Set the pool pointer */
ObjPool [Index] = O;
/* Next object */
++Index;
O = O->Next;
}
}
const char* GetObjName (unsigned Index)
/* Get the name of a module by index */
{
PRECONDITION (ObjPool != 0 && Index < ObjCount && ObjPool [Index] != 0);
return ObjPool [Index]->Name;
}
const char* GetObjString (const ObjData* O, unsigned Index) const char* GetObjString (const ObjData* O, unsigned Index)
/* Get a string from the string pool of an object file */ /* Get a string from the string pool of an object file */
{ {
if (Index >= O->StringCount) { if (Index >= O->StringCount) {
Error ("Invalid string index (%u) in module `%s'", Error ("Invalid string index (%u) in module `%s'",
Index, GetObjName (O->Index)); Index, O->Name);
} }
return O->Strings[Index]; return O->Strings[Index];
} }

View file

@ -6,8 +6,8 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 1998-2003 Ullrich von Bassewitz */ /* (C) 1998-2011, Ullrich von Bassewitz */
/* merstrasse 52 */ /* Roemerstrasse 52 */
/* D-70794 Filderstadt */ /* D-70794 Filderstadt */
/* EMail: uz@cc65.org */ /* EMail: uz@cc65.org */
/* */ /* */
@ -38,6 +38,11 @@
/* common */
#include "coll.h"
/*****************************************************************************/ /*****************************************************************************/
/* Data */ /* Data */
/*****************************************************************************/ /*****************************************************************************/
@ -52,9 +57,7 @@
/* Internal structure holding object file data */ /* Internal structure holding object file data */
typedef struct ObjData ObjData; typedef struct ObjData ObjData;
struct ObjData { struct ObjData {
ObjData* Next; /* Linked list of all objects */
char* Name; /* Module name */ char* Name; /* Module name */
unsigned Index; /* Module index */
unsigned Flags; unsigned Flags;
unsigned long MTime; /* Modifiation time of object file */ unsigned long MTime; /* Modifiation time of object file */
unsigned long Start; /* Start offset of data in library */ unsigned long Start; /* Start offset of data in library */
@ -69,11 +72,8 @@ struct ObjData {
/* Object data list management */ /* Collection with object files */
extern unsigned ObjCount; /* Count of files in the list */ extern Collection ObjPool;
extern ObjData* ObjRoot; /* List of object files */
extern ObjData* ObjLast; /* Last entry in list */
extern ObjData** ObjPool; /* Object files as array */
@ -97,12 +97,6 @@ ObjData* FindObjData (const char* Module);
void DelObjData (const char* Module); void DelObjData (const char* Module);
/* Delete the object module from the list */ /* Delete the object module from the list */
void MakeObjPool (void);
/* Allocate memory, index the entries and make the ObjPool valid */
const char* GetObjName (unsigned Index);
/* Get the name of a module by index */
const char* GetObjString (const ObjData* O, unsigned Index); const char* GetObjString (const ObjData* O, unsigned Index);
/* Get a string from the string pool of an object file */ /* Get a string from the string pool of an object file */