allocb.c 3.1 KB

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