allocb.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "error.h"
  7. enum
  8. {
  9. Hdrspc = 64, /* leave room for high-level headers */
  10. Bdead = 0x51494F42, /* "QIOB" */
  11. };
  12. struct
  13. {
  14. Lock;
  15. ulong bytes;
  16. } ialloc;
  17. static Block*
  18. _allocb(int size)
  19. {
  20. Block *b;
  21. ulong addr;
  22. if((b = mallocz(sizeof(Block)+size+Hdrspc, 0)) == nil)
  23. return nil;
  24. b->next = nil;
  25. b->list = nil;
  26. b->free = 0;
  27. b->flag = 0;
  28. b->ref = 0;
  29. _xinc(&b->ref);
  30. /* align start of data portion by rounding up */
  31. addr = (ulong)b;
  32. addr = ROUND(addr + sizeof(Block), BLOCKALIGN);
  33. b->base = (uchar*)addr;
  34. /* align end of data portion by rounding down */
  35. b->lim = ((uchar*)b) + msize(b);
  36. addr = (ulong)(b->lim);
  37. addr = addr & ~(BLOCKALIGN-1);
  38. b->lim = (uchar*)addr;
  39. /* leave sluff at beginning for added headers */
  40. b->rp = b->lim - ROUND(size, BLOCKALIGN);
  41. if(b->rp < b->base)
  42. panic("_allocb");
  43. b->wp = b->rp;
  44. return b;
  45. }
  46. Block*
  47. allocb(int size)
  48. {
  49. Block *b;
  50. /*
  51. * Check in a process and wait until successful.
  52. * Can still error out of here, though.
  53. */
  54. if(up == nil)
  55. panic("allocb without up: %#p", getcallerpc(&size));
  56. if((b = _allocb(size)) == nil){
  57. xsummary();
  58. mallocsummary();
  59. panic("allocb: no memory for %d bytes", size);
  60. }
  61. setmalloctag(b, getcallerpc(&size));
  62. return b;
  63. }
  64. Block*
  65. iallocb(int size)
  66. {
  67. Block *b;
  68. static int m1, m2, mp;
  69. if(ialloc.bytes > conf.ialloc){
  70. if((m1++%10000)==0){
  71. if(mp++ > 1000){
  72. active.exiting = 1;
  73. exit(0);
  74. }
  75. iprint("iallocb: limited %lud/%lud\n",
  76. ialloc.bytes, conf.ialloc);
  77. }
  78. return nil;
  79. }
  80. if((b = _allocb(size)) == nil){
  81. if((m2++%10000)==0){
  82. if(mp++ > 1000){
  83. active.exiting = 1;
  84. exit(0);
  85. }
  86. iprint("iallocb: no memory %lud/%lud\n",
  87. ialloc.bytes, conf.ialloc);
  88. }
  89. return nil;
  90. }
  91. setmalloctag(b, getcallerpc(&size));
  92. b->flag = BINTR;
  93. ilock(&ialloc);
  94. ialloc.bytes += b->lim - b->base;
  95. iunlock(&ialloc);
  96. return b;
  97. }
  98. void
  99. freeb(Block *b)
  100. {
  101. void *dead = (void*)Bdead;
  102. long ref;
  103. if(b == nil || (ref = _xdec(&b->ref)) > 0)
  104. return;
  105. if(ref < 0){
  106. dumpstack();
  107. panic("ref %ld callerpc %#p", ref, getcallerpc(&b));
  108. }
  109. /*
  110. * drivers which perform non cache coherent DMA manage their own buffer
  111. * pool of uncached buffers and provide their own free routine.
  112. */
  113. if(b->free) {
  114. b->free(b);
  115. return;
  116. }
  117. if(b->flag & BINTR) {
  118. ilock(&ialloc);
  119. ialloc.bytes -= b->lim - b->base;
  120. iunlock(&ialloc);
  121. }
  122. /* poison the block in case someone is still holding onto it */
  123. b->next = dead;
  124. b->rp = dead;
  125. b->wp = dead;
  126. b->lim = dead;
  127. b->base = dead;
  128. free(b);
  129. }
  130. void
  131. checkb(Block *b, char *msg)
  132. {
  133. void *dead = (void*)Bdead;
  134. if(b == dead)
  135. panic("checkb b %s %lux", msg, b);
  136. if(b->base == dead || b->lim == dead || b->next == dead
  137. || b->rp == dead || b->wp == dead){
  138. print("checkb: base %#p lim %#p next %#p\n",
  139. b->base, b->lim, b->next);
  140. print("checkb: rp %#p wp %#p\n", b->rp, b->wp);
  141. panic("checkb dead: %s", msg);
  142. }
  143. if(b->base > b->lim)
  144. panic("checkb 0 %s %#p %#p", msg, b->base, b->lim);
  145. if(b->rp < b->base)
  146. panic("checkb 1 %s %#p %#p", msg, b->base, b->rp);
  147. if(b->wp < b->base)
  148. panic("checkb 2 %s %#p %#p", msg, b->base, b->wp);
  149. if(b->rp > b->lim)
  150. panic("checkb 3 %s %#p %#p", msg, b->rp, b->lim);
  151. if(b->wp > b->lim)
  152. panic("checkb 4 %s %#p %#p", msg, b->wp, b->lim);
  153. }
  154. void
  155. iallocsummary(void)
  156. {
  157. print("ialloc %lud/%lud\n", ialloc.bytes, conf.ialloc);
  158. }