@@ -43,6 +43,20 @@ void heap_init() {
4343 kprint ("\n" );
4444}
4545
46+ // TODO: Paging is not implemented
47+ void * get_physaddr (void * virtualaddr ) {
48+ unsigned long pdindex = (unsigned long )virtualaddr >> 22 ;
49+ unsigned long ptindex = (unsigned long )virtualaddr >> 12 & 0x03FF ;
50+
51+ unsigned long * pd = (unsigned long * )0xFFFFF000 ;
52+ // Here you need to check whether the PD entry is present.
53+
54+ unsigned long * pt = ((unsigned long * )0xFFC00000 ) + (0x400 * pdindex );
55+ // Here you need to check whether the PT entry is present.
56+
57+ return (void * )((pt [ptindex ] & ~0xFFF ) + ((unsigned long )virtualaddr & 0xFFF ));
58+ }
59+
4660void * kmalloc_new (u32 size ) {
4761 // Выравниваем размер до границы BLOCK_SIZE
4862 if (size % BLOCK_SIZE != 0 ) {
@@ -76,13 +90,13 @@ void* kmalloc_new(u32 size) {
7690 }
7791
7892 kprint ("No free blocks found\n" );
79- return NULL ; // Не нашли свободного блока
93+ return NULL ;
8094}
8195
8296void kfree (void * ptr ) {
8397 if (!ptr ) return ;
8498
85- // Получаем указатель на заголовок блока
99+ // получаем указатель на заголовок блока
86100 mem_block_t * block = (mem_block_t * )((u32 )ptr - sizeof (mem_block_t ));
87101
88102 if (block -> is_free ) { // блок уже освобожден
@@ -92,13 +106,14 @@ void kfree(void* ptr) {
92106
93107 block -> is_free = 1 ;
94108
95- // Попробуем объединить с соседними свободными блоками
109+ /* попробуем объединить с соседними свободными блоками*/
96110 mem_block_t * current = free_blocks ;
97111 while (current ) {
98112 if (current -> is_free && current -> next && current -> next -> is_free ) {
99- // Объединяем текущий блок со следующим
113+ // соединим текущий блок со следующим
100114 current -> size += sizeof (mem_block_t ) + current -> next -> size ;
101115 current -> next = current -> next -> next ;
116+ free_mem_addr -= current -> size ;
102117 }
103118 current = current -> next ;
104119 }
@@ -112,8 +127,6 @@ meminfo_t get_meminfo() {
112127 u32 total_free = 0 ;
113128 u32 block_count = 0 ;
114129
115- kprint ("Memory dump:\n" );
116-
117130 while (current ) {
118131 block_count ++ ;
119132
@@ -137,46 +150,19 @@ meminfo_t get_meminfo() {
137150 return meminfo ;
138151}
139152
140- void kmemdump () { // дамп памяти
141- mem_block_t * current = free_blocks ;
142- u32 total_used = 0 ;
143- u32 total_free = 0 ;
144- u32 block_count = 0 ;
153+ void kmemdump () {
154+ meminfo_t info = get_meminfo ();
155+ mem_block_t * current = info .free_blocks ;
156+ u32 counter = 0 ;
145157
146- kprint ("Memory dump:\n" );
158+ kprintf ("Heap: %x - %x (%d bytes)\n" , info .heap_start , info .heap_start + info .heap_size , info .heap_size );
159+ kprintf ("Block size: %d bytes\n" , info .block_size );
160+ kprintf ("Total: USED=%d bytes, FREE=%d bytes, in %d blocks\n\n" , info .total_used , info .total_free , info .block_count );
147161
148162 while (current ) {
149- kprint ("Block " );
150- char buf [32 ];
151- int_to_ascii (block_count ++ , buf );
152- kprint (buf );
153- kprint (": Addr=" );
154- hex_to_ascii ((u32 )current , buf );
155- kprint (buf );
156- kprint (", Size=" );
157- int_to_ascii (current -> size , buf );
158- kprint (buf );
159- kprint (", " );
160- kprint (current -> is_free ? "FREE" : "USED" );
161- kprint ("\n" );
162-
163- if (current -> is_free ) {
164- total_free += current -> size ;
165- } else {
166- total_used += current -> size ;
167- }
168-
163+ kprintf ("Block %d: %x, Size=%d, %s\n" , counter ++ , (u32 )current , current -> size , current -> is_free ? "FREE" : "USED" );
169164 current = current -> next ;
170165 }
171-
172- kprint ("Total: USED=" );
173- char buf2 [32 ];
174- int_to_ascii (total_used , buf2 );
175- kprint (buf2 );
176- kprint (", FREE=" );
177- int_to_ascii (total_free , buf2 );
178- kprint (buf2 );
179- kprint ("\n" );
180166}
181167
182168// LEGACY Arena
0 commit comments