]> git.sur5r.net Git - cc65/blobdiff - src/da65/segment.c
Merge pull request #503 from jedeoric/master
[cc65] / src / da65 / segment.c
index e648a36184d5ae7dfc0abb6353d5d05acc0df5a7..12d4cf65699dc428f408d11838d6b6540b2c58e7 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2007      Ullrich von Bassewitz                                       */
-/*               Roemerstrasse 52                                            */
-/*               D-70794 Filderstadt                                         */
-/* EMail:        uz@cc65.org                                                 */
+/* (C) 2007-2014, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
 
 
 /* Hash definitions */
-#define HASH_SIZE       64              /* Must be power of two */
-#define HASH_MASK       (HASH_SIZE-1)
+#define HASH_SIZE       53
 
 /* Segment definition */
 typedef struct Segment Segment;
 struct Segment {
     Segment*            NextStart;      /* Pointer to next segment */
-    Segment*            NextEnd;        /* Pointer to next segment */
     unsigned long       Start;
-    unsigned long       End;
     unsigned            AddrSize;
     char                Name[1];        /* Name, dynamically allocated */
 };
 
-/* Tables containing the segments. A segment is inserted using it's hash
- * value. Collision is done by single linked lists.
- */
+/* Table containing the segments. A segment is inserted using its hash
+** value. Collisions are handled by single-linked lists.
+*/
 static Segment* StartTab[HASH_SIZE];    /* Table containing segment starts */
-static Segment* EndTab[HASH_SIZE];      /* Table containing segment ends */
 
 
 
 /*****************************************************************************/
-/*                                          Code                                    */
+/*                                   Code                                    */
 /*****************************************************************************/
 
 
@@ -91,15 +87,16 @@ void AddAbsSegment (unsigned Start, unsigned End, const char* Name)
 
     /* Fill in the data */
     S->Start    = Start;
-    S->End      = End;
     S->AddrSize = ADDR_SIZE_ABS;
     memcpy (S->Name, Name, Len + 1);
 
-    /* Insert the segment into the hash tables */
-    S->NextStart = StartTab[Start & HASH_MASK];
-    StartTab[Start & HASH_MASK] = S;
-    S->NextEnd = EndTab[End & HASH_MASK];
-    EndTab[End & HASH_MASK] = S;
+    /* Insert the segment into the hash table */
+    S->NextStart = StartTab[Start % HASH_SIZE];
+    StartTab[Start % HASH_SIZE] = S;
+
+    /* Mark start and end of the segment */
+    MarkAddr (Start, atSegmentStart);
+    MarkAddr (End, atSegmentEnd);
 
     /* Mark the addresses within the segment */
     MarkRange (Start, End, atSegment);
@@ -107,3 +104,36 @@ void AddAbsSegment (unsigned Start, unsigned End, const char* Name)
 
 
 
+char* GetSegmentStartName (unsigned Addr)
+/* Return the name of the segment which starts at the given address */
+{
+    Segment* S = StartTab[Addr % HASH_SIZE];
+
+    /* Search the collision list for the exact address */
+    while (S != 0) {
+        if (S->Start == Addr) {
+            return S->Name;
+        }
+        S = S->NextStart;
+    }
+
+    return 0;
+}
+
+
+
+unsigned GetSegmentAddrSize (unsigned Addr)
+/* Return the address size of the segment which starts at the given address */
+{
+    Segment* S = StartTab[Addr % HASH_SIZE];
+
+    /* Search the collision list for the exact address */
+    while (S != 0) {
+        if (S->Start == Addr) {
+            return S->AddrSize;
+        }
+        S = S->NextStart;
+    }
+
+    return 0;
+}