Subversion Repositories Kolibri OS

Rev

Rev 589 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. CHECK_FOR_LEAKS = 0
  2. if CHECK_FOR_LEAKS
  3. uglobal
  4. allocatedregions rd 1024
  5. endg
  6. iglobal
  7. numallocatedregions dd 0
  8. endg
  9. end if
  10. pgalloc:
  11. ; in: ecx=size
  12. ; out: eax=pointer or NULL
  13.         push    ebx
  14.         push    68
  15.         pop     eax
  16.         push    12
  17.         pop     ebx
  18.         int     0x40
  19. if CHECK_FOR_LEAKS
  20.         test    eax, eax
  21.         jz      .no
  22. .b:
  23.         mov     ebx, [numallocatedregions]
  24.         cmp     ebx, 1024
  25.         jb      @f
  26.         int3
  27.         jmp     $
  28. @@:
  29.         mov     [allocatedregions+ebx*4], eax
  30.         inc     [numallocatedregions]
  31. .no:
  32. end if
  33.         pop     ebx
  34.         ret
  35.  
  36. pgfree:
  37. ; in: ecx=pointer
  38. ; destroys eax
  39. if CHECK_FOR_LEAKS
  40.         jecxz   .no
  41.         mov     eax, [numallocatedregions]
  42. @@:
  43.         dec     eax
  44.         js      .a
  45.         cmp     [allocatedregions+eax*4], ecx
  46.         jnz     @b
  47.         jmp     @f
  48. .a:
  49.         int3
  50.         jmp     $
  51. @@:
  52.         dec     [numallocatedregions]
  53. @@:
  54.         cmp     eax, [numallocatedregions]
  55.         jae     @f
  56.         push    [allocatedregions+eax*4+4]
  57.         pop     [allocatedregions+eax*4]
  58.         inc     eax
  59.         jmp     @b
  60. @@:
  61. .no:
  62. end if
  63.         push    ebx
  64.         push    68
  65.         pop     eax
  66.         push    13
  67.         pop     ebx
  68.         int     0x40
  69.         pop     ebx
  70.         ret
  71.  
  72. pgrealloc:
  73. ; in: ecx=size, edx=pointer
  74. ; out: eax=pointer
  75.         push    ebx
  76.         push    68
  77.         pop     eax
  78.         push    20
  79.         pop     ebx
  80.         int     0x40
  81. if CHECK_FOR_LEAKS
  82.         test    edx, edx
  83.         jz      pgalloc.b
  84.         test    eax, eax
  85.         jz      .no
  86.         cmp     eax, edx
  87.         jz      .no
  88.         xor     ebx, ebx
  89. @@:
  90.         cmp     ebx, [numallocatedregions]
  91.         jae     .a
  92.         cmp     [allocatedregions+ebx*4], edx
  93.         jz      @f
  94.         inc     ebx
  95.         jmp     @b
  96. @@:
  97.         mov     [allocatedregions+ebx*4], eax
  98.         jmp     .no
  99. .a:
  100.         int3
  101.         jmp     $
  102. .no:
  103. end if
  104.         pop     ebx
  105.         ret
  106.  
  107. xpgalloc:
  108. ; in: ecx=size
  109. ; out: eax=pointer or NULL
  110.         call    pgalloc
  111. .common:
  112.         test    eax, eax
  113.         jnz     @f
  114.         call    SayNoMem
  115.         xor     eax, eax
  116. @@:
  117.         ret
  118.  
  119. xpgrealloc:
  120. ; in: edx=pointer, ecx=new size
  121. ; out: eax=pointer or NULL
  122.         call    pgrealloc
  123.         jmp     xpgalloc.common
  124.  
  125. getfreemem:
  126. ; out: eax=size of free RAM in Kb
  127.         push    ebx
  128.         push    18
  129.         pop     eax
  130.         push    16
  131.         pop     ebx
  132.         int     0x40
  133.         pop     ebx
  134.         ret
  135.  
  136. get_error_msg:
  137. ; in: eax=error code
  138. ; out: eax=pointer to message (in static buffer)
  139.         push    esi edi
  140.         mov     edi, error_msg
  141.         cmp     eax, 11
  142.         ja      .no1
  143.         mov     esi, [errors1+eax*4]
  144.         jmp     .copy
  145. .no1:
  146.         cmp     eax, 30
  147.         jb      .no2
  148.         cmp     eax, 32
  149.         ja      .no2
  150.         mov     esi, [errors2+(eax-30)*4]
  151. .copy:
  152.         lodsb
  153.         stosb
  154.         test    al, al
  155.         jnz     .copy
  156. .ret:
  157.         mov     eax, error_msg
  158.         pop     edi esi
  159.         ret
  160. .no2:
  161.         mov     esi, aUnknownError
  162.         push    eax
  163. @@:
  164.         lodsb
  165.         stosb
  166.         test    al, al
  167.         jnz     @b
  168.         pop     eax
  169.         dec     edi
  170.         push    edx ecx
  171.         push    -'0'
  172.         test    eax, eax
  173.         jns     @f
  174.         mov     byte [edi], '-'
  175.         inc     edi
  176.         neg     eax
  177. @@:
  178.         xor     edx, edx
  179.         mov     ecx, 10
  180.         div     ecx
  181.         push    edx
  182.         test    eax, eax
  183.         jnz     @b
  184. @@:
  185.         pop     eax
  186.         add     al, '0'
  187.         stosb
  188.         jnz     @b
  189.         pop     ecx edx
  190.         jmp     .ret
  191.  
  192. libini_alloc:
  193.         push    ecx
  194.         mov     ecx, [esp+8]
  195.         call    xpgalloc
  196.         pop     ecx
  197.         ret     4
  198. libini_free:
  199.         push    ecx
  200.         mov     ecx, [esp+8]
  201.         call    pgfree
  202.         pop     ecx
  203.         ret     4
  204. libini_realloc:
  205.         push    ecx edx
  206.         mov     edx, [esp+8+4]
  207.         mov     ecx, [esp+8+8]
  208.         call    xpgrealloc
  209.         pop     edx ecx
  210.         ret     8
  211.  
  212. libini_dllload:
  213.         push    esi
  214.         mov     esi, [esp+8]
  215. .lib:
  216.         lodsd
  217.         test    eax, eax
  218.         jz      .ret
  219.         push    eax
  220.         lodsd
  221.         xchg    esi, [esp]
  222.         xor     ebp, ebp        ; no version control
  223.         call    load_dll_and_import
  224.         pop     esi
  225.         test    eax, eax
  226.         jz      .lib
  227. .ret:
  228.         pop     esi
  229.         ret     4
  230.  
  231. load_dll_and_import:
  232.         cmp     byte [eax], '/'
  233.         jz      .do
  234.         push    esi
  235.         mov     edi, saved_file_name
  236.         push    edi
  237.         mov     esi, standard_dll_path
  238.         mov     ecx, standard_dll_path_size
  239.         rep     movsb
  240.         mov     esi, eax
  241.         mov     ecx, 1024-standard_dll_path_size
  242. @@:
  243.         lodsb
  244.         stosb
  245.         test    al, al
  246.         loopnz  @b
  247.         pop     eax
  248.         pop     esi
  249.         jz      .do
  250. .big:
  251.         push    esi
  252.         mov     edi, aFileNameTooBig
  253. .sayerr:
  254.         push    dword aCannotLoadDLL
  255.         push    edi
  256.         mov     eax, esp
  257.         push    dword aOk
  258.         push    esp
  259.         push    1
  260.         push    eax
  261.         push    3
  262.         call    SayErr
  263.         add     esp, 16
  264.         xor     eax, eax
  265.         inc     eax
  266.         ret
  267. .do:
  268.         push    eax
  269.         mov     ecx, eax
  270.         push    68
  271.         pop     eax
  272.         push    19
  273.         pop     ebx
  274.         int     0x40
  275.         mov     edi, aInvalidDLL
  276.         test    eax, eax
  277.         jz      .sayerr
  278.         mov     edx, eax
  279.         cmp     ebp, -1
  280.         jnz     @f
  281.         pop     eax
  282.         xor     eax, eax
  283.         ret
  284. @@:
  285. ; initialize import
  286.         mov     edi, aMissingExport
  287. .import_loop:
  288.         lodsd
  289.         test    eax, eax
  290.         jz      .import_done
  291.         call    .find_exported_function
  292.         jc      .sayerr
  293.         mov     [esi-4], eax
  294.         jmp     .import_loop
  295. .import_done:
  296. ; check version
  297.         test    ebp, ebp
  298.         jz      .version_ok
  299.         mov     edi, aIncompatibleVersion
  300.         mov     eax, aVersion
  301.         call    .find_exported_function
  302.         jc      .sayerr
  303.         cmp     ax, bp
  304.         jb      .sayerr
  305.         shr     eax, 16
  306.         cmp     eax, ebp
  307.         ja      .sayerr
  308. .version_ok:
  309. ; initialize library
  310.         mov     eax, aStart
  311.         call    .find_exported_function
  312.         jc      @f
  313.         push    1       ; DLL_ENTRY
  314.         call    eax
  315. .ret0:
  316.         pop     eax
  317.         xor     eax, eax
  318.         ret
  319. @@:
  320.         mov     eax, aLibInit
  321.         call    .find_exported_function
  322.         jc      .ret0
  323.         mov     esi, eax
  324.         mov     eax, libini_alloc
  325.         mov     ebx, libini_free
  326.         mov     ecx, libini_realloc
  327.         mov     edx, libini_dllload
  328.         call    esi
  329.         mov     edi, aInitFailed
  330.         test    eax, eax
  331.         jnz     .sayerr
  332.         jmp     .ret0
  333.  
  334. .find_exported_function:
  335.         push    edx
  336. .import_loop_i:
  337.         mov     ebx, [edx]
  338.         test    ebx, ebx
  339.         jz      .import_notfound
  340.         push    eax
  341. @@:
  342.         mov     cl, [eax]
  343.         cmp     cl, [ebx]
  344.         jnz     .import_find_next
  345.         test    cl, cl
  346.         jz      .import_found
  347.         inc     eax
  348.         inc     ebx
  349.         jmp     @b
  350. .import_find_next:
  351.         pop     eax
  352.         add     edx, 8
  353.         jmp     .import_loop_i
  354. .import_found:
  355.         pop     eax
  356.         mov     eax, [edx+4]
  357.         pop     edx
  358.         ret
  359. .import_notfound:
  360.         pop     edx
  361.         stc
  362.         ret
  363.