I am currently translating some C headers into Delphi. I am unable find a reference for converting a function pointer from C into Delphi.
typedef _JAlloc JAlloc;
struct _JAlloc {
void *(*alloc) (JAlloc *allocator, size_t size);
void (*free) (JAlloc *allocator, void *p);
void *(*realloc) (JAlloc *allocator, void *p, size_t size);
};
What will be the Delphi translation of this?
Where can I find good resources for manual conversion of C headers to Delphi (including pointer, preprocessor directives, etc.)?
Use this kind of code
type
PJAlloc = ^TJAlloc;
TJAllocAlloc = function(allocator: PJAlloc; size: integer): pointer; cdecl;
TJAllocFree = procedure(allocator: PJAlloc; p: pointer); cdecl;
TJAllocRealloc = function(allocator: PJAlloc; p: pointer; size: integer); cdecl;
TJAlloc = record
alloc: ^TJAllocAlloc;
free: ^TJAllocFree;
realloc: ^TJAllocRealloc;
end;
And change cdecl to stdcall, depending of the calling convention of your C library.
An alternative declaration (more 'pascalish' perhaps) could be:
type
TJAllocAlloc = function(var allocator: TJAlloc; size: integer): pointer; cdecl;
TJAllocFree = procedure(var allocator: TJAlloc; p: pointer); cdecl;
TJAllocRealloc = function(var allocator: TJAlloc; p: pointer; size: integer); cdecl;
TJAlloc = record
alloc: ^TJAllocAlloc;
free: ^TJAllocFree;
realloc: ^TJAllocRealloc;
end;
Dr. Bob's HeadConv utility is a good one to use for converting C declarations into Delphi, and is a good learning tool for comparing C source code to the equivalent Pascal source code.
You can find it here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With