From: uz Date: Fri, 28 Jan 2011 11:54:35 +0000 (+0000) Subject: Simplify things using collections. Some more generic overhaul. X-Git-Tag: V2.13.3~520 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=5d7001dc93f290d1f512d1b57bfae9ba14bec4bd;p=cc65 Simplify things using collections. Some more generic overhaul. git-svn-id: svn://svn.cc65.org/cc65/trunk@4940 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- diff --git a/src/ar65/exports.c b/src/ar65/exports.c index 745ed7bba..84f0a0308 100644 --- a/src/ar65/exports.c +++ b/src/ar65/exports.c @@ -1,15 +1,15 @@ /*****************************************************************************/ /* */ -/* exports.c */ +/* exports.c */ /* */ /* Duplicate export checking for the ar65 archiver */ /* */ /* */ /* */ -/* (C) 1998-2003 Ullrich von Bassewitz */ -/* Römerstraße 52 */ -/* D-70794 Filderstadt */ -/* EMail: uz@cc65.org */ +/* (C) 1998-2011, Ullrich von Bassewitz */ +/* Roemerstrasse 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -47,16 +47,16 @@ /*****************************************************************************/ -/* Data */ +/* Data */ /*****************************************************************************/ /* A hash table entry */ -typedef struct HashEntry HashEntry; +typedef struct HashEntry HashEntry; struct HashEntry { - HashEntry* Next; /* Next in list */ - unsigned Module; /* Module index */ + HashEntry* Next; /* Next in list */ + const ObjData* Module; /* Pointer to object module */ char Name [1]; /* Name of identifier */ }; @@ -67,12 +67,12 @@ static HashEntry* HashTab [HASHTAB_SIZE]; /*****************************************************************************/ -/* Code */ +/* Code */ /*****************************************************************************/ -static HashEntry* NewHashEntry (const char* Name, unsigned Module) +static HashEntry* NewHashEntry (const char* Name, const ObjData* Module) /* Create and return a new hash entry */ { /* 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 */ { HashEntry* L; @@ -114,7 +114,7 @@ void ExpInsert (const char* Name, unsigned Module) /* Duplicate entry */ Warning ("External symbol `%s' in module `%s' is duplicated in " "module `%s'", - Name, GetObjName (L->Module), GetObjName (Module)); + Name, L->Name, Module->Name); } if (L->Next == 0) { break; @@ -127,9 +127,9 @@ void ExpInsert (const char* Name, unsigned Module) -int ExpFind (const char* Name) -/* Check for an identifier in the list. Return -1 if not found, otherwise - * return the number of the module, that exports the identifer. +const ObjData* ExpFind (const char* Name) +/* Check for an identifier in the list. Return NULL if not found, otherwise + * return a pointer to the module, that exports the identifer. */ { /* Get a pointer to the list with the symbols hash value */ @@ -144,7 +144,7 @@ int ExpFind (const char* Name) } /* Not found */ - return -1; + return 0; } diff --git a/src/ar65/exports.h b/src/ar65/exports.h index 4ae8fc1f2..1ac30edf3 100644 --- a/src/ar65/exports.h +++ b/src/ar65/exports.h @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 1998 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (C) 1998-2011, Ullrich von Bassewitz */ +/* Roemerstrasse 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -39,17 +39,27 @@ /*****************************************************************************/ -/* Code */ +/* Forwards */ /*****************************************************************************/ -void ExpInsert (const char* Name, unsigned Module); +struct ObjData; + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +void ExpInsert (const char* Name, const struct ObjData* Module); /* Insert an exported identifier and check if it's already in the list */ -int ExpFind (const char* Name); -/* Check for an identifier in the list. Return -1 if not found, otherwise - * return the number of the module, that exports the identifer. +const struct ObjData* ExpFind (const char* Name); +/* Check for an identifier in the list. Return NULL if not found, otherwise + * return a pointer to the module, that exports the identifer. */ diff --git a/src/ar65/library.c b/src/ar65/library.c index 9f1944109..4b03a4e1d 100644 --- a/src/ar65/library.c +++ b/src/ar65/library.c @@ -38,9 +38,7 @@ #include /* common */ -#include "bitops.h" #include "exprdefs.h" -#include "filepos.h" #include "libdefs.h" #include "print.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 */ { unsigned I; @@ -211,7 +209,7 @@ static void WriteIndexEntry (ObjData* O) static void WriteIndex (void) /* Write the index of a library file */ { - ObjData* O; + unsigned I; /* Sync I/O in case the last operation was a read */ fseek (NewLib, 0, SEEK_CUR); @@ -220,13 +218,11 @@ static void WriteIndex (void) Header.IndexOffs = ftell (NewLib); /* Write the object file count */ - WriteVar (NewLib, ObjCount); + WriteVar (NewLib, CollCount (&ObjPool)); /* Write the object files */ - O = ObjRoot; - while (O) { - WriteIndexEntry (O); - O = O->Next; + for (I = 0; I < CollCount (&ObjPool); ++I) { + WriteIndexEntry (CollConstAt (&ObjPool, I)); } } @@ -438,7 +434,7 @@ static void LibCheckExports (ObjData* O) /* Insert the name into the hash table */ Print (stdout, 1, " %s\n", Name); - ExpInsert (Name, O->Index); + ExpInsert (Name, O); } } @@ -456,17 +452,14 @@ void LibClose (void) unsigned char Buf [4096]; 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 - * export list checking for duplicates. Copy any data that is still - * in the old library into the new one. - */ - for (I = 0; I < ObjCount; ++I) { + * export list checking for duplicates. Copy any data that is still + * in the old library into the new one. + */ + for (I = 0; I < CollCount (&ObjPool); ++I) { /* Get a pointer to the object */ - ObjData* O = ObjPool [I]; + ObjData* O = CollAt (&ObjPool, I); /* Check exports, make global export table */ LibCheckExports (O); diff --git a/src/ar65/list.c b/src/ar65/list.c index c54f65efe..a1132517f 100644 --- a/src/ar65/list.c +++ b/src/ar65/list.c @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 1998 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (C) 1998-2011, Ullrich von Bassewitz */ +/* Roemerstrasse 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -36,10 +36,11 @@ #include #include +/* ar65 */ #include "error.h" -#include "objdata.h" #include "library.h" #include "list.h" +#include "objdata.h" @@ -52,7 +53,8 @@ void ListObjFiles (int argc, char* argv []) /* List modules in a library */ { - ObjData* O; + unsigned I; + const ObjData* O; /* Check the argument count */ if (argc <= 0) { @@ -66,10 +68,14 @@ void ListObjFiles (int argc, char* argv []) LibOpen (argv [0], 1, 0); /* List the modules */ - O = ObjRoot; - while (O) { + for (I = 0; I < CollCount (&ObjPool); ++I) { + + /* Get the entry */ + O = CollConstAt (&ObjPool, I); + + /* Print the name */ printf ("%s\n", O->Name); - O = O->Next; + } /* Create a new library file and close the old one */ diff --git a/src/ar65/objdata.c b/src/ar65/objdata.c index 6d25bb850..5b53513aa 100644 --- a/src/ar65/objdata.c +++ b/src/ar65/objdata.c @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 1998-2003 Ullrich von Bassewitz */ -/* Römerstrasse 52 */ -/* D-70794 Filderstadt */ -/* EMail: uz@cc65.org */ +/* (C) 1998-2011, Ullrich von Bassewitz */ +/* Roemerstrasse 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -51,11 +51,8 @@ -/* Object data list management */ -unsigned ObjCount = 0; /* Count of object files in the list */ -ObjData* ObjRoot = 0; /* List of object files */ -ObjData* ObjLast = 0; /* Last entry in list */ -ObjData** ObjPool = 0; /* Object files as array */ +/* Collection with object files */ +Collection ObjPool = STATIC_COLLECTION_INITIALIZER; @@ -72,9 +69,7 @@ ObjData* NewObjData (void) ObjData* O = xmalloc (sizeof (ObjData)); /* Initialize the data */ - O->Next = 0; O->Name = 0; - O->Index = ~0; O->Flags = 0; O->MTime = 0; O->Start = 0; @@ -86,17 +81,8 @@ ObjData* NewObjData (void) O->ExportSize = 0; O->Exports = 0; - /* Link it into the list */ - if (ObjLast) { - ObjLast->Next = O; - ObjLast = O; - } else { - /* First entry */ - ObjRoot = ObjLast = O; - } - - /* One object file more now */ - ++ObjCount; + /* Add it to the list */ + CollAppend (&ObjPool, O); /* Return the new entry */ return O; @@ -126,13 +112,18 @@ ObjData* FindObjData (const char* Module) * module is not in the list. */ { + unsigned I; + /* Hmm. Maybe we should hash the module names? */ - ObjData* O = ObjRoot; - while (O) { + for (I = 0; I < CollCount (&ObjPool); ++I) { + + /* Get this object file */ + ObjData* O = CollAtUnchecked (&ObjPool, I); + + /* Did we find it? */ if (strcmp (O->Name, Module) == 0) { return O; } - O = O->Next; } return 0; } @@ -142,31 +133,22 @@ ObjData* FindObjData (const char* Module) void DelObjData (const char* Module) /* Delete the object module from the list */ { - ObjData* O = ObjRoot; - ObjData* Last = 0; - while (O) { - 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; + unsigned I; + for (I = 0; I < CollCount (&ObjPool); ++I) { + + /* Get this object file */ + ObjData* O = CollAtUnchecked (&ObjPool, I); + + /* Did we find it? */ + if (strcmp (O->Name, Module) == 0) { /* Free the entry */ + CollDelete (&ObjPool, I); FreeObjData (O); /* Done */ return; - } - Last = O; - O = O->Next; + } } /* 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) /* Get a string from the string pool of an object file */ { if (Index >= O->StringCount) { Error ("Invalid string index (%u) in module `%s'", - Index, GetObjName (O->Index)); + Index, O->Name); } return O->Strings[Index]; } diff --git a/src/ar65/objdata.h b/src/ar65/objdata.h index 731040af4..499cf76ab 100644 --- a/src/ar65/objdata.h +++ b/src/ar65/objdata.h @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 1998-2003 Ullrich von Bassewitz */ -/* Römerstrasse 52 */ -/* D-70794 Filderstadt */ -/* EMail: uz@cc65.org */ +/* (C) 1998-2011, Ullrich von Bassewitz */ +/* Roemerstrasse 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -38,6 +38,11 @@ +/* common */ +#include "coll.h" + + + /*****************************************************************************/ /* Data */ /*****************************************************************************/ @@ -52,9 +57,7 @@ /* Internal structure holding object file data */ typedef struct ObjData ObjData; struct ObjData { - ObjData* Next; /* Linked list of all objects */ char* Name; /* Module name */ - unsigned Index; /* Module index */ unsigned Flags; unsigned long MTime; /* Modifiation time of object file */ unsigned long Start; /* Start offset of data in library */ @@ -69,11 +72,8 @@ struct ObjData { -/* Object data list management */ -extern unsigned ObjCount; /* Count of files in the list */ -extern ObjData* ObjRoot; /* List of object files */ -extern ObjData* ObjLast; /* Last entry in list */ -extern ObjData** ObjPool; /* Object files as array */ +/* Collection with object files */ +extern Collection ObjPool; @@ -97,12 +97,6 @@ ObjData* FindObjData (const char* Module); void DelObjData (const char* Module); /* 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); /* Get a string from the string pool of an object file */