2021-08-23 23:47:29 +00:00
|
|
|
with Ada.Unchecked_Deallocation;
|
|
|
|
|
|
|
|
package body H3.MM is
|
|
|
|
procedure Create (R: in out Ref_Counted) is
|
|
|
|
begin
|
|
|
|
Finalize (R);
|
|
|
|
R.Data := new Ref_Counted_Record;
|
2021-08-24 16:07:29 +00:00
|
|
|
R.Data.Ref_Count := 1;
|
|
|
|
--System.Atomic_Counters.Initialize (R.Data.Ref_Count); -- initialize to 1
|
2021-08-23 23:47:29 +00:00
|
|
|
end Create;
|
|
|
|
|
|
|
|
procedure Create (R: in out Ref_Counted; V: in Item_Type) is
|
|
|
|
begin
|
|
|
|
Create (R);
|
|
|
|
R.Data.Item := V;
|
|
|
|
end Create;
|
|
|
|
|
|
|
|
function Get_Item_Pointer (R: in out Ref_Counted) return Item_Pointer is
|
|
|
|
begin
|
|
|
|
if R.Data /= null then
|
|
|
|
return R.Data.Item'Access;
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
end if;
|
|
|
|
end Get_Item_Pointer;
|
|
|
|
|
|
|
|
function Is_Shared (R: in Ref_Counted) return Standard.Boolean is
|
|
|
|
begin
|
2021-08-24 16:07:29 +00:00
|
|
|
--return R.Data /= null and then not System.Atomic_Counters.Is_One(R.Data.Ref_Count);
|
|
|
|
return R.Data /= null and then R.Data.Ref_Count > 1;
|
2021-08-23 23:47:29 +00:00
|
|
|
end Is_Shared;
|
|
|
|
|
|
|
|
procedure Initialize (R: in out Ref_Counted) is
|
|
|
|
begin
|
|
|
|
R.Data := null;
|
|
|
|
end Initialize;
|
|
|
|
|
|
|
|
procedure Adjust (R: in out Ref_Counted) is
|
|
|
|
begin
|
|
|
|
if R.Data /= null then
|
2021-08-24 16:07:29 +00:00
|
|
|
R.Data.Ref_Count := R.Data.Ref_Count + 1;
|
|
|
|
--System.Atomic_Counters.Increment (R.Data.Ref_Count);
|
2021-08-23 23:47:29 +00:00
|
|
|
end if;
|
|
|
|
end Adjust;
|
|
|
|
|
|
|
|
procedure Finalize (R: in out Ref_Counted) is
|
|
|
|
procedure Dealloc is new Ada.Unchecked_Deallocation(Ref_Counted_Record, Ref_Counted_Pointer);
|
|
|
|
begin
|
|
|
|
if R.Data /= null then
|
2021-08-24 16:07:29 +00:00
|
|
|
--if System.Atomic_Counters.Decrement(R.Data.Ref_Count) then
|
|
|
|
-- -- The reference count reached 0
|
|
|
|
-- Dealloc (R.Data);
|
|
|
|
-- -- R.DAta must be null here
|
|
|
|
--end if;
|
|
|
|
if R.Data.Ref_Count = 1 then
|
2021-08-23 23:47:29 +00:00
|
|
|
Dealloc (R.Data);
|
2021-08-24 16:07:29 +00:00
|
|
|
else
|
|
|
|
R.Data.Ref_Count := R.Data.Ref_Count - 1;
|
2021-08-23 23:47:29 +00:00
|
|
|
end if;
|
|
|
|
end if;
|
|
|
|
end Finalize;
|
2021-08-24 16:07:29 +00:00
|
|
|
end H3.MM;
|