/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

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