summaryrefslogtreecommitdiff
path: root/rtl/inc/osheap.inc
blob: 7d356234a6f97ed40bfebe897bbfe43c97b06cdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
{
    This file is part of the Free Pascal run time library.
    Copyright (c) 2021 by the Free Pascal development team.

    OS heap manager for small targets

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}

{ 
  The OS heap manager is a small heap manager for smaller targets with an
  operating system. It's similar in comcept to the "cmem" memory manager
  for systems with libc support, but it aims systems that have a direct
  heap management API (Sinclair QL, AmigaOS, MacOS Classic, some
  embedded systems, etc), but not necessarily libc. It's also designed
  to be included in the System unit directly, unlike cmem.

  The main advantage is that it's much smaller than the regular heap
  manager, and it's a thinner layer towards the OS as well, therefore it
  can be slightly faster on resource constrained systems. 

  The disadvantage is that it may not be well suited for bigger Pascal
  programs that rely heavily on consistent heap performance, as the
  properties of the OS heap manager like alignment and size constraints,
  and allocation time are also directly exposed towards the Pascal code.
  Additionally, querying the heap status is not supported.

  Its main difference to tinyheap is that it can release the allocated
  memory back to the system, therefore it suits systems that can run more
  than one software in parallel better.

  OSHeap needs SysOSAlloc and SysOSFree implemented in the system-specific
  code, and nothing else.
}

    function SysGetMem(Size: ptruint): pointer;
      begin
        Inc(size,sizeof(size));
        result := SysOSAlloc(size);
        if assigned(result) then
          begin
            pptruint(result)[0] := size;
            Inc(result,sizeof(size));
          end;
      end;

    function SysFreeMem(p: Pointer): ptruint;
      begin
        if assigned(p) then
          begin
            Dec(p,sizeof(ptruint));
            SysOSFree(p,pptruint(p)[0]);
          end;
        result := 0;
      end;

    function SysFreeMemSize(p: Pointer; Size: Ptruint): ptruint;
      begin
        result := SysFreeMem(p);
      end;

    function SysMemSize(p: pointer): ptruint;
      begin
        Dec(p,sizeof(ptruint));
        result:=pptruint(p)[0]-sizeof(ptruint);
      end;

    function SysTryResizeMem(var p: pointer; size: ptruint) : boolean;
      begin
        result := false;
      end;

    function SysAllocMem(size: ptruint): pointer;
      begin
        result := SysGetMem(size);
        if not assigned(result) then
          FillChar(result^,SysMemSize(result),0);
      end;

    function SysReAllocMem(var p: pointer; size: ptruint):pointer;
      var
        oldsize: ptruint;
      begin
        result := nil;
        if assigned(p) then
          begin
            oldsize := SysMemSize(p);
            if size <> oldsize then
              begin
                if size > 0 then
                  begin
                    result := SysGetMem(size);
                    if assigned(result) then
                      begin
                        if size < oldsize then
                          oldsize := size;
                        Move(p^,result^,oldsize);
                      end;
                  end;
                SysFreeMem(p);
              end
            else
              result := p;
          end
        else
          result := SysGetMem(size);

        p := result;
      end;

    function SysGetFPCHeapStatus : TFPCHeapStatus;
      begin
        FillChar(result,sizeof(result),0);
      end;

    function SysGetHeapStatus : THeapStatus;
      begin
        FillChar(result,sizeof(result),0);
      end;