ref: 4974f11f7c2e917de605d02d5c07ad87c241ff80
parent: 747cddf7870c548043f44adec04b62440e5abd21
author: sirjofri <sirjofri@sirjofri.de>
date: Thu Feb 15 12:56:33 EST 2024
more filesystem stuff
--- a/gpufs.c
+++ b/gpufs.c
@@ -3,6 +3,7 @@
#include <fcall.h>
#include <thread.h>
#include <9p.h>
+#include <String.h>
#include "vm.h"
void
@@ -52,6 +53,7 @@
struct Gpuobject {
Ref;
int type;
+ int id;
};
typedef struct Gpufid Gpufid;
@@ -64,9 +66,11 @@
static int nobjects = 0;
#define OBJECTID(c) ((int)(((Gpuobject*)(c)) - objects))
+#define GO(c) ((Gpuobject*)(c))
+String *fsqueue = nil;
+
int debug = 0;
-int compile = 0;
static char *user;
static long time0;
@@ -122,14 +126,24 @@
switch (level) {
case Qobject:
- snprint(buf, sizeof(buf), "%d", OBJECTID(aux));
+ snprint(buf, sizeof(buf), "%d", GO(aux)->id);
d->name = estrdup(buf);
break;
case Qrctl:
case Qctl:
+ d->mode = 0666;
+ d->name = estrdup(nametab[level]);
+ break;
case Qbuffer:
+ d->length = getbufferlength(GO(aux)->id);
+ d->mode = 0666;
+ d->name = estrdup(nametab[level]);
+ break;
case Qshader:
+ d->length = getshaderlength(GO(aux)->id);
d->mode = 0666;
+ d->name = estrdup(nametab[level]);
+ break;
default:
d->name = estrdup(nametab[level]);
}
@@ -158,12 +172,19 @@
fsmkdir(d, i, aux);
// Qshader is last file in list
- if (i == Qshader)
+ if (i >= Qshader)
return -1;
return 0;
}
static void
+readrctl(Req *r)
+{
+ readstr(r, s_to_c(fsqueue));
+ s_reset(fsqueue);
+}
+
+static void
fsread(Req *r)
{
//char buf[1024];
@@ -180,6 +201,7 @@
respond(r, nil);
return;
case Qrctl:
+ readrctl(r);
respond(r, nil);
return;
}
@@ -189,6 +211,7 @@
static void
fsstart(Srv*)
{
+ fsqueue = s_new();
}
static void
@@ -279,43 +302,77 @@
static int
newshader(void)
{
+ int id = genshader();
+ if (id < 0) {
+ return -1;
+ }
Gpuobject *o = &objects[nobjects];
memset(o, 0, sizeof(*o));
o->type = Qshader;
- return nobjects++;
+ o->id = id;
+
+ char *s = smprint("s %d\n", o->id);
+ s_append(fsqueue, s);
+ free(s);
+
+ nobjects++;
+ return o->id;
}
static int
-newbuffer(void)
+newbuffer(long len)
{
+ int id = genbuffer(len);
+ if (id < 0) {
+ return -1;
+ }
Gpuobject *o = &objects[nobjects];
memset(o, 0, sizeof(*o));
o->type = Qbuffer;
- return nobjects++;
+ o->id = id;
+
+ char *s = smprint("b %d\n", o->id);
+ s_append(fsqueue, s);
+ free(s);
+
+ nobjects++;
+ return o->id;
}
static void
-procrootcommand(int argc, char **argv)
+procrootcommand(Req *r, int argc, char **argv)
{
// n(ew) s(hader)
- // n(ew) b(uffer)
+ // n(ew) b(uffer) <len>
- if (argc == 2) {
- if (strcmp(argv[0], "n") == 0) {
+ if (strcmp(argv[0], "n") == 0) {
+ switch (argc) {
+ case 2:
if (strcmp(argv[1], "s") == 0) {
- newshader();
+ if (newshader() < 0)
+ respond(r, "error creating shader!");
+ else
+ respond(r, nil);
return;
}
+ break;
+ case 3:
if (strcmp(argv[1], "b") == 0) {
- newbuffer();
+ long len = atol(argv[2]);
+ if (newbuffer(len) < 0)
+ respond(r, "error creating buffer!");
+ else
+ respond(r, nil);
return;
}
}
}
+
+ respond(r, "error: bad command!");
}
static void
-rootcommand(char *cmd)
+rootcommand(Req *r, char *cmd)
{
char *lines[10];
int linec;
@@ -325,7 +382,7 @@
for (int i = 0; i < linec; i++) {
char *c[10];
int num = getfields(lines[i], c, 10, 1, " \t");
- procrootcommand(num, c);
+ procrootcommand(r, num, c);
}
}
@@ -342,8 +399,7 @@
n = r->ofcall.count = r->ifcall.count;
s = emalloc(n+1);
memmove(s, r->ifcall.data, n);
- rootcommand(s);
- respond(r, nil);
+ rootcommand(r, s);
return;
}
respond(r, "not implemented");
--- a/vm.c
+++ b/vm.c
@@ -2,6 +2,7 @@
#include <libc.h>
#include "vm.h"
#include "ops.h"
+#include "objects.h"
extern int debug;
@@ -65,4 +66,87 @@
{
stack = nil; // TODO clean stack
runstack(ptr);
+}
+
+int compile = 0;
+
+enum {
+ SHADER,
+ BUFFER,
+};
+
+typedef struct Object Object;
+struct Object {
+ int id;
+ char type;
+ union {
+ Shader s;
+ Buffer b;
+ };
+};
+
+Object objects[256];
+int numobjects = 0;
+
+int
+genshader(void)
+{
+ int id;
+ if (numobjects >= 256) {
+ werrstr("not enough free objects!");
+ return -1;
+ }
+ id = numobjects++;
+ objects[id].id = id;
+ objects[id].type = SHADER;
+ objects[id].s.buffer = nil;
+ objects[id].s.len = -1;
+ return id;
+}
+
+int
+genbuffer(long size)
+{
+ int id;
+ if (numobjects >= 256) {
+ werrstr("not enough free objects!");
+ return -1;
+ }
+ id = numobjects;
+ objects[id].id = id;
+ objects[id].type = BUFFER;
+ objects[id].b.len = size;
+ objects[id].b.buffer = malloc(size);
+ if (!objects[id].b.buffer) {
+ werrstr("cannot allocate memory: %r");
+ return -1;
+ }
+ numobjects++;
+ return id;
+}
+
+int
+getnumobjects()
+{
+ return numobjects;
+}
+
+long
+getbufferlength(int id)
+{
+ if (objects[id].type != BUFFER) {
+ werrstr("invalid object type!");
+ return -1;
+ }
+ return objects[id].b.len;
+}
+
+long
+getshaderlength(int id)
+{
+ if (objects[id].type != SHADER) {
+ werrstr("invalid object type!");
+ return -1;
+ }
+ return objects[id].s.len;
}
--- a/vm.h
+++ b/vm.h
@@ -1,3 +1,10 @@
void vmrun(u32int *ptr);
void runstack(u32int *ptr);
void retstack(void);
+
+int genshader(void);
+int genbuffer(long size);
+
+int getnumobjects(void);
+long getshaderlength(int id);
+long getbufferlength(int id);