1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-21 02:27:10 +03:00
mal/ada.2/garbage_collected.adb
Nicolas Boulenguez 5a07bb5331 ada.2: fix memory leaks with garbage collection. Various simplifications.
Cyclic references were never deallocated by reference conuting.
Symbols cannot create cyclic structures and are less frequent (one
allocation per symbol), keep reference counting for them.

This slightly improves performances even though many previous
optimizations are removed (environment stack, reuse of memory).

Step caching hash of symbols. This does not seem to improve
performances. Hashing them instead of ordering them does.

Define Repl in the step file instead of globally. Move the eval
built-in function from core into the step file.

When possible, pass Ada records instead of explicit pointers.

In the reader, construct more objects directly as described in the MAL
process, reserve the buffer for sequences and maps

In eval, iterate on vectors without delegation. The increased
complexity was not improving performances.  Keep demonstrating Ada
type-safe genericity for maps, where iterating outside Types.Maps
would be less easy and/or efficient.

In quasiquote_list, concatenate in one buffer instead of allocating a
list for each element. The buffer may be reallocated behind the
curtain, but not once per element anymore.

In environments, illustrate tail call optimization when recursion is
more readable than a loop.
2019-03-31 19:06:00 +02:00

55 lines
1.4 KiB
Ada

with Ada.Unchecked_Deallocation;
package body Garbage_Collected is
procedure Free is new Ada.Unchecked_Deallocation (Class, Pointer);
Top : Pointer := null;
----------------------------------------------------------------------
procedure Clean is
Current : Pointer := Top;
Previous : Pointer;
begin
while Current /= null and then not Current.all.Kept loop
Previous := Current;
Current := Current.all.Next;
Free (Previous);
end loop;
Top := Current;
while Current /= null loop
if Current.all.Kept then
Current.all.Kept := False;
Previous := Current;
else
Previous.all.Next := Current.all.Next;
Free (Current);
end if;
Current := Previous.all.Next;
end loop;
end Clean;
procedure Keep (Object : in out Instance) is
begin
if not Object.Kept then
Object.Kept := True;
Class (Object).Keep_References; -- dispatching
end if;
end Keep;
procedure Check_Allocations is
begin
pragma Assert (Top = null);
end Check_Allocations;
procedure Register (Ref : in not null Pointer) is
begin
pragma Assert (Ref.all.Kept = False);
pragma Assert (Ref.all.Next = null);
Ref.all.Next := Top;
Top := Ref;
end Register;
end Garbage_Collected;