1 /*
2  * weak.h - Public API for weak pointers
3  *
4  *   Copyright (c) 2000-2020  Shiro Kawai  <shiro@acm.org>
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *   1. Redistributions of source code must retain the above copyright
11  *      notice, this list of conditions and the following disclaimer.
12  *
13  *   2. Redistributions in binary form must reproduce the above copyright
14  *      notice, this list of conditions and the following disclaimer in the
15  *      documentation and/or other materials provided with the distribution.
16  *
17  *   3. Neither the name of the authors nor the names of its contributors
18  *      may be used to endorse or promote products derived from this
19  *      software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27  *   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28  *   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  *   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /* This file is included from gauche.h */
35 
36 #ifndef GAUCHE_WEAK_H
37 #define GAUCHE_WEAK_H
38 
39 /*================================================================
40  * Weak box
41  */
42 
43 typedef struct ScmWeakBoxRec ScmWeakBox; /* opaque */
44 
45 SCM_EXTERN ScmWeakBox *Scm_MakeWeakBox(void *value);
46 SCM_EXTERN int         Scm_WeakBoxEmptyP(ScmWeakBox *wbox);
47 SCM_EXTERN void        Scm_WeakBoxSet(ScmWeakBox *wbox, void *value);
48 SCM_EXTERN void       *Scm_WeakBoxRef(ScmWeakBox *wbox);
49 
50 /*================================================================
51  * Weak vector
52  */
53 
54 typedef struct ScmWeakVectorRec {
55     SCM_HEADER;
56     ScmSmallInt size;
57     void *pointers;  /* opaque */
58 } ScmWeakVector;
59 
60 #define SCM_WEAK_VECTOR(obj)   ((ScmWeakVector*)(obj))
61 #define SCM_WEAK_VECTOR_P(obj)  SCM_XTYPEP(obj, SCM_CLASS_WEAK_VECTOR)
62 SCM_CLASS_DECL(Scm_WeakVectorClass);
63 #define SCM_CLASS_WEAK_VECTOR  (&Scm_WeakVectorClass)
64 
65 SCM_EXTERN ScmObj Scm_MakeWeakVector(ScmSmallInt size);
66 SCM_EXTERN ScmObj Scm_WeakVectorRef(ScmWeakVector *v,
67                                     ScmSmallInt index, ScmObj fallback);
68 SCM_EXTERN ScmObj Scm_WeakVectorSet(ScmWeakVector *v,
69                                     ScmSmallInt index, ScmObj val);
70 
71 /*================================================================
72  * Weak hash tables
73  */
74 
75 typedef enum {
76     SCM_WEAK_KEY   = (1L<<0),
77     SCM_WEAK_VALUE = (1L<<1),
78     SCM_WEAK_BOTH  = (SCM_WEAK_KEY|SCM_WEAK_VALUE)
79 } ScmWeakness;
80 
81 
82 typedef struct ScmWeakHashTableRec {
83     SCM_HEADER;
84     ScmWeakness weakness;
85     ScmHashType type;
86     ScmHashCore core;
87     ScmObj      defaultValue;
88     ScmHashProc        *hashfn;
89     ScmHashCompareProc *cmpfn;
90     u_int       goneEntries;
91 } ScmWeakHashTable;
92 
93 typedef struct ScmWeakHashIterRec {
94     ScmWeakHashTable *table;
95     ScmHashIter iter;
96 } ScmWeakHashIter;
97 
98 
99 SCM_CLASS_DECL(Scm_WeakHashTableClass);
100 #define SCM_CLASS_WEAK_HASH_TABLE     (&Scm_WeakHashTableClass)
101 #define SCM_WEAK_HASH_TABLE(obj)      ((ScmWeakHashTable*)(obj))
102 #define SCM_WEAK_HASH_TABLE_P(obj)    SCM_ISA(obj, SCM_CLASS_WEAK_HASH_TABLE)
103 #define SCM_WEAK_HASH_TABLE_CORE(obj) (&SCM_WEAK_HASH_TABLE(obj)->core)
104 
105 SCM_EXTERN ScmObj Scm_MakeWeakHashTableSimple(ScmHashType type,
106                                               ScmWeakness weakness,
107                                               int initSize,
108                                               ScmObj defaultValue);
109 
110 SCM_EXTERN ScmObj Scm_WeakHashTableCopy(ScmWeakHashTable *tab);
111 SCM_EXTERN ScmObj Scm_WeakHashTableRef(ScmWeakHashTable *ht,
112                                        ScmObj key, ScmObj fallback);
113 SCM_EXTERN ScmObj Scm_WeakHashTableSet(ScmWeakHashTable *ht,
114                                        ScmObj key, ScmObj value, int flags);
115 SCM_EXTERN ScmObj Scm_WeakHashTableDelete(ScmWeakHashTable *ht, ScmObj key);
116 SCM_EXTERN ScmObj Scm_WeakHashTableKeys(ScmWeakHashTable *ht);
117 SCM_EXTERN ScmObj Scm_WeakHashTableValues(ScmWeakHashTable *ht);
118 
119 SCM_EXTERN void   Scm_WeakHashIterInit(ScmWeakHashIter *iter,
120                                        ScmWeakHashTable *ht);
121 SCM_EXTERN int    Scm_WeakHashIterNext(ScmWeakHashIter *iter,
122                                        ScmObj *key, ScmObj *value);
123 
124 #endif /* GAUCHE_WEAK_H */
125 
126