winheap.inc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1998 by the Free Pascal development team.
  5. Win32 Memory Functions
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. { memory functions }
  13. function GlobalAlloc(mode,size:longint):longint;
  14. external 'kernel32' name 'GlobalAlloc';
  15. function GlobalHandle(p:pointer):longint;
  16. external 'kernel32' name 'GlobalHandle';
  17. function GlobalLock(handle:longint):pointer;
  18. external 'kernel32' name 'GlobalLock';
  19. function GlobalUnlock(h:longint):longint;
  20. external 'kernel32' name 'GlobalUnlock';
  21. function GlobalFree(h:longint):longint;
  22. external 'kernel32' name 'GlobalFree';
  23. procedure GlobalMemoryStatus(p:pointer);
  24. external 'kernel32' name 'GlobalMemoryStatus';
  25. type
  26. errproc=function(size:longint):integer;
  27. procedure MemError(size:longint);
  28. const
  29. message:pchar='Abnormal Termination';
  30. caption:pchar='Memory Management Error!';
  31. var
  32. res:integer;
  33. begin
  34. repeat
  35. res:=errproc(heaperror)(size);
  36. if res=0 then
  37. begin;
  38. messagebox(0,caption,message,$10);
  39. halt(getlasterror);
  40. end;
  41. until res<>2;
  42. end;
  43. procedure getmem(var p:pointer;size:longint);[public,alias: 'GETMEM'];
  44. begin
  45. p:=GlobalLock(GlobalAlloc($102,size));
  46. if p=nil then
  47. memerror(size)
  48. end;
  49. procedure freemem(var p:pointer;size:longint);[public,alias: 'FREEMEM'];
  50. var
  51. h:longint;
  52. begin
  53. h:=GlobalHandle(p);
  54. if (h<>0) and (globalunlock(h)=0) and (GlobalFree(h)=0) then
  55. begin
  56. p:=nil;
  57. exit;
  58. end;
  59. p:=nil;
  60. memerror(size);
  61. end;
  62. function memmax(_maxavail:boolean):longint;
  63. const
  64. status:record
  65. dwLength,
  66. dwMemoryLoad,
  67. dwTotalPhys,
  68. dwAvailPhys,
  69. dwTotalPageFile,
  70. dwAvailPageFile,
  71. dwTotalVirtual,
  72. dwAvailVirtual:longint;
  73. end=(dwLength:32);
  74. begin
  75. GlobalMemoryStatus(@status);
  76. if _maxavail then
  77. memmax:=status.dwAvailPageFile
  78. else
  79. memmax:=status.dwAvailVirtual;
  80. end;
  81. function memavail:longint;
  82. begin
  83. memavail:=memmax(false);
  84. end;
  85. function maxavail:longint;
  86. begin
  87. maxavail:=memmax(true);
  88. end;
  89. function HeapSize:longint;
  90. begin
  91. HeapSize:=memmax(true);
  92. end;
  93. function growheap(size:longint):integer;
  94. begin
  95. growheap:=0;
  96. end;
  97. {
  98. $Log$
  99. Revision 1.4 1998-07-01 15:30:03 peter
  100. * better readln/writeln
  101. Revision 1.3 1998/06/10 10:39:19 peter
  102. * working w32 rtl
  103. }