note description: "[ To easily manage allocation and release of allocated C memory, and to perform insertion of basic elements. Byte order is by default platform specific. Although memory allocation routines do not accept a zero sized pointer MANAGED_POINTER does by allocating in fact a 1 byte sized pointer for this particular case. ]" library: "Free implementation of ELKS library" status: "See notice at end of class." legal: "See notice at end of class." date: "$Date: 2020-05-19 14:29:33 +0000 (Tue, 19 May 2020) $" revision: "$Revision: 104259 $" class MANAGED_POINTER inherit DISPOSABLE redefine is_equal, copy end PLATFORM redefine is_equal, copy end create make, make_from_array, make_from_pointer, share_from_pointer, own_from_pointer feature {NONE} -- Initialization make (n: INTEGER_32) -- Allocate item with n bytes. require n_non_negative: n >= 0 do increment_counter item := item.memory_calloc (n.max (1), 1) if item = default_pointer then (create {EXCEPTIONS}).raise ("No more memory") end count := n is_shared := False ensure item_set: item /= default_pointer count_set: count = n is_shared_set: not is_shared end make_from_array (data: ARRAY [NATURAL_8]) -- Allocate item with data.count bytes and copy -- content of data into item. require data_not_void: data /= Void do increment_counter count := data.count item := item.memory_alloc (count.max (1)) if item = default_pointer then (create {EXCEPTIONS}).raise ("No more memory") end put_array (data, 0) is_shared := False ensure item_set: item /= default_pointer count_set: count = data.count is_shared_set: not is_shared end make_from_pointer (a_ptr: POINTER; n: INTEGER_32) -- Copy a_count bytes from a_ptr into current. require a_ptr_not_null: a_ptr /= default_pointer n_non_negative: n >= 0 do increment_counter item := item.memory_alloc (n.max (1)) if item = default_pointer then (create {EXCEPTIONS}).raise ("No more memory") end; item.memory_copy (a_ptr, n) count := n is_shared := False ensure item_set: item /= default_pointer count_set: count = n is_shared_set: not is_shared end share_from_pointer (a_ptr: POINTER; n: INTEGER_32) -- Use directly a_ptr with count n to hold current data. require a_ptr_valid: a_ptr = default_pointer implies n = 0 n_non_negative: n >= 0 do increment_counter item := a_ptr count := n is_shared := True ensure item_set: item = a_ptr count_set: count = n is_shared_set: is_shared end own_from_pointer (a_ptr: POINTER; n: INTEGER_32) -- Use directly a_ptr with count n to hold current data and free -- its associated C memory when Current is collected. -- It assumes that a_ptr was allocated using the C-malloc routine and thus -- will be freed by calling the C-free routine. require a_ptr_valid: a_ptr /= default_pointer n_non_negative: n >= 0 do increment_counter item := a_ptr count := n is_shared := False ensure item_set: item = a_ptr count_set: count = n is_shared_set: not is_shared end feature -- Settings set_from_pointer (a_ptr: POINTER; n: INTEGER_32) -- Use directly a_ptr with count n to hold current data. require is_shared: is_shared a_ptr_not_null: a_ptr = default_pointer implies n = 0 n_non_negative: n >= 0 do item := a_ptr count := n ensure item_set: item = a_ptr count_set: count = n is_shared_unchanged: is_shared end feature -- Access item: POINTER note option: transient attribute end count: INTEGER_32 -- Number of elements that Current can hold. is_shared: BOOLEAN -- Is item shared with another memory area? feature -- Comparison is_equal (other: like Current): BOOLEAN -- Is other attached to an object considered equal to current object? do if count = other.count then Result := (item = other.item) or else item.memory_compare (other.item, count) end end feature -- Duplication copy (other: like Current) -- Update current object using fields of object attached -- to other, so as to yield equal objects. If is_shared -- and current is not large enough to hold other create -- a new pointer area and is_shared is set to False. do if other /= Current then if item = other.item or is_shared then make_from_pointer (other.item, other.count) else resize (other.count); item.memory_copy (other.item, other.count) end end ensure then sharing_status_not_preserved: (other /= Current) implies (old is_shared implies not is_shared) count_preserved: count = other.count end feature -- Access: Platform specific read_natural_8 (pos: INTEGER_32): NATURAL_8 -- Read NATURAL_8 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Natural_8_bytes) <= count do ($Result).memory_copy (item + pos, Natural_8_bytes) end read_natural_16 (pos: INTEGER_32): NATURAL_16 -- Read NATURAL_16 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Natural_16_bytes) <= count do ($Result).memory_copy (item + pos, Natural_16_bytes) end read_natural_32 (pos: INTEGER_32): NATURAL_32 -- Read NATURAL_32 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Natural_32_bytes) <= count do ($Result).memory_copy (item + pos, Natural_32_bytes) end read_natural_64 (pos: INTEGER_32): NATURAL_64 -- Read NATURAL_64 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Natural_64_bytes) <= count do ($Result).memory_copy (item + pos, Natural_64_bytes) end read_integer_8 (pos: INTEGER_32): INTEGER_8 -- Read INTEGER_8 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Integer_8_bytes) <= count do Result := read_natural_8 (pos).as_integer_8 end read_integer_16 (pos: INTEGER_32): INTEGER_16 -- Read INTEGER_16 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Integer_16_bytes) <= count do Result := read_natural_16 (pos).as_integer_16 end read_integer_32 (pos: INTEGER_32): INTEGER_32 -- Read INTEGER at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Integer_32_bytes) <= count do Result := read_natural_32 (pos).as_integer_32 end read_integer_64 (pos: INTEGER_32): INTEGER_64 -- Read INTEGER_64 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Integer_64_bytes) <= count do Result := read_natural_64 (pos).as_integer_64 end read_pointer (pos: INTEGER_32): POINTER -- Read POINTER at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + pointer_bytes) <= count do ($Result).memory_copy (item + pos, pointer_bytes) end read_boolean (pos: INTEGER_32): BOOLEAN -- Read BOOLEAN at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + boolean_bytes) <= count do ($Result).memory_copy (item + pos, boolean_bytes) end read_character (pos: INTEGER_32): CHARACTER_8 -- Read CHARACTER at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Character_8_bytes) <= count do ($Result).memory_copy (item + pos, Character_8_bytes) end read_real (pos: INTEGER_32): REAL_32 obsolete "Use read_real_32 instead. [2017-05-31]" -- Read REAL_32 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Real_32_bytes) <= count do ($Result).memory_copy (item + pos, Real_32_bytes) end read_real_32 (pos: INTEGER_32): REAL_32 -- Read REAL_32 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Real_32_bytes) <= count do ($Result).memory_copy (item + pos, Real_32_bytes) end read_double (pos: INTEGER_32): REAL_64 obsolete "Use read_real_64 instead. [2017-05-31]" -- Read REAL_64 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Real_64_bytes) <= count do ($Result).memory_copy (item + pos, Real_64_bytes) end read_real_64 (pos: INTEGER_32): REAL_64 -- Read REAL_64 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Real_64_bytes) <= count do ($Result).memory_copy (item + pos, Real_64_bytes) end read_array (pos, a_count: INTEGER_32): ARRAY [NATURAL_8] -- Read count bytes at position pos. require pos_nonnegative: pos >= 0 count_positive: a_count > 0 valid_position: (pos + a_count) <= count local l_area: SPECIAL [NATURAL_8] do create l_area.make_filled (0, a_count) read_into_special_natural_8 (l_area, pos, 0, a_count) create Result.make_from_special (l_area) ensure read_array_not_void: Result /= Void read_array_valid_count: Result.count = a_count end read_special_natural_8 (source_index, n: INTEGER_32): SPECIAL [NATURAL_8] -- Read n bytes of Current from position source_index. require source_index_non_negative: source_index >= 0 n_non_negative: n >= 0 n_is_small_enough_for_source: source_index + n <= count do create Result.make_filled (0, n) read_into_special_natural_8 (Result, source_index, 0, n) end read_special_character_8 (source_index, n: INTEGER_32): SPECIAL [CHARACTER_8] -- Read n bytes of Current from position source_index. require source_index_non_negative: source_index >= 0 n_non_negative: n >= 0 n_is_small_enough_for_source: source_index + n <= count do create Result.make_filled ('%U', n) read_into_special_character_8 (Result, source_index, 0, n) end read_into_special_natural_8 (a_spec: SPECIAL [NATURAL_8]; source_index, destination_index, n: INTEGER_32) -- Read n bytes of Current from position source_index and store them in a_spec at destination_index. require a_spec_not_void: a_spec /= Void source_index_non_negative: source_index >= 0 destination_index_non_negative: destination_index >= 0 destination_index_in_bound: destination_index <= a_spec.count n_non_negative: n >= 0 n_is_small_enough_for_source: source_index + n <= count n_is_small_enough_for_destination: destination_index + n <= a_spec.count do a_spec.item_address (destination_index).memory_copy (item + source_index, n) end read_into_special_character_8 (a_spec: SPECIAL [CHARACTER_8]; source_index, destination_index, n: INTEGER_32) -- Read n bytes of Current from position source_index and store them in a_spec at destination_index. require a_spec_not_void: a_spec /= Void source_index_non_negative: source_index >= 0 destination_index_non_negative: destination_index >= 0 destination_index_in_bound: destination_index <= a_spec.count n_non_negative: n >= 0 n_is_small_enough_for_source: source_index + n <= count n_is_small_enough_for_destination: destination_index + n <= a_spec.count do a_spec.item_address (destination_index).memory_copy (item + source_index, n) end feature -- Element change: Platform specific put_natural_8 (i: NATURAL_8; pos: INTEGER_32) -- Insert i at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Natural_8_bytes) <= count do (item + pos).memory_copy ($i.to_pointer, Natural_8_bytes) ensure inserted: i = read_natural_8 (pos) end put_natural_16 (i: NATURAL_16; pos: INTEGER_32) -- Insert i at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Natural_16_bytes) <= count do (item + pos).memory_copy ($i.to_pointer, Natural_16_bytes) ensure inserted: i = read_natural_16 (pos) end put_natural_32 (i: NATURAL_32; pos: INTEGER_32) -- Insert i at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Natural_32_bytes) <= count do (item + pos).memory_copy ($i.to_pointer, Natural_32_bytes) ensure inserted: i = read_natural_32 (pos) end put_natural_64 (i: NATURAL_64; pos: INTEGER_32) -- Insert i at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Natural_64_bytes) <= count do (item + pos).memory_copy ($i.to_pointer, Natural_64_bytes) ensure inserted: i = read_natural_64 (pos) end put_integer_8 (i: INTEGER_8; pos: INTEGER_32) -- Insert i at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Integer_8_bytes) <= count do put_natural_8 (i.as_natural_8, pos) ensure inserted: i = read_integer_8 (pos) end put_integer_16 (i: INTEGER_16; pos: INTEGER_32) -- Insert i at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Integer_16_bytes) <= count do put_natural_16 (i.as_natural_16, pos) ensure inserted: i = read_integer_16 (pos) end put_integer_32 (i: INTEGER_32; pos: INTEGER_32) -- Insert i at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Integer_32_bytes) <= count do put_natural_32 (i.as_natural_32, pos) ensure inserted: i = read_integer_32 (pos) end put_integer_64 (i: INTEGER_64; pos: INTEGER_32) -- Insert i at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Integer_64_bytes) <= count do put_natural_64 (i.as_natural_64, pos) ensure inserted: i = read_integer_64 (pos) end put_pointer (p: POINTER; pos: INTEGER_32) -- Insert p at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + pointer_bytes) <= count do (item + pos).memory_copy ($p.to_pointer, pointer_bytes) ensure inserted: p = read_pointer (pos) end put_boolean (b: BOOLEAN; pos: INTEGER_32) -- Insert b at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + boolean_bytes) <= count do (item + pos).memory_copy ($b.to_pointer, boolean_bytes) ensure inserted: b = read_boolean (pos) end put_character (c: CHARACTER_8; pos: INTEGER_32) -- Insert c at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Character_8_bytes) <= count do (item + pos).memory_copy ($c.to_pointer, Character_8_bytes) ensure inserted: c = read_character (pos) end put_real (r: REAL_32; pos: INTEGER_32) obsolete "Use put_real_32 instead. [2017-05-31]" -- Insert r at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Real_32_bytes) <= count do (item + pos).memory_copy ($r.to_pointer, Real_32_bytes) ensure inserted: r = read_real_32 (pos) end put_real_32 (r: REAL_32; pos: INTEGER_32) -- Insert r at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Real_32_bytes) <= count do (item + pos).memory_copy ($r.to_pointer, Real_32_bytes) ensure inserted: r = read_real_32 (pos) end put_double (d: REAL_64; pos: INTEGER_32) obsolete "Use put_real_64 instead. [2017-05-31]" -- Insert d at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Real_64_bytes) <= count do (item + pos).memory_copy ($d.to_pointer, Real_64_bytes) ensure inserted: d = read_real_64 (pos) end put_real_64 (d: REAL_64; pos: INTEGER_32) -- Insert d at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Real_64_bytes) <= count do (item + pos).memory_copy ($d.to_pointer, Real_64_bytes) ensure inserted: d = read_real_64 (pos) end put_array (data: ARRAY [NATURAL_8]; pos: INTEGER_32) -- Copy content of data into item at position pos. require data_not_void: data /= Void pos_nonnegative: pos >= 0 valid_position: (pos + data.count) <= count local l_sp: SPECIAL [NATURAL_8] do l_sp := data.area; (item + pos).memory_copy ($l_sp.to_pointer, data.count) ensure inserted: read_array (pos, data.count) ~ data end put_special_natural_8 (a_spec: SPECIAL [NATURAL_8]; source_index, destination_index, n: INTEGER_32) -- Write n bytes of a_spec from position source_index to Current at position destination_index. require a_spec_not_void: a_spec /= Void source_index_non_negative: source_index >= 0 destination_index_non_negative: destination_index >= 0 destination_index_in_bound: destination_index <= count n_non_negative: n >= 0 n_is_small_enough_for_source: source_index + n <= a_spec.count n_is_small_enough_for_destination: destination_index + n <= count do (item + destination_index).memory_copy (a_spec.item_address (source_index), n) ensure inserted: a_spec.same_items (read_special_natural_8 (destination_index, n), 0, source_index, n) end put_special_character_8 (a_spec: SPECIAL [CHARACTER_8]; source_index, destination_index, n: INTEGER_32) -- Write n bytes of a_spec from position source_index to Current at position destination_index. require a_spec_not_void: a_spec /= Void source_index_non_negative: source_index >= 0 destination_index_non_negative: destination_index >= 0 destination_index_in_bound: destination_index <= count n_non_negative: n >= 0 n_is_small_enough_for_source: source_index + n <= a_spec.count n_is_small_enough_for_destination: destination_index + n <= count do (item + destination_index).memory_copy (a_spec.item_address (source_index), n) ensure inserted: a_spec.same_items (read_special_character_8 (destination_index, n), 0, source_index, n) end feature -- Access: Little-endian format read_natural_8_le (pos: INTEGER_32): NATURAL_8 -- Read NATURAL_8 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Natural_8_bytes) <= count do ($Result).memory_copy (item + pos, Natural_8_bytes) end read_natural_16_le (pos: INTEGER_32): NATURAL_16 -- Read NATURAL_16 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Natural_16_bytes) <= count local l_low: NATURAL_16 do if Is_little_endian then Result := read_natural_16 (pos) else l_low := {NATURAL_16} 255 & read_natural_8 (pos).to_natural_16 Result := (read_natural_8 (pos + Natural_8_bytes).to_natural_16 |<< 8) | l_low end end read_natural_32_le (pos: INTEGER_32): NATURAL_32 -- Read NATURAL_32 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Natural_32_bytes) <= count local l_high, l_low: NATURAL_32 do if Is_little_endian then Result := read_natural_32 (pos) else l_low := {NATURAL_32} 65535 & read_natural_16_le (pos).to_natural_32 l_high := read_natural_16_le (pos + Natural_16_bytes).to_natural_32 Result := (l_high.to_natural_32 |<< 16) | l_low end end read_natural_64_le (pos: INTEGER_32): NATURAL_64 -- Read NATURAL_64 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Natural_64_bytes) <= count local l_high, l_low: NATURAL_64 do if Is_little_endian then Result := read_natural_64 (pos) else l_low := {NATURAL_64} 4294967295 & read_natural_32_le (pos).to_natural_64 l_high := read_natural_32_le (pos + Natural_32_bytes).to_natural_64 Result := (l_high.to_natural_64 |<< 32) | l_low end end read_integer_8_le (pos: INTEGER_32): INTEGER_8 -- Read INTEGER_8 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Integer_8_bytes) <= count do Result := read_natural_8_le (pos).as_integer_8 end read_integer_16_le (pos: INTEGER_32): INTEGER_16 -- Read INTEGER_16 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Integer_16_bytes) <= count do Result := read_natural_16_le (pos).as_integer_16 end read_integer_32_le (pos: INTEGER_32): INTEGER_32 -- Read INTEGER at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Integer_32_bytes) <= count do Result := read_natural_32_le (pos).as_integer_32 end read_integer_64_le (pos: INTEGER_32): INTEGER_64 -- Read INTEGER_64 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Integer_64_bytes) <= count do Result := read_natural_64_le (pos).as_integer_64 end read_real_32_le (pos: INTEGER_32): REAL_32 -- Read REAL_32 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Real_32_bytes) <= count local l_nat32: NATURAL_32 do check correct_size: Real_32_bytes = Natural_32_bytes end l_nat32 := read_natural_32_le (pos); ($Result).memory_copy ($l_nat32.to_pointer, Natural_32_bytes) end read_real_64_le (pos: INTEGER_32): REAL_64 -- Read REAL_64 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Real_64_bytes) <= count local l_nat64: NATURAL_64 do check correct_size: Real_64_bytes = Natural_64_bytes end l_nat64 := read_natural_64_le (pos); ($Result).memory_copy ($l_nat64.to_pointer, Natural_64_bytes) end feature -- Element change: Little-endian format put_natural_8_le (i: NATURAL_8; pos: INTEGER_32) -- Insert i at position pos in little-endian format. require pos_nonnegative: pos >= 0 valid_position: (pos + Natural_8_bytes) <= count do (item + pos).memory_copy ($i.to_pointer, Natural_8_bytes) ensure inserted: i = read_natural_8_le (pos) end put_natural_16_le (i: NATURAL_16; pos: INTEGER_32) -- Insert i at position pos in little-endian format. require pos_nonnegative: pos >= 0 valid_position: (pos + Natural_16_bytes) <= count do if Is_little_endian then put_natural_16 (i, pos) else put_natural_8 ((i & 255).to_natural_8, pos) put_natural_8 ((((i & 65280) |>> 8) & 255).to_natural_8, pos + Natural_8_bytes) end ensure inserted: i = read_natural_16_le (pos) end put_natural_32_le (i: NATURAL_32; pos: INTEGER_32) -- Insert i at position pos in little-endian format. require pos_nonnegative: pos >= 0 valid_position: (pos + Natural_32_bytes) <= count do if Is_little_endian then put_natural_32 (i, pos) else put_natural_16_le ((i & 65535).to_natural_16, pos) put_natural_16_le ((((i & 4294901760) |>> 16) & 65535).to_natural_16, pos + Natural_16_bytes) end ensure inserted: i = read_natural_32_le (pos) end put_natural_64_le (i: NATURAL_64; pos: INTEGER_32) -- Insert i at position pos in little-endian format. require pos_nonnegative: pos >= 0 valid_position: (pos + Natural_64_bytes) <= count do if Is_little_endian then put_natural_64 (i, pos) else put_natural_32_le ((i & 4294967295).to_natural_32, pos) put_natural_32_le ((((i & 18446744069414584320) |>> 32) & 4294967295).to_natural_32, pos + Natural_32_bytes) end ensure inserted: i = read_natural_64_le (pos) end put_integer_8_le (i: INTEGER_8; pos: INTEGER_32) -- Insert i at position pos in little-endian format. require pos_nonnegative: pos >= 0 valid_position: (pos + Integer_8_bytes) <= count do put_natural_8_le (i.as_natural_8, pos) ensure inserted: i = read_integer_8_le (pos) end put_integer_16_le (i: INTEGER_16; pos: INTEGER_32) -- Insert i at position pos in little-endian format. require pos_nonnegative: pos >= 0 valid_position: (pos + Integer_16_bytes) <= count do put_natural_16_le (i.as_natural_16, pos) ensure inserted: i = read_integer_16_le (pos) end put_integer_32_le (i: INTEGER_32; pos: INTEGER_32) -- Insert i at position pos in little-endian format. require pos_nonnegative: pos >= 0 valid_position: (pos + Integer_32_bytes) <= count do put_natural_32_le (i.as_natural_32, pos) ensure inserted: i = read_integer_32_le (pos) end put_integer_64_le (i: INTEGER_64; pos: INTEGER_32) -- Insert i at position pos in little-endian format. require pos_nonnegative: pos >= 0 valid_position: (pos + Integer_64_bytes) <= count do put_natural_64_le (i.as_natural_64, pos) ensure inserted: i = read_integer_64_le (pos) end put_real_32_le (v: REAL_32; pos: INTEGER_32) -- Insert v at position pos in little-endian format. require pos_nonnegative: pos >= 0 valid_position: (pos + Real_32_bytes) <= count local l_nat32: NATURAL_32 do check correct_size: Real_32_bytes = Natural_32_bytes end; ($l_nat32).memory_copy ($v.to_pointer, Natural_32_bytes) put_natural_32_le (l_nat32, pos) ensure inserted: v = read_real_32_le (pos) end put_real_64_le (v: REAL_64; pos: INTEGER_32) -- Insert v at position pos in little-endian format. require pos_nonnegative: pos >= 0 valid_position: (pos + Real_64_bytes) <= count local l_nat64: NATURAL_64 do check correct_size: Real_64_bytes = Natural_64_bytes end; ($l_nat64).memory_copy ($v.to_pointer, Natural_64_bytes) put_natural_64_le (l_nat64, pos) ensure inserted: v = read_real_64_le (pos) end feature -- Access: Big-endian format read_natural_8_be (pos: INTEGER_32): NATURAL_8 -- Read NATURAL_8 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Natural_8_bytes) <= count do ($Result).memory_copy (item + pos, Natural_8_bytes) end read_natural_16_be (pos: INTEGER_32): NATURAL_16 -- Read NATURAL_16 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Natural_16_bytes) <= count local l_high, l_low: NATURAL_16 do if Is_little_endian then l_high := read_natural_8 (pos).to_natural_16 l_low := (255).to_natural_16 & read_natural_8 (pos + Natural_8_bytes).to_natural_16 Result := (l_high.to_natural_16 |<< 8) | l_low else Result := read_natural_16 (pos) end end read_natural_32_be (pos: INTEGER_32): NATURAL_32 -- Read NATURAL_32 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Natural_32_bytes) <= count local l_high, l_low: NATURAL_32 do if Is_little_endian then l_high := read_natural_16_be (pos).to_natural_32 l_low := (65535).to_natural_32 & read_natural_16_be (pos + Natural_16_bytes).to_natural_32 Result := (l_high.to_natural_32 |<< 16) | l_low else Result := read_natural_32 (pos) end end read_natural_64_be (pos: INTEGER_32): NATURAL_64 -- Read NATURAL_64 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Natural_64_bytes) <= count local l_high, l_low: NATURAL_64 do if Is_little_endian then l_high := read_natural_32_be (pos).to_natural_64 l_low := {NATURAL_64} 4294967295 & read_natural_32_be (pos + Natural_32_bytes).to_natural_64 Result := (l_high.to_natural_64 |<< 32) | l_low else Result := read_natural_64 (pos) end end read_integer_8_be (pos: INTEGER_32): INTEGER_8 -- Read INTEGER_8 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Integer_8_bytes) <= count do Result := read_natural_8_be (pos).as_integer_8 end read_integer_16_be (pos: INTEGER_32): INTEGER_16 -- Read INTEGER_16 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Integer_16_bytes) <= count do Result := read_natural_16_be (pos).as_integer_16 end read_integer_32_be (pos: INTEGER_32): INTEGER_32 -- Read INTEGER at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Integer_32_bytes) <= count do Result := read_natural_32_be (pos).as_integer_32 end read_integer_64_be (pos: INTEGER_32): INTEGER_64 -- Read INTEGER_64 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Integer_64_bytes) <= count do Result := read_natural_64_be (pos).as_integer_64 end read_real_32_be (pos: INTEGER_32): REAL_32 -- Read REAL_32 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Real_32_bytes) <= count local l_nat32: NATURAL_32 do check correct_size: Real_32_bytes = Natural_32_bytes end l_nat32 := read_natural_32_be (pos); ($Result).memory_copy ($l_nat32.to_pointer, Natural_32_bytes) end read_real_64_be (pos: INTEGER_32): REAL_64 -- Read REAL_64 at position pos. require pos_nonnegative: pos >= 0 valid_position: (pos + Real_64_bytes) <= count local l_nat64: NATURAL_64 do check correct_size: Real_64_bytes = Natural_64_bytes end l_nat64 := read_natural_64_be (pos); ($Result).memory_copy ($l_nat64.to_pointer, Natural_64_bytes) end feature -- Element change: Big-endian format put_natural_8_be (i: NATURAL_8; pos: INTEGER_32) -- Insert i at position pos in big-endian format. require pos_nonnegative: pos >= 0 valid_position: (pos + Natural_8_bytes) <= count do (item + pos).memory_copy ($i.to_pointer, Natural_8_bytes) ensure inserted: i = read_natural_8_be (pos) end put_natural_16_be (i: NATURAL_16; pos: INTEGER_32) -- Insert i at position pos in big-endian format. require pos_nonnegative: pos >= 0 valid_position: (pos + Natural_16_bytes) <= count do if Is_little_endian then put_natural_8 ((((i & 65280) |>> 8) & 255).to_natural_8, pos) put_natural_8 ((i & 255).to_natural_8, pos + Natural_8_bytes) else put_natural_16 (i, pos) end ensure inserted: i = read_natural_16_be (pos) end put_natural_32_be (i: NATURAL_32; pos: INTEGER_32) -- Insert i at position pos in big-endian format. require pos_nonnegative: pos >= 0 valid_position: (pos + Natural_32_bytes) <= count do if Is_little_endian then put_natural_16_be ((((i & 4294901760) |>> 16) & 65535).to_natural_16, pos) put_natural_16_be ((i & 65535).to_natural_16, pos + Natural_16_bytes) else put_natural_32 (i, pos) end ensure inserted: i = read_natural_32_be (pos) end put_natural_64_be (i: NATURAL_64; pos: INTEGER_32) -- Insert i at position pos in big-endian format. require pos_nonnegative: pos >= 0 valid_position: (pos + Natural_64_bytes) <= count do if Is_little_endian then put_natural_32_be ((((i & 18446744069414584320) |>> 32) & 4294967295).to_natural_32, pos) put_natural_32_be ((i & 4294967295).to_natural_32, pos + Natural_32_bytes) else put_natural_64 (i, pos) end ensure inserted: i = read_natural_64_be (pos) end put_integer_8_be (i: INTEGER_8; pos: INTEGER_32) -- Insert i at position pos in big-endian format. require pos_nonnegative: pos >= 0 valid_position: (pos + Integer_8_bytes) <= count do put_natural_8_be (i.as_natural_8, pos) ensure inserted: i = read_integer_8_be (pos) end put_integer_16_be (i: INTEGER_16; pos: INTEGER_32) -- Insert i at position pos in big-endian format. require pos_nonnegative: pos >= 0 valid_position: (pos + Integer_16_bytes) <= count do put_natural_16_be (i.as_natural_16, pos) ensure inserted: i = read_integer_16_be (pos) end put_integer_32_be (i: INTEGER_32; pos: INTEGER_32) -- Insert i at position pos in big-endian format. require pos_nonnegative: pos >= 0 valid_position: (pos + Integer_32_bytes) <= count do put_natural_32_be (i.as_natural_32, pos) ensure inserted: i = read_integer_32_be (pos) end put_integer_64_be (i: INTEGER_64; pos: INTEGER_32) -- Insert i at position pos in big-endian format. require pos_nonnegative: pos >= 0 valid_position: (pos + Integer_64_bytes) <= count do put_natural_64_be (i.as_natural_64, pos) ensure inserted: i = read_integer_64_be (pos) end put_real_32_be (v: REAL_32; pos: INTEGER_32) -- Insert v at position pos in big-endian format. require pos_nonnegative: pos >= 0 valid_position: (pos + Real_32_bytes) <= count local l_nat32: NATURAL_32 do check correct_size: Real_32_bytes = Natural_32_bytes end; ($l_nat32).memory_copy ($v.to_pointer, Natural_32_bytes) put_natural_32_be (l_nat32, pos) ensure inserted: v = read_real_32_be (pos) end put_real_64_be (v: REAL_64; pos: INTEGER_32) -- Insert v at position pos in big-endian format. require pos_nonnegative: pos >= 0 valid_position: (pos + Real_64_bytes) <= count local l_nat64: NATURAL_64 do check correct_size: Real_64_bytes = Natural_64_bytes end; ($l_nat64).memory_copy ($v.to_pointer, Natural_64_bytes) put_natural_64_be (l_nat64, pos) ensure inserted: v = read_real_64_be (pos) end feature -- Concatenation append (other: like Current) -- Append other at the end of Current. require not_shared: not is_shared other_not_void: other /= Void local new_count: INTEGER_32 do new_count := count + other.count item := item.memory_realloc (new_count.max (1)) if item = default_pointer then (create {EXCEPTIONS}).raise ("No more memory") end; (item + count).memory_copy (other.item, other.count) count := new_count end feature -- Resizing resize (n: INTEGER_32) -- Reallocate item to hold n bytes. require n_non_negative: n >= 0 not_shared: not is_shared do if n /= count then item := item.memory_realloc (n.max (1)) if item = default_pointer then (create {EXCEPTIONS}).raise ("No more memory") end end if n > count then (item + count).memory_set (0, n - count) end count := n end feature -- Disposal dispose -- Release memory pointed by item. local null: POINTER do if not is_shared then item.memory_free end item := null is_shared := False ensure then shared_reset: not is_shared end feature {NONE} -- Debugging Allocation_counter: CELL [NATURAL_64] -- Store current number of allocation being made. once create Result.put (0) end counter: NATURAL_64 note option: transient attribute end increment_counter -- Set counter with a new allocation number. do debug ("managed_pointer_allocation") counter := Allocation_counter.item + 1; Allocation_counter.put (counter) end end invariant item_not_null: item = default_pointer implies (count = 0 and is_shared) valid_count: count >= 0 note copyright: "Copyright (c) 1984-2020, 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 MANAGED_POINTER
Generated by ISE EiffelStudio