malloc.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "all.h"
  10. #include "io.h"
  11. int32_t niob;
  12. int32_t nhiob;
  13. Hiob *hiob;
  14. /*
  15. * Called to allocate permanent data structures
  16. * Alignment is in number of bytes. It pertains both to the start and
  17. * end of the allocated memory.
  18. */
  19. void*
  20. ialloc(uint32_t n, int align)
  21. {
  22. void *p = mallocalign(n, align, 0, 0);
  23. if (p == nil)
  24. panic("ialloc: out of memory");
  25. memset(p, 0, n);
  26. return p;
  27. }
  28. void
  29. prbanks(void)
  30. {
  31. Mbank *mbp;
  32. for(mbp = mconf.bank; mbp < &mconf.bank[mconf.nbank]; mbp++)
  33. print("bank[%ld]: base 0x%8.8lux, limit 0x%8.8lux (%.0fMB)\n",
  34. mbp - mconf.bank, mbp->base, mbp->limit,
  35. (mbp->limit - mbp->base)/(double)MB);
  36. }
  37. static void
  38. cmd_memory(int, int8_t *[])
  39. {
  40. prbanks();
  41. }
  42. enum { HWIDTH = 8 }; /* buffers per hash */
  43. /*
  44. * allocate rest of mem
  45. * for io buffers.
  46. */
  47. void
  48. iobufinit(void)
  49. {
  50. int32_t m;
  51. int i;
  52. char *xiop;
  53. Iobuf *p, *q;
  54. Hiob *hp;
  55. Mbank *mbp;
  56. wlock(&mainlock); /* init */
  57. wunlock(&mainlock);
  58. prbanks();
  59. m = 0;
  60. for(mbp = mconf.bank; mbp < &mconf.bank[mconf.nbank]; mbp++)
  61. m += mbp->limit - mbp->base;
  62. niob = m / (sizeof(Iobuf) + RBUFSIZE + sizeof(Hiob)/HWIDTH);
  63. nhiob = niob / HWIDTH;
  64. while(!prime(nhiob))
  65. nhiob++;
  66. print("\t%ld buffers; %ld hashes\n", niob, nhiob);
  67. hiob = ialloc(nhiob * sizeof(Hiob), 0);
  68. hp = hiob;
  69. for(i=0; i<nhiob; i++) {
  70. lock(hp);
  71. unlock(hp);
  72. hp++;
  73. }
  74. p = ialloc(niob * sizeof(Iobuf), 0);
  75. xiop = ialloc(niob * RBUFSIZE, 0);
  76. hp = hiob;
  77. for(i=0; i < niob; i++) {
  78. // p->name = "buf";
  79. qlock(p);
  80. qunlock(p);
  81. if(hp == hiob)
  82. hp = hiob + nhiob;
  83. hp--;
  84. q = hp->link;
  85. if(q) {
  86. p->fore = q;
  87. p->back = q->back;
  88. q->back = p;
  89. p->back->fore = p;
  90. } else {
  91. hp->link = p;
  92. p->fore = p;
  93. p->back = p;
  94. }
  95. p->dev = devnone;
  96. p->addr = -1;
  97. // p->xiobuf = ialloc(RBUFSIZE, RBUFSIZE);
  98. p->xiobuf = xiop;
  99. p->iobuf = (char*)-1;
  100. p++;
  101. xiop += RBUFSIZE;
  102. }
  103. /*
  104. * Make sure that no more of bank[0] can be used.
  105. */
  106. mconf.bank[0].base = mconf.bank[0].limit;
  107. i = 0;
  108. for(mbp = mconf.bank; mbp < &mconf.bank[mconf.nbank]; mbp++)
  109. i += mbp->limit - mbp->base;
  110. print("\tmem left = %,d, out of %,ld\n", i, conf.mem);
  111. /* paranoia: add this command as late as is easy */
  112. cmd_install("memory", "-- print ranges of memory banks", cmd_memory);
  113. }
  114. void*
  115. iobufmap(Iobuf *p)
  116. {
  117. return p->iobuf = p->xiobuf;
  118. }
  119. void
  120. iobufunmap(Iobuf *p)
  121. {
  122. p->iobuf = (char*)-1;
  123. }