]> git.sur5r.net Git - cc65/blob - src/da65/segment.c
Started to add support for segments.
[cc65] / src / da65 / segment.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 segment.c                                 */
4 /*                                                                           */
5 /*                         Segment handling for da65                         */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2007      Ullrich von Bassewitz                                       */
10 /*               Roemerstrasse 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 "addrsize.h"
40 #include "xmalloc.h"
41
42 /* da65 */
43 #include "attrtab.h"
44 #include "segment.h"
45
46
47
48 /*****************************************************************************/
49 /*                                   Data                                    */
50 /*****************************************************************************/
51
52
53
54 /* Hash definitions */
55 #define HASH_SIZE       64              /* Must be power of two */
56 #define HASH_MASK       (HASH_SIZE-1)
57
58 /* Segment definition */
59 typedef struct Segment Segment;
60 struct Segment {
61     Segment*            NextStart;      /* Pointer to next segment */
62     Segment*            NextEnd;        /* Pointer to next segment */
63     unsigned long       Start;
64     unsigned long       End;
65     unsigned            AddrSize;
66     char                Name[1];        /* Name, dynamically allocated */
67 };
68
69 /* Tables containing the segments. A segment is inserted using it's hash
70  * value. Collision is done by single linked lists.
71  */
72 static Segment* StartTab[HASH_SIZE];    /* Table containing segment starts */
73 static Segment* EndTab[HASH_SIZE];      /* Table containing segment ends */
74
75
76
77 /*****************************************************************************/
78 /*                                   Code                                    */
79 /*****************************************************************************/
80
81
82
83 void AddAbsSegment (unsigned Start, unsigned End, const char* Name)
84 /* Add an absolute segment to the segment table */
85 {
86     /* Get the length of the name */
87     unsigned Len = strlen (Name);
88
89     /* Create a new segment */
90     Segment* S = xmalloc (sizeof (Segment) + Len);
91
92     /* Fill in the data */
93     S->Start    = Start;
94     S->End      = End;
95     S->AddrSize = ADDR_SIZE_ABS;
96     memcpy (S->Name, Name, Len + 1);
97
98     /* Insert the segment into the hash tables */
99     S->NextStart = StartTab[Start & HASH_MASK];
100     StartTab[Start & HASH_MASK] = S;
101     S->NextEnd = EndTab[End & HASH_MASK];
102     EndTab[End & HASH_MASK] = S;
103
104     /* Mark the addresses within the segment */
105     MarkRange (Start, End, atSegment);
106 }
107
108
109