allocb.c 3.3 KB

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