Recent

Author Topic: compilerproc Problem by using it  (Read 457 times)

paule32

  • Hero Member
  • *****
  • Posts: 516
  • One in all. But, not all in one.
compilerproc Problem by using it
« on: May 24, 2025, 04:30:14 pm »
I have an compilerproc Procedure that the FPC Compiler spite me on the output, and I have implemented it by declaring
the signature of the procedure is in interface + implemntation part:

Code: Pascal  [Select][+][-]
  1. procedure fpc_ansistr_assign(var dest: AnsiString; const source: AnsiString); compilerproc;
  2. var
  3.   pSrc, pDest: PLongInt;
  4. begin
  5.   // Wenn Ziel gleich Quellstring ist: nichts tun
  6.   if Pointer(dest) = Pointer(source) then
  7.     Exit;
  8.  
  9.   // Referenz von Zielstring ggf. freigeben
  10.   if Pointer(dest) <> nil then
  11.   begin
  12.     pDest := PLongInt(PAnsiChar(dest) - 8); // Refcount-Adresse
  13.     Dec(pDest^);
  14.     if pDest^ = 0 then
  15.       FreeMem(pDest); // Speicher ganz freigeben
  16.   end;
  17.  
  18.   // Quelle kopieren (Pointer übernehmen)
  19.   Pointer(dest) := Pointer(source);
  20.  
  21.   // Referenzzähler erhöhen (falls nicht nil)
  22.   if Pointer(dest) <> nil then
  23.   begin
  24.     pSrc := PLongInt(PAnsiChar(dest) - 8);
  25.     Inc(pSrc^);
  26.   end;
  27. end;

the compilerproc is also defined, but I get:

Code: Bash  [Select][+][-]
  1. T:\a>fpc -dDLLEXPORT -dLANGDEU -dDLLDEBUG -n -B -FE. -O3 -Os -gw2 -CD RTLLib.pas
  2. SysUtils.pas(112,25) Fatal: Unknown compilerproc "fpc_ansistr_assign". Check if you use the correct run time library.
  3. Fatal: Compilation aborted
  4. Error: C:\fpcupdeluxe\fpc\bin\x86_64-win64\ppcx64.exe returned an error exitcode
  5.  

The Line: 12 is as follows:

S := '';

by this Source:

Code: Pascal  [Select][+][-]
  1. procedure SetLength(var S: AnsiString; NewLen: LongInt); stdcall; export;
  2. var
  3.   NewPtr: PAnsiChar;
  4.   TotalSize: LongInt;
  5. begin
  6.   if NewLen <= 0 then
  7.   begin
  8.     S := '';    // <--- there
  9.     Exit;
  10.   end;
  11.  
  12.   // 12 Bytes für RefCount, Length, Capacity
  13.   TotalSize := 12 + NewLen + 1; // +1 für null-Terminator
  14.  
  15.   GetMem(NewPtr, TotalSize);
  16.  
  17.   // Initialisiere Header
  18.   PLongInt(NewPtr)^ := 1;                        // RefCount
  19.   PLongInt(NewPtr + 4)^ := NewLen;              // Length
  20.   PLongInt(NewPtr + 8)^ := NewLen;              // Capacity
  21.  
  22.   // Null-Terminierung (nicht zwingend nötig für AnsiString, aber sicherer)
  23.   (NewPtr + 12 + NewLen)^ := #0;
  24.  
  25.   // Setze Stringpointer (S zeigt auf Daten, nicht auf Header!)
  26.   Pointer(S) := NewPtr + 12;
  27. end;
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12269
  • FPC developer.
Re: compilerproc Problem by using it
« Reply #1 on: May 24, 2025, 07:14:58 pm »
Compileprocs probably need to be in the system unit.

paule32

  • Hero Member
  • *****
  • Posts: 516
  • One in all. But, not all in one.
Re: compilerproc Problem by using it
« Reply #2 on: May 24, 2025, 07:53:37 pm »
The following code is in system.pas - and it is implemented in interface part of pascal source code (signature)...

Code: Pascal  [Select][+][-]
  1. procedure fpc_ansistr_decr_ref(var s: AnsiString); [public, alias: 'FPC_ANSISTR_DECR_REF']; compilerproc;
  2. var
  3.   rec: PAnsiRec;
  4. begin
  5.   if s = nil then
  6.     Exit;
  7.  
  8.   // Der Header steht 12 Byte vor den String-Daten
  9.   rec := PAnsiRec(s - SizeOf(TAnsiRec));
  10.  
  11.   // Referenzzähler verringern
  12.   Dec(rec^.ref);
  13.  
  14.   // Wenn keine Referenzen mehr, Speicher freigeben
  15.   if rec^.ref <= 0 then
  16.   begin
  17.     // Speicher freigeben
  18.     FreeMem(rec);
  19.   end;
  20. end;

but the Function is not found by the compiler
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

paule32

  • Hero Member
  • *****
  • Posts: 516
  • One in all. But, not all in one.
Re: compilerproc Problem by using it
« Reply #3 on: May 25, 2025, 10:09:21 am »
Thanks @marcov
I have back port the source.
now, it works as before.

good thing on versioning software like git is: you can get back prior versions of source code.
I don't know how long backup's are stored on the Microsoft Servers.
But I thinking in mind, that is a Range of 90 days.
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

 

TinyPortal © 2005-2018