heap.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * heap.c
  3. *
  4. * Copyright (C) 2013 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <heap.h>
  20. #include <common.h>
  21. #include <exception.h>
  22. #include <memory.h>
  23. void *__crt_heap_realloc(__crt_heap_t *heap, void *ptr, size_t alignment, size_t size);
  24. extern __crt_heap_t *__crt_default_heap;
  25. heap_t system_heap;
  26. heap_t evictable_heap;
  27. static void heap_problem(int problem)
  28. {
  29. switch (problem)
  30. {
  31. case __CRT_HEAP_CORRUPTED:
  32. KERNEL_CRASH("Heap corrupted");
  33. case __CRT_HEAP_DOUBLE_FREE:
  34. KERNEL_CRASH("Attempt to free an already released region");
  35. case __CRT_HEAP_BAD_POINTER:
  36. KERNEL_CRASH("Bad pointer passed to heap function");
  37. }
  38. }
  39. void *heap_realloc(heap_t *heap, void *ptr, uintptr_t size)
  40. {
  41. return __crt_heap_realloc(&heap->crt, ptr, sizeof(long), size);
  42. }
  43. void *heap_alloc(heap_t *heap, uintptr_t size)
  44. {
  45. return __crt_heap_realloc(&heap->crt, NULL, sizeof(long), size);
  46. }
  47. void heap_free(heap_t *heap, void *ptr)
  48. {
  49. __crt_heap_realloc(&heap->crt, ptr, sizeof(long), 0);
  50. }
  51. dword_t heap_create(heap_t *heap, uintptr_t start, uintptr_t end, dword_t flags, dword_t magic)
  52. {
  53. start = PAGE_ALIGN(start);
  54. end = PAGE_ALIGN(end);
  55. ASSERT(start < end);
  56. heap->crt.magic = magic;
  57. heap->crt.base = (void*)start;
  58. heap->crt.size = end - start;
  59. heap->crt.next_offset = 0;
  60. heap->crt.flags = 0;
  61. heap->crt.mutex = (void*)&heap->lock;
  62. heap->crt.problem = heap_problem;
  63. heap->crt.lock_mutex_proc = (void (*)(void*))&lock_acquire;
  64. heap->crt.unlock_mutex_proc = (void (*)(void*))&lock_release;
  65. lock_init(&heap->lock);
  66. if (flags & HEAP_ZEROFILL) heap->crt.flags |= __CRT_HEAP_FLAG_ZEROFILL;
  67. dword_t block_flags = MEMORY_BLOCK_ACCESSIBLE | MEMORY_BLOCK_WRITABLE;
  68. if (flags & HEAP_EVICTABLE) block_flags |= MEMORY_BLOCK_EVICTABLE;
  69. void *address = alloc_pool(heap->crt.base, heap->crt.size, block_flags);
  70. ASSERT(address == heap->crt.base);
  71. return ERR_SUCCESS;
  72. }
  73. dword_t heap_destroy(heap_t *heap)
  74. {
  75. free_pool(heap->crt.base);
  76. return ERR_SUCCESS;
  77. }
  78. void heap_init(void)
  79. {
  80. heap_create(&system_heap, SYSTEM_HEAP_START, SYSTEM_HEAP_END, 0, SYSTEM_HEAP_MAGIC);
  81. heap_create(&evictable_heap, EVICTABLE_HEAP_START, EVICTABLE_HEAP_END, HEAP_EVICTABLE, EVICTABLE_HEAP_MAGIC);
  82. __crt_default_heap = &system_heap.crt;
  83. }