vtable.cpp

00001 /*<std-header orig-src='shore'>
00002 
00003  $Id: vtable.cpp,v 1.2 2010/05/26 01:20:23 nhall Exp $
00004 
00005 SHORE -- Scalable Heterogeneous Object REpository
00006 
00007 Copyright (c) 1994-99 Computer Sciences Department, University of
00008                       Wisconsin -- Madison
00009 All Rights Reserved.
00010 
00011 Permission to use, copy, modify and distribute this software and its
00012 documentation is hereby granted, provided that both the copyright
00013 notice and this permission notice appear in all copies of the
00014 software, derivative works or modified versions, and any portions
00015 thereof, and that both notices appear in supporting documentation.
00016 
00017 THE AUTHORS AND THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY
00018 OF WISCONSIN - MADISON ALLOW FREE USE OF THIS SOFTWARE IN ITS
00019 "AS IS" CONDITION, AND THEY DISCLAIM ANY LIABILITY OF ANY KIND
00020 FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
00021 
00022 This software was developed with support by the Advanced Research
00023 Project Agency, ARPA order number 018 (formerly 8230), monitored by
00024 the U.S. Army Research Laboratory under contract DAAB07-91-C-Q518.
00025 Further funding for this work was provided by DARPA through
00026 Rome Research Laboratory Contract No. F30602-97-2-0247.
00027 
00028 */
00029 
00030 #include "w_defines.h"
00031 
00032 /*  -- do not edit anything above this line --   </std-header>*/
00033 
00034 #include <w.h>
00035 #include <vtable.h>
00036 
00037 #include <w_strstream.h>
00038 
00039 
00040 void 
00041 vtable_row_t::set_uint(int a, unsigned int v) {
00042     // check for reasonable attribute #
00043     w_assert1(a < N);
00044     // check attribute not already set
00045     w_assert3(strlen(_get_const(a)) == 0); 
00046     // Create a buffered ostrstream that will write to this
00047     // entry for 'a'
00048     w_ostrstream o(_insert_attribute(a), value_size());
00049     // Create the entry for a
00050     o << v << ends;
00051     _inserted(a);
00052 }
00053 
00054 void 
00055 vtable_row_t::set_base(int a, w_base_t::base_float_t v) {
00056     // check for reasonable attribute #
00057     w_assert1(a < N);
00058     // check attribute not already set
00059     w_assert9(strlen(_get_const(a)) == 0); 
00060     w_ostrstream o(_insert_attribute(a), value_size());
00061     o << v << ends;
00062     _inserted(a);
00063 }
00064 
00065 void 
00066 vtable_row_t::set_base(int a,  w_base_t::base_stat_t v) {
00067     // check for reasonable attribute #
00068     w_assert1(a < N);
00069     // check attribute not already set
00070     w_assert9(strlen(_get_const(a)) == 0); 
00071     w_ostrstream o(_insert_attribute(a), value_size());
00072     o << v << ends;
00073     _inserted(a);
00074 }
00075 
00076 void 
00077 vtable_row_t::set_int(int a, int v) {
00078     w_assert1(a < N);
00079     w_assert9(strlen(_get_const(a)) == 0); 
00080     w_ostrstream o(_insert_attribute(a), value_size());
00081     o << v << ends;
00082     _inserted(a);
00083 }
00084 
00085 void 
00086 vtable_row_t::set_string(int a, const char *v) {
00087     // check for reasonable attribute #
00088     w_assert1(a < N);
00089     w_assert1((int)strlen(v) < value_size());
00090     // check attribute not already set
00091     w_assert9(strlen(_get_const(a)) == 0); 
00092     strcpy(_insert_attribute(a), v);
00093     w_assert9(_get_const(a)[strlen(v)] == '\0'); 
00094     _inserted(a);
00095 }
00096 
00097 void
00098 vtable_row_t::dump(const char *msg) const
00099 {
00100     fprintf(stderr, "vtable_row_t (%s) at %p {\n", msg, this );
00101     fprintf(stderr, "N %d M %d _in_use %d\n", N, M, _in_use);
00102     fprintf(stderr, "_list %p -> _list_end %p\n", _list, _list_end);
00103 
00104     for(int i=0; i<=_in_use; i++) {
00105         const char *not_yet_set = "Not yet set";
00106         const char *p = _entry[i];
00107         const char *A = "";
00108         if(p < _list) {
00109             A = "BOGUS: addr too low";
00110         }
00111         const char *B = "";
00112         if(p > _list_end) {
00113             B = "BOGUS: addr too high";
00114         }
00115         fprintf(stderr, "%s %s _entry[%d] = %p, string= %s \n" ,
00116                 A, B, i, p, (i==_in_use?not_yet_set:p));
00117     }
00118     fprintf(stderr, "}\n");
00119 }
00120 
00121 void 
00122 vtable_row_t::_inserted(int a) {
00123     w_assert0(a < N);
00124     w_assert0(a == _in_use); // assume we insert in order always.
00125     int l = strlen(_entry[a]);
00126     w_assert0(*(_entry[a] + l +1) == '\0');
00127     if(a == N-1) {
00128         // it's full
00129         if(l==0) l++; // this is a null string. Must add at least one.
00130         _list_end = _entry[a] + l;
00131     } else {
00132         _entry[a+1] = _entry[a] + l + 1;
00133         _list_end = _entry[a+1];
00134     }
00135 
00136 
00137     _in_use++;
00138     w_assert0(_in_use <= N); // still. Origin 0
00139 }
00140 
00141 ostream& 
00142 vtable_row_t::operator<<(ostream &o) 
00143 {
00144 
00145     for(int i=0; i<_in_use; i++) {
00146         if(strlen(_get_const(i)) > 0) {
00147             o <<  i << ": " << _get_const(i) <<endl;
00148         }
00149     }
00150     o <<  endl;
00151     return o;
00152 }
00153 
00154 
00155 int 
00156 vtable_t::init(int R, int A, int S) {
00157 
00158     _rows_filled = 0;
00159     _rows = R;
00160     _rowsize_attributes = A;
00161 
00162     // how many bytes are needed for a single row of N attributes with
00163     // maximum attribute size of S?
00164     // Align it
00165     _rowsize_bytes = align(vtable_row_t::bytes_required_for(A, S));
00166 
00167     _array_alias = new char [_rows * _rowsize_bytes];
00168 
00169     if(_array_alias) {
00170         // initialize the array
00171         memset(_array_alias, '\0', _rows * _rowsize_bytes);
00172 
00173         for(int i=0; i<_rows; i++) {
00174             vtable_row_t *row = _get_row(i);
00175             row->init_for_attributes(_rowsize_attributes, S);
00176             w_assert1(row->size_in_bytes() <= _rowsize_bytes);
00177         }
00178         return 0;
00179     } else {
00180         return -1;
00181     }
00182 }
00183 
00184 void           
00185 vtable_t::filled_one() 
00186 { 
00187     W_IFDEBUG1(vtable_row_t *t = _get_row(_rows_filled);)
00188     w_assert1(t->size_in_bytes() <= _rowsize_bytes);
00189 
00190     _rows_filled++; 
00191     w_assert9(_rows_filled <= _rows);
00192 }
00193 
00194 ostream& 
00195 vtable_t::operator<<(ostream &o) const {
00196 
00197     for(int i=0; i<_rows; i++) {
00198         _get_row(i)->operator<<(o) ;
00199     }
00200     o <<  endl;
00201     return o;
00202 }
00203 
00204 vtable_row_t* 
00205 vtable_t::_get_row(int i) const {
00206     w_assert9(i >= 0);
00207     w_assert9(i <= _rows);
00208     w_assert9(_rows_filled <= _rows);
00209     vtable_row_t* v =
00210         (vtable_row_t *)&_array_alias[i * _rowsize_bytes];
00211     return v;
00212 }
00213 
00214 int            
00215 vtable_t::realloc() // doubles the size
00216 {
00217     W_FATAL_MSG(fcINTERNAL, <<"vtable_t::realloc is not implemented");
00218     return 0;
00219 }

Generated on Wed Jul 7 17:22:32 2010 for Shore Storage Manager by  doxygen 1.4.7