kmalloc.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Really really *really* Q&D malloc() and free() implementations
  3. * just to get going. Don't ever let anyone see this shit. :^)
  4. */
  5. #include "types.h"
  6. #include "kmalloc.h"
  7. #include "StdLib.h"
  8. #include "i386.h"
  9. #include "system.h"
  10. #include "Process.h"
  11. #include "Scheduler.h"
  12. #include <AK/Assertions.h>
  13. #define SANITIZE_KMALLOC
  14. typedef struct
  15. {
  16. dword start;
  17. dword nchunk;
  18. } PACKED allocation_t;
  19. #define CHUNK_SIZE 128
  20. #define POOL_SIZE (1024 * 1024)
  21. #define ETERNAL_BASE_PHYSICAL 0x100000
  22. #define ETERNAL_RANGE_SIZE 0x100000
  23. #define BASE_PHYSICAL 0x200000
  24. #define RANGE_SIZE 0x100000
  25. static byte alloc_map[POOL_SIZE / CHUNK_SIZE / 8];
  26. volatile size_t sum_alloc = 0;
  27. volatile size_t sum_free = POOL_SIZE;
  28. volatile size_t kmalloc_sum_eternal = 0;
  29. static byte* s_next_eternal_ptr;
  30. static byte* s_end_of_eternal_range;
  31. bool is_kmalloc_address(void* ptr)
  32. {
  33. if (ptr >= (byte*)ETERNAL_BASE_PHYSICAL && ptr < s_next_eternal_ptr)
  34. return true;
  35. return (dword)ptr >= BASE_PHYSICAL && (dword)ptr <= (BASE_PHYSICAL + POOL_SIZE);
  36. }
  37. void kmalloc_init()
  38. {
  39. memset(&alloc_map, 0, sizeof(alloc_map));
  40. memset((void *)BASE_PHYSICAL, 0, POOL_SIZE);
  41. kmalloc_sum_eternal = 0;
  42. sum_alloc = 0;
  43. sum_free = POOL_SIZE;
  44. s_next_eternal_ptr = (byte*)ETERNAL_BASE_PHYSICAL;
  45. s_end_of_eternal_range = s_next_eternal_ptr + ETERNAL_RANGE_SIZE;
  46. }
  47. void* kmalloc_eternal(size_t size)
  48. {
  49. void* ptr = s_next_eternal_ptr;
  50. s_next_eternal_ptr += size;
  51. ASSERT(s_next_eternal_ptr < s_end_of_eternal_range);
  52. kmalloc_sum_eternal += size;
  53. return ptr;
  54. }
  55. void* kmalloc_aligned(size_t size, size_t alignment)
  56. {
  57. void* ptr = kmalloc(size + alignment + sizeof(void*));
  58. dword max_addr = (dword)ptr + alignment;
  59. void* aligned_ptr = (void*)(max_addr - (max_addr % alignment));
  60. ((void**)aligned_ptr)[-1] = ptr;
  61. return aligned_ptr;
  62. }
  63. void kfree_aligned(void* ptr)
  64. {
  65. kfree(((void**)ptr)[-1]);
  66. }
  67. void* kmalloc_page_aligned(size_t size)
  68. {
  69. void* ptr = kmalloc_aligned(size, PAGE_SIZE);
  70. dword d = (dword)ptr;
  71. ASSERT((d & PAGE_MASK) == d);
  72. return ptr;
  73. }
  74. void* kmalloc_impl(dword size)
  75. {
  76. InterruptDisabler disabler;
  77. dword chunks_needed, chunks_here, first_chunk;
  78. dword real_size;
  79. dword i, j, k;
  80. /* We need space for the allocation_t structure at the head of the block. */
  81. real_size = size + sizeof(allocation_t);
  82. if (sum_free < real_size) {
  83. kprintf("%s<%u> kmalloc(): PANIC! Out of memory (sucks, dude)\nsum_free=%u, real_size=%x\n", current->name().characters(), current->pid(), sum_free, real_size);
  84. HANG;
  85. return 0L;
  86. }
  87. chunks_needed = real_size / CHUNK_SIZE;
  88. if( real_size % CHUNK_SIZE )
  89. chunks_needed++;
  90. chunks_here = 0;
  91. first_chunk = 0;
  92. for( i = 0; i < (POOL_SIZE / CHUNK_SIZE / 8); ++i )
  93. {
  94. if (alloc_map[i] == 0xff) {
  95. // Skip over completely full bucket.
  96. chunks_here = 0;
  97. continue;
  98. }
  99. // FIXME: This scan can be optimized further with LZCNT.
  100. for( j = 0; j < 8; ++j )
  101. {
  102. if( !(alloc_map[i] & (1<<j)) )
  103. {
  104. if( chunks_here == 0 )
  105. {
  106. /* Mark where potential allocation starts. */
  107. first_chunk = i * 8 + j;
  108. }
  109. chunks_here++;
  110. if( chunks_here == chunks_needed )
  111. {
  112. auto* a = (allocation_t *)(BASE_PHYSICAL + (first_chunk * CHUNK_SIZE));
  113. byte *ptr = (byte *)a;
  114. ptr += sizeof(allocation_t);
  115. a->nchunk = chunks_needed;
  116. a->start = first_chunk;
  117. for( k = first_chunk; k < (first_chunk + chunks_needed); ++k )
  118. {
  119. alloc_map[k / 8] |= 1 << (k % 8);
  120. }
  121. sum_alloc += a->nchunk * CHUNK_SIZE;
  122. sum_free -= a->nchunk * CHUNK_SIZE;
  123. #ifdef SANITIZE_KMALLOC
  124. memset(ptr, 0xbb, (a->nchunk * CHUNK_SIZE) - sizeof(allocation_t));
  125. #endif
  126. return ptr;
  127. }
  128. }
  129. else
  130. {
  131. /* This is in use, so restart chunks_here counter. */
  132. chunks_here = 0;
  133. }
  134. }
  135. }
  136. kprintf("%s<%u> kmalloc(): PANIC! Out of memory (no suitable block for size %u)\n", current->name().characters(), current->pid(), size);
  137. HANG;
  138. return nullptr;
  139. }
  140. void kfree(void *ptr)
  141. {
  142. if( !ptr )
  143. return;
  144. InterruptDisabler disabler;
  145. allocation_t *a = (allocation_t *)((((byte *)ptr) - sizeof(allocation_t)));
  146. #if 0
  147. dword hdr = (dword)a;
  148. dword mhdr = hdr & ~0x7;
  149. kprintf("hdr / mhdr %p / %p\n", hdr, mhdr);
  150. ASSERT(hdr == mhdr);
  151. #endif
  152. for (dword k = a->start; k < (a->start + a->nchunk); ++k) {
  153. alloc_map[k / 8] &= ~(1 << (k % 8));
  154. }
  155. sum_alloc -= a->nchunk * CHUNK_SIZE;
  156. sum_free += a->nchunk * CHUNK_SIZE;
  157. #ifdef SANITIZE_KMALLOC
  158. memset(a, 0xaa, a->nchunk * CHUNK_SIZE);
  159. #endif
  160. }
  161. void* operator new(size_t size)
  162. {
  163. return kmalloc(size);
  164. }
  165. void* operator new[](size_t size)
  166. {
  167. return kmalloc(size);
  168. }
  169. void operator delete(void* ptr)
  170. {
  171. return kfree(ptr);
  172. }
  173. void operator delete[](void* ptr)
  174. {
  175. return kfree(ptr);
  176. }
  177. void operator delete(void* ptr, unsigned int)
  178. {
  179. return kfree(ptr);
  180. }
  181. void operator delete[](void* ptr, unsigned int)
  182. {
  183. return kfree(ptr);
  184. }