summaryrefslogtreecommitdiff
path: root/command_line/CmdLine/Src/Project/CLSegs.c
diff options
context:
space:
mode:
authorAsh Wolf <ninji@wuffs.org>2022-10-19 21:16:13 +0100
committerAsh Wolf <ninji@wuffs.org>2022-10-19 21:16:13 +0100
commitd1f153d34b023d81768f6087f67dbfff714bafc9 (patch)
treea694d470a60655d0cda15a70791fbdb90a2398cf /command_line/CmdLine/Src/Project/CLSegs.c
parent775b6861666af36d317fb577cf489e2c6377f878 (diff)
downloadMWCC-d1f153d34b023d81768f6087f67dbfff714bafc9.tar.gz
MWCC-d1f153d34b023d81768f6087f67dbfff714bafc9.zip
let's commit all this before my VM blows up and nukes my work
Diffstat (limited to '')
-rw-r--r--command_line/CmdLine/Src/Project/CLSegs.c123
1 files changed, 123 insertions, 0 deletions
diff --git a/command_line/CmdLine/Src/Project/CLSegs.c b/command_line/CmdLine/Src/Project/CLSegs.c
index e69de29..a0e006c 100644
--- a/command_line/CmdLine/Src/Project/CLSegs.c
+++ b/command_line/CmdLine/Src/Project/CLSegs.c
@@ -0,0 +1,123 @@
+#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;
+}