]> git.sur5r.net Git - cc65/blob - src/da65/segment.c
Normalized code.
[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       53
56
57 /* Segment definition */
58 typedef struct Segment Segment;
59 struct Segment {
60     Segment*            NextStart;      /* Pointer to next segment */
61     Segment*            NextEnd;        /* Pointer to next segment */
62     unsigned long       Start;
63     unsigned long       End;
64     unsigned            AddrSize;
65     char                Name[1];        /* Name, dynamically allocated */
66 };
67
68 /* Tables containing the segments. A segment is inserted using it's hash
69  * value. Collision is done by single linked lists.
70  */
71 static Segment* StartTab[HASH_SIZE];    /* Table containing segment starts */
72 static Segment* EndTab[HASH_SIZE];      /* Table containing segment ends */
73
74
75
76 /*****************************************************************************/
77 /*                                   Code                                    */
78 /*****************************************************************************/
79
80
81
82 void AddAbsSegment (unsigned Start, unsigned End, const char* Name)
83 /* Add an absolute segment to the segment table */
84 {
85     /* Get the length of the name */
86     unsigned Len = strlen (Name);
87
88     /* Create a new segment */
89     Segment* S = xmalloc (sizeof (Segment) + Len);
90
91     /* Fill in the data */
92     S->Start    = Start;
93     S->End      = End;
94     S->AddrSize = ADDR_SIZE_ABS;
95     memcpy (S->Name, Name, Len + 1);
96
97     /* Insert the segment into the hash tables */
98     S->NextStart = StartTab[Start % HASH_SIZE];
99     StartTab[Start % HASH_SIZE] = S;
100     S->NextEnd = EndTab[End % HASH_SIZE];
101     EndTab[End % HASH_SIZE] = S;
102
103     /* Mark start and end of the segment */
104     MarkAddr (Start, atSegmentChange);
105     MarkAddr (End, atSegmentChange);
106
107     /* Mark the addresses within the segment */
108     MarkRange (Start, End, atSegment);
109 }