| 1 |
/* |
| 2 |
* box.c - boxes |
| 3 |
* |
| 4 |
* Copyright (c) 2010-2024 Shiro Kawai <[email protected]> |
| 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 |
#define LIBGAUCHE_BODY |
| 35 |
#include "gauche.h" |
| 36 |
#include "gauche/priv/configP.h" |
| 37 |
#include "gauche/priv/boxP.h" |
| 38 |
|
| 39 |
/* Srfi-195 extends box type to allow muliple values. We use separate |
| 40 |
types for single-value box and multi-value box, for we want to optimize |
| 41 |
single-value box; it is used internally for mutable local variables. */ |
| 42 |
|
| 43 |
/* Box is a heap-allocated object that can hold one Scheme value. |
| 44 |
* It is internally used to realize indirection for mutable local |
| 45 |
* variables. Conceptually, the compiler translates initialization, |
| 46 |
* reference and mutation of mutable local variables into construction, |
| 47 |
* dereference and mutation of box, as follows: |
| 48 |
* |
| 49 |
* Original code: |
| 50 |
* |
| 51 |
* (let ((var 1)) |
| 52 |
* ... |
| 53 |
* (foo var) |
| 54 |
* ... |
| 55 |
* (set! var 2) |
| 56 |
* ...) |
| 57 |
* |
| 58 |
* Translated: |
| 59 |
* |
| 60 |
* (let ((var (%make-box 1))) |
| 61 |
* ... |
| 62 |
* (foo (%box-ref var)) |
| 63 |
* ... |
| 64 |
* (%box-set! var 2) |
| 65 |
* ...) |
| 66 |
* |
| 67 |
* Local variables that are never set! won't be translated. |
| 68 |
* |
| 69 |
* After this translation, *all* local variables are immutable. It makes |
| 70 |
* optimization a lot easier. |
| 71 |
*/ |
| 72 |
|
| 73 |
/* SRFI-111 defines box API. Scheme interface is in libalpha.scm. */ |
| 74 |
/* NB: SRFI-111 leave equal? behavior implementation-dependent (except |
| 75 |
it must return #f if eqv? returns #f). We compare the contents. |
| 76 |
*/ |
| 77 |
static void box_print(ScmObj obj, ScmPort *port, |
| 78 |
ScmWriteContext *ctx SCM_UNUSED) |
| 79 |
{ |
| 80 |
Scm_Printf(port, "#<box %S>", SCM_BOX_VALUE(obj)); |
| 81 |
} |
| 82 |
|
| 83 |
static int box_compare(ScmObj x, ScmObj y, int equalp) |
| 84 |
{ |
| 85 |
if (equalp) { |
| 86 |
/* should return 0 if x == y */ |
| 87 |
return (Scm_EqualP(SCM_BOX_VALUE(x), SCM_BOX_VALUE(y))? 0:1); |
| 88 |
} else { |
| 89 |
return Scm_Compare(SCM_BOX_VALUE(x), SCM_BOX_VALUE(y)); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
SCM_DEFINE_BUILTIN_CLASS(Scm_BoxClass, box_print, box_compare, |
| 94 |
NULL, NULL, SCM_CLASS_DEFAULT_CPL); |
| 95 |
|
| 96 |
ScmBox *Scm_MakeBox(ScmObj value) |
| 97 |
{ |
| 98 |
ScmBox *b = SCM_NEW(ScmBox); |
| 99 |
SCM_SET_CLASS(b, &Scm_BoxClass); |
| 100 |
SCM_BOX_SET(b, value); |
| 101 |
return b; |
| 102 |
} |
| 103 |
|
| 104 |
/* MVBox is a box that can hold multiple values. |
| 105 |
*/ |
| 106 |
|
| 107 |
static void mvbox_print(ScmObj obj, ScmPort *port, |
| 108 |
ScmWriteContext *ctx SCM_UNUSED) |
| 109 |
{ |
| 110 |
Scm_Printf(port, "#<mv-box[%d]", SCM_MVBOX_SIZE(obj)); |
| 111 |
ScmObj *vs = SCM_MVBOX_VALUES(obj); |
| 112 |
for (ScmSmallInt i = 0; i < SCM_MVBOX_SIZE(obj); i++) { |
| 113 |
Scm_Printf(port, " %S", vs[i]); |
| 114 |
} |
| 115 |
Scm_Printf(port, ">"); |
| 116 |
} |
| 117 |
|
| 118 |
static int mvbox_compare(ScmObj x, ScmObj y, int equalp) |
| 119 |
{ |
| 120 |
ScmMVBox *mx = SCM_MVBOX(x); |
| 121 |
ScmMVBox *my = SCM_MVBOX(y); |
| 122 |
if (equalp) { |
| 123 |
/* should return 0 if x == y */ |
| 124 |
if (mx->size != my->size) return 1; |
| 125 |
for (ScmSmallInt i=0; i<mx->size; i++) { |
| 126 |
if (!Scm_EqualP(mx->values[i], my->values[i])) return 1; |
| 127 |
} |
| 128 |
return 0; |
| 129 |
} else { |
| 130 |
if (mx->size < my->size) return -1; |
| 131 |
if (mx->size > my->size) return 1; |
| 132 |
for (ScmSmallInt i=0; i<mx->size; i++) { |
| 133 |
int r = Scm_Compare(mx->values[i], my->values[i]); |
| 134 |
if (r < 0 || r > 0) return r; |
| 135 |
} |
| 136 |
return 0; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
SCM_DEFINE_BUILTIN_CLASS(Scm_MVBoxClass, mvbox_print, mvbox_compare, |
| 141 |
NULL, NULL, SCM_CLASS_DEFAULT_CPL); |
| 142 |
|
| 143 |
static ScmMVBox *make_mvbox(ScmSmallInt size) |
| 144 |
{ |
| 145 |
SCM_ASSERT(size == 0 || size >= 2); |
| 146 |
ScmSmallInt allocsize = (size == 0)? 1 : size; |
| 147 |
ScmMVBox *b = |
| 148 |
SCM_NEW2(ScmMVBox*, sizeof(ScmMVBox) + allocsize*sizeof(ScmObj)); |
| 149 |
SCM_SET_CLASS(b, &Scm_MVBoxClass); |
| 150 |
b->size = size; |
| 151 |
return b; |
| 152 |
} |
| 153 |
|
| 154 |
ScmMVBox *Scm_MakeMVBox(ScmSmallInt size, ScmObj init) |
| 155 |
{ |
| 156 |
ScmMVBox *b = make_mvbox(size); |
| 157 |
for (ScmSmallInt i=0; i<size; i++) { |
| 158 |
b->values[i] = init; |
| 159 |
} |
| 160 |
return b; |
| 161 |
} |
| 162 |
|
| 163 |
ScmMVBox *Scm_ListToMVBox(ScmObj elts) |
| 164 |
{ |
| 165 |
ScmSmallInt size = Scm_Length(elts); |
| 166 |
if (size < 0) Scm_Error("Improper list not allowed: %S", elts); |
| 167 |
ScmMVBox *b = make_mvbox(size); |
| 168 |
for (ScmSmallInt i=0; i<size; i++, elts = SCM_CDR(elts)) { |
| 169 |
b->values[i] = SCM_CAR(elts); |
| 170 |
} |
| 171 |
return b; |
| 172 |
} |
| 173 |
|
| 174 |
/* Atomic Box. |
| 175 |
* SRFI-230 defines four types of atomic boxes. |
| 176 |
* Internally all of them shared the same C structure, and derived |
| 177 |
* from a common base class, <atomic-base>. |
| 178 |
*/ |
| 179 |
|
| 180 |
static ScmObj atomic_box_allocate(ScmClass *klass, ScmObj initargs SCM_UNUSED) |
| 181 |
{ |
| 182 |
return SCM_OBJ(Scm_MakeAtomicBox(klass, SCM_UNDEFINED)); |
| 183 |
} |
| 184 |
|
| 185 |
static void atomic_box_print(ScmObj obj, ScmPort *port, |
| 186 |
ScmWriteContext *ctx SCM_UNUSED) |
| 187 |
{ |
| 188 |
Scm_Printf(port, "#<%A %S>", |
| 189 |
Scm_ShortClassName(Scm_ClassOf(obj)), |
| 190 |
Scm_AtomicBoxRef(SCM_ATOMIC_BOX(obj))); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
SCM_DEFINE_BUILTIN_CLASS(Scm_AtomicBaseClass, atomic_box_print, NULL, NULL, |
| 195 |
atomic_box_allocate, |
| 196 |
SCM_CLASS_DEFAULT_CPL); |
| 197 |
|
| 198 |
static ScmClass *atomic_box_cpl[] = { |
| 199 |
SCM_CLASS_STATIC_PTR(Scm_AtomicBaseClass), |
| 200 |
SCM_CLASS_STATIC_PTR(Scm_TopClass), |
| 201 |
NULL |
| 202 |
}; |
| 203 |
|
| 204 |
SCM_DEFINE_BUILTIN_CLASS(Scm_AtomicBoxClass, atomic_box_print, NULL, NULL, |
| 205 |
atomic_box_allocate, |
| 206 |
atomic_box_cpl); |
| 207 |
SCM_DEFINE_BUILTIN_CLASS(Scm_AtomicFlagClass, atomic_box_print, NULL, NULL, |
| 208 |
atomic_box_allocate, |
| 209 |
atomic_box_cpl); |
| 210 |
SCM_DEFINE_BUILTIN_CLASS(Scm_AtomicFxboxClass, atomic_box_print, NULL, NULL, |
| 211 |
atomic_box_allocate, |
| 212 |
atomic_box_cpl); |
| 213 |
SCM_DEFINE_BUILTIN_CLASS(Scm_AtomicPairClass, atomic_box_print, NULL, NULL, |
| 214 |
atomic_box_allocate, |
| 215 |
atomic_box_cpl); |
| 216 |
|
| 217 |
ScmAtomicBox *Scm_MakeAtomicBox(ScmClass *klass, ScmObj obj) |
| 218 |
{ |
| 219 |
SCM_ASSERT(Scm_SubclassP(klass, SCM_CLASS_ATOMIC_BASE)); |
| 220 |
ScmAtomicBox *z = SCM_NEW(ScmAtomicBox); |
| 221 |
SCM_SET_CLASS(z, klass); |
| 222 |
Scm_AtomicStoreFull(&z->val, (ScmAtomicWord)obj); |
| 223 |
return z; |
| 224 |
} |
| 225 |
|
| 226 |
ScmObj Scm_AtomicBoxRef(ScmAtomicBox *abox) |
| 227 |
{ |
| 228 |
return SCM_OBJ(Scm_AtomicLoad(&abox->val)); |
| 229 |
} |
| 230 |
|
| 231 |
void Scm_AtomicBoxSet(ScmAtomicBox *abox, ScmObj obj) |
| 232 |
{ |
| 233 |
Scm_AtomicStoreFull(&abox->val, (ScmAtomicWord)obj); |
| 234 |
} |
| 235 |
|
| 236 |
ScmObj Scm_AtomicBoxSwap(ScmAtomicBox *abox, ScmObj obj) |
| 237 |
{ |
| 238 |
ScmAtomicWord old = Scm_AtomicExchange(&abox->val, (ScmAtomicWord)obj); |
| 239 |
return SCM_OBJ(old); |
| 240 |
} |
| 241 |
|
| 242 |
/* Returns old value. Caller can compare expected and returned value |
| 243 |
to see if swap is succeeded. */ |
| 244 |
ScmObj Scm_AtomicBoxCompareAndSwap(ScmAtomicBox *abox, |
| 245 |
ScmObj expected, |
| 246 |
ScmObj obj) |
| 247 |
{ |
| 248 |
ScmAtomicWord desired = (ScmAtomicWord)obj; |
| 249 |
ScmAtomicWord old = (ScmAtomicWord)expected;; |
| 250 |
(void)Scm_AtomicCompareExchange(&abox->val, &old, desired); |
| 251 |
return SCM_OBJ(old); |
| 252 |
} |