Add an Epoll spec and some C interop code

This should help with resolving #2 and support running with Epoll
This commit is contained in:
R. Tyler Croy 2018-11-27 21:00:36 -08:00
parent 59139820c4
commit 5696474dd9
No known key found for this signature in database
GPG Key ID: E5C92681BEF6CEA2
3 changed files with 81 additions and 0 deletions

View File

@ -12,5 +12,7 @@ project Mozzoni is
for Switches ("ada") use ("-gnato", "-gnat12", "-gnatf", "-fstack-check", "-g");
end Compiler;
for Languages use ("Ada", "C");
end Mozzoni;

72
src/epoll.ads Normal file
View File

@ -0,0 +1,72 @@
with GNAT.Sockets,
Interfaces.C,
System;
package Epoll is
subtype Epoll_Fd_Type is Integer;
type Epoll_Events_Type is (Epoll_In,
Epoll_Pri,
Epoll_Out,
Epoll_Et,
Epoll_In_And_Et
);
for Epoll_Events_Type use (Epoll_In => 1,
Epoll_Pri => 2,
Epoll_Out => 4,
Epoll_Et => 2147483648,
Epoll_In_And_Et => 2147483649);
for Epoll_Events_Type'Size use Interfaces.C.int'Size;
type Epoll_Ctl_Type is (Epoll_Ctl_Add,
Epoll_Ctl_Del,
Epoll_Ctl_Mod);
for Epoll_Ctl_Type use (Epoll_Ctl_Add => 1,
Epoll_Ctl_Del => 2,
Epoll_Ctl_Mod => 3);
type Data_Type (Discriminant : Interfaces.C.unsigned := 0) is record
case Discriminant is
when 0 =>
Ptr : System.Address;
when 1 =>
FD : aliased GNAT.Sockets.Socket_Type; -- Interfaces.C.int
when 2 =>
U32 : aliased Interfaces.Unsigned_32;
when others =>
U64 : aliased Interfaces.Unsigned_64;
end case;
end record;
pragma Convention (C_Pass_By_Copy, Data_Type);
pragma Unchecked_Union (Data_Type);
type Event_Type is record
Events : aliased Epoll_Events_Type;
Data : Data_Type;
end record;
pragma Convention (C_Pass_By_Copy, Event_Type);
type Event_Array_Type is array (Integer range <>) of aliased Event_Type;
pragma Convention (C, Event_Array_Type);
function Create (Size : Integer) return Epoll_Fd_Type;
pragma Import (C, Create, "epoll_create");
function Control (Epfd : Epoll_Fd_Type;
Op : Epoll_Ctl_Type;
Fd : Epoll_Fd_Type;
Events : access Event_Type) return Integer;
pragma Import (C, Control, "epoll_ctl");
function Wait (Epfd : Epoll_Fd_Type;
Events : access Event_Type;
Max_Events : Integer;
Timeout : Integer) return Integer;
pragma Import (C, Wait, "epoll_wait");
end Epoll;

7
src/interop.c Normal file
View File

@ -0,0 +1,7 @@
#include <errno.h>
/*
* errno is a macro on most modern GNU/libc's which means that we cannot
* Import it like other bindings from Ada
*/
int fetch_errno() { return errno; }