]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/libdraw/alloc.c
python: arm64 support
[plan9front.git] / sys / src / libdraw / alloc.c
old mode 100755 (executable)
new mode 100644 (file)
index 7884321..28caaac
@@ -3,18 +3,18 @@
 #include <draw.h>
 
 Image*
-allocimage(Display *d, Rectangle r, ulong chan, int repl, ulong val)
+allocimage(Display *d, Rectangle r, ulong chan, int repl, ulong col)
 {
-       Image*  i;
+       Image *i;
 
-       i =  _allocimage(nil, d, r, chan, repl, val, 0, 0);
-       if (i)
+       i = _allocimage(nil, d, r, chan, repl, col, 0, 0);
+       if(i != nil)
                setmalloctag(i, getcallerpc(&d));
        return i;
 }
 
 Image*
-_allocimage(Image *ai, Display *d, Rectangle r, ulong chan, int repl, ulong val, int screenid, int refresh)
+_allocimage(Image *ai, Display *d, Rectangle r, ulong chan, int repl, ulong col, int screenid, int refresh)
 {
        uchar *a;
        char *err;
@@ -23,9 +23,13 @@ _allocimage(Image *ai, Display *d, Rectangle r, ulong chan, int repl, ulong val,
        int id;
        int depth;
 
-       err = 0;
-       i = 0;
+       err = nil;
+       i = nil;
 
+       if(badrect(r)){
+               werrstr("bad rectangle");
+               return nil;
+       }
        if(chan == 0){
                werrstr("bad channel descriptor");
                return nil;
@@ -35,18 +39,16 @@ _allocimage(Image *ai, Display *d, Rectangle r, ulong chan, int repl, ulong val,
        if(depth == 0){
                err = "bad channel descriptor";
     Error:
-               if(err)
+               if(err != nil)
                        werrstr("allocimage: %s", err);
                else
                        werrstr("allocimage: %r");
                free(i);
-               return 0;
+               return nil;
        }
 
-       /* flush pending data so we don't get error allocating the image */
-       flushimage(d, 0);
        a = bufimage(d, 1+4+4+1+4+1+4*4+4*4+4);
-       if(a == 0)
+       if(a == nil)
                goto Error;
        d->imageid++;
        id = d->imageid;
@@ -69,17 +71,15 @@ _allocimage(Image *ai, Display *d, Rectangle r, ulong chan, int repl, ulong val,
        BPLONG(a+35, clipr.min.y);
        BPLONG(a+39, clipr.max.x);
        BPLONG(a+43, clipr.max.y);
-       BPLONG(a+47, val);
-       if(flushimage(d, 0) < 0)
-               goto Error;
+       BPLONG(a+47, col);
 
-       if(ai)
+       if(ai != nil)
                i = ai;
        else{
                i = malloc(sizeof(Image));
                if(i == nil){
                        a = bufimage(d, 1+4);
-                       if(a){
+                       if(a != nil){
                                a[0] = 'f';
                                BPLONG(a+1, id);
                                flushimage(d, 0);
@@ -94,8 +94,8 @@ _allocimage(Image *ai, Display *d, Rectangle r, ulong chan, int repl, ulong val,
        i->r = r;
        i->clipr = clipr;
        i->repl = repl;
-       i->screen = 0;
-       i->next = 0;
+       i->screen = nil;
+       i->next = nil;
        return i;
 }
 
@@ -108,25 +108,24 @@ namedimage(Display *d, char *name)
        int id, n;
        ulong chan;
 
-       err = 0;
-       i = 0;
+       err = nil;
+       i = nil;
 
        n = strlen(name);
        if(n >= 256){
                err = "name too long";
     Error:
-               if(err)
+               if(err != nil)
                        werrstr("namedimage: %s", err);
                else
                        werrstr("namedimage: %r");
-               if(i)
-                       free(i);
-               return 0;
+               free(i);
+               return nil;
        }
        /* flush pending data so we don't get error allocating the image */
        flushimage(d, 0);
        a = bufimage(d, 1+4+1+n);
-       if(a == 0)
+       if(a == nil)
                goto Error;
        d->imageid++;
        id = d->imageid;
@@ -145,7 +144,7 @@ namedimage(Display *d, char *name)
        if(i == nil){
        Error1:
                a = bufimage(d, 1+4);
-               if(a){
+               if(a != nil){
                        a[0] = 'f';
                        BPLONG(a+1, id);
                        flushimage(d, 0);
@@ -169,8 +168,8 @@ namedimage(Display *d, char *name)
        i->clipr.min.y = atoi(buf+9*12);
        i->clipr.max.x = atoi(buf+10*12);
        i->clipr.max.y = atoi(buf+11*12);
-       i->screen = 0;
-       i->next = 0;
+       i->screen = nil;
+       i->next = nil;
        return i;
 }
 
@@ -182,7 +181,7 @@ nameimage(Image *i, char *name, int in)
 
        n = strlen(name);
        a = bufimage(i->display, 1+4+1+1+n);
-       if(a == 0)
+       if(a == nil)
                return 0;
        a[0] = 'N';
        BPLONG(a+1, i->id);
@@ -201,23 +200,15 @@ _freeimage1(Image *i)
        Display *d;
        Image *w;
 
-       if(i == 0 || i->display == 0)
+       if(i == nil || i->display == nil)
                return 0;
-       /* make sure no refresh events occur on this if we block in the write */
        d = i->display;
-       /* flush pending data so we don't get error deleting the image */
-       flushimage(d, 0);
-       a = bufimage(d, 1+4);
-       if(a == 0)
-               return -1;
-       a[0] = 'f';
-       BPLONG(a+1, i->id);
-       if(i->screen){
+       if(i->screen != nil){
                w = d->windows;
                if(w == i)
                        d->windows = i->next;
                else
-                       while(w){
+                       while(w != nil){
                                if(w->next == i){
                                        w->next = i->next;
                                        break;
@@ -225,9 +216,11 @@ _freeimage1(Image *i)
                                w = w->next;
                        }
        }
-       if(flushimage(d, i->screen!=0) < 0)
+       a = bufimage(d, 1+4);
+       if(a == nil)
                return -1;
-
+       a[0] = 'f';
+       BPLONG(a+1, i->id);
        return 0;
 }