commit 513287ec8cbda4788bdd0294588207eae8a5126d
parent b8639014820b2d93ed06319f304d16b522385bc6
Author: Pavel Renev <an2qzavok@gmail.com>
Date: Thu, 26 Aug 2021 20:01:43 +0000
consread: don´t just ignore r->ifcall.count
Diffstat:
M | TODO | | | 4 | ---- |
M | array.c | | | 41 | ++++++++++++++++++++++++++--------------- |
M | fs.c | | | 23 | ++++++++++++++++++----- |
3 files changed, 44 insertions(+), 24 deletions(-)
diff --git a/TODO b/TODO
@@ -1,7 +1,3 @@
-fix crashes
-
-% man man
-
# images
draw them
diff --git a/array.c b/array.c
@@ -15,6 +15,12 @@ _arraycheck(Array *ap, long n, char *s)
return 0;
}
+void *
+_arrayget(Array *ap, long n)
+{
+ return (void *)(ap->p + ap->size * n);
+}
+
Array *
arraycreate(long size, long n, void (*free)(void *))
{
@@ -41,10 +47,9 @@ arrayfree(Array *ap)
qlock(ap->l);
if (ap->free != nil) {
for (i = 0; i < ap->count; i ++) {
- void *v;
- v = nil;
- arrayget(ap, i, &v);
- if (v != nil) ap->free(v);
+ void **v;
+ v = _arrayget(ap, i);
+ if (*v != nil) ap->free(*v);
}
}
qunlock(ap->l);
@@ -90,36 +95,40 @@ arraydel(Array *ap, long offset, long count)
void *v, *ve;
long i;
- if (_arraycheck(ap, offset + count, "arraydel") != 0) return -1;
+ if (_arraycheck(ap, offset, "arraydel") != 0) return -1;
+ if (offset + count > ap->count) {
+ werrstr("arraydel: count past array limit");
+ return -1;
+ };
+
+ qlock(ap->l);
if (ap->free != nil) {
for (i = offset; i < offset+ count; i++) {
- v = arrayget(ap, i, nil);
+ v = _arrayget(ap, i);
ap->free(v);
}
}
- v = arrayget(ap, offset, nil);
- ve = arrayget(ap, offset + count, nil);
- qlock(ap->l);
+ v = _arrayget(ap, offset);
+ ve = _arrayget(ap, offset + count);
memcpy(v, ve, (ap->count - offset - count) * ap->size);
ap->count -= count;
qunlock(ap->l);
return 0;
}
-void *
+void *
arrayget(Array *ap, long n, void *v)
{
assert(ap->magic == MAGIC);
-
if (_arraycheck(ap, n, "arrayget") != 0) return nil;
qlock(ap->l);
if (v != nil) {
memcpy(v, ap->p + ap->size * n, ap->size);
}
qunlock(ap->l);
- return (void *)(ap->p + ap->size * n);
+ return _arrayget(ap, n);
}
void *
@@ -127,7 +136,7 @@ arrayend(Array *ap)
{
assert(ap->magic == MAGIC);
- return (void *)(ap->p + ap->size * ap->count);
+ return _arrayget(ap, ap->count);
}
void *
@@ -145,9 +154,11 @@ arrayinsert(Array *ap, long n, long m, void *v)
werrstr("arrayinsert: %r");
return nil;
}
- vs = arrayget(ap, n, nil);
- vn = arrayget(ap, n + m, nil);
+
qlock(ap->l);
+
+ vs = _arrayget(ap, n);
+ vn = _arrayget(ap, n + m);
memcpy(vn, vs, (ap->count - n - m) * ap->size);
memset(vs, 0, m * ap->size);
if (v != nil) {
diff --git a/fs.c b/fs.c
@@ -12,6 +12,7 @@ File *new, *ctl, *text, *cons, *consctl;
Object *newobj;
File *fsroot;
Channel *consc;
+Array *consbuf;
void fs_open(Req *);
void fs_read(Req *);
@@ -27,6 +28,7 @@ initfs(char *srvname)
.write = fs_write,
};
newobj = nil;
+ consbuf = nil;
consc = chancreate(sizeof(Array *), 1024);
srv.tree = alloctree("richterm", "richterm", DMDIR|0555, ftree_destroy);
fsroot = srv.tree->root;
@@ -270,11 +272,15 @@ fontwrite(Req *r)
void
consread(Req *r)
{
- Array *dv;
- recv(consc, &dv);
- r->ofcall.count = dv->count;
- memcpy(r->ofcall.data, dv->p, dv->count);
- arrayfree(dv);
+ if (consbuf == nil) recv(consc, &consbuf);
+ r->ifcall.offset = 0;
+ readbuf(r, consbuf->p, consbuf->count);
+ if (arraydel(consbuf, 0, r->ofcall.count) != 0)
+ sysfatal("consread: %r");
+ if (consbuf->count == 0) {
+ arrayfree(consbuf);
+ consbuf = nil;
+ }
}
void
@@ -286,3 +292,9 @@ conswrite(Req *r)
send(insertc, &a);
r->ofcall.count = r->ifcall.count;
}
+
+void
+ctlread(Req *)
+{
+
+}
+\ No newline at end of file