mm.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* include/kernel/mm.h
  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. #ifndef __MM_H
  19. #define __MM_H
  20. #ifndef __ASSEMBLER__
  21. #include <sys/types.h>
  22. #include <lib/klist.h>
  23. #include <arch/archinfo.h>
  24. /* a descriptor for each page to describe a page, hopely be smallest
  25. * and this is create only for the buddy memory management system. we
  26. * can't implement that without this, because we need more memory when
  27. * inserting.
  28. *
  29. * we should initiallize this as late as we can, after maps okay.
  30. * */
  31. struct page {
  32. /* for cow, the reference count. we won't free if count-- */
  33. __uint32_t count;
  34. struct page *next;
  35. };
  36. /* okay, that's may only in the x86, but... */
  37. struct zone {
  38. __uint32_t total_pages;
  39. #define ZONETYPE_RAM 1
  40. #define ZONETYPE_OTHER 0
  41. __uint32_t type;
  42. void *start_addr;
  43. struct page *pages;
  44. };
  45. /* the bucket_entry are for free objects. next_free points to next free
  46. * object. we replace the freeptr pointer in struct mempool and let that
  47. * points to old freeptr */
  48. struct bucket_entry {
  49. struct bucket_entry *next;
  50. };
  51. #define CACHE_SIZE 4096
  52. struct mempool {
  53. int size, used;
  54. void *cache;
  55. struct bucket_entry *freeptr;
  56. /* it has the same size */
  57. struct mempool *next_pool;
  58. };
  59. struct mempool_dir {
  60. int size;
  61. struct mempool *pools;
  62. };
  63. void *__mempool_alloc(struct mempool *);
  64. int __mempool_free(struct mempool *, void *);
  65. void *kmalloc(int size);
  66. int free_s(void *p, int size);
  67. int kmalloc_report(void);
  68. int dump_pool(struct mempool_dir *);
  69. void *early_kmalloc(int nbytes, int align);
  70. int early_free(void *p, int nbytes);
  71. #define free(p) early_free(p, 0)
  72. #define BUDDY_LEVELS 5
  73. extern struct klist_node buddy_pages[BUDDY_LEVELS];
  74. /* nr_ram_pages is the total */
  75. extern int nr_ram_pages, usable_ram_pages;
  76. /* sets by arch_mm_init */
  77. extern struct page *first_usable_ram,
  78. *kmem_pages;
  79. #define kmem_zones ((struct zone *) kernel_end)
  80. /* they're really early but fast. actually, most free() did nothing
  81. *
  82. * WARNING that you should't use align, because we didn't save old
  83. * freeptr and it will be cracked by it: not freeable */
  84. #endif
  85. #endif