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