1 #pragma once
2 
3 /** @file the_Foundation/garbage.h  Garbage collector.
4 
5 @authors Copyright (c) 2017 Jaakko Keränen <jaakko.keranen@iki.fi>
6 
7 @par License
8 
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
11 
12 1. Redistributions of source code must retain the above copyright notice, this
13    list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15    this list of conditions and the following disclaimer in the documentation
16    and/or other materials provided with the distribution.
17 
18 <small>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</small>
28 */
29 
30 #include "defs.h"
31 
32 #include <stdlib.h>
33 
34 iBeginPublic
35 
36 void        init_Garbage        (void);
37 
38 void        recycle_Garbage     (void);
39 void        beginScope_Garbage  (void);
40 void        endScope_Garbage    (void);
41 
42 iAny *      collect_Garbage     (iAny *ptr, iDeleteFunc del);
43 
44 #if !defined (__cplusplus)
iCollectMem(iAny * ptr)45 iLocalDef iAny *iCollectMem(iAny *ptr) { return collect_Garbage(ptr, free); }
46 #endif
47 
48 #define iCollectDel(ptr, del)   collect_Garbage(ptr, (iDeleteFunc) (del))
49 
iBeginCollect(void)50 iLocalDef void      iBeginCollect (void) { beginScope_Garbage(); }
iEndCollect(void)51 iLocalDef void      iEndCollect   (void) { endScope_Garbage(); }
iRecycle(void)52 iLocalDef void      iRecycle      (void) { recycle_Garbage(); }
53 
54 iEndPublic
55 
56 #if defined (__cplusplus)
57 template <typename T>
collect_Garbage(T * ptr,iDeleteFunc del)58 inline T *collect_Garbage(T *ptr, iDeleteFunc del) {
59     return reinterpret_cast<T *>(collect_Garbage(reinterpret_cast<iAny *>(ptr), del));
60 }
61 template <typename T>
iCollectMem(T * ptr)62 inline T *iCollectMem(T *ptr) {
63     return collect_Garbage(ptr, free);
64 }
65 #endif
66