xalloc.c 4.2 KB

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