mempool.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 <sys/types.h>
  23. /* early memory pools are for klist nodes because most early_free cannot
  24. * release memory.
  25. *
  26. * of course you can use them for some other structures such as struct task
  27. * etc. */
  28. static struct mempool *mempool_create(int size, void *(*) (int), void *(*) (int));
  29. static void *__early_alloc(int size) {
  30. return early_kmalloc(size, 1);
  31. }
  32. static void *__pool_alloc(int size) {
  33. return early_kmalloc(size, CACHE_SIZE);
  34. }
  35. struct mempool_dir mempools[] = {
  36. /* you can join a structure in here too */
  37. { 16, NULL },
  38. { 32, NULL },
  39. { 64, NULL },
  40. { 128, NULL },
  41. { 256, NULL },
  42. { 512, NULL },
  43. { 1024, NULL },
  44. { 2048, NULL },
  45. { 4096, NULL },
  46. { 0, NULL }
  47. };
  48. int early_mempool_init(void) {
  49. struct mempool_dir *dir;
  50. for (dir = mempools; dir->size; dir++)
  51. dir->pools = mempool_create(dir->size, __early_alloc, __pool_alloc);
  52. return 0;
  53. }
  54. struct mempool *mempool_create(int size, void *(* alloc) (int size), void *(* pool_alloc) (int)) {
  55. struct mempool *pool;
  56. struct bucket_entry *e;
  57. int i;
  58. /* we don't support multiple-pools now */
  59. pool = alloc(sizeof(struct mempool));
  60. if (!pool) {
  61. /* this printk is not safe, as you know */
  62. printk("mempool_create: Out of memory\n");
  63. return NULL;
  64. }
  65. pool->size = size;
  66. /* we'll not free this */
  67. pool->cache = pool_alloc(CACHE_SIZE);
  68. if (!pool->cache) {
  69. printk("mempool_create: Out of memory\n");
  70. return NULL;
  71. }
  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. printk("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. printk("WARNING: kmalloc: size is too big\n");
  107. return NULL;
  108. }
  109. return __mempool_alloc(d->pools);
  110. }
  111. int free_s(void *p, int size) {
  112. struct mempool_dir *d;
  113. void *cache = (void *) ((__archptr_t) p & ~CACHE_SIZE);
  114. for (d = mempools; d->size; d++)
  115. if (d->size >= size && d->pools->cache == cache)
  116. break;
  117. if (!d->size) {
  118. printk("WARNING: free_s: object is not in any mempool\n");
  119. return -1;
  120. }
  121. return __mempool_free(d->pools, p);
  122. }
  123. int dump_pool(struct mempool_dir *d) {
  124. struct mempool *p;
  125. int total;
  126. p = d->pools;
  127. total = CACHE_SIZE / p->size;
  128. printk("dump_pool(%d): \t%d / %d: %d%%\n", d->size, p->used, total, p->used * 100 / total);
  129. return 0;
  130. }
  131. int kmalloc_report(void) {
  132. struct mempool_dir *d;
  133. for (d = mempools; d->size; d++)
  134. dump_pool(d);
  135. return 0;
  136. }