Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C function pointer translation into Delphi/Pascal?

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);  
};
  1. What will be the Delphi translation of this?

  2. Where can I find good resources for manual conversion of C headers to Delphi (including pointer, preprocessor directives, etc.)?

like image 585
Ramnish Avatar asked Jan 20 '11 10:01

Ramnish


Video Answer


2 Answers

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;
like image 178
Arnaud Bouchez Avatar answered Sep 28 '22 07:09

Arnaud Bouchez


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

like image 45
Tim Young - Elevate Software Avatar answered Sep 28 '22 08:09

Tim Young - Elevate Software