Re: Выделение памяти
Posted: Sat Oct 22, 2016 9:41 pm
Все правильно. free application memory. Свободная память приложения. У каждого процесса 2Гб юзерспейса. Соответственно максимальный размер кучи почти 2Гб.
Code: Select all
Unit Unit1;
(* -------------------------------------------------------- *)
Interface
(* -------------------------------------------------------- *)
Type
Dword = Cardinal;
Var
hConsole: Pointer;
ConsoleInit: Procedure(WndWidth, WndHeight, ScrWidth, ScrHeight: Dword; Caption: PChar); StdCall;
ConsoleExit: Procedure(bCloseWindow: Cardinal); StdCall;
printf: Function(Const Format: PChar): Integer; CDecl VarArgs;
getch: Function: Char; StdCall;
(* -------------------------------------------------------- *)
Procedure Main; Forward;
Procedure ThreadTerminate; Forward;
Function LoadLibrary(Path: PChar): Pointer; StdCall; Forward;
Function GetProcAddress(hLib: Pointer; ProcName: PChar): Pointer; StdCall; Forward;
Function HeapCreate: Cardinal; Forward;
Function HeapAllocate(Bytes: Cardinal): Pointer; StdCall; Forward;
Function HeapFree(MemPtr: Pointer): Cardinal; StdCall; Forward;
Function GetFreeMemory: Dword; Forward;
Function GetAvailableMemory: Dword; Forward;
(* -------------------------------------------------------- *)
Implementation
(* -------------------------------------------------------- *)
Procedure Main();
Var
i: Cardinal;
HeapSize: Dword;
FreeMemory: Dword;
AvailableMemory: Dword;
MemPtr: PChar;
Begin
hConsole := LoadLibrary('/sys/lib/console.obj');
ConsoleInit := GetProcAddress(hConsole, 'con_init');
ConsoleExit := GetProcAddress(hConsole, 'con_exit');
printf := GetProcAddress(hConsole, 'con_printf');
getch := GetProcAddress(hConsole, 'con_getch');
ConsoleInit($FFFFFFFF, $FFFFFFFF, $FFFFFFFF, $FFFFFFFF, 'Test');
FreeMemory := GetFreeMemory();
AvailableMemory := GetAvailableMemory();
HeapSize := HeapCreate();
printf('Free memory = %d kilobytes.'#10, FreeMemory);
printf('Available memory = %d kilobytes.'#10, AvailableMemory);
If HeapSize = 0 Then
printf('HeapCreate Error!'#10)
Else Begin
printf('Heap created. HeapSize = %d bytes.'#10, HeapSize);
MemPtr := HeapAllocate(FreeMemory * 1024);
If MemPtr = nil Then
printf('HeapAllocate Error!'#10)
Else
printf('Allocated %d kilobytes. Pointer to memory = %#x'#10, FreeMemory, MemPtr);
getch();
i := 0;
While i < (FreeMemory * 1024) Do Begin
MemPtr[i] := #0;
Inc(i, 4096);
printf('i = %d'#10, i);
End;
printf('Free memory = %d kilobytes.'#10, GetFreeMemory());
printf('Available memory = %d kilobytes.'#10, GetAvailableMemory());
getch();
If HeapFree(MemPtr) = 0 Then
printf('HeapFree Error!'#10)
Else
printf('Memory freed.'#10);
End;
ConsoleExit(0);
ThreadTerminate;
End;
(* -------------------------------------------------------- *)
Function HeapCreate: Cardinal;
Asm
push ebx
mov eax, 68
mov ebx, 11
int 64
pop ebx
End;
(* -------------------------------------------------------- *)
Function HeapAllocate(Bytes: Cardinal): Pointer;
Asm
push ebx
mov eax, 68
mov ebx, 12
mov ecx, Bytes
int 64
pop ebx
End;
(* -------------------------------------------------------- *)
Function HeapFree(MemPtr: Pointer): Cardinal;
Asm
push ebx
mov eax, 68
mov ebx, 13
mov ecx, MemPtr
int 64
pop ebx
End;
(* -------------------------------------------------------- *)
Function GetFreeMemory: Dword;
Asm
push ebx
mov eax, 18
mov ebx, 16
int 64
pop ebx
End;
(* -------------------------------------------------------- *)
Function GetAvailableMemory: Dword;
Asm
push ebx
mov eax, 18
mov ebx, 17
int 64
pop ebx
End;
(* -------------------------------------------------------- *)
Procedure ThreadTerminate();
Asm
mov eax, $FFFFFFFF
int 64
End;
(* -------------------------------------------------------- *)
Function GetProcAddress(hLib: Pointer; ProcName: PChar): Pointer;
Asm
push esi
push edi
push ebx
mov edx, hLib
xor eax, eax
test edx, edx
jz @end
mov edi, ProcName
mov ecx, $FFFFFFFF
repne scasb
mov ebx, ecx
not ebx
@next:
mov esi, [edx]
test esi, esi
jz @end
mov ecx, ebx
mov edi, ProcName
add edx, 8
repe cmpsb
jne @next
mov eax, [edx - 4]
@end:
pop ebx
pop edi
pop esi
End;
(* -------------------------------------------------------- *)
Function LoadLibrary(Path: PChar): Pointer;
Asm
push ebx
mov eax, 68
mov ebx, 19
mov ecx, Path
int 64
pop ebx
End;
(* -------------------------------------------------------- *)
End.
Потому что это виртуальная память. 18.6 количество свободной физ. памяти на данный момент. 18.7 общее кол-во физической памяти.А тогда SysFn18.16 и SysFn18.17 что возвращают? Размер физической? Просто получается, что размер свободной памяти приложения больше, чем размер всей имеющейся памяти.
Code: Select all
ORG 0
BITS 32
STACK_SIZE equ 256
MEMORY_ALLOC_SIZE equ 1024 * 1024 * 128
db 'MENUET01'
dd 1
dd START
dd END
dd END + STACK_SIZE
dd END + STACK_SIZE
dd 0
dd 0
START:
; HeapCreate
mov eax, 68
mov ebx, 11
int 64
; HeapAllocate
mov eax, 68
mov ebx, 12
mov ecx, MEMORY_ALLOC_SIZE
int 64
mov esi, eax
mov ecx, MEMORY_ALLOC_SIZE / 4
rep lodsd
.loop:
jmp .loop
END:
Ничего подобного. И с чего ты только это взялSerge wrote:CPU показывает кол-во памяти после загрузки приложения.
А с другими приложениями, значит, по-другому? Как у нас ф.9 избирательно работает, оказываетсяSerge wrote:Так по скриншоту получается.
А, точно! Восьмёрка очень сильно на нолик похожаSerge wrote:CPU всё показывает правильно
Это всё равно будет убрано. Если автор того кода не удосужится это сделать, то я сам лично это сделаю. Так как это тупо деоптимизация.Leency « Вт сен 12, 2017 1:22 pm » напомните, все еще нужно инициализировать кучу или это делается автоматически?