allocb.c 3.5 KB

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