/src/lib/storage/low_level/recycling_pool.e

http://github.com/tybor/Liberty · Specman e · 105 lines · 43 code · 7 blank · 55 comment · 0 complexity · dc1447c23f88b0c498dfe4fff161e56f MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. class RECYCLING_POOL[R_ -> RECYCLABLE]
  5. --
  6. -- RECYCLABLE objects container.
  7. --
  8. -- Useful to implement recycling support in libraries.
  9. --
  10. -- Has no sense for expanded types (it will not even compile).
  11. --
  12. -- This class is designed to work with and without the garbage collector. It takes advantage of the GC if
  13. -- it is used, but it also minimises the memory footprint when the GC is ''not'' used.
  14. --
  15. -- See also STRING_RECYCLING_POOL, especially tuned for STRING usage.
  16. --
  17. insert
  18. STACK[R_]
  19. rename
  20. item as collection_item,
  21. recycle as stack_recycle
  22. export
  23. {RECYCLING_POOL} lower, upper, storage, count;
  24. {ANY} is_empty;
  25. {} all
  26. redefine
  27. mark_native_arrays
  28. end
  29. ANY
  30. -- To get reasonable default exports
  31. undefine default_create, out_in_tagged_out_memory, copy, is_equal
  32. end
  33. create {ANY}
  34. make
  35. create {RECYCLING_POOL}
  36. collection_make
  37. feature {ANY}
  38. item: R_
  39. --
  40. -- Returns a recycled object, if there is one to be obtained. Returns Void otherwise.
  41. --
  42. -- See also `recycle'
  43. --
  44. require
  45. not is_empty
  46. do
  47. Result := top
  48. pop
  49. ensure
  50. Result /= Void
  51. end
  52. recycle (an_item: like item)
  53. --
  54. -- Stores the object as being reusable. Automatically calls the "recycle" feature of the object.
  55. --
  56. -- Two notes:
  57. --
  58. -- * You should not directly reuse the object after it is recycled; call `item' instead to obtain a
  59. -- fresh reference. Using it directly will lead to classic problems of double referencing. In a
  60. -- nutshell, '''be sure not to hold any reference to a recycled object'''.
  61. --
  62. -- * If the GC is used it may free some recycled objects so using a RECYCLING_POOL does not interfere
  63. -- with memory conservation.
  64. --
  65. -- See also `item'
  66. --
  67. require
  68. an_item /= Void
  69. do
  70. an_item.recycle
  71. push(an_item)
  72. end
  73. feature {}
  74. mark_native_arrays
  75. do
  76. clear_count
  77. end
  78. end -- class RECYCLING_POOL
  79. --
  80. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  81. --
  82. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  83. -- of this software and associated documentation files (the "Software"), to deal
  84. -- in the Software without restriction, including without limitation the rights
  85. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  86. -- copies of the Software, and to permit persons to whom the Software is
  87. -- furnished to do so, subject to the following conditions:
  88. --
  89. -- The above copyright notice and this permission notice shall be included in
  90. -- all copies or substantial portions of the Software.
  91. --
  92. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  93. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  94. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  95. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  96. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  97. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  98. -- THE SOFTWARE.