xdr.cc

00001 /* 
00002    EyeDB Object Database Management System
00003    Copyright (C) 1994-2008 SYSRA
00004    
00005    EyeDB is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009    
00010    EyeDB is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014    
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA 
00018 */
00019 
00020 /*
00021   Author: Eric Viara <viara@sysra.com>
00022 */
00023 
00024 
00025 #include <eyedbconfig.h>
00026 
00027 #include "kern_p.h"
00028 
00029 #define NEW_OID_CODE
00030 
00031 namespace eyedbsm {
00032 
00033   void x2h_oidloc(OidLoc *oidloc, const void *addr)
00034   {
00035     memcpy(oidloc, addr, OIDLOCSIZE);
00036 #ifndef EYEDBLIB_BIG_ENDIAN
00037     oidloc->ns = x2h_u32(oidloc->ns);
00038     oidloc->datid = x2h_16(oidloc->datid);
00039 #endif
00040   }
00041 
00042   // #2 argument is conceptually constant
00043   void h2x_oidloc(void *addr, OidLoc *oidloc)
00044   {
00045 #ifndef EYEDBLIB_BIG_ENDIAN
00046     oidloc->ns = h2x_u32(oidloc->ns);
00047     oidloc->datid = h2x_16(oidloc->datid);
00048 #endif
00049     memcpy(addr, oidloc, OIDLOCSIZE);
00050   }
00051 
00052   struct OidCode {
00053     unsigned int x[2];
00054   };
00055 
00056   void x2h_oid(Oid *hoid, const void *xoid)
00057   {
00058 #ifndef EYEDBLIB_BIG_ENDIAN
00059 
00060 #ifdef NEW_OID_CODE
00061     OidCode o;
00062     memcpy(&o, xoid, sizeof(o));
00063 
00064     o.x[0] = x2h_u32(o.x[0]);
00065     o.x[1] = x2h_u32(o.x[1]);
00066 
00067     hoid->setNX(o.x[0]);
00068     hoid->setDbID((o.x[1] >> Oid_UNIQUE) & 0x3ff);
00069     hoid->setUnique(o.x[1] & 0x3fffff);
00070 
00071 #else
00072     unsigned long long l = 0;
00073     memcpy(&l, xoid, sizeof(l));
00074     l = x2h_u64(l);
00075 
00076     hoid->setNX(l >> Oid_NX);
00077     hoid->setDbID((l >> Oid_UNIQUE) & 0x3ff);
00078     hoid->setUnique(l & 0x3fffff);
00079 #endif
00080 
00081 
00082 #else
00083     if (hoid != xoid)
00084       memcpy(hoid, xoid, sizeof(Oid));
00085 #endif
00086   }
00087 
00088   void h2x_oid(void *xoid, const Oid *hoid)
00089   {
00090 #ifndef EYEDBLIB_BIG_ENDIAN
00091 
00092 #ifdef NEW_OID_CODE
00093     OidCode o;
00094     o.x[0] = h2x_u32(hoid->getNX());
00095     o.x[1] = h2x_u32(hoid->getDbID() << Oid_UNIQUE | hoid->getUnique());
00096 
00097     memcpy(xoid, &o, sizeof(o));
00098 #else
00099     unsigned long long l = h2x_u64(((unsigned long long)hoid->getNX() << Oid_NX |
00100                                     hoid->getDbID() << Oid_UNIQUE | hoid->getUnique()));
00101     memcpy(xoid, &l, sizeof(l));
00102 #endif
00103 
00104 #else
00105     if (hoid != xoid)
00106       memcpy(xoid, hoid, sizeof(Oid));
00107 
00108 #endif
00109   }
00110 
00111   void x2h_oids(Oid *hoid, const void *xoid, unsigned int cnt)
00112   {
00113 #ifdef EYEDBLIB_BIG_ENDIAN
00114     if (hoid != xoid)
00115 #endif
00116       for (int i = 0; i < cnt; i++)
00117         x2h_oid(&hoid[i], (char *)xoid+i*sizeof(Oid)); //, &xoid[i]);
00118   }
00119 
00120   void h2x_oids(void *xoid, const Oid *hoid, unsigned int cnt)
00121   {
00122 #ifdef EYEDBLIB_BIG_ENDIAN
00123     if (hoid != xoid)
00124 #endif
00125       for (int i = 0; i < cnt; i++)
00126         h2x_oid((char *)xoid+i*sizeof(Oid), &hoid[i]);
00127   }
00128 
00129   int cmp_oid(const void *xoid, const Oid *hoid)
00130   {
00131     Oid hoid_t;
00132     x2h_oid(&hoid_t, xoid);
00133     return memcmp(&hoid_t, hoid, sizeof(Oid));
00134   }
00135 
00136   int cmp_oids(const void *xoid, const Oid *hoid, unsigned int cnt)
00137   {
00138     for (int i = 0; i < cnt; i++) {
00139       Oid hoid_t;
00140       x2h_oid(&hoid_t, (char *)xoid+i*sizeof(Oid));
00141       int r = memcmp(&hoid_t, &hoid[i], sizeof(Oid));
00142       if (r) return r;
00143     }
00144   
00145     return 0;
00146   }
00147 
00148   unsigned int x2h_getSize(unsigned int size)
00149   {
00150     return getSize(x2h_u32(size));
00151   }
00152 
00153   unsigned int x2h_makeValid(unsigned int size)
00154   {
00155     return makeValid(x2h_u32(size));
00156   }
00157 
00158   void x2h_mapHeader(MapHeader *hmap, const MapHeader *_xmap)
00159   {
00160     MapHeader *xmap = const_cast<MapHeader *>(_xmap);
00161     hmap->mtype() = x2h_16(xmap->mtype());
00162     hmap->sizeslot() = x2h_u32(xmap->sizeslot());
00163     hmap->pow2() = x2h_u32(xmap->pow2());
00164     hmap->nslots() = x2h_u32(xmap->nslots());
00165     hmap->nbobjs() = x2h_u32(xmap->nbobjs());
00166     hmap->mstat_mtype() = x2h_16(xmap->mstat_mtype());
00167 
00168     if (hmap->mtype() == BitmapType) {
00169       hmap->u_bmh_slot_cur() = x2h_u32(xmap->u_bmh_slot_cur());
00170       hmap->u_bmh_slot_lastbusy() = x2h_u32(xmap->u_bmh_slot_lastbusy());
00171       hmap->u_bmh_retry() = x2h_u16(xmap->u_bmh_retry());
00172 
00173       hmap->mstat_u_bmstat_obj_count() = x2h_u32(xmap->mstat_u_bmstat_obj_count());
00174       hmap->mstat_u_bmstat_busy_slots() = x2h_u32(xmap->mstat_u_bmstat_busy_slots());
00175       hmap->mstat_u_bmstat_busy_size() = x2h_u64(xmap->mstat_u_bmstat_busy_size());
00176       hmap->mstat_u_bmstat_hole_size() = x2h_u64(xmap->mstat_u_bmstat_hole_size());
00177     }
00178     else if (hmap->mtype() == LinkmapType) {
00179       abort();
00180     }
00181     else
00182       abort();
00183   }
00184 
00185   void h2x_mapHeader(MapHeader *xmap, const MapHeader *_hmap)
00186   {
00187     MapHeader *hmap = const_cast<MapHeader *>(_hmap);
00188     unsigned short mtype = hmap->mtype();
00189     xmap->mtype() = h2x_16(hmap->mtype());
00190     xmap->sizeslot() = h2x_u32(hmap->sizeslot());
00191     xmap->pow2() = h2x_u32(hmap->pow2());
00192     xmap->nslots() = h2x_u32(hmap->nslots());
00193     xmap->nbobjs() = h2x_u32(hmap->nbobjs());
00194     xmap->mstat_mtype() = h2x_16(hmap->mstat_mtype());
00195 
00196     if (mtype == BitmapType) {
00197       xmap->u_bmh_slot_cur() = h2x_u32(hmap->u_bmh_slot_cur());
00198       xmap->u_bmh_slot_lastbusy() = h2x_u32(hmap->u_bmh_slot_lastbusy());
00199       xmap->u_bmh_retry() = h2x_u16(hmap->u_bmh_retry());
00200 
00201       xmap->mstat_u_bmstat_obj_count() = h2x_u32(hmap->mstat_u_bmstat_obj_count());
00202       xmap->mstat_u_bmstat_busy_slots() = h2x_u32(hmap->mstat_u_bmstat_busy_slots());
00203       xmap->mstat_u_bmstat_busy_size() = h2x_u64(hmap->mstat_u_bmstat_busy_size());
00204       xmap->mstat_u_bmstat_hole_size() = h2x_u64(hmap->mstat_u_bmstat_hole_size());
00205     }
00206     else if (mtype == LinkmapType) {
00207       abort();
00208     }
00209     else
00210       abort();
00211   }
00212 
00213   void x2h_datafileDesc(DatafileDesc *hdat,
00214                         const DatafileDesc *_xdat)
00215   {
00216     DatafileDesc *xdat = const_cast<DatafileDesc *>(_xdat);
00217     hdat->__maxsize() = x2h_u32(xdat->__maxsize());
00218     x2h_mapHeader(hdat->mp(), xdat->mp());
00219     hdat->__lastslot() = x2h_u32(xdat->__lastslot());
00220     unsigned short __dspid = xdat->__dspid();
00221     hdat->__dspid() = x2h_16(xdat->__dspid());
00222 
00223     memcpy(hdat->file(), xdat->file(), L_FILENAME);
00224     memcpy(hdat->name(), xdat->name(), L_NAME+1);
00225   }
00226 
00227   void h2x_datafileDesc(DatafileDesc *xdat,
00228                         const DatafileDesc *_hdat)
00229   {
00230     DatafileDesc *hdat = const_cast<DatafileDesc *>(_hdat);
00231     xdat->__maxsize() = h2x_u32(hdat->__maxsize());
00232     h2x_mapHeader(xdat->mp(), hdat->mp());
00233     xdat->__lastslot() = h2x_u32(hdat->__lastslot());
00234     unsigned short __dspid = hdat->__dspid();
00235     xdat->__dspid() = h2x_16(hdat->__dspid());
00236 
00237     memcpy(xdat->file(), hdat->file(), L_FILENAME);
00238     memcpy(xdat->name(), hdat->name(), L_NAME+1);
00239   }
00240 
00241 
00242   void x2h_dataspaceDesc(DataspaceDesc *hdsp,
00243                          const DataspaceDesc *_xdsp)
00244   {
00245     DataspaceDesc *xdsp = const_cast<DataspaceDesc *>(_xdsp);
00246     hdsp->__cur() = x2h_32(xdsp->__cur());
00247     hdsp->__ndat() = x2h_32(xdsp->__ndat());
00248     for (int i = 0; i < hdsp->__ndat(); i++)
00249       hdsp->__datid(i) = x2h_16(xdsp->__datid(i));
00250 
00251     memcpy(hdsp->name(), xdsp->name(), L_NAME+1);
00252   }
00253 
00254   void h2x_dataspaceDesc(DataspaceDesc *xdsp,
00255                          const DataspaceDesc *_hdsp)
00256   {
00257     DataspaceDesc *hdsp = const_cast<DataspaceDesc *>(_hdsp);
00258     unsigned int ndat = hdsp->__ndat();
00259 
00260     xdsp->__cur() = h2x_32(hdsp->__cur());
00261     xdsp->__ndat() = h2x_32(hdsp->__ndat());
00262     for (int i = 0; i < ndat; i++)
00263       xdsp->__datid(i) = h2x_16(hdsp->__datid(i));
00264 
00265     memcpy(xdsp->name(), hdsp->name(), L_NAME+1);
00266   }
00267 
00268   void x2h_dbHeader(DbHeader *hdbh, const DbHeader *_xdbh)
00269   {
00270     DbHeader *xdbh = const_cast<DbHeader *>(_xdbh);
00271     hdbh->__magic() = x2h_u32(xdbh->__magic());
00272     hdbh->__dbid() = x2h_32(xdbh->__dbid());
00273     hdbh->__guest_uid() = x2h_32(xdbh->__guest_uid());
00274     hdbh->state() = xdbh->state();
00275     memcpy(hdbh->shmfile(), xdbh->shmfile(), L_FILENAME);
00276 
00277     Oid hoid, xoid;
00278     xoid = xdbh->__prot_uid_oid();
00279     x2h_oid(&hoid, &xoid);
00280     hdbh->__prot_uid_oid() = hoid;
00281 
00282     xoid = xdbh->__prot_list_oid();
00283     x2h_oid(&hoid, &xoid);
00284     hdbh->__prot_list_oid() = hoid;
00285 
00286     xoid = xdbh->__prot_lock_oid();
00287     x2h_oid(&hoid, &xoid);
00288     hdbh->__prot_lock_oid() = hoid;
00289 
00290     hdbh->__nbobjs() = x2h_u32(xdbh->__nbobjs());
00291     hdbh->__ndat() = x2h_u32(xdbh->__ndat());
00292     for (int i = 0; i < hdbh->__ndat(); i++) {
00293       DatafileDesc hd = hdbh->dat(i);
00294       DatafileDesc xd = xdbh->dat(i);
00295       x2h_datafileDesc(&hd, &xd);
00296     }
00297     hdbh->__ndsp() = x2h_u32(xdbh->__ndsp());
00298     for (int i = 0; i < hdbh->__ndsp(); i++) {
00299       DataspaceDesc hd = hdbh->dsp(i);
00300       DataspaceDesc xd = xdbh->dsp(i);
00301       x2h_dataspaceDesc(&hd, &xd);
00302     }
00303     hdbh->__def_dspid() = x2h_16(xdbh->__def_dspid());
00304     hdbh->__lastidxbusy() = x2h_u32(xdbh->__lastidxbusy());
00305     hdbh->__curidxbusy() = x2h_u32(xdbh->__curidxbusy());
00306     hdbh->__lastidxblkalloc() = x2h_u32(xdbh->__lastidxblkalloc());
00307 
00308     for (int i = 0; i < hdbh->__ndat(); i++)
00309       hdbh->__lastnsblkalloc(i) = x2h_u32(xdbh->__lastnsblkalloc(i));
00310 
00311     memcpy(hdbh->vre_addr(0), xdbh->vre_addr(0),
00312            DbRootEntry_SIZE * MAX_ROOT_ENTRIES);
00313   }
00314 
00315   void h2x_dbHeader(DbHeader *xdbh, const DbHeader *_hdbh)
00316   {
00317     DbHeader *hdbh = const_cast<DbHeader *>(_hdbh);
00318     unsigned int ndat = hdbh->__ndat();
00319     unsigned int ndsp = hdbh->__ndsp();
00320 
00321     xdbh->__magic() = h2x_u32(hdbh->__magic());
00322     xdbh->__dbid() = h2x_32(hdbh->__dbid());
00323     xdbh->__guest_uid() = h2x_32(hdbh->__guest_uid());
00324     xdbh->state() = hdbh->state();
00325     memcpy(xdbh->shmfile(), hdbh->shmfile(), L_FILENAME);
00326     
00327     Oid xoid, hoid;
00328 
00329     hoid = hdbh->__prot_uid_oid();
00330     h2x_oid(&xoid, &hoid);
00331     xdbh->__prot_uid_oid() = xoid;
00332 
00333     hoid = hdbh->__prot_list_oid();
00334     h2x_oid(&xoid, &hoid);
00335     xdbh->__prot_list_oid() = xoid;
00336 
00337     hoid = hdbh->__prot_lock_oid();
00338     h2x_oid(&xoid, &hoid);
00339     xdbh->__prot_lock_oid() = xoid;
00340 
00341     xdbh->__nbobjs() = h2x_u32(hdbh->__nbobjs());
00342     xdbh->__ndat() = h2x_u32(hdbh->__ndat());
00343     for (int i = 0; i < ndat; i++) {
00344       DatafileDesc hd = hdbh->dat(i);
00345       DatafileDesc xd = xdbh->dat(i);
00346       h2x_datafileDesc(&xd, &hd);
00347     }
00348     xdbh->__ndsp() = h2x_u32(hdbh->__ndsp());
00349     for (int i = 0; i < ndsp; i++) {
00350       DataspaceDesc hd = hdbh->dsp(i);
00351       DataspaceDesc xd = xdbh->dsp(i);
00352       h2x_dataspaceDesc(&xd, &hd);
00353     }
00354     xdbh->__def_dspid() = h2x_16(hdbh->__def_dspid());
00355     xdbh->__lastidxbusy() = h2x_u32(hdbh->__lastidxbusy());
00356     xdbh->__curidxbusy() = h2x_u32(hdbh->__curidxbusy());
00357     xdbh->__lastidxblkalloc() = h2x_u32(hdbh->__lastidxblkalloc());
00358     for (int i = 0; i < ndat; i++)
00359       xdbh->__lastnsblkalloc(i) = h2x_u32(hdbh->__lastnsblkalloc(i));
00360 
00361     memcpy(xdbh->vre_addr(0), hdbh->vre_addr(0),
00362            DbRootEntry_SIZE * MAX_ROOT_ENTRIES);
00363   }
00364 
00365   void
00366   x2h_protoids(Oid *prot_lock_oid, Oid *prot_list_oid,
00367                Oid *prot_uid_oid, DbHeader *dbh)
00368   {
00369     Oid xoid;
00370 
00371     xoid = dbh->__prot_list_oid();
00372     x2h_oid(prot_list_oid, &xoid);
00373 
00374     xoid = dbh->__prot_lock_oid();
00375     x2h_oid(prot_lock_oid, &xoid);
00376 
00377     xoid = dbh->__prot_uid_oid();
00378     x2h_oid(prot_uid_oid, &xoid);
00379   }
00380 
00381   void
00382   h2x_protoids(Oid *prot_lock_oid, Oid *prot_list_oid,
00383                Oid *prot_uid_oid, DbHeader *dbh)
00384   {
00385     Oid xoid;
00386 
00387     h2x_oid(&xoid, prot_list_oid);
00388     dbh->__prot_list_oid() = xoid;
00389 
00390     h2x_oid(&xoid, prot_lock_oid);
00391     dbh->__prot_lock_oid() = xoid;
00392 
00393     h2x_oid(&xoid, prot_uid_oid);
00394     dbh->__prot_uid_oid() = xoid;
00395   }
00396 }

Generated on Mon Dec 22 18:16:15 2008 for eyedb by  doxygen 1.5.3