mempool.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 *p) {
  79. struct bucket_entry *e = p->freeptr;
  80. if (!e) {
  81. kmlog(KERN_INFO "__mempool_alloc: creating new pool ...\n");
  82. p = mempool_create(p->size, __early_alloc, __pool_alloc);
  83. }
  84. p->freeptr = e->next;
  85. p->used++;
  86. return (void *) e;
  87. }
  88. int __mempool_free(struct mempool *pool, void *p) {
  89. struct bucket_entry *old, *e;
  90. e = (struct bucket_entry *) p;
  91. old = pool->freeptr;
  92. pool->freeptr = e;
  93. e->next = old;
  94. pool->used--;
  95. return 0;
  96. }
  97. void *kmalloc(int size) {
  98. struct mempool_dir *d;
  99. for (d = mempools; d->size; d++)
  100. if (d->size >= size)
  101. break;
  102. if (!d->size)
  103. panic("WARNING: kmalloc: size is too big\n");
  104. return __mempool_alloc(d->pools);
  105. }
  106. int free_s(void *p, int size) {
  107. struct mempool_dir *d;
  108. void *cache = (void *) ((__archptr_t) p & ~CACHE_SIZE);
  109. for (d = mempools; d->size; d++)
  110. if (d->size >= size && d->pools->cache == cache)
  111. break;
  112. if (!d->size)
  113. panic("WARNING: free_s: object is not in any mempool\n");
  114. return __mempool_free(d->pools, p);
  115. }
  116. int dump_pool(struct mempool_dir *d) {
  117. struct mempool *p;
  118. int total;
  119. p = d->pools;
  120. total = CACHE_SIZE / p->size;
  121. kmlog(KERN_INFO "dump_pool(%d): \t%d / %d: %d\n", d->size, p->used, total, p->used / total);
  122. return 0;
  123. }
  124. int kmalloc_report(void) {
  125. struct mempool_dir *d;
  126. for (d = mempools; d->size; d++)
  127. dump_pool(d);
  128. return 0;
  129. }