MWCC/command_line/CmdLine/Src/Project/CLSegs.c

124 lines
2.8 KiB
C
Raw Normal View History

#include "cmdline.h"
Segment *Segment_New(const char *name, UInt16 attrs) {
Segment *seg = xmalloc(NULL, sizeof(Segment));
strncpy(seg->name, name, sizeof(seg->name));
seg->name[sizeof(seg->name) - 1] = 0;
seg->attrs = attrs;
return seg;
}
void Segment_Free(Segment *seg) {
if (seg)
xfree(seg);
}
Boolean Segments_Initialize(Segments *segs) {
Segment *jump;
Segment *main;
UInt16 idx;
#line 36
OPTION_ASSERT(segs != NULL);
memset(segs, 0, sizeof(Segments));
segs->segsArray = NULL;
jump = Segment_New("Jump Table", 0x28);
Segments_AddSegment(segs, jump, &idx);
#line 44
OPTION_ASSERT(idx==0);
main = Segment_New("Main", 0xFFFF);
Segments_AddSegment(segs, main, &idx);
#line 49
OPTION_ASSERT(idx==1);
return 1;
}
Boolean Segments_Terminate(Segments *segs) {
UInt16 index;
#line 57
OPTION_ASSERT(segs != NULL);
if (segs->segsArray) {
for (index = 0; index < segs->segsCount; index++)
Segment_Free(segs->segsArray[index]);
xfree(segs->segsArray);
}
segs->segsArray = NULL;
return 1;
}
static Boolean Segments_GrowSegments(Segments *segs, UInt16 *index) {
Segment **newArray;
#line 78
OPTION_ASSERT(segs != NULL);
if (segs->segsCount >= segs->arraySize) {
segs->arraySize += 20;
newArray = xrealloc("segments", segs->segsArray, sizeof(Segment *) * segs->arraySize);
segs->segsArray = newArray;
}
*index = segs->segsCount++;
return 1;
}
Boolean Segments_AddSegment(Segments *segs, Segment *seg, UInt16 *index) {
UInt16 ni;
if (Segments_GrowSegments(segs, &ni)) {
segs->segsArray[ni] = seg;
*index = ni;
return 1;
} else {
return 0;
}
}
Boolean Segments_InsertSegment(Segments *segs, UInt16 index, Segment *seg) {
UInt16 ni;
if (Segments_GrowSegments(segs, &ni)) {
if (index > ni)
index = ni - 1;
memmove(&segs->segsArray[index + 1], &segs->segsArray[index], sizeof(Segment *) * (segs->segsCount - index));
segs->segsArray[index] = seg;
return 1;
} else {
return 0;
}
}
Boolean Segments_DeleteSegment(Segments *segs, UInt16 index) {
if (index >= segs->segsCount)
return 0;
Segment_Free(segs->segsArray[index]);
memmove(&segs->segsArray[index], &segs->segsArray[index + 1], sizeof(Segment *) * (segs->segsCount - index));
segs->segsCount--;
return 1;
}
Segment *Segments_GetSegment(Segments *segs, UInt16 segnum) {
#line 137
OPTION_ASSERT(segs != NULL);
if (segnum < segs->segsCount)
return segs->segsArray[segnum];
else
return NULL;
}
UInt16 Segments_Count(const Segments *segs) {
#line 147
OPTION_ASSERT(segs != NULL);
return segs->segsCount;
}