1 /* 2 * pairP.h - Pair API private header 3 * 4 * Copyright (c) 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 #ifndef GAUCHE_PRIV_PAIRP_H 35 #define GAUCHE_PRIV_PAIRP_H 36 37 /* ScmExtendedPair actually has a hidden tagged pointer before it. The 38 * user won't see it. 39 * 40 * The tagged pointer is the same as usual class tag ('111' lower bits) but 41 * point to ScmExtendedPairDescriptor instead of ScmClass. 42 * 43 * Read access to car and cdr is simply a single pointer reference, with no 44 * overhead. Mutations on an extended pair, OTOH, are intercepted by setCar 45 * and setCdr procedure of ScmExtendedPairDescriptor, which can do tricks. 46 * 47 * We distinguish a normal pair and an extended pair by looking at the address. 48 * Both normal pairs and ScmRealExtendedPairs are aligned on even word boundary. 49 * That makes ScmExtendedPair to be on odd word boundary. 50 * 51 * Statically allocated pairs are a bit tricky; if the compiler has a way 52 * to specify alignment, we use it. Otherwise, we need to do extra check 53 * that looks at the previous word when a pointer to a pair falls on 54 * on an odd-word---the previous word of normal pair will never have the 55 * low-bit tag 111. 56 * (It is safe to do so---all heap allocated objects are on even-word boundary, 57 * so only the statically allocated pairs matter. And we generate a dummy 58 * pair at the beginning of every static ScmPair array.) 59 * 60 * The reason we don't use the class pointer in the first word is that 61 * we don't want to split classes for mutable and immutable pairs. We'd rather 62 * see mutability as 'const' qualifier of C, rather than different types. 63 * One reason is that existing code expects <pair> for both (class-of '(a . b)) 64 * and (class-of (cons 'a 'b)) --- we could've subclass <pair> for mutable 65 * and immutable pairs, but that'll break existing code which tests equality 66 * of class, rather than is-a relationship. 67 */ 68 typedef struct ScmExtendedPairDescriptorRec { 69 ScmClass *klass; 70 u_long flags; 71 void (*setCar)(ScmObj, ScmObj); 72 void (*setCdr)(ScmObj, ScmObj); 73 } ScmExtendedPairDescriptor; 74 75 typedef struct ScmRealExtendedPairRec { 76 ScmWord hiddenTag; 77 ScmExtendedPair data; 78 } ScmRealExtendedPair; 79 80 /* Pair flags */ 81 enum { 82 SCM_PAIR_IMMUTABLE = (1L<<0) 83 }; 84 85 SCM_EXTERN ScmExtendedPairDescriptor *Scm__GetExtendedPairDescriptor(ScmObj); 86 SCM_EXTERN void Scm__InitIPairClass(ScmClass *klass); 87 88 #endif /*GAUCHE_PRIV_PAIRP_H*/ 89