vec_mkchunk.cpp

00001 /*<std-header orig-src='shore'>
00002 
00003  $Id: vec_mkchunk.cpp,v 1.15 2010/05/26 01:20:12 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 /**\cond skip */
00035 #ifdef __GNUC__
00036      #pragma implementation
00037 #endif
00038 
00039 #define VEC_T_C
00040 #include <cstdlib>
00041 #include <w_stream.h>
00042 #include <w_base.h>
00043 #include <w_minmax.h>
00044 #include "basics.h"
00045 #include "vec_t.h"
00046 #include "umemcmp.h"
00047 #include "w_debug.h"
00048 
00049 
00050 /////////////////////////////////////////////////////
00051 // "result" is a vector with size() no larger than 
00052 // maxsize, whose contents are taken from the part
00053 // of *this that's left after you skip the first
00054 // "offset" bytes of *this.
00055 // CALLER provides result, which is reset() right
00056 // away, and re-used each time this is called.
00057 //
00058 // The general idea is that this allows you to take
00059 // a vector of arbitrary configuration (in the context
00060 // of writes, say) and break it up into vectors of
00061 // "maxsize" sizes so that you can limit the sizes of
00062 // writes.
00063 /////////////////////////////////////////////////////
00064 void
00065 vec_t::mkchunk(
00066     int                maxsize,
00067     int                offset, // # skipped
00068     vec_t            &result // provided by the caller
00069 ) const
00070 {
00071     int i;
00072 
00073     w_assert1( _base[0].ptr != zero_location );
00074 
00075     DBG(<<"offset " << offset << " in vector :");
00076     result.reset();
00077 
00078     // return a vector representing the next
00079     // maxsize bytes starting at the given offset
00080     // from the data represented by the input vector
00081     int        first_chunk=0, first_chunk_offset=0, first_chunk_len=0;
00082     {
00083         // find first_chunk
00084         int skipped=0, skipping;
00085 
00086         for(i=0; i<this->count(); i++) {
00087             skipping = this->len(i);
00088             if(skipped + skipping > offset) {
00089                 // found
00090                 first_chunk = i;
00091                 first_chunk_offset = offset - skipped;
00092                 first_chunk_len = skipping - first_chunk_offset;
00093                 if(first_chunk_len > maxsize) {
00094                     first_chunk_len = maxsize;
00095                 }
00096 
00097         DBG(<<"put " << W_ADDR(this->ptr(i)) << 
00098             "+" << first_chunk_offset << ", " << first_chunk_len);
00099 
00100                 result.put((char*)this->ptr(i)+first_chunk_offset,first_chunk_len);
00101                 break;
00102             }
00103             skipped += skipping;
00104         }
00105         if(first_chunk_len == 0) return;
00106     }
00107 
00108     if(first_chunk_len < maxsize) {
00109         // find next chunks up to the last
00110         int used, is_using ;
00111 
00112         used = first_chunk_len;
00113         for(i=first_chunk+1; i<this->count(); i++) {
00114             is_using = this->len(i);
00115             if(used + is_using <= maxsize) {
00116                 // use the whole thing
00117                 used += is_using;
00118 
00119                 DBG(<<"put " << W_ADDR(this->ptr(i)) << ", " << is_using);
00120                 result.put(this->ptr(i),is_using);
00121             } else {
00122                 // gotta use part
00123                 result.put(this->ptr(i),maxsize-used);
00124                 used = maxsize;
00125                 break;
00126             }
00127         }
00128     }
00129 }
00130 
00131 /**\endcond skip */

Generated on Thu Dec 9 08:42:26 2010 for Shore Storage Manager by  doxygen 1.4.7