mempool.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* kernel/mempool.c
  2. *
  3. * This file is part of evx.
  4. *
  5. * evx is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * evx is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with evx. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <kernel/init.h>
  19. #include <kernel/module.h>
  20. #include <kernel/mm.h>
  21. #include <kernel/kernel.h>
  22. #include <kernel/kmlog.h>
  23. #include <sys/types.h>
  24. /* early memory pools are for klist nodes because most early_free cannot
  25. * release memory.
  26. *
  27. * of course you can use them for some other structures such as struct task
  28. * etc. */
  29. static struct mempool *mempool_create(int size, void *(*) (int), void *(*) (int));
  30. static void *__early_alloc(int size) {
  31. return early_kmalloc(size, 1);
  32. }
  33. static void *__pool_alloc(int size) {
  34. return early_kmalloc(size, CACHE_SIZE);
  35. }
  36. struct mempool_dir mempools[] = {
  37. /* you can join a structure in here too */
  38. { 16, NULL },
  39. { 32, NULL },
  40. { 64, NULL },
  41. { 128, NULL },
  42. { 256, NULL },
  43. { 512, NULL },
  44. { 1024, NULL },
  45. { 2048, NULL },
  46. { 4096, NULL },
  47. { 0, NULL }
  48. };
  49. int early_mempool_init(void) {
  50. struct mempool_dir *dir;
  51. for (dir = mempools; dir->size; dir++) {
  52. kmlog(KERN_DEBUG "creating mempool%d ...\n", dir - mempools);
  53. dir->pools = mempool_create(dir->size, __early_alloc, __pool_alloc);
  54. }
  55. kmalloc_report();
  56. return 0;
  57. }
  58. struct mempool *mempool_create(int size, void *(* alloc) (int size), void *(* pool_alloc) (int)) {
  59. struct mempool *pool;
  60. struct bucket_entry *e;
  61. int i;
  62. /* we don't support multiple-pools now */
  63. pool = alloc(sizeof(struct mempool));
  64. if (!pool)
  65. /* this printk is not safe, as you know */
  66. panic("mempool_create: Out of memory\n");
  67. pool->size = size;
  68. /* we'll not free this */
  69. pool->cache = pool_alloc(CACHE_SIZE);
  70. if (!pool->cache)
  71. panic("mempool_create: Out of memory\n");
  72. e = pool->freeptr = (struct bucket_entry *) pool->cache;
  73. for (i = 0; i < CACHE_SIZE / size; i++) {
  74. e->next = e + 1;
  75. ++e;
  76. }
  77. pool->next_pool = NULL;
  78. pool->used = 0;
  79. return pool;
  80. }
  81. void *__mempool_alloc(struct mempool *p) {
  82. struct bucket_entry *e = p->freeptr;
  83. if (!e) {
  84. kmlog("WARNING: __mempool_alloc: no objects left\n");
  85. return NULL;
  86. }
  87. p->freeptr = e->next;
  88. p->used++;
  89. return (void *) e;
  90. }
  91. int __mempool_free(struct mempool *pool, void *p) {
  92. struct bucket_entry *old, *e;
  93. e = (struct bucket_entry *) p;
  94. old = pool->freeptr;
  95. pool->freeptr = e;
  96. e->next = old;
  97. pool->used--;
  98. return 0;
  99. }
  100. void *kmalloc(int size) {
  101. struct mempool_dir *d;
  102. for (d = mempools; d->size; d++)
  103. if (d->size >= size)
  104. break;
  105. if (!d->size)
  106. panic("WARNING: kmalloc: size is too big\n");
  107. return __mempool_alloc(d->pools);
  108. }
  109. int free_s(void *p, int size) {
  110. struct mempool_dir *d;
  111. void *cache = (void *) ((__archptr_t) p & ~CACHE_SIZE);
  112. for (d = mempools; d->size; d++)
  113. if (d->size >= size && d->pools->cache == cache)
  114. break;
  115. if (!d->size)
  116. panic("WARNING: free_s: object is not in any mempool\n");
  117. return __mempool_free(d->pools, p);
  118. }
  119. int dump_pool(struct mempool_dir *d) {
  120. struct mempool *p;
  121. int total;
  122. p = d->pools;
  123. total = CACHE_SIZE / p->size;
  124. kmlog(KERN_INFO "dump_pool(%d): \t%d / %d: %d\n", d->size, p->used, total, p->used / total);
  125. return 0;
  126. }
  127. int kmalloc_report(void) {
  128. struct mempool_dir *d;
  129. for (d = mempools; d->size; d++)
  130. dump_pool(d);
  131. return 0;
  132. }