-
Notifications
You must be signed in to change notification settings - Fork 2
/
enet_callbacks.pas
91 lines (67 loc) · 1.91 KB
/
enet_callbacks.pas
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
unit enet_callbacks;
(**
@file callbacks.c
@brief ENet callback functions
1.3.12 freepascal
- fix callback pointer nil check
*)
interface
uses enet_consts, enet_socket;
function enet_initialize_with_callbacks (version : integer; inits : pENetCallbacks):integer;
function enet_malloc (size : enet_size_t):pointer;
procedure enet_free (memory : pointer);
function enet_linked_version:enet_uint32;
implementation
uses sysutils;
var
callbacks : ENetCallbacks;
(**
Gives the linked version of the ENet library.
@returns the version number
*)
function enet_linked_version:enet_uint32;
begin
Result:=ENET_VERSION;
end;
function enet_initialize_with_callbacks (version : integer; inits : pENetCallbacks):integer;
begin
if (version < $00103000) then
begin result := -1; exit; end;
if (inits^ . malloc <> nil) or (inits^ . free <> nil) then
begin
if (inits^ . malloc = nil) or (inits^ . free = nil) then
begin result := -1; exit; end;
callbacks.malloc := ENETCALLBACK_malloc(inits^. malloc);
callbacks.free := ENETCALLBACK_free(inits ^. free);
end;
if (inits^ . nomemory <> nil) then
callbacks.nomemory := ENETCALLBACK_nomemory(inits^ . nomemory);
result := enet_initialize;
end;
function enet_malloc (size : enet_size_t):pointer;
begin
result := callbacks.malloc (size);
if (result = nil) then
callbacks.nomemory;
end;
procedure enet_free (memory : pointer);
begin
callbacks.free (memory);
end;
function __malloc(size : enet_size_t):pointer; stdcall;
begin
getmem(result,size);
end;
procedure __free(memory : pointer); stdcall;
begin
freemem(memory);
end;
procedure __nomemory; stdcall;
begin
raise Exception.Create('Out of Memory');
end;
initialization
callbacks.malloc := @__malloc;
callbacks.free := @__free;
callbacks.nomemory := @__nomemory;
end.