mempool.c 4.0 KB

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