/tutorial/cecil/etl/example.e

http://github.com/tybor/Liberty · Specman e · 69 lines · 38 code · 11 blank · 20 comment · 0 complexity · 1f10fe772463c1dde68c18349d3058b1 MD5 · raw file

  1. class EXAMPLE
  2. --
  3. -- This example shows how simulate `eif_adopt' and `eif_wean' as they are described in ETL.
  4. --
  5. -- Note: it is not necessary to use these features when passing to the C side the address of an
  6. -- Eiffel object which remains referenced in the Eiffel side (and thus will not be garbage
  7. -- collected).
  8. --
  9. -- To compile this example, use command:
  10. --
  11. -- se c -cecil cecil.se example c_prog.c
  12. --
  13. create {ANY}
  14. make
  15. feature {ANY}
  16. make
  17. local
  18. string: STRING; i: INTEGER
  19. do
  20. send_factory_to_c(Current)
  21. -- Just play to remove or to add comments for the following
  22. -- two lines (one or both):
  23. call_eif_adopt_from_c
  24. --call_eif_wean_from_c
  25. -- The following loop should trigger the GC a few times (use
  26. -- the -gc_info option to be sure that the GC is called).
  27. -- As the allocated STRING in the following loop ("bar%N") has
  28. -- exactly the same size as the one allocated on the C side
  29. -- ("foo%N"), the former should overwrite the latter.
  30. from
  31. i := 10_000
  32. until
  33. i = 0
  34. loop
  35. -- Many many many allocation below:
  36. string := ("bar%N").twin.twin.twin.twin
  37. i := i - 1
  38. end
  39. string := string_back_to_eiffel -- Should print "foo%N" or "bar%N".
  40. io.put_string(string)
  41. end
  42. new_string (c_string: POINTER): STRING
  43. do
  44. create Result.from_external_copy(c_string)
  45. end
  46. send_factory_to_c (factory: like Current)
  47. external "C"
  48. end
  49. string_back_to_eiffel: STRING
  50. external "C"
  51. end
  52. call_eif_adopt_from_c
  53. external "C"
  54. end
  55. call_eif_wean_from_c
  56. external "C"
  57. end
  58. end -- class EXAMPLE