allocb.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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;
  67. if(ialloc.bytes > conf.ialloc){
  68. if((m1++%10000)==0)
  69. print("iallocb: limited %lud/%lud\n",
  70. ialloc.bytes, conf.ialloc);
  71. return 0;
  72. }
  73. if((b = _allocb(size)) == nil){
  74. if((m2++%10000)==0)
  75. print("iallocb: no memory %lud/%lud\n",
  76. ialloc.bytes, conf.ialloc);
  77. return nil;
  78. }
  79. setmalloctag(b, getcallerpc(&size));
  80. b->flag = BINTR;
  81. ilock(&ialloc);
  82. ialloc.bytes += b->lim - b->base;
  83. iunlock(&ialloc);
  84. return b;
  85. }
  86. void
  87. freeb(Block *b)
  88. {
  89. void *dead = (void*)Bdead;
  90. if(b == nil)
  91. return;
  92. /*
  93. * drivers which perform non cache coherent DMA manage their own buffer
  94. * pool of uncached buffers and provide their own free routine.
  95. */
  96. if(b->free) {
  97. b->free(b);
  98. return;
  99. }
  100. if(b->flag & BINTR) {
  101. ilock(&ialloc);
  102. ialloc.bytes -= b->lim - b->base;
  103. iunlock(&ialloc);
  104. }
  105. /* poison the block in case someone is still holding onto it */
  106. b->next = dead;
  107. b->rp = dead;
  108. b->wp = dead;
  109. b->lim = dead;
  110. b->base = dead;
  111. free(b);
  112. }
  113. void
  114. checkb(Block *b, char *msg)
  115. {
  116. void *dead = (void*)Bdead;
  117. if(b == dead)
  118. panic("checkb b %s %lux", msg, b);
  119. if(b->base == dead || b->lim == dead || b->next == dead
  120. || b->rp == dead || b->wp == dead){
  121. print("checkb: base 0x%8.8luX lim 0x%8.8luX next 0x%8.8luX\n",
  122. b->base, b->lim, b->next);
  123. print("checkb: rp 0x%8.8luX wp 0x%8.8luX\n", b->rp, b->wp);
  124. panic("checkb dead: %s\n", msg);
  125. }
  126. if(b->base > b->lim)
  127. panic("checkb 0 %s %lux %lux", msg, b->base, b->lim);
  128. if(b->rp < b->base)
  129. panic("checkb 1 %s %lux %lux", msg, b->base, b->rp);
  130. if(b->wp < b->base)
  131. panic("checkb 2 %s %lux %lux", msg, b->base, b->wp);
  132. if(b->rp > b->lim)
  133. panic("checkb 3 %s %lux %lux", msg, b->rp, b->lim);
  134. if(b->wp > b->lim)
  135. panic("checkb 4 %s %lux %lux", msg, b->wp, b->lim);
  136. }
  137. void
  138. iallocsummary(void)
  139. {
  140. print("ialloc %lud/%lud\n", ialloc.bytes, conf.ialloc);
  141. }