1
0

malloc.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "all.h"
  2. #include "mem.h"
  3. #include "io.h"
  4. long niob;
  5. long nhiob;
  6. Hiob *hiob;
  7. /*
  8. * Called to allocate permanent data structures
  9. * Alignment is in number of bytes. It pertains both to the start and
  10. * end of the allocated memory.
  11. */
  12. void*
  13. ialloc(ulong n, int align)
  14. {
  15. Mbank *mbp;
  16. ulong p;
  17. int m;
  18. ilock(&mconf);
  19. for(mbp = &mconf.bank[0]; mbp < &mconf.bank[mconf.nbank]; mbp++){
  20. p = mbp->base;
  21. if(align <= 0)
  22. align = 4;
  23. if(m = n % align)
  24. n += align - m;
  25. if(m = p % align)
  26. p += align - m;
  27. if(p+n > mbp->limit)
  28. continue;
  29. mbp->base = p+n;
  30. iunlock(&mconf);
  31. memset((void*)(p|KZERO), 0, n);
  32. return (void*)(p|KZERO);
  33. }
  34. iunlock(&mconf);
  35. for(mbp = mconf.bank; mbp < &mconf.bank[mconf.nbank]; mbp++)
  36. print("bank[%ld]: base 0x%8.8lux, limit 0x%8.8lux\n",
  37. mbp - mconf.bank, mbp->base, mbp->limit);
  38. panic("ialloc(0x%lux, 0x%lx): out of memory\n", n, align);
  39. return 0;
  40. }
  41. void
  42. prbanks(void)
  43. {
  44. Mbank *mbp;
  45. for(mbp = mconf.bank; mbp < &mconf.bank[mconf.nbank]; mbp++)
  46. print("bank[%ld]: base 0x%8.8lux, limit 0x%8.8lux\n",
  47. mbp - mconf.bank, mbp->base, mbp->limit);
  48. }
  49. static void
  50. cmd_memory(int, char *[])
  51. {
  52. prbanks();
  53. }
  54. /*
  55. * allocate rest of mem
  56. * for io buffers.
  57. */
  58. #define HWIDTH 8 /* buffers per hash */
  59. void
  60. iobufinit(void)
  61. {
  62. long m;
  63. int i;
  64. Iobuf *p, *q;
  65. Hiob *hp;
  66. Mbank *mbp;
  67. wlock(&mainlock); /* init */
  68. wunlock(&mainlock);
  69. prbanks();
  70. m = 0;
  71. for(mbp = mconf.bank; mbp < &mconf.bank[mconf.nbank]; mbp++) {
  72. m += mbp->limit - mbp->base;
  73. }
  74. m -= conf.sparemem;
  75. niob = m / (sizeof(Iobuf) + RBUFSIZE + sizeof(Hiob)/HWIDTH);
  76. nhiob = niob / HWIDTH;
  77. while(!prime(nhiob))
  78. nhiob++;
  79. print(" %ld buffers; %ld hashes\n", niob, nhiob);
  80. hiob = ialloc(nhiob * sizeof(Hiob), 0);
  81. hp = hiob;
  82. for(i=0; i<nhiob; i++) {
  83. lock(hp);
  84. unlock(hp);
  85. hp++;
  86. }
  87. p = ialloc(niob * sizeof(Iobuf), 0);
  88. hp = hiob;
  89. for(i=0; i<niob; i++) {
  90. p->name = "buf";
  91. qlock(p);
  92. qunlock(p);
  93. if(hp == hiob)
  94. hp = hiob + nhiob;
  95. hp--;
  96. q = hp->link;
  97. if(q) {
  98. p->fore = q;
  99. p->back = q->back;
  100. q->back = p;
  101. p->back->fore = p;
  102. } else {
  103. hp->link = p;
  104. p->fore = p;
  105. p->back = p;
  106. }
  107. p->dev = devnone;
  108. p->addr = -1;
  109. p->xiobuf = ialloc(RBUFSIZE, RBUFSIZE);
  110. p->iobuf = (char*)-1;
  111. p++;
  112. }
  113. /*
  114. * Make sure that no more of bank[0] can be used:
  115. * 'check' will do an ialloc(0, 1) to find the base of
  116. * sparemem.
  117. */
  118. mconf.bank[0].base = mconf.bank[0].limit+1;
  119. i = 0;
  120. for(mbp = mconf.bank; mbp < &mconf.bank[mconf.nbank]; mbp++)
  121. i += mbp->limit - mbp->base;
  122. print(" mem left = %d\n", i);
  123. print(" out of = %ld\n", conf.mem);
  124. /* paranoia: add this command as late as is easy */
  125. cmd_install("memory", "-- print ranges of memory banks", cmd_memory);
  126. }
  127. void*
  128. iobufmap(Iobuf *p)
  129. {
  130. return p->iobuf = p->xiobuf;
  131. }
  132. void
  133. iobufunmap(Iobuf *p)
  134. {
  135. p->iobuf = (char*)-1;
  136. }