summaryrefslogtreecommitdiff
path: root/packages/amunits/src/utilunits/pastoc.pas
blob: e6fd71535ae780090662459eda439cd1a80a13de (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
{
    This file is part of the Free Pascal run time library.

    A file in Amiga system run time library.
    Copyright (c) 2000-2003 by Nils Sjoholm
    member of the Amiga RTL development team.

    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.

 **********************************************************************}
{
    History:

    Added the define use_amiga_smartlink.
    13 Jan 2003.

    nils.sjoholm@mailbox.swipnet.se Nils Sjoholm
}

{
  This unit must be deprecated because at least:
  - It is leaking memory. It allocates a new buffer for each string which won't
    be freed until the program exits.
  - The unit doesn't provide any way to free allocated string buffers manually.
    (Because ReleasePas2C is not a public function.)
  - It does allocations outside the Pascal heap, which the compiler has no control
    over, and makes it very hard to track these allocations, because the heaptrc
    unit doesn't work.
  - The intuition.library documentation states that AllocRemember() is a quite
    ineffective function, because it does two memory allocations and because it
    doesn't use memory pools it has a terrible effect on memory fragmentation.
  - It uses a for loop byte to copy the string contents, which is very slow.
  - It uses a global handle without any protection, therefore it's not thread safe.
  - The strings unit provide equivalent functionality, without the leaking problem.
  - Because of the above reasons, this unit will be removed as soon as nothing
    else in the AmUnits package and among the examples depend on it.
    (KB)
}

unit PasToC
  deprecated 'Pas2C function is leaking memory, don''t use it. StrPCopy in strings unit provides equivalent functionality.';

interface

function Pas2C( s : String): PChar;

implementation

const
   MEMF_ANY      = %000000000000000000000000;   { * Any type of memory will do * }
   MEMF_PUBLIC   = %000000000000000000000001;

   MEMF_CLEAR    = %000000010000000000000000;

Type

    ULONG = Longint;

    pRemember = ^tRemember;
    tRemember = record
        NextRemember    : pRemember;
        RememberSize    : ULONG;
        Memory          : Pointer;
    end;

var
    myrememberkey : pRemember;
    remember_exit : pointer;

FUNCTION fpcAllocRemember(VAR rememberKey : pRemember location 'a0'; size : ULONG location 'd0'; flags : ULONG location 'd1') : POINTER; syscall _IntuitionBase 396;
PROCEDURE fpcFreeRemember(VAR rememberKey : pRemember location 'a0'; reallyForget : LONGINT location 'd0'); syscall _IntuitionBase 408;

Function StringPcharCopy(Dest: PChar; Source: String):PChar;
var
   counter : byte;
Begin
   counter := 0;
  { if empty pascal string  }
  { then setup and exit now }
  if Source = '' then
  Begin
    Dest[0] := #0;
    StringPCharCopy := Dest;
    exit;
  end;
  for counter:=1 to length(Source) do
  begin
    Dest[counter-1] := Source[counter];
  end;
  { terminate the string }
  Dest[counter] := #0;
  StringPcharCopy:=Dest;
end;

function Pas2C(s : string): PChar;
var
    themem : Pointer;
begin
    themem := fpcAllocRemember(myrememberkey,length(s)+1, MEMF_CLEAR or MEMF_PUBLIC);
    if themem = nil then begin
        writeln('Can''t allocate memory for string');
        halt(20);
    end else begin
        StringPCharCopy(themem,s);
        Pas2C := themem;
    end;
end;

procedure ReleasePasToC;
begin
    ExitProc := remember_exit;
    fpcFreeRemember(myrememberkey,1);
end;

begin
    myrememberkey := nil;
    remember_exit := ExitProc;
    ExitProc := @ReleasePasToC;
end.