kmalloc.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 "VGA.h"
  10. #include "system.h"
  11. #include "Assertions.h"
  12. #define SANITIZE_KMALLOC
  13. typedef struct
  14. {
  15. DWORD start;
  16. DWORD nchunk;
  17. } PACKED allocation_t;
  18. #define CHUNK_SIZE 128
  19. #define POOL_SIZE (1024 * 1024)
  20. #define PAGE_ALIGNED_BASE_PHYSICAL 0x300000
  21. #define ETERNAL_BASE_PHYSICAL 0x200000
  22. #define BASE_PHYS 0x100000
  23. #define RANGE_SIZE 0x100000
  24. PRIVATE BYTE alloc_map[POOL_SIZE / CHUNK_SIZE / 8];
  25. volatile DWORD sum_alloc = 0;
  26. volatile DWORD sum_free = POOL_SIZE;
  27. volatile size_t kmalloc_sum_eternal = 0;
  28. volatile size_t kmalloc_sum_page_aligned = 0;
  29. static byte* s_next_eternal_ptr;
  30. static byte* s_next_page_aligned_ptr;
  31. static byte* s_end_of_eternal_range;
  32. static byte* s_end_of_page_aligned_range;
  33. bool is_kmalloc_address(void* ptr)
  34. {
  35. if (ptr >= (byte*)ETERNAL_BASE_PHYSICAL && ptr < s_next_eternal_ptr)
  36. return true;
  37. if (ptr >= (byte*)PAGE_ALIGNED_BASE_PHYSICAL && ptr < s_next_page_aligned_ptr)
  38. return true;
  39. return ptr >= (void*)BASE_PHYS && ptr <= ((void*)BASE_PHYS + POOL_SIZE);
  40. }
  41. PUBLIC void
  42. kmalloc_init()
  43. {
  44. memset( &alloc_map, 0, sizeof(alloc_map) );
  45. memset( (void *)BASE_PHYS, 0, POOL_SIZE );
  46. kmalloc_sum_eternal = 0;
  47. kmalloc_sum_page_aligned = 0;
  48. sum_alloc = 0;
  49. sum_free = POOL_SIZE;
  50. s_next_eternal_ptr = (byte*)ETERNAL_BASE_PHYSICAL;
  51. s_next_page_aligned_ptr = (byte*)PAGE_ALIGNED_BASE_PHYSICAL;
  52. s_end_of_eternal_range = s_next_eternal_ptr + RANGE_SIZE;
  53. s_end_of_page_aligned_range = s_next_page_aligned_ptr + RANGE_SIZE;
  54. }
  55. void* kmalloc_eternal(size_t size)
  56. {
  57. void* ptr = s_next_eternal_ptr;
  58. s_next_eternal_ptr += size;
  59. ASSERT(s_next_eternal_ptr < s_end_of_eternal_range);
  60. kmalloc_sum_eternal += size;
  61. return ptr;
  62. }
  63. void* kmalloc_page_aligned(size_t size)
  64. {
  65. ASSERT((size % PAGE_SIZE) == 0);
  66. void* ptr = s_next_page_aligned_ptr;
  67. s_next_page_aligned_ptr += size;
  68. ASSERT(s_next_page_aligned_ptr < s_end_of_page_aligned_range);
  69. kmalloc_sum_page_aligned += size;
  70. return ptr;
  71. }
  72. PUBLIC void *
  73. kmalloc( DWORD size )
  74. {
  75. InterruptDisabler disabler;
  76. DWORD chunks_needed, chunks_here, first_chunk;
  77. DWORD real_size;
  78. DWORD i, j, k;
  79. /* We need space for the allocation_t structure at the head of the block. */
  80. real_size = size + sizeof(allocation_t);
  81. if (sum_free < real_size) {
  82. kprintf("kmalloc(): PANIC! Out of memory (sucks, dude)\nsum_free=%u, real_size=%x\n", sum_free, real_size);
  83. HANG;
  84. return 0L;
  85. }
  86. chunks_needed = real_size / CHUNK_SIZE;
  87. if( real_size % CHUNK_SIZE )
  88. chunks_needed++;
  89. chunks_here = 0;
  90. first_chunk = 0;
  91. for( i = 0; i < (POOL_SIZE / CHUNK_SIZE / 8); ++i )
  92. {
  93. for( j = 0; j < 8; ++j )
  94. {
  95. if( !(alloc_map[i] & (1<<j)) )
  96. {
  97. if( chunks_here == 0 )
  98. {
  99. /* Mark where potential allocation starts. */
  100. first_chunk = i * 8 + j;
  101. }
  102. chunks_here++;
  103. if( chunks_here == chunks_needed )
  104. {
  105. auto* a = (allocation_t *)(BASE_PHYS + (first_chunk * CHUNK_SIZE));
  106. BYTE *ptr = (BYTE *)a;
  107. ptr += sizeof(allocation_t);
  108. a->nchunk = chunks_needed;
  109. a->start = first_chunk;
  110. for( k = first_chunk; k < (first_chunk + chunks_needed); ++k )
  111. {
  112. alloc_map[k / 8] |= 1 << (k % 8);
  113. }
  114. sum_alloc += a->nchunk * CHUNK_SIZE;
  115. sum_free -= a->nchunk * CHUNK_SIZE;
  116. #ifdef SANITIZE_KMALLOC
  117. memset(ptr, 0xbb, (a->nchunk * CHUNK_SIZE) - sizeof(allocation_t));
  118. #endif
  119. return ptr;
  120. }
  121. }
  122. else
  123. {
  124. /* This is in use, so restart chunks_here counter. */
  125. chunks_here = 0;
  126. }
  127. }
  128. }
  129. kprintf("kmalloc(): PANIC! Out of memory (no suitable block for size %u)\n", size);
  130. HANG;
  131. return nullptr;
  132. }
  133. PUBLIC void
  134. kfree( void *ptr )
  135. {
  136. if( !ptr )
  137. return;
  138. InterruptDisabler disabler;
  139. allocation_t *a = (allocation_t *)((((BYTE *)ptr) - sizeof(allocation_t)));
  140. #if 0
  141. DWORD hdr = (DWORD)a;
  142. DWORD mhdr = hdr & ~0x7;
  143. kprintf("hdr / mhdr %p / %p\n", hdr, mhdr);
  144. ASSERT(hdr == mhdr);
  145. #endif
  146. for (DWORD k = a->start; k < (a->start + a->nchunk); ++k) {
  147. alloc_map[k / 8] &= ~(1 << (k % 8));
  148. }
  149. sum_alloc -= a->nchunk * CHUNK_SIZE;
  150. sum_free += a->nchunk * CHUNK_SIZE;
  151. #ifdef SANITIZE_KMALLOC
  152. memset(a, 0xaa, a->nchunk * CHUNK_SIZE);
  153. #endif
  154. }
  155. void* operator new(unsigned int size)
  156. {
  157. return kmalloc(size);
  158. }
  159. void* operator new[](unsigned int size)
  160. {
  161. return kmalloc(size);
  162. }
  163. void operator delete(void* ptr)
  164. {
  165. return kfree(ptr);
  166. }
  167. void operator delete[](void* ptr)
  168. {
  169. return kfree(ptr);
  170. }
  171. void operator delete(void* ptr, unsigned int)
  172. {
  173. return kfree(ptr);
  174. }
  175. void operator delete[](void* ptr, unsigned int)
  176. {
  177. return kfree(ptr);
  178. }