From 3f37e8836883bf66e352fa16e272c4fafc9b6540 Mon Sep 17 00:00:00 2001 From: "Evgeny Grechnikov (Diamond)" Date: Thu, 25 Jan 2007 13:42:23 +0000 Subject: [PATCH] * Fixed memory leak in new_mem_resize (caused by off-by-one error). * user_alloc and user_free now update memory size information. git-svn-id: svn://kolibrios.org@294 a494cfbc-eb01-0410-851d-a64ba20cac60 --- kernel/trunk/core/heap.inc | 24 +++++++++++--- kernel/trunk/core/memory.inc | 61 ++++++++++++++++++++---------------- 2 files changed, 53 insertions(+), 32 deletions(-) diff --git a/kernel/trunk/core/heap.inc b/kernel/trunk/core/heap.inc index 9c85769263..264ca67525 100644 --- a/kernel/trunk/core/heap.inc +++ b/kernel/trunk/core/heap.inc @@ -613,6 +613,7 @@ proc init_heap mov esi, [PROC_BASE+APPDATA.mem_size+ebx] add esi, 4095 and esi, not 4095 + mov [PROC_BASE+APPDATA.mem_size+ebx], esi mov eax, HEAP_TOP mov [PROC_BASE+APPDATA.heap_base+ebx], esi mov [PROC_BASE+APPDATA.heap_top+ebx], eax @@ -664,7 +665,7 @@ l_0: shr edx, 12 mov [pages_tab+edx*4], eax -@@: +@@: or ecx, USED_BLOCK mov [pages_tab+ebx*4], ecx shr ecx, 12 @@ -676,6 +677,14 @@ l_0: dec ecx jnz @B + mov edx, [CURRENT_TASK] + shl edx, 8 + mov ebx, [alloc_size] + add ebx, 0xFFF + and ebx, not 0xFFF + add ebx, [PROC_BASE+APPDATA.mem_size+edx] + call update_mem_size + mov eax, esi add eax, 4096 sub eax, new_app_base @@ -702,6 +711,7 @@ proc user_free stdcall, base:dword test esi, esi jz .exit + xor ebx, ebx sub esi, 4096 shr esi, 12 mov eax, [pages_tab+esi*4] @@ -715,6 +725,7 @@ proc user_free stdcall, base:dword inc esi sub ecx, 4096 shr ecx, 12 + mov ebx, ecx .release: xor eax, eax xchg eax, [pages_tab+esi*4] @@ -726,10 +737,13 @@ proc user_free stdcall, base:dword dec ecx jnz .release .not_used: - mov ebx, [CURRENT_TASK] - shl ebx, 8 - mov esi, dword [ebx+PROC_BASE+APPDATA.heap_base]; heap_base - mov edi, dword [ebx+PROC_BASE+APPDATA.heap_top]; heap_top + mov edx, [CURRENT_TASK] + shl edx, 8 + mov esi, dword [edx+PROC_BASE+APPDATA.heap_base]; heap_base + mov edi, dword [edx+PROC_BASE+APPDATA.heap_top]; heap_top + sub ebx, [edx+PROC_BASE+APPDATA.mem_size] + neg ebx + call update_mem_size add esi, new_app_base add edi, new_app_base shr esi, 12 diff --git a/kernel/trunk/core/memory.inc b/kernel/trunk/core/memory.inc index 824abb0123..779e766213 100644 --- a/kernel/trunk/core/memory.inc +++ b/kernel/trunk/core/memory.inc @@ -475,33 +475,9 @@ proc new_mem_resize stdcall, new_size:dword jb @B .update_size: + mov ebx, [new_size] + call update_mem_size - mov ebx, [new_size] - mov [PROC_BASE+0x8c+edx],ebx - -;search threads and update -;application memory size infomation - mov ecx,[PROC_BASE+0xb8+edx] - mov eax,2 - -.search_threads: -;eax = current slot -;ebx = new memory size -;ecx = page directory - cmp eax,[TASK_COUNT] - jg .search_threads_end - mov edx,eax - shl edx,5 - cmp word [CURRENT_TASK+edx+0xa],9 ;if slot empty? - jz .search_threads_next - shl edx,3 - cmp [PROC_BASE+edx+0xb8],ecx ;if it is our thread? - jnz .search_threads_next - mov [PROC_BASE+edx+0x8c],ebx ;update memory size -.search_threads_next: - inc eax - jmp .search_threads -.search_threads_end: xor eax, eax dec [pg_data.pg_mutex] ret @@ -561,7 +537,7 @@ proc new_mem_resize stdcall, new_size:dword add esi, 0x1000 cmp esi, edi - jna @B + jb @B jmp .update_size .exit: @@ -571,6 +547,37 @@ proc new_mem_resize stdcall, new_size:dword ret endp +update_mem_size: +; in: edx = slot shl 8 +; ebx = new memory size +; destroys eax,ecx,edx + + mov [PROC_BASE+APPDATA.mem_size+edx],ebx +;search threads and update +;application memory size infomation + mov ecx,[PROC_BASE+APPDATA.dir_table+edx] + mov eax,2 + +.search_threads: +;eax = current slot +;ebx = new memory size +;ecx = page directory + cmp eax,[TASK_COUNT] + jg .search_threads_end + mov edx,eax + shl edx,5 + cmp word [CURRENT_TASK+edx+TASKDATA.state],9 ;if slot empty? + jz .search_threads_next + shl edx,3 + cmp [PROC_BASE+edx+APPDATA.dir_table],ecx ;if it is our thread? + jnz .search_threads_next + mov [PROC_BASE+edx+APPDATA.mem_size],ebx ;update memory size +.search_threads_next: + inc eax + jmp .search_threads +.search_threads_end: + ret + ; param ; eax= linear address ;