xalloc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. enum
  7. {
  8. Nhole = 128,
  9. Magichole = 0x484F4C45, /* HOLE */
  10. };
  11. typedef struct Hole Hole;
  12. typedef struct Xalloc Xalloc;
  13. typedef struct Xhdr Xhdr;
  14. struct Hole
  15. {
  16. ulong addr;
  17. ulong size;
  18. ulong top;
  19. Hole* link;
  20. };
  21. struct Xhdr
  22. {
  23. ulong size;
  24. ulong magix;
  25. char data[];
  26. };
  27. struct Xalloc
  28. {
  29. Lock;
  30. Hole hole[Nhole];
  31. Hole* flist;
  32. Hole* table;
  33. };
  34. static Xalloc xlists;
  35. void
  36. xinit(void)
  37. {
  38. int i, n, upages, kpages;
  39. ulong maxpages;
  40. Confmem *m;
  41. Pallocmem *pm;
  42. Hole *h, *eh;
  43. eh = &xlists.hole[Nhole-1];
  44. for(h = xlists.hole; h < eh; h++)
  45. h->link = h+1;
  46. xlists.flist = xlists.hole;
  47. upages = conf.upages;
  48. kpages = conf.npage - upages;
  49. pm = palloc.mem;
  50. for(i=0; i<nelem(conf.mem); i++){
  51. m = &conf.mem[i];
  52. n = m->npage;
  53. if(n > kpages)
  54. n = kpages;
  55. /* don't try to use non-KADDR-able memory for kernel */
  56. maxpages = cankaddr(m->base)/BY2PG;
  57. if(n > maxpages)
  58. n = maxpages;
  59. /* first give to kernel */
  60. if(n > 0){
  61. m->kbase = (ulong)KADDR(m->base);
  62. m->klimit = (ulong)KADDR(m->base+n*BY2PG);
  63. xhole(m->base, n*BY2PG);
  64. kpages -= n;
  65. }
  66. /* if anything left over, give to user */
  67. if(n < m->npage){
  68. if(pm >= palloc.mem+nelem(palloc.mem)){
  69. print("xinit: losing %lud pages\n", m->npage-n);
  70. continue;
  71. }
  72. pm->base = m->base+n*BY2PG;
  73. pm->npage = m->npage - n;
  74. pm++;
  75. }
  76. }
  77. xsummary();
  78. }
  79. void*
  80. xspanalloc(ulong size, int align, ulong span)
  81. {
  82. ulong a, v, t;
  83. a = (ulong)xalloc(size+align+span);
  84. if(a == 0)
  85. panic("xspanalloc: %lud %d %lux", size, align, span);
  86. if(span > 2) {
  87. v = (a + span) & ~(span-1);
  88. t = v - a;
  89. if(t > 0)
  90. xhole(PADDR(a), t);
  91. t = a + span - v;
  92. if(t > 0)
  93. xhole(PADDR(v+size+align), t);
  94. }
  95. else
  96. v = a;
  97. if(align > 1)
  98. v = (v + align) & ~(align-1);
  99. return (void*)v;
  100. }
  101. void*
  102. xallocz(ulong size, int zero)
  103. {
  104. Xhdr *p;
  105. Hole *h, **l;
  106. /* add room for magix & size overhead, round up to nearest vlong */
  107. size += BY2V + offsetof(Xhdr, data[0]);
  108. size &= ~(BY2V-1);
  109. ilock(&xlists);
  110. l = &xlists.table;
  111. for(h = *l; h; h = h->link) {
  112. if(h->size >= size) {
  113. p = (Xhdr*)KADDR(h->addr);
  114. h->addr += size;
  115. h->size -= size;
  116. if(h->size == 0) {
  117. *l = h->link;
  118. h->link = xlists.flist;
  119. xlists.flist = h;
  120. }
  121. iunlock(&xlists);
  122. if(zero)
  123. memset(p, 0, size);
  124. p->magix = Magichole;
  125. p->size = size;
  126. return p->data;
  127. }
  128. l = &h->link;
  129. }
  130. iunlock(&xlists);
  131. return nil;
  132. }
  133. void*
  134. xalloc(ulong size)
  135. {
  136. return xallocz(size, 1);
  137. }
  138. void
  139. xfree(void *p)
  140. {
  141. Xhdr *x;
  142. x = (Xhdr*)((ulong)p - offsetof(Xhdr, data[0]));
  143. if(x->magix != Magichole) {
  144. xsummary();
  145. panic("xfree(%#p) %#ux != %#lux", p, Magichole, x->magix);
  146. }
  147. xhole(PADDR((uintptr)x), x->size);
  148. }
  149. int
  150. xmerge(void *vp, void *vq)
  151. {
  152. Xhdr *p, *q;
  153. p = (Xhdr*)(((ulong)vp - offsetof(Xhdr, data[0])));
  154. q = (Xhdr*)(((ulong)vq - offsetof(Xhdr, data[0])));
  155. if(p->magix != Magichole || q->magix != Magichole) {
  156. int i;
  157. ulong *wd;
  158. void *badp;
  159. xsummary();
  160. badp = (p->magix != Magichole? p: q);
  161. wd = (ulong *)badp - 12;
  162. for (i = 24; i-- > 0; ) {
  163. print("%#p: %lux", wd, *wd);
  164. if (wd == badp)
  165. print(" <-");
  166. print("\n");
  167. wd++;
  168. }
  169. panic("xmerge(%#p, %#p) bad magic %#lux, %#lux",
  170. vp, vq, p->magix, q->magix);
  171. }
  172. if((uchar*)p+p->size == (uchar*)q) {
  173. p->size += q->size;
  174. return 1;
  175. }
  176. return 0;
  177. }
  178. void
  179. xhole(ulong addr, ulong size)
  180. {
  181. ulong top;
  182. Hole *h, *c, **l;
  183. if(size == 0)
  184. return;
  185. top = addr + size;
  186. ilock(&xlists);
  187. l = &xlists.table;
  188. for(h = *l; h; h = h->link) {
  189. if(h->top == addr) {
  190. h->size += size;
  191. h->top = h->addr+h->size;
  192. c = h->link;
  193. if(c && h->top == c->addr) {
  194. h->top += c->size;
  195. h->size += c->size;
  196. h->link = c->link;
  197. c->link = xlists.flist;
  198. xlists.flist = c;
  199. }
  200. iunlock(&xlists);
  201. return;
  202. }
  203. if(h->addr > addr)
  204. break;
  205. l = &h->link;
  206. }
  207. if(h && top == h->addr) {
  208. h->addr -= size;
  209. h->size += size;
  210. iunlock(&xlists);
  211. return;
  212. }
  213. if(xlists.flist == nil) {
  214. iunlock(&xlists);
  215. print("xfree: no free holes, leaked %lud bytes\n", size);
  216. return;
  217. }
  218. h = xlists.flist;
  219. xlists.flist = h->link;
  220. h->addr = addr;
  221. h->top = top;
  222. h->size = size;
  223. h->link = *l;
  224. *l = h;
  225. iunlock(&xlists);
  226. }
  227. void
  228. xsummary(void)
  229. {
  230. int i;
  231. Hole *h;
  232. i = 0;
  233. for(h = xlists.flist; h; h = h->link)
  234. i++;
  235. print("%d holes free\n", i);
  236. i = 0;
  237. for(h = xlists.table; h; h = h->link) {
  238. print("%.8lux %.8lux %lud\n", h->addr, h->top, h->size);
  239. i += h->size;
  240. }
  241. print("%d bytes free\n", i);
  242. }