; _______________________________________________________________________________________
;|                                                                                       |
;| ..::FreshLib::..  Free, open source. Licensed under "BSD 2-clause" license."          |
;|_______________________________________________________________________________________|
;
;  Description: Dynamic memory management library. OS dependent part.
;
;  Target OS: Dummy
;
;  Dependencies:
;
;  Notes:
;
;_________________________________________________________________________________________


MAX_HEAP_SIZE = $60000000

uglobal
  var __ptrHeapStart = ?
  var __HeapMaxSize  = ?
endg


if used mem.alloc_heap

initialize InitMenuetHeap
begin
        mov     eax, sys_f68
        mov     ebx, 11
        int     $40

        mov     eax, sys_f68
        mov     ebx, 12
        mov     ecx, MAX_HEAP_SIZE          ; reserve the address space
        int     $40
        test    eax, eax
        jz      .error
        mov     [__ptrHeapStart], eax
        return

.error:
        int3
endp
end if


proc mem.alloc_heap ;, .size
begin
        mov     eax, [__ptrHeapStart]
        test    eax, eax
        jz      .error
        clc
        retn 4

.error:
        stc
        retn 4
endp




proc mem.free_heap, .block, .size
begin
        push    eax ebx ecx
        mov     eax, sys_f68
        mov     ebx, 13
        mov     ecx, [.block]
        int     $40
        clc
        pop     ecx ebx eax
        return
endp



proc mem.realloc_heap, .heapptr, .newsize, .oldsize
begin
        push    eax ebx ecx edx

        mov     eax, sys_f68
        mov     ebx, 20
        mov     ecx, [.newsize]
        mov     edx, [__ptrHeapStart]
        int     $40

        cmp     eax, [__ptrHeapStart]
        jne     .error

        mov     eax, sys_f68
        mov     ebx, 20
        mov     ecx, MAX_HEAP_SIZE
        mov     edx, [__ptrHeapStart]
        int     $40

        cmp     eax, [__ptrHeapStart]
        jne     .error

        pop     edx ecx ebx eax
        return

.error:
        int3
endp
