note description: "[ Facilities for tuning up the garbage collection mechanism. This class may be used as ancestor by classes needing its facilities. ]" library: "Free implementation of ELKS library" status: "See notice at end of class." legal: "See notice at end of class." date: "$Date: 2018-04-28 20:47:46 +0000 (Sat, 28 Apr 2018) $" revision: "$Revision: 101696 $" class MEMORY create default_create feature {NONE} -- Initialization default_create -- Process instances of classes with no creation clause. -- (Default: do nothing.) -- (from ANY) do end feature -- Access C_memory: INTEGER_32 = 2 -- Code for the C memory managed -- by the garbage collector -- (from MEM_CONST) Eiffel_memory: INTEGER_32 = 1 -- Code for the Eiffel memory managed -- by the garbage collector -- (from MEM_CONST) Full_collector: INTEGER_32 = 0 -- Statistics for full collections -- (from MEM_CONST) generating_type: TYPE [detachable MEMORY] -- Type of current object -- (type of which it is a direct instance) -- (from ANY) external "built_in" ensure -- from ANY generating_type_not_void: Result /= Void end generator: STRING_8 -- Name of current object's generating class -- (base class of the type of which it is a direct instance) -- (from ANY) external "built_in" ensure -- from ANY generator_not_void: Result /= Void generator_not_empty: not Result.is_empty end Incremental_collector: INTEGER_32 = 1 -- Statistics for incremental collections -- (from MEM_CONST) Total_memory: INTEGER_32 = 0 -- Code for all the memory managed -- by the garbage collector -- (from MEM_CONST) feature -- Measurement gc_statistics (collector_type: INTEGER_32): GC_INFO -- Garbage collection information for collector_type. require type_ok: collector_type = Full_collector or collector_type = Incremental_collector do create Result.make (collector_type) ensure instance_free: class end memory_statistics (memory_type: INTEGER_32): MEM_INFO -- Memory usage information for memory_type require type_ok: memory_type = Total_memory or memory_type = Eiffel_memory or memory_type = C_memory do create Result.make (memory_type) ensure instance_free: class end feature -- Comparison frozen deep_equal (a: detachable ANY; b: like arg #1): BOOLEAN -- Are a and b either both void -- or attached to isomorphic object structures? -- (from ANY) do if a = Void then Result := b = Void else Result := b /= Void and then a.is_deep_equal (b) end ensure -- from ANY instance_free: class shallow_implies_deep: standard_equal (a, b) implies Result both_or_none_void: (a = Void) implies (Result = (b = Void)) same_type: (Result and (a /= Void)) implies (b /= Void and then a.same_type (b)) symmetric: Result implies deep_equal (b, a) end frozen equal (a: detachable ANY; b: like arg #1): BOOLEAN -- Are a and b either both void or attached -- to objects considered equal? -- (from ANY) do if a = Void then Result := b = Void else Result := b /= Void and then a.is_equal (b) end ensure -- from ANY instance_free: class definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.is_equal (b)) end frozen is_deep_equal alias "≡≡≡" (other: MEMORY): BOOLEAN -- Are Current and other attached to isomorphic object structures? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" ensure -- from ANY shallow_implies_deep: standard_is_equal (other) implies Result same_type: Result implies same_type (other) symmetric: Result implies other.is_deep_equal (Current) end is_equal (other: MEMORY): BOOLEAN -- Is other attached to an object considered -- equal to current object? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" ensure -- from ANY symmetric: Result implies other ~ Current consistent: standard_is_equal (other) implies Result end frozen standard_equal (a: detachable ANY; b: like arg #1): BOOLEAN -- Are a and b either both void or attached to -- field-by-field identical objects of the same type? -- Always uses default object comparison criterion. -- (from ANY) do if a = Void then Result := b = Void else Result := b /= Void and then a.standard_is_equal (b) end ensure -- from ANY instance_free: class definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.standard_is_equal (b)) end frozen standard_is_equal alias "≜" (other: MEMORY): BOOLEAN -- Is other attached to an object of the same type -- as current object, and field-by-field identical to it? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" ensure -- from ANY same_type: Result implies same_type (other) symmetric: Result implies other.standard_is_equal (Current) end feature -- Status report chunk_size: INTEGER_32 -- Minimal size of a memory chunk. The run-time always -- allocates a multiple of this size. -- If the environment variable EIF_MEMORY_CHUNK -- is defined, it is set to the closest reasonable -- value from it. external "C use %"eif_memory.h%"" alias "eif_get_chunk_size" ensure instance_free: class end coalesce_period: INTEGER_32 -- Period of full coalesce (in number of collections) -- If the environment variable EIF_FULL_COALESCE_PERIOD -- is defined, it is set to the closest reasonable -- value from it. -- If null, no full coalescing is launched. external "C use %"eif_memory.h%"" alias "eif_coalesce_period" ensure instance_free: class end collecting: BOOLEAN -- Is garbage collection enabled? external "C use %"eif_memory.h%"" alias "eif_gc_ison" ensure instance_free: class end collection_period: INTEGER_32 -- Period of full collection. -- If the environment variable EIF_FULL_COLLECTION_PERIOD -- is defined, it is set to the closest reasonable -- value from it. -- If null, no full collection is launched. external "C use %"eif_memory.h%"" alias "eif_mem_pget" ensure instance_free: class end conforms_to (other: ANY): BOOLEAN -- Does type of current object conform to type -- of other (as per Eiffel: The Language, chapter 13)? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" end generation_object_limit: INTEGER_32 -- Maximum size of object in generational scavenge zone. -- If the environment variable EIF_GS_LIMIT -- is defined, it is set to the closest reasonable -- value from it. external "C use %"eif_memory.h%"" alias "eif_generation_object_limit" ensure instance_free: class end largest_coalesced_block: INTEGER_32 -- Size of largest coalesced block since last call to -- largest_coalesced; 0 if none. external "C use %"eif_memory.h%"" alias "eif_mem_largest" ensure instance_free: class end max_mem: INTEGER_32 -- Maximum amount of bytes the run-time can allocate. external "C use %"eif_memory.h%"" alias "eif_get_max_mem" ensure instance_free: class end memory_count_map: HASH_TABLE [INTEGER_32, INTEGER_32] -- Number of instances per dynamic type present in system. -- Same as memory_map except that no references on the objects themselves -- is kept. local i, nb, dtype: INTEGER_32 l_spec: SPECIAL [ANY] l_item: ANY do l_spec := find_all_instances (Special_any_dynamic_type) create Result.make (100) from i := 0 nb := l_spec.count until i >= nb loop l_item := l_spec.item (i) if l_item /= Void then dtype := l_item.generating_type.type_id; Result.search (dtype) if Result.found then Result.force (Result.found_item + 1, dtype) else Result.put (1, dtype) end end i := i + 1 end ensure instance_free: class end memory_map: HASH_TABLE [ARRAYED_LIST [ANY], INTEGER_32] -- Retrieves all object in system as a table indexed by dynamic type -- where elements are all instances of a given data type. local i, nb, dtype: INTEGER_32 l_spec: SPECIAL [ANY] l_item: ANY l_list: detachable ARRAYED_LIST [ANY] l_memory_count_map: HASH_TABLE [INTEGER_32, INTEGER_32] do l_spec := find_all_instances (Special_any_dynamic_type) create l_memory_count_map.make (100) from i := 0 nb := l_spec.count until i >= nb loop l_item := l_spec.item (i) if l_item /= Void then dtype := l_item.generating_type.type_id; l_memory_count_map.force (l_memory_count_map.item (dtype) + 1, dtype) end i := i + 1 end create Result.make (100) from i := 0 nb := l_spec.count until i >= nb loop l_item := l_spec.item (i) if l_item /= Void then dtype := l_item.generating_type.type_id; Result.search (dtype) if Result.found and then attached Result.found_item as l_found_item then l_list := l_found_item else create l_list.make (l_memory_count_map.item (dtype)); Result.put (l_list, dtype) end; l_list.extend (l_item) end i := i + 1 end ensure instance_free: class end memory_threshold: INTEGER_32 -- Minimum amount of bytes to be allocated before -- starting an automatic garbage collection. external "C use %"eif_memory.h%"" alias "eif_mem_tget" ensure instance_free: class end objects_instance_of (an_object: ANY): SPECIAL [ANY] -- Objects that have same dynamic type as an_object. do Result := objects_instance_of_type (an_object.generating_type.type_id) ensure instance_free: class end objects_instance_of_type (a_type_id: INTEGER_32): SPECIAL [ANY] -- Objects that have same dynamic type as an_object. do Result := find_instance_of (a_type_id, Special_any_dynamic_type) ensure instance_free: class end referers (an_object: ANY): SPECIAL [ANY] -- Objects that refer to an_object. do Result := find_referers (an_object, Special_any_dynamic_type) ensure instance_free: class end same_type (other: ANY): BOOLEAN -- Is type of current object identical to type of other? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" ensure -- from ANY definition: Result = (conforms_to (other) and other.conforms_to (Current)) end scavenge_zone_size: INTEGER_32 -- Size of generational scavenge zone. -- If the environment variable EIF_MEMORY_SCAVENGE -- is defined, it is set to the closest reasonable -- value from it. external "C use %"eif_memory.h%"" alias "eif_scavenge_zone_size" ensure instance_free: class end tenure: INTEGER_32 -- Maximum age of object before being considered -- as old (old objects are not scanned during -- partial collection). -- If the environment variable EIF_TENURE_MAX -- is defined, it is set to the closest reasonable -- value from it. external "C use %"eif_memory.h%"" alias "eif_tenure" ensure instance_free: class end feature {NONE} -- Status report is_in_final_collect: BOOLEAN -- Is GC currently performing final collection -- after execution of current program? -- Safe to use in dispose. -- (from DISPOSABLE) external "C inline use %"eif_memory.h%"" alias "return eif_is_in_final_collect;" end feature -- Status setting allocate_compact -- Enter memory mode: will try to compact memory -- before requesting more from the operating system. external "C use %"eif_memory.h%"" alias "eif_mem_slow" ensure instance_free: class end allocate_fast -- Enter speed mode: will optimize speed of memory -- allocation rather than memory usage. external "C use %"eif_memory.h%"" alias "eif_mem_speed" ensure instance_free: class end allocate_tiny -- Enter tiny mode: will enter memory mode -- after having freed as much memory as possible. external "C use %"eif_memory.h%"" alias "eif_mem_tiny" ensure instance_free: class end collection_off -- Disable garbage collection. external "C use %"eif_memory.h%"" alias "eif_gc_stop" ensure instance_free: class end collection_on -- Enable garbage collection. external "C use %"eif_memory.h%"" alias "eif_gc_run" ensure instance_free: class end disable_time_accounting -- Disable GC time accounting (default). do gc_monitoring (False) ensure instance_free: class end enable_time_accounting -- Enable GC time accouting, accessible in gc_statistics. do gc_monitoring (True) ensure instance_free: class end execute_without_collection (a_action: PROCEDURE) -- Execute a_action with the garbage collector disabled. -- If a_action modifies the status of collecting, we restore -- it no matter what at the end. require a_action_not_void: a_action /= Void local l_is_collecting: like collecting retried: BOOLEAN do if not retried then l_is_collecting := collecting if l_is_collecting then collection_off; a_action.call (Void) collection_on else a_action.call (Void) collection_off end else if l_is_collecting then collection_on else collection_off end end ensure instance_free: class collection_status_preserved: collecting = old collecting rescue retried := True retry end set_coalesce_period (value: INTEGER_32) -- Set coalesce_period. Every value collection, -- the Garbage Collector will coalesce -- the whole memory. require positive_value: value >= 0 external "C use %"eif_memory.h%"" alias "eif_set_coalesce_period" ensure instance_free: class end set_collection_period (value: INTEGER_32) -- Set collection_period. Every value collection, -- the Garbage collector will perform a collection -- on the whole memory (full collection), otherwise -- a simple partial collection is done. require positive_value: value >= 0 external "C use %"eif_memory.h%"" alias "eif_mem_pset" ensure instance_free: class end set_max_mem (value: INTEGER_32) -- Set the maximum amount of memory the run-time can allocate. require positive_value: value > 0 external "C use %"eif_memory.h%"" alias "eif_set_max_mem" ensure instance_free: class end set_memory_threshold (value: INTEGER_32) -- Set a new memory_threshold in bytes. Whenever the memory -- allocated for Eiffel reaches this value, an automatic -- collection is performed. require positive_value: value > 0 external "C use %"eif_memory.h%"" alias "eif_mem_tset" ensure instance_free: class end feature -- Removal collect -- Force a partial collection cycle if garbage -- collection is enabled; do nothing otherwise. external "C use %"eif_memory.h%"" ensure instance_free: class end dispose -- Action to be executed just before garbage collection -- reclaims an object. -- Default version does nothing; redefine in descendants -- to perform specific dispose actions. Those actions -- should only take care of freeing external resources; -- they should not perform remote calls on other objects -- since these may also be dead and reclaimed. do end free (object: ANY) -- Free object, by-passing garbage collection. -- Erratic behavior will result if the object is still -- referenced. external "built_in static" ensure instance_free: class end full_coalesce -- Coalesce the whole memory: merge adjacent free -- blocks to reduce fragmentation. Useful, when -- a lot of memory is allocated with garbage collector off. external "C use %"eif_memory.h%"" alias "eif_mem_coalesc" ensure instance_free: class end full_collect -- Force a full collection cycle if garbage -- collection is enabled; do nothing otherwise. external "C use %"eif_memory.h%"" alias "plsc" ensure instance_free: class end feature -- Duplication frozen clone (other: detachable ANY): like other obsolete "Use `twin' instead. [2017-05-31]" -- Void if other is void; otherwise new object -- equal to other -- -- For non-void other, clone calls copy; -- to change copying/cloning semantics, redefine copy. -- (from ANY) do if other /= Void then Result := other.twin end ensure -- from ANY instance_free: class equal: Result ~ other end copy (other: MEMORY) -- Update current object using fields of object attached -- to other, so as to yield equal objects. -- (from ANY) require -- from ANY other_not_void: other /= Void type_identity: same_type (other) external "built_in" ensure -- from ANY is_equal: Current ~ other end frozen deep_clone (other: detachable ANY): like other obsolete "Use `deep_twin' instead. [2017-05-31]" -- Void if other is void: otherwise, new object structure -- recursively duplicated from the one attached to other -- (from ANY) do if other /= Void then Result := other.deep_twin end ensure -- from ANY instance_free: class deep_equal: deep_equal (other, Result) end frozen deep_copy (other: MEMORY) -- Effect equivalent to that of: -- copy (other . deep_twin) -- (from ANY) require -- from ANY other_not_void: other /= Void do copy (other.deep_twin) ensure -- from ANY deep_equal: deep_equal (Current, other) end frozen deep_twin: MEMORY -- New object structure recursively duplicated from Current. -- (from ANY) external "built_in" ensure -- from ANY deep_twin_not_void: Result /= Void deep_equal: deep_equal (Current, Result) end frozen standard_clone (other: detachable ANY): like other obsolete "Use `standard_twin' instead. [2017-05-31]" -- Void if other is void; otherwise new object -- field-by-field identical to other. -- Always uses default copying semantics. -- (from ANY) do if other /= Void then Result := other.standard_twin end ensure -- from ANY instance_free: class equal: standard_equal (Result, other) end frozen standard_copy (other: MEMORY) -- Copy every field of other onto corresponding field -- of current object. -- (from ANY) require -- from ANY other_not_void: other /= Void type_identity: same_type (other) external "built_in" ensure -- from ANY is_standard_equal: standard_is_equal (other) end frozen standard_twin: MEMORY -- New object field-by-field identical to other. -- Always uses default copying semantics. -- (from ANY) external "built_in" ensure -- from ANY standard_twin_not_void: Result /= Void equal: standard_equal (Result, Current) end frozen twin: MEMORY -- New object equal to Current -- twin calls copy; to change copying/twinning semantics, redefine copy. -- (from ANY) external "built_in" ensure -- from ANY twin_not_void: Result /= Void is_equal: Result ~ Current end feature -- Basic operations frozen as_attached: attached MEMORY obsolete "Remove calls to this feature. [2017-05-31]" -- Attached version of Current. -- (Can be used during transitional period to convert -- non-void-safe classes to void-safe ones.) -- (from ANY) do Result := Current end frozen default: detachable MEMORY -- Default value of object's type -- (from ANY) do end frozen default_pointer: POINTER -- Default value of type POINTER -- (Avoid the need to write p.default for -- some p of type POINTER.) -- (from ANY) do ensure -- from ANY instance_free: class end default_rescue -- Process exception for routines with no Rescue clause. -- (Default: do nothing.) -- (from ANY) do end frozen do_nothing -- Execute a null action. -- (from ANY) do ensure -- from ANY instance_free: class end feature {NONE} -- Implementation find_all_instances (result_type: INTEGER_32): SPECIAL [ANY] external "C signature (EIF_INTEGER): EIF_REFERENCE use %"eif_traverse.h%"" ensure instance_free: class end find_instance_of (dtype, result_type: INTEGER_32): SPECIAL [ANY] external "C signature (EIF_INTEGER, EIF_INTEGER): EIF_REFERENCE use %"eif_traverse.h%"" ensure instance_free: class end find_referers (target: ANY; result_type: INTEGER_32): SPECIAL [ANY] external "built_in static" ensure instance_free: class end gc_monitoring (flag: BOOLEAN) -- Set up GC monitoring according to flag external "C use %"eif_memory.h%"" alias "eif_gc_mon" ensure instance_free: class end Special_any_dynamic_type: INTEGER_32 -- Dynamic type ID of an instance of SPECIAL [ANY] once Result := ({SPECIAL [ANY]}).type_id ensure instance_free: class end feature -- Output Io: STD_FILES -- Handle to standard file setup -- (from ANY) once create Result; Result.set_output_default ensure -- from ANY instance_free: class io_not_void: Result /= Void end out: STRING_8 -- New string containing terse printable representation -- of current object -- (from ANY) do Result := tagged_out ensure -- from ANY out_not_void: Result /= Void end print (o: detachable ANY) -- Write terse external representation of o -- on standard output. -- (from ANY) local s: READABLE_STRING_8 do if attached o then s := o.out if attached {READABLE_STRING_32} s as s32 then Io.put_string_32 (s32) elseif attached {READABLE_STRING_8} s as s8 then Io.put_string (s8) else Io.put_string_32 (s.as_string_32) end end ensure -- from ANY instance_free: class end frozen tagged_out: STRING_8 -- New string containing terse printable representation -- of current object -- (from ANY) external "built_in" ensure -- from ANY tagged_out_not_void: Result /= Void end feature -- Platform Operating_environment: OPERATING_ENVIRONMENT -- Objects available from the operating system -- (from ANY) once create Result ensure -- from ANY instance_free: class operating_environment_not_void: Result /= Void end feature {NONE} -- Retrieval frozen internal_correct_mismatch -- Called from runtime to perform a proper dynamic dispatch on correct_mismatch -- from MISMATCH_CORRECTOR. -- (from ANY) local l_msg: STRING_32 l_exc: EXCEPTIONS do if attached {MISMATCH_CORRECTOR} Current as l_corrector then l_corrector.correct_mismatch else create l_msg.make_from_string ("Mismatch: ".as_string_32) create l_exc; l_msg.append (generating_type.name_32); l_exc.raise_retrieval_exception (l_msg) end end invariant -- from ANY reflexive_equality: standard_is_equal (Current) reflexive_conformance: conforms_to (Current) note copyright: "Copyright (c) 1984-2018, Eiffel Software and others" license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" source: "[ Eiffel Software 5949 Hollister Ave., Goleta, CA 93117 USA Telephone 805-685-1006, Fax 805-685-6869 Website http://www.eiffel.com Customer support http://support.eiffel.com ]" end -- class MEMORY
Generated by ISE EiffelStudio