mempool.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. dir->pools = mempool_create(dir->size, __early_alloc, __pool_alloc);
  53. return 0;
  54. }
  55. struct mempool *mempool_create(int size, void *(* alloc) (int size), void *(* pool_alloc) (int)) {
  56. struct mempool *pool;
  57. struct bucket_entry *e;
  58. int i;
  59. /* we don't support multiple-pools now */
  60. pool = alloc(sizeof(struct mempool));
  61. if (!pool)
  62. /* this printk is not safe, as you know */
  63. panic("mempool_create: Out of memory\n");
  64. pool->size = size;
  65. /* we'll not free this */
  66. pool->cache = pool_alloc(CACHE_SIZE);
  67. if (!pool->cache)
  68. panic("mempool_create: Out of memory\n");
  69. e = pool->freeptr = (struct bucket_entry *) pool->cache;
  70. for (i = 0; i < CACHE_SIZE / size; i++) {
  71. e->next = (struct bucket_entry *) ((char *) e + size);
  72. ++e;
  73. }
  74. pool->next_pool = NULL;
  75. pool->used = 0;
  76. return pool;
  77. }
  78. void *__mempool_alloc(struct mempool *pool) {
  79. struct bucket_entry *e = pool->freeptr;
  80. struct mempool *p = pool;
  81. if (!e) {
  82. kmlog(KERN_INFO "__mempool_alloc: creating new pool ...\n");
  83. pool->next_pool = p = mempool_create(p->size, __early_alloc, __pool_alloc);
  84. }
  85. p->freeptr = e->next;
  86. p->used++;
  87. return (void *) e;
  88. }
  89. int __mempool_free(struct mempool *pool, void *p) {
  90. struct bucket_entry *old, *e;
  91. e = (struct bucket_entry *) p;
  92. old = pool->freeptr;
  93. pool->freeptr = e;
  94. e->next = old;
  95. pool->used--;
  96. return 0;
  97. }
  98. void *kmalloc(int size) {
  99. struct mempool_dir *d;
  100. for (d = mempools; d->size; d++)
  101. if (d->size >= size)
  102. break;
  103. if (!d->size)
  104. panic("WARNING: kmalloc: size is too big\n");
  105. return __mempool_alloc(d->pools);
  106. }
  107. int free_s(void *p, int size) {
  108. struct mempool_dir *d;
  109. struct mempool *pool;
  110. void *cache = (void *) ((__archptr_t) p & ~CACHE_SIZE);
  111. for (d = mempools; d->size; d++) {
  112. if (d->size < size)
  113. continue;
  114. pool = d->pools;
  115. while (pool && pool->cache != cache)
  116. pool = pool->next_pool;
  117. if (!pool && size)
  118. panic("WARNING: free_s: object is not in any mempool\n");
  119. }
  120. if (!d->size)
  121. panic("WARNING: free_s: object is not in any mempool\n");
  122. return __mempool_free(pool, p);
  123. }
  124. int dump_pool(struct mempool_dir *d) {
  125. struct mempool *p;
  126. int total;
  127. p = d->pools;
  128. total = CACHE_SIZE / p->size;
  129. kmlog(KERN_INFO "dump_pool(%d): \t%d / %d: %d\n", d->size, p->used, total, p->used / total);
  130. return 0;
  131. }
  132. int kmalloc_report(void) {
  133. struct mempool_dir *d;
  134. for (d = mempools; d->size; d++)
  135. dump_pool(d);
  136. return 0;
  137. }