alloc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. #include <memdraw.h>
  13. void
  14. memimagemove(void *from, void *to)
  15. {
  16. Memdata *md;
  17. md = *(Memdata**)to;
  18. if(md->base != from){
  19. print("compacted data not right: #%p\n", md->base);
  20. abort();
  21. }
  22. md->base = to;
  23. /* if allocmemimage changes this must change too */
  24. md->bdata = (uint8_t*)md->base+sizeof(Memdata*)+sizeof(uint32_t);
  25. }
  26. Memimage*
  27. allocmemimaged(Rectangle r, uint32_t chan, Memdata *md)
  28. {
  29. int d;
  30. uint32_t l;
  31. Memimage *i;
  32. if(Dx(r) <= 0 || Dy(r) <= 0){
  33. werrstr("bad rectangle %R", r);
  34. return nil;
  35. }
  36. if((d = chantodepth(chan)) == 0) {
  37. werrstr("bad channel descriptor %.8lux", chan);
  38. return nil;
  39. }
  40. l = wordsperline(r, d);
  41. i = mallocz(sizeof(Memimage), 1);
  42. if(i == nil)
  43. return nil;
  44. i->data = md;
  45. i->zero = sizeof(uint32_t)*l*r.min.y;
  46. if(r.min.x >= 0)
  47. i->zero += (r.min.x*d)/8;
  48. else
  49. i->zero -= (-r.min.x*d+7)/8;
  50. i->zero = -i->zero;
  51. i->width = l;
  52. i->r = r;
  53. i->clipr = r;
  54. i->flags = 0;
  55. i->layer = nil;
  56. i->cmap = memdefcmap;
  57. if(memsetchan(i, chan) < 0){
  58. free(i);
  59. return nil;
  60. }
  61. return i;
  62. }
  63. Memimage*
  64. allocmemimage(Rectangle r, uint32_t chan)
  65. {
  66. int d;
  67. uint8_t *p;
  68. uint32_t l, nw;
  69. Memdata *md;
  70. Memimage *i;
  71. if((d = chantodepth(chan)) == 0) {
  72. werrstr("bad channel descriptor %.8lux", chan);
  73. return nil;
  74. }
  75. l = wordsperline(r, d);
  76. nw = l*Dy(r);
  77. md = malloc(sizeof(Memdata));
  78. if(md == nil)
  79. return nil;
  80. md->ref = 1;
  81. md->base = malloc(sizeof(Memdata*)+(1+nw)*sizeof(uint32_t));
  82. if(md->base == nil){
  83. free(md);
  84. return nil;
  85. }
  86. p = (uint8_t*)md->base;
  87. *(Memdata**)p = md;
  88. p += sizeof(Memdata*);
  89. *(uint32_t*)p = getcallerpc();
  90. p += sizeof(uint32_t);
  91. /* if this changes, memimagemove must change too */
  92. md->bdata = p;
  93. md->allocd = 1;
  94. i = allocmemimaged(r, chan, md);
  95. if(i == nil){
  96. free(md->base);
  97. free(md);
  98. return nil;
  99. }
  100. md->imref = i;
  101. return i;
  102. }
  103. void
  104. freememimage(Memimage *i)
  105. {
  106. if(i == nil)
  107. return;
  108. if(i->data->ref-- == 1 && i->data->allocd){
  109. if(i->data->base)
  110. free(i->data->base);
  111. free(i->data);
  112. }
  113. free(i);
  114. }
  115. /*
  116. * Wordaddr is deprecated.
  117. */
  118. uint32_t*
  119. wordaddr(Memimage *i, Point p)
  120. {
  121. return (uint32_t*) ((uintptr)byteaddr(i, p) & ~(sizeof(uint32_t)-1));
  122. }
  123. uint8_t*
  124. byteaddr(Memimage *i, Point p)
  125. {
  126. uint8_t *a;
  127. a = i->data->bdata+i->zero+sizeof(uint32_t)*p.y*i->width;
  128. if(i->depth < 8){
  129. /*
  130. * We need to always round down,
  131. * but C rounds toward zero.
  132. */
  133. int np;
  134. np = 8/i->depth;
  135. if(p.x < 0)
  136. return a+(p.x-np+1)/np;
  137. else
  138. return a+p.x/np;
  139. }
  140. else
  141. return a+p.x*(i->depth/8);
  142. }
  143. int
  144. memsetchan(Memimage *i, uint32_t chan)
  145. {
  146. int d;
  147. int t, j, k;
  148. uint32_t cc;
  149. int bytes;
  150. if((d = chantodepth(chan)) == 0) {
  151. werrstr("bad channel descriptor");
  152. return -1;
  153. }
  154. i->depth = d;
  155. i->chan = chan;
  156. i->flags &= ~(Fgrey|Falpha|Fcmap|Fbytes);
  157. bytes = 1;
  158. for(cc=chan, j=0, k=0; cc; j+=NBITS(cc), cc>>=8, k++){
  159. t=TYPE(cc);
  160. if(t < 0 || t >= NChan){
  161. werrstr("bad channel string");
  162. return -1;
  163. }
  164. if(t == CGrey)
  165. i->flags |= Fgrey;
  166. if(t == CAlpha)
  167. i->flags |= Falpha;
  168. if(t == CMap && i->cmap == nil){
  169. i->cmap = memdefcmap;
  170. i->flags |= Fcmap;
  171. }
  172. i->shift[t] = j;
  173. i->mask[t] = (1<<NBITS(cc))-1;
  174. i->nbits[t] = NBITS(cc);
  175. if(NBITS(cc) != 8)
  176. bytes = 0;
  177. }
  178. i->nchan = k;
  179. if(bytes)
  180. i->flags |= Fbytes;
  181. return 0;
  182. }