6 #define poolalloc(a, b) malloc(b)
7 #define poolfree(a, b) free(b)
10 memimagemove(void *from, void *to)
16 print("compacted data not right: #%p\n", md->base);
21 /* if allocmemimage changes this must change too */
22 md->bdata = (uchar*)md->base+sizeof(Memdata*)+sizeof(ulong);
26 allocmemimaged(Rectangle r, u32int chan, Memdata *md, void *X)
32 if(Dx(r) <= 0 || Dy(r) <= 0){
33 werrstr("bad rectangle %R", r);
36 if((d = chantodepth(chan)) == 0) {
37 werrstr("bad channel descriptor %.8lux", chan);
41 l = wordsperline(r, d);
43 i = mallocz(sizeof(Memimage), 1);
49 i->zero = sizeof(u32int)*l*r.min.y;
52 i->zero += (r.min.x*d)/8;
54 i->zero -= (-r.min.x*d+7)/8;
62 if(memsetchan(i, chan) < 0){
70 _allocmemimage(Rectangle r, u32int chan)
78 if((d = chantodepth(chan)) == 0) {
79 werrstr("bad channel descriptor %.8lux", chan);
83 l = wordsperline(r, d);
85 md = malloc(sizeof(Memdata));
90 md->base = poolalloc(imagmem, sizeof(Memdata*)+(1+nw)*sizeof(ulong));
98 p += sizeof(Memdata*);
100 *(ulong*)p = getcallerpc(&r);
103 /* if this changes, memimagemove must change too */
107 i = allocmemimaged(r, chan, md, nil);
109 poolfree(imagmem, md->base);
118 _freememimage(Memimage *i)
122 if(i->data->ref-- == 1 && i->data->allocd){
124 poolfree(imagmem, i->data->base);
131 * Wordaddr is deprecated.
134 wordaddr(Memimage *i, Point p)
136 return (u32int*) ((ulong)byteaddr(i, p) & ~(sizeof(u32int)-1));
140 byteaddr(Memimage *i, Point p)
144 a = i->data->bdata+i->zero+sizeof(u32int)*p.y*i->width;
148 * We need to always round down,
149 * but C rounds toward zero.
154 return a+(p.x-np+1)/np;
159 return a+p.x*(i->depth/8);
163 memsetchan(Memimage *i, u32int chan)
170 if((d = chantodepth(chan)) == 0) {
171 werrstr("bad channel descriptor");
177 i->flags &= ~(Fgrey|Falpha|Fcmap|Fbytes);
179 for(cc=chan, j=0, k=0; cc; j+=NBITS(cc), cc>>=8, k++){
181 if(t < 0 || t >= NChan){
182 werrstr("bad channel string");
189 if(t == CMap && i->cmap == nil){
190 i->cmap = memdefcmap;
195 i->mask[t] = (1<<NBITS(cc))-1;
196 i->nbits[t] = NBITS(cc);