@@ -17,13 +17,20 @@ u32 free_mem_addr = HEAP_START;
1717static mem_block_t * free_blocks = NULL ;
1818u32 heap_current_end = HEAP_START + HEAP_SIZE ;
1919
20+ static meminfo_t stats ;
21+
2022// инициализация кучи
2123void heap_init () {
2224 free_blocks = (mem_block_t * )HEAP_START ;
2325 free_blocks -> size = HEAP_SIZE - sizeof (mem_block_t );
2426 free_blocks -> next = NULL ;
2527 free_blocks -> is_free = 1 ;
2628 heap_current_end = HEAP_START + HEAP_SIZE ;
29+
30+ stats .alloc_count = 0 ;
31+ stats .free_count = 0 ;
32+ stats .max_used = 0 ;
33+ stats .leak_count = 0 ;
2734}
2835
2936int expand_heap (u32 size ) {
@@ -98,6 +105,8 @@ void* kmalloc(u32 size) {
98105 guard_end [i ] = MAGIC_NUMBER ;
99106 }
100107
108+ stats .alloc_count ++ ;
109+
101110 return user_ptr ;
102111 }
103112
@@ -185,6 +194,8 @@ void kfree(void* ptr) {
185194
186195 block -> is_free = 1 ;
187196
197+ stats .free_count ++ ;
198+
188199 if (block -> next && block -> next -> is_free ) {
189200 block -> size += sizeof (mem_block_t ) + block -> next -> size ;
190201 block -> next = block -> next -> next ;
@@ -208,49 +219,55 @@ void kfree(void* ptr) {
208219}
209220
210221meminfo_t get_meminfo () {
211- meminfo_t meminfo ;
212222 mem_block_t * current = free_blocks ;
213223 u32 total_used = 0 ;
214224 u32 total_free = 0 ;
215225 u32 block_count = 0 ;
226+ u32 used_blocks = 0 ;
216227
217228 while (current ) {
218229 block_count ++ ;
219230 if (current -> is_free ) {
220231 total_free += current -> size ;
221232 } else {
222233 total_used += current -> size ;
234+ used_blocks ++ ;
223235 }
224236 current = current -> next ;
225237 }
226238
227- meminfo .heap_start = HEAP_START ;
228- meminfo .heap_size = heap_current_end - HEAP_START ;
229- meminfo .heap_current_end = heap_current_end ;
230- meminfo .block_size = BLOCK_SIZE ;
231- meminfo .free_blocks = free_blocks ;
232- meminfo .total_used = total_used ;
233- meminfo .total_free = total_free ;
234- meminfo .block_count = block_count ;
239+ stats .total_used = total_used ;
240+ stats .total_free = total_free ;
241+ stats .block_count = block_count ;
242+ stats .leak_count = used_blocks ;
243+
244+ stats .heap_start = HEAP_START ;
245+ stats .heap_size = heap_current_end - HEAP_START ;
246+ stats .heap_current_end = heap_current_end ;
247+ stats .block_size = BLOCK_SIZE ;
248+ stats .free_blocks = free_blocks ;
235249
236- return meminfo ;
250+ return stats ;
237251}
238252
239253void kmemdump () {
240254 meminfo_t info = get_meminfo ();
241255 mem_block_t * current = info .free_blocks ;
242256 u32 counter = 0 ;
243257
258+ printf ("\nHeap: 0x%x - 0x%x (%d bytes)\n" ,
259+ HEAP_START ,
260+ info .heap_current_end ,
261+ info .heap_current_end - HEAP_START );
262+ printf ("Block size: %d bytes, Guard: %d bytes\n" , info .block_size , GUARD_SIZE );
263+ printf (
264+ "Allocations: %d, Frees: %d, Leaks: %d blocks\n" , info .alloc_count , info .free_count , info .leak_count );
244265 printf (
245- "Heap: %x - %x (%d bytes)\n" , HEAP_START , info .heap_current_end , info .heap_current_end - HEAP_START );
246- printf ("Block size: %d bytes\n" , info .block_size );
247- printf ("Total: USED=%d bytes, FREE=%d bytes, in %d blocks\n" ,
248- info .total_used ,
249- info .total_free ,
250- info .block_count );
266+ "Max used: %d bytes, Current: USED=%d, FREE=%d\n" , info .max_used , info .total_used , info .total_free );
267+ printf ("Total blocks: %d\n" , info .block_count );
251268
252269 while (current ) {
253- printf ("Block %d: %x, Size=%d, %s\n " ,
270+ printf ("Block %d: 0x %x, Size=%d, %s" ,
254271 counter ++ ,
255272 (u32 )current ,
256273 current -> size ,
0 commit comments