PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 1ms

/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
 1class 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
14create {ANY}
15   make
16
17feature {ANY}
18   make
19      local
20         string: STRING; i: INTEGER
21      do
22         send_factory_to_c(Current)
23         -- Just play to remove or to add comments for the following
24         -- two lines (one or both):
25
26         call_eif_adopt_from_c
27         --call_eif_wean_from_c
28         -- The following loop should trigger the GC a few times (use
29         -- the -gc_info option to be sure that the GC is called).
30         -- As the allocated STRING in the following loop ("bar%N") has
31         -- exactly the same size as the one allocated on the C side
32         -- ("foo%N"), the former should overwrite the latter.
33
34         from
35            i := 10_000
36         until
37            i = 0
38         loop
39            -- Many many many allocation below:
40            string := ("bar%N").twin.twin.twin.twin
41            i := i - 1
42         end
43
44         string := string_back_to_eiffel -- Should print "foo%N" or "bar%N".
45         io.put_string(string)
46      end
47
48   new_string (c_string: POINTER): STRING
49      do
50         create Result.from_external_copy(c_string)
51      end
52
53   send_factory_to_c (factory: like Current)
54      external "C"
55      end
56
57   string_back_to_eiffel: STRING
58      external "C"
59      end
60
61   call_eif_adopt_from_c
62      external "C"
63      end
64
65   call_eif_wean_from_c
66      external "C"
67      end
68
69end -- class EXAMPLE