1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
37 #define LEAFSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_LEAF))
38 #define BRANCHSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_BRANCH))
40 #define LAYERSHIFT(r) ( (r)->layers==0 ? RAA_BLKSHIFT : RAA_LAYERSHIFT )
42 static struct RAA *real_raa_init(int layers)
48 r = nasm_zalloc(LEAFSIZ);
51 r = nasm_malloc(BRANCHSIZ);
53 for (i = 0; i < RAA_LAYERSIZE; i++)
54 r->u.b.data[i] = NULL;
56 (RAA_BLKSHIFT - RAA_LAYERSHIFT) + layers * RAA_LAYERSHIFT;
61 struct RAA *raa_init(void)
63 return real_raa_init(0);
66 void raa_free(struct RAA *r)
70 for (p = r->u.b.data; p - r->u.b.data < RAA_LAYERSIZE; p++)
77 int64_t raa_read(struct RAA *r, int32_t posn)
79 if ((uint32_t) posn >= (UINT32_C(1) << (r->shift + LAYERSHIFT(r))))
80 return 0; /* Return 0 for undefined entries */
81 while (r->layers > 0) {
82 int32_t l = posn >> r->shift;
83 posn &= (UINT32_C(1) << r->shift) - 1;
86 return 0; /* Return 0 for undefined entries */
88 return r->u.l.data[posn];
91 struct RAA *raa_write(struct RAA *r, int32_t posn, int64_t value)
95 nasm_assert(posn >= 0);
97 while ((UINT32_C(1) << (r->shift + LAYERSHIFT(r))) <= (uint32_t) posn) {
104 s = nasm_malloc(BRANCHSIZ);
105 for (i = 0; i < RAA_LAYERSIZE; i++)
106 s->u.b.data[i] = NULL;
107 s->layers = r->layers + 1;
108 s->shift = LAYERSHIFT(r) + r->shift;
115 while (r->layers > 0) {
117 int32_t l = posn >> r->shift;
118 posn &= (UINT32_C(1) << r->shift) - 1;
121 *s = real_raa_init(r->layers - 1);
125 r->u.l.data[posn] = value;