Move our internal Callback_Registry away from a Vectors.Vector to a Hashed_Maps.Map

My previous assumptions about using a Vector as a sparse array turned
out to be incorrect. Whoopsies.
This commit is contained in:
R. Tyler Croy 2011-02-18 19:04:14 -08:00
parent 87945ca59c
commit 09345b97d5
2 changed files with 23 additions and 9 deletions

View File

@ -4,7 +4,8 @@
--
private with Ada.Text_IO;
private with Ada.Strings.Hash,
Ada.Text_IO;
use Ada.Text_IO;
@ -14,6 +15,7 @@ package body Async.Epoll is
Cb : in Callback_Tuple) is
Event : aliased Epoll_Event;
Descriptor : constant C.int := C.int (GNAT.Sockets.To_C (Cb.Socket));
Descriptor_Str : constant String := Natural'Image (Natural(Descriptor));
begin
Validate_Hub (This);
@ -35,13 +37,13 @@ package body Async.Epoll is
raise Descriptor_Registration_Falied;
end if;
This.Debug_Trace ("<Register>> Successfully added to the Epoll_Fd");
This.Debug_Trace ("<Register>> Successfully added descriptor:" &
Descriptor_Str & " to the Epoll_Fd");
end;
This.Debug_Trace ("<Register>> Inserting descriptor:" &
Natural'Image (Natural (descriptor)));
This.Debug_Trace ("<Register>> Inserting descriptor:" & Descriptor_Str);
Callback_Registry.Insert (This.Callbacks, Natural(Descriptor), Cb);
Callback_Registry.Insert (This.Callbacks, Descriptor, Cb);
end Register;
@ -71,7 +73,7 @@ package body Async.Epoll is
Event.Data.Fd;
Cb : constant Callback_Tuple :=
Callback_Registry.Element (This.Callbacks,
Natural (Descriptor));
Descriptor);
begin
Cb.Callback.all (Cb.Socket, Cb.Context);
end;
@ -120,4 +122,9 @@ package body Async.Epoll is
Put_Line (Line);
end Debug_Trace;
function Descriptor_Hash (Id : in C.int) return Hash_Type is
begin
return Ada.Strings.Hash (C.int'Image (Id));
end Descriptor_Hash;
end Async.Epoll;

View File

@ -2,7 +2,7 @@
-- Main interface for dealing with epoll(7) from Ada
--
with Ada.Containers.Vectors,
with Ada.Containers.Hashed_Maps,
Interfaces.C,
GNAT.Sockets;
@ -24,8 +24,15 @@ package Async.Epoll is
Context : Context_Type;
end record;
function Descriptor_Hash (Id : in C.int) return Hash_Type;
-- My_Callback.all (Fd);
package Callback_Registry is new Vectors (Natural, Callback_Tuple);
use Interfaces.C;
package Callback_Registry is new Hashed_Maps (Key_Type => C.int,
Element_Type => Callback_Tuple,
Hash => Descriptor_Hash,
Equivalent_Keys => "="
);
type Hub is tagged private;
@ -53,7 +60,7 @@ package Async.Epoll is
Timeout : C.int := -1;
Should_Continue : Boolean := True;
Debug : Boolean := False;
Callbacks : Callback_Registry.Vector;
Callbacks : Callback_Registry.Map;
end record;
procedure Validate_Hub (H: in Hub);