commit b06256d81a433245c592c9b78c716861f9d9a4d3
parent f7f62c8ee1a3fc9a4dcd48748a5355e9b3103cf3
Author: prenev <an2qzavok@gmail.com>
Date: Sun, 10 Jul 2022 11:48:57 +0300
more bus code
Diffstat:
6 files changed, 63 insertions(+), 5 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,4 +1,10 @@
-main: main.c
- cc -o main main.c
+main: main.o bus.o cell.o
+ cc -o main main.o bus.o cell.o
+
+bus.o: bus.c bus.h cell.h
+ cc -c bus.c
+
+cell.o: cell.c cell.h
+ cc -c cell.c
diff --git a/bus.c b/bus.c
@@ -1,3 +1,5 @@
+#include <stdlib.h>
+
#include "cell.h"
#include "bus.h"
@@ -16,10 +18,10 @@ buspush(Bus *bus, Msg *msg)
}
Msg *
-busspull(Bus *bus, int dst)
+buspull(Bus *bus, int dst)
{
int i;
- Msg *msg = NULL:
+ Msg *msg = NULL;
for (i = 0; i < BusBufMax; i++) {
msg = bus->buf[i];
if (msg == NULL) continue;
@@ -31,3 +33,20 @@ busspull(Bus *bus, int dst)
return NULL;
}
+Msg *
+createmsg(int src, int dst, Cell *dat)
+{
+ Msg *new = malloc(sizeof(Msg));
+ new->src = src;
+ new->dst = dst;
+ new->dat = dat;
+ return new;
+}
+
+void
+freemsg(Msg *msg)
+{
+ if (msg != NULL) freecell(msg->dat);
+ free(msg);
+}
+
diff --git a/bus.h b/bus.h
@@ -21,3 +21,5 @@ struct Msg {
};
Msg * createmsg(int, int, Cell *);
+void freemsg(Msg *);
+
diff --git a/cell.c b/cell.c
@@ -0,0 +1,19 @@
+#include <stdlib.h>
+
+#include "cell.h"
+
+Cell *
+createcell(int n, char *p)
+{
+ Cell *new = malloc(sizeof(Cell));
+ new->n = n;
+ new->p = p;
+ return new;
+}
+
+void
+freecell(Cell *cell)
+{
+ if (cell != NULL) free(cell->p);
+ free(cell);
+}
diff --git a/cell.h b/cell.h
@@ -7,3 +7,5 @@ struct Cell {
char *p;
};
+Cell * createcell(int, char *);
+void freecell(Cell *);
diff --git a/main.c b/main.c
@@ -1,11 +1,21 @@
#include <stdio.h>
+#include <string.h>
+#include "cell.h"
#include "bus.h"
int
main(void)
{
- printf("blurb\n");
+ Bus *bus = createbus();
+ Cell *cell = createcell(6, strdup("hello"));
+ Msg *msg = createmsg(0, 0, cell);
+ printf("in: %s\n", msg->dat->p);
+ buspush(bus, msg);
+ Msg *ret = buspull(bus, 0);
+ if (ret != NULL) printf("out: %s\n", ret->dat->p);
+ else printf("nope\n");
+ freemsg(ret);
return 0;
}