PageRenderTime 22ms CodeModel.GetById 14ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/wrappers/common/library/mixed_memory_handling.e

http://github.com/tybor/Liberty
Specman e | 80 lines | 59 code | 9 blank | 12 comment | 6 complexity | 8ed8c817bc521c2eae5ab359d1153496 MD5 | raw file
 1note
 2   description:
 3      "Wrapper for a generic C structure, that is shared with external code, handle will not be freed on dispose of the Eiffel wrapper object, if is_shared is true"
 4   copyright:
 5      "[
 6Copyright (C) 2008-2017: Raphael Mack <mail@raphael-mack.de>, Paolo Redaelli <paolo.redaelli@poste.it>
 7
 8This library is free software; you can redistribute it and/or
 9modify it under the terms of the GNU Lesser General Public License
10as published by the Free Software Foundation; either version 2.1 of
11the License, or (at your option) any later version.
12
13This library is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16Lesser General Public License for more details.
17
18You should have received a copy of the GNU Lesser General Public
19License along with this library; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
2102110-1301 USA
22]"
23   author:
24      "Raphael Mack <mail@raphael-mack.de>, Paolo Redaelli <paolo.redaelli@poste.it>"
25deferred class MIXED_MEMORY_HANDLING
26   -- Wrapper for a generic C structure, whose memory can either by
27   -- handled by the Eiffel library or by the underlying C code. Who
28   -- handles memory is decided on a per-object based on the value of
29   -- the flag `is_shared': handle will not be freed on dispose of the
30   -- Eiffel wrapper object, when `is_shared' is true.
31
32insert
33   WRAPPER
34      undefine from_external_pointer
35      end
36   STDLIB_EXTERNALS
37
38feature {ANY}
39   dispose
40         -- Action to be executed just before garbage collection
41         -- reclaims an object; if not shared frees the memory pointed
42         -- by `handle'
43      do
44         if handle.is_not_null then
45            if is_shared then
46               debug
47                  print(once "Disposing a shared ")
48                  print(generating_type)
49                  print(once "; handle not freed%N")
50               end
51            else
52               debug
53                  print("Disposing an unshared ")
54                  print(generating_type)
55                  print(" and freeing its handle.%N")
56               end
57               free(handle)
58            end
59            handle := default_pointer
60         end
61      end
62
63feature {WRAPPER, WRAPPER_HANDLER}
64   is_shared: BOOLEAN
65      -- Does anybody else (Eiffel or non-Eiffel) have a reference
66      -- to `handle'? If False, then the C-object will be
67      -- destroyed when the the Eiffel object will be collected
68      -- (via destroy object). If True the C-object will not be destroyed.
69
70   set_shared
71      do
72         is_shared := True
73      end
74
75   unset_shared, set_unshared
76      do
77         is_shared := False
78      end
79
80end -- class MIXED_MEMORY_HANDLING