bt_conv.c

Go to the documentation of this file.
00001 /*-
00002  * Copyright (c) 1990, 1993, 1994
00003  * The Regents of the University of California.  All rights reserved.
00004  *
00005  * This code is derived from software contributed to Berkeley by
00006  * Mike Olson.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  * 3. All advertising materials mentioning features or use of this software
00017  *    must display the following acknowledgement:
00018  * This product includes software developed by the University of
00019  * California, Berkeley and its contributors.
00020  * 4. Neither the name of the University nor the names of its contributors
00021  *    may be used to endorse or promote products derived from this software
00022  *    without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00025  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00027  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00030  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00031  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00033  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00034  * SUCH DAMAGE.
00035  */
00036 
00037 #if defined(LIBC_SCCS) && !defined(lint)
00038 static char sccsid[] = "@(#)bt_conv.c  8.5 (Berkeley) 8/17/94";
00039 #endif /* LIBC_SCCS and not lint */
00040 
00041 #include <sys/param.h>
00042 
00043 #include <stdio.h>
00044 
00045 #include "../include/db.h"
00046 #include "btree.h"
00047 
00048 static void mswap __P((PAGE *));
00049 
00050 /*
00051  * __BT_BPGIN, __BT_BPGOUT --
00052  * Convert host-specific number layout to/from the host-independent
00053  * format stored on disk.
00054  *
00055  * Parameters:
00056  * t: tree
00057  * pg:   page number
00058  * h: page to convert
00059  */
00060 void
00061 __bt_pgin(t, pg, pp)
00062    void *t;
00063    pgno_t pg;
00064    void *pp;
00065 {
00066    PAGE *h;
00067    indx_t i, top;
00068    u_char flags;
00069    char *p;
00070 
00071    if (!F_ISSET(((BTREE *)t), B_NEEDSWAP))
00072       return;
00073    if (pg == P_META) {
00074       mswap(pp);
00075       return;
00076    }
00077 
00078    h = pp;
00079    M_32_SWAP(h->pgno);
00080    M_32_SWAP(h->prevpg);
00081    M_32_SWAP(h->nextpg);
00082    M_32_SWAP(h->flags);
00083    M_16_SWAP(h->lower);
00084    M_16_SWAP(h->upper);
00085 
00086    top = NEXTINDEX(h);
00087    if ((h->flags & P_TYPE) == P_BINTERNAL)
00088       for (i = 0; i < top; i++) {
00089          M_16_SWAP(h->linp[i]);
00090          p = (char *)GETBINTERNAL(h, i);
00091          P_32_SWAP(p);
00092          p += sizeof(u_int32_t);
00093          P_32_SWAP(p);
00094          p += sizeof(pgno_t);
00095          if (*(u_char *)p & P_BIGKEY) {
00096             p += sizeof(u_char);
00097             P_32_SWAP(p);
00098             p += sizeof(pgno_t);
00099             P_32_SWAP(p);
00100          }
00101       }
00102    else if ((h->flags & P_TYPE) == P_BLEAF)
00103       for (i = 0; i < top; i++) {
00104          M_16_SWAP(h->linp[i]);
00105          p = (char *)GETBLEAF(h, i);
00106          P_32_SWAP(p);
00107          p += sizeof(u_int32_t);
00108          P_32_SWAP(p);
00109          p += sizeof(u_int32_t);
00110          flags = *(u_char *)p;
00111          if (flags & (P_BIGKEY | P_BIGDATA)) {
00112             p += sizeof(u_char);
00113             if (flags & P_BIGKEY) {
00114                P_32_SWAP(p);
00115                p += sizeof(pgno_t);
00116                P_32_SWAP(p);
00117             }
00118             if (flags & P_BIGDATA) {
00119                p += sizeof(u_int32_t);
00120                P_32_SWAP(p);
00121                p += sizeof(pgno_t);
00122                P_32_SWAP(p);
00123             }
00124          }
00125       }
00126 }
00127 
00128 void
00129 __bt_pgout(t, pg, pp)
00130    void *t;
00131    pgno_t pg;
00132    void *pp;
00133 {
00134    PAGE *h;
00135    indx_t i, top;
00136    u_char flags;
00137    char *p;
00138 
00139    if (!F_ISSET(((BTREE *)t), B_NEEDSWAP))
00140       return;
00141    if (pg == P_META) {
00142       mswap(pp);
00143       return;
00144    }
00145 
00146    h = pp;
00147    top = NEXTINDEX(h);
00148    if ((h->flags & P_TYPE) == P_BINTERNAL)
00149       for (i = 0; i < top; i++) {
00150          p = (char *)GETBINTERNAL(h, i);
00151          P_32_SWAP(p);
00152          p += sizeof(u_int32_t);
00153          P_32_SWAP(p);
00154          p += sizeof(pgno_t);
00155          if (*(u_char *)p & P_BIGKEY) {
00156             p += sizeof(u_char);
00157             P_32_SWAP(p);
00158             p += sizeof(pgno_t);
00159             P_32_SWAP(p);
00160          }
00161          M_16_SWAP(h->linp[i]);
00162       }
00163    else if ((h->flags & P_TYPE) == P_BLEAF)
00164       for (i = 0; i < top; i++) {
00165          p = (char *)GETBLEAF(h, i);
00166          P_32_SWAP(p);
00167          p += sizeof(u_int32_t);
00168          P_32_SWAP(p);
00169          p += sizeof(u_int32_t);
00170          flags = *(u_char *)p;
00171          if (flags & (P_BIGKEY | P_BIGDATA)) {
00172             p += sizeof(u_char);
00173             if (flags & P_BIGKEY) {
00174                P_32_SWAP(p);
00175                p += sizeof(pgno_t);
00176                P_32_SWAP(p);
00177             }
00178             if (flags & P_BIGDATA) {
00179                p += sizeof(u_int32_t);
00180                P_32_SWAP(p);
00181                p += sizeof(pgno_t);
00182                P_32_SWAP(p);
00183             }
00184          }
00185          M_16_SWAP(h->linp[i]);
00186       }
00187 
00188    M_32_SWAP(h->pgno);
00189    M_32_SWAP(h->prevpg);
00190    M_32_SWAP(h->nextpg);
00191    M_32_SWAP(h->flags);
00192    M_16_SWAP(h->lower);
00193    M_16_SWAP(h->upper);
00194 }
00195 
00196 /*
00197  * MSWAP -- Actually swap the bytes on the meta page.
00198  *
00199  * Parameters:
00200  * p: page to convert
00201  */
00202 static void
00203 mswap(pg)
00204    PAGE *pg;
00205 {
00206    char *p;
00207 
00208    p = (char *)pg;
00209    P_32_SWAP(p);     /* magic */
00210    p += sizeof(u_int32_t);
00211    P_32_SWAP(p);     /* version */
00212    p += sizeof(u_int32_t);
00213    P_32_SWAP(p);     /* psize */
00214    p += sizeof(u_int32_t);
00215    P_32_SWAP(p);     /* free */
00216    p += sizeof(u_int32_t);
00217    P_32_SWAP(p);     /* nrecs */
00218    p += sizeof(u_int32_t);
00219    P_32_SWAP(p);     /* flags */
00220    p += sizeof(u_int32_t);
00221 }

Generated on Thu Apr 16 06:27:16 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6