commit 9524898ffaacc3ffd1f6b61f5dace2edc6b86329
parent 55ed39b217938f122632716897ba8d39b60e5907
Author: glenda <glenda@9front.local>
Date: Wed, 25 Aug 2021 13:04:31 +0000
to debug mem issues add MAGIC to array, move text insertion to main proc, fix bug in arraygrow
Diffstat:
5 files changed, 62 insertions(+), 21 deletions(-)
diff --git a/array.c b/array.c
@@ -3,6 +3,8 @@
#include "array.h"
+#define MAGIC 0x1234
+
int
_arraycheck(Array *ap, long n, char *s)
{
@@ -19,6 +21,7 @@ arraycreate(long size, long n, void (*free)(void *))
Array *ap;
ap = mallocz(sizeof(Array), 1);
*ap = (Array) {
+ MAGIC,
mallocz(sizeof(QLock), 1),
size,
n,
@@ -32,6 +35,8 @@ arraycreate(long size, long n, void (*free)(void *))
void
arrayfree(Array *ap)
{
+ assert(ap->magic == MAGIC);
+
long i;
qlock(ap->l);
if (ap->free != nil) {
@@ -49,32 +54,42 @@ arrayfree(Array *ap)
void *
arraygrow(Array *ap, long n, void *v)
{
+ assert(ap->magic == MAGIC);
+
void *ve;
if (n < 0) {
werrstr("arraygrow: negative growth size");
return nil;
}
- ve = arrayend(ap);
+ if (n == 0) {
+ werrstr("arraygrow: zero growth size");
+ return nil;
+ }
qlock(ap->l);
ap->count += n;
if (ap->count > ap->n) {
ap->n += ap->n;
- if (ap->count > ap->n) ap->n = ap->count;
+ if (ap->count > ap->n)
+ ap->n = ap->count;
ap->p = realloc(ap->p, ap->size * ap->n);
}
+ ve = (void *)(ap->p + ap->size * (ap->count - n));
+
memset(ve, 0, n * ap->size);
if (v != nil) {
memcpy(ve, v, n * ap->size);
}
qunlock(ap->l);
- return (void *)(ap->p + ap->size * (ap->count - n));
+ return ve;
}
int
arraydel(Array *ap, long offset, long count)
{
+ assert(ap->magic == MAGIC);
+
void *v, *ve;
long i;
if (_arraycheck(ap, offset + count, "arraydel") != 0) return -1;
@@ -98,6 +113,8 @@ arraydel(Array *ap, long offset, long count)
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) {
@@ -110,19 +127,26 @@ arrayget(Array *ap, long n, void *v)
void *
arrayend(Array *ap)
{
+ assert(ap->magic == MAGIC);
+
return (void *)(ap->p + ap->size * ap->count);
}
void *
arrayinsert(Array *ap, long n, long m, void *v)
{
+ assert(ap->magic == MAGIC);
+
void *vs, *vn;
if (n == ap->count) {
vs = arraygrow(ap, m, v);
return vs;
}
if (_arraycheck(ap, n, "arrayinsert") != 0) return nil;
- arraygrow(ap, m, nil);
+ if (arraygrow(ap, m, nil) == nil) {
+ werrstr("arrayinsert: %r");
+ return nil;
+ }
vs = arrayget(ap, n, nil);
vn = arrayget(ap, n + m, nil);
qlock(ap->l);
diff --git a/array.h b/array.h
@@ -1,6 +1,7 @@
typedef struct Array Array;
struct Array {
+ short magic;
QLock *l;
long size;
long n;
diff --git a/devfs.c b/devfs.c
@@ -38,22 +38,24 @@ devfs_write(Req *r)
File *f;
f = r->fid->file;
if (f == cons){
- Object *obj;
- r->ofcall.count = r->ifcall.count;
-
- obj = objectcreate();
- mkobjectftree(obj, fsctl->tree->root);
- objinsertbeforelast(obj);
+// Object *obj;
+ Array *a;
+ a = arraycreate(sizeof(char), r->ifcall.count, nil);
+ arraygrow(a, r->ifcall.count, r->ifcall.data);
+ send(insertc, &a);
-//print("%ld -> ", olast->offset);
-
- arrayinsert(rich.text, olast->offset, r->ifcall.count, r->ifcall.data);
- olast->offset += r->ifcall.count;
+ r->ofcall.count = r->ifcall.count;
-//print("%ld\n", olast->offset);
+// obj = objectcreate();
+// mkobjectftree(obj, fsctl->tree->root);
+// objinsertbeforelast(obj);
+// arrayinsert(rich.text, olast->offset,
+// r->ifcall.count,
+// r->ifcall.data);
+// olast->offset += r->ifcall.count;
- nbsend(redrawc, &obj);
+// nbsend(redrawc, &obj);
respond(r, nil);
} else if (f == consctl) {
respond(r, "not implemented");
diff --git a/richterm.c b/richterm.c
@@ -23,7 +23,7 @@ long getsel(Point pt);
Rich rich;
int hostpid = -1;
-Channel *pidchan, *redrawc;
+Channel *pidchan, *redrawc, *insertc;
Mousectl *mctl;
Keyboardctl *kctl;
Devfsctl *dctl;
@@ -82,7 +82,8 @@ threadmain(int argc, char **argv)
int rv[2], mmode;
Mouse mv;
Rune kv;
- Object *ov;
+ Object *ov, *obj;
+ Array *av;
ARGBEGIN{
case 'D':
@@ -136,6 +137,7 @@ threadmain(int argc, char **argv)
qunlock(rich.l);
redrawc = chancreate(sizeof(Object *), 8);
+ insertc = chancreate(sizeof(Array *), 8);
resize();
draw(screen, screen->r, Inormbg, nil, ZP);
@@ -151,11 +153,12 @@ threadmain(int argc, char **argv)
proccreate(runcmd, argv, 16 * 1024);
hostpid = recvul(pidchan);
- enum {MOUSE, RESIZE, REDRAW, KBD, NONE};
- Alt alts[5] = {
+ enum {MOUSE, RESIZE, REDRAW, INSERT, KBD, AEND};
+ Alt alts[AEND + 1] = {
{mctl->c, &mv, CHANRCV},
{mctl->resizec, rv, CHANRCV},
{redrawc, &ov, CHANRCV},
+ {insertc, &av, CHANRCV},
{kctl->c, &kv, CHANRCV},
{nil, nil, CHANEND},
};
@@ -179,6 +182,14 @@ threadmain(int argc, char **argv)
drawscrollbar();
flushimage(display, 1);
break;
+ case INSERT:
+ obj = objectcreate();
+ mkobjectftree(obj, fsctl->tree->root);
+ objinsertbeforelast(obj);
+ objsettext(obj, arrayget(av, 0, nil), av->count);
+ arrayfree(av);
+ nbsend(redrawc, &obj);
+ break;
case KBD:
if (kv == 0x7f) shutdown(); /* delete */
if (kv == 0xf00e) { /* d-pad up */
@@ -249,7 +260,7 @@ threadmain(int argc, char **argv)
nbsend(redrawc, &obj);
}
break;
- case NONE:
+ case AEND:
break;
}
}
@@ -824,6 +835,8 @@ objsettext(Object *obj, char *data, long count)
memcpy(p, data, count);
qunlock(rich.text->l);
+ assert(rich.objects->magic == 0x1234);
+
qlock(rich.objects->l);
for (obj = obj->next; obj != nil; obj = obj->next) {
obj->offset += dn;
diff --git a/richterm.h b/richterm.h
@@ -1,4 +1,5 @@
extern Channel *redrawc;
+extern Channel *insertc;
void drawscrollbar(void);