[f450f2f] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
| 7 | // string_res -- variable-length, mutable run of text, with resource semantics |
---|
| 8 | // |
---|
| 9 | // Author : Michael L. Brooks |
---|
| 10 | // Created On : Fri Sep 03 11:00:00 2021 |
---|
[7d25f44] | 11 | // Last Modified By : Peter A. Buhr |
---|
[5764204] | 12 | // Last Modified On : Mon Jan 22 23:12:42 2024 |
---|
| 13 | // Update Count : 43 |
---|
[f450f2f] | 14 | // |
---|
| 15 | |
---|
| 16 | #include "string_res.hfa" |
---|
[0f781fb8] | 17 | #include "string_sharectx.hfa" |
---|
[08ed947] | 18 | #include "stdlib.hfa" |
---|
[d32679d5] | 19 | #include <ctype.h> |
---|
[08ed947] | 20 | |
---|
| 21 | // Workaround for observed performance penalty from calling CFA's alloc. |
---|
| 22 | // Workaround is: EndVbyte = TEMP_ALLOC(char, CurrSize) |
---|
| 23 | // Should be: EndVbyte = alloc(CurrSize) |
---|
[681e12f] | 24 | #define TEMP_ALLOC(T, n) (( T * ) malloc( n * sizeof( T ) )) |
---|
[0f781fb8] | 25 | |
---|
[218096f] | 26 | #include <assert.h> |
---|
[f2898df] | 27 | #include <complex.h> // creal, cimag |
---|
[f450f2f] | 28 | |
---|
| 29 | //######################### VbyteHeap "header" ######################### |
---|
| 30 | |
---|
| 31 | #ifdef VbyteDebug |
---|
[6cc87c0] | 32 | HandleNode *HeaderPtr; |
---|
[f450f2f] | 33 | #endif // VbyteDebug |
---|
| 34 | |
---|
| 35 | struct VbyteHeap { |
---|
[681e12f] | 36 | int NoOfCompactions; // number of compactions of the byte area |
---|
| 37 | int NoOfExtensions; // number of extensions in the size of the byte area |
---|
| 38 | int NoOfReductions; // number of reductions in the size of the byte area |
---|
[f450f2f] | 39 | |
---|
[681e12f] | 40 | int InitSize; // initial number of bytes in the byte-string area |
---|
| 41 | int CurrSize; // current number of bytes in the byte-string area |
---|
| 42 | char *StartVbyte; // pointer to the `st byte of the start of the byte-string area |
---|
| 43 | char *EndVbyte; // pointer to the next byte after the end of the currently used portion of byte-string area |
---|
| 44 | void *ExtVbyte; // pointer to the next byte after the end of the byte-string area |
---|
[f450f2f] | 45 | |
---|
[681e12f] | 46 | HandleNode Header; // header node for handle list |
---|
[f450f2f] | 47 | }; // VbyteHeap |
---|
| 48 | |
---|
| 49 | |
---|
[681e12f] | 50 | static void compaction( VbyteHeap & ); // compaction of the byte area |
---|
| 51 | static void garbage( VbyteHeap &, int ); // garbage collect the byte area |
---|
[4e8df745] | 52 | static void extend( VbyteHeap &, int ); // extend the size of the byte area |
---|
| 53 | static void reduce( VbyteHeap &, int ); // reduce the size of the byte area |
---|
[f450f2f] | 54 | |
---|
[4e8df745] | 55 | static void ?{}( VbyteHeap &, size_t = 1000 ); |
---|
| 56 | static void ^?{}( VbyteHeap & ); |
---|
[94647b0b] | 57 | |
---|
[4e8df745] | 58 | static int ByteCmp( char *, int, int, char *, int, int ); // compare 2 blocks of bytes |
---|
| 59 | static char *VbyteAlloc( VbyteHeap &, int ); // allocate a block bytes in the heap |
---|
| 60 | static char *VbyteTryAdjustLast( VbyteHeap &, int ); |
---|
[f450f2f] | 61 | |
---|
[4e8df745] | 62 | static void AddThisAfter( HandleNode &, HandleNode & ); |
---|
| 63 | static void DeleteNode( HandleNode & ); |
---|
| 64 | static void MoveThisAfter( HandleNode &, const HandleNode & ); // move current handle after parameter handle |
---|
[f450f2f] | 65 | |
---|
| 66 | |
---|
| 67 | // Allocate the storage for the variable sized area and intialize the heap variables. |
---|
| 68 | |
---|
[681e12f] | 69 | static void ?{}( VbyteHeap & s, size_t Size ) with(s) { |
---|
[f450f2f] | 70 | #ifdef VbyteDebug |
---|
[681e12f] | 71 | serr | "enter:VbyteHeap::VbyteHeap, s:" | &s | " Size:" | Size; |
---|
[f450f2f] | 72 | #endif // VbyteDebug |
---|
| 73 | NoOfCompactions = NoOfExtensions = NoOfReductions = 0; |
---|
| 74 | InitSize = CurrSize = Size; |
---|
[08ed947] | 75 | StartVbyte = EndVbyte = TEMP_ALLOC(char, CurrSize); |
---|
[f450f2f] | 76 | ExtVbyte = (void *)( StartVbyte + CurrSize ); |
---|
| 77 | Header.flink = Header.blink = &Header; |
---|
[681e12f] | 78 | Header.ulink = &s; |
---|
[f450f2f] | 79 | #ifdef VbyteDebug |
---|
| 80 | HeaderPtr = &Header; |
---|
[681e12f] | 81 | serr | "exit:VbyteHeap::VbyteHeap, s:" | &s; |
---|
[f450f2f] | 82 | #endif // VbyteDebug |
---|
| 83 | } // VbyteHeap |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | // Release the dynamically allocated storage for the byte area. |
---|
| 87 | |
---|
[681e12f] | 88 | static void ^?{}( VbyteHeap & s ) with(s) { |
---|
[f450f2f] | 89 | free( StartVbyte ); |
---|
| 90 | } // ~VbyteHeap |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | //######################### HandleNode ######################### |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | // Create a handle node. The handle is not linked into the handle list. This is the responsibilitiy of the handle |
---|
| 97 | // creator. |
---|
| 98 | |
---|
[681e12f] | 99 | static void ?{}( HandleNode & s ) with(s) { |
---|
[f450f2f] | 100 | #ifdef VbyteDebug |
---|
[681e12f] | 101 | serr | "enter:HandleNode::HandleNode, s:" | &s; |
---|
[f450f2f] | 102 | #endif // VbyteDebug |
---|
| 103 | s = 0; |
---|
| 104 | lnth = 0; |
---|
| 105 | #ifdef VbyteDebug |
---|
[681e12f] | 106 | serr | "exit:HandleNode::HandleNode, s:" | &s; |
---|
[f450f2f] | 107 | #endif // VbyteDebug |
---|
| 108 | } // HandleNode |
---|
| 109 | |
---|
| 110 | // Create a handle node. The handle is linked into the handle list at the end. This means that this handle will NOT be |
---|
| 111 | // in order by string address, but this is not a problem because a string with length zero does nothing during garbage |
---|
| 112 | // collection. |
---|
| 113 | |
---|
[681e12f] | 114 | static void ?{}( HandleNode & s, VbyteHeap & vh ) with(s) { |
---|
[f450f2f] | 115 | #ifdef VbyteDebug |
---|
[681e12f] | 116 | serr | "enter:HandleNode::HandleNode, s:" | &s; |
---|
[f450f2f] | 117 | #endif // VbyteDebug |
---|
| 118 | s = 0; |
---|
| 119 | lnth = 0; |
---|
[0f781fb8] | 120 | ulink = &vh; |
---|
[681e12f] | 121 | AddThisAfter( s, *vh.Header.blink ); |
---|
[f450f2f] | 122 | #ifdef VbyteDebug |
---|
[681e12f] | 123 | serr | "exit:HandleNode::HandleNode, s:" | &s; |
---|
[f450f2f] | 124 | #endif // VbyteDebug |
---|
| 125 | } // HandleNode |
---|
| 126 | |
---|
| 127 | |
---|
| 128 | // Delete a node from the handle list by unchaining it from the list. If the handle node was allocated dynamically, it |
---|
| 129 | // is the responsibility of the creator to destroy it. |
---|
| 130 | |
---|
[681e12f] | 131 | static void ^?{}( HandleNode & s ) with(s) { |
---|
[f450f2f] | 132 | #ifdef VbyteDebug |
---|
[681e12f] | 133 | serr | "enter:HandleNode::~HandleNode, s:" | & s; |
---|
[f450f2f] | 134 | { |
---|
| 135 | serr | nlOff; |
---|
| 136 | serr | " lnth:" | lnth | " s:" | (void *)s | ",\""; |
---|
[9ca5e56] | 137 | for ( i; lnth ) { |
---|
[f450f2f] | 138 | serr | s[i]; |
---|
| 139 | } // for |
---|
| 140 | serr | "\" flink:" | flink | " blink:" | blink | nl; |
---|
| 141 | serr | nlOn; |
---|
| 142 | } |
---|
| 143 | #endif // VbyteDebug |
---|
[681e12f] | 144 | DeleteNode( s ); |
---|
[f450f2f] | 145 | } // ~HandleNode |
---|
| 146 | |
---|
| 147 | |
---|
[0f781fb8] | 148 | //######################### String Sharing Context ######################### |
---|
[f450f2f] | 149 | |
---|
[0f781fb8] | 150 | static string_sharectx * ambient_string_sharectx; // fickle top of stack |
---|
| 151 | static string_sharectx default_string_sharectx = {NEW_SHARING}; // stable bottom of stack |
---|
[f450f2f] | 152 | |
---|
[681e12f] | 153 | void ?{}( string_sharectx & s, StringSharectx_Mode mode ) with( s ) { |
---|
[0f781fb8] | 154 | (older){ ambient_string_sharectx }; |
---|
| 155 | if ( mode == NEW_SHARING ) { |
---|
[804bf677] | 156 | (activeHeap){ new( (size_t) 1000 ) }; |
---|
[0f781fb8] | 157 | } else { |
---|
| 158 | verify( mode == NO_SHARING ); |
---|
| 159 | (activeHeap){ 0p }; |
---|
| 160 | } |
---|
[681e12f] | 161 | ambient_string_sharectx = & s; |
---|
[0f781fb8] | 162 | } |
---|
| 163 | |
---|
[681e12f] | 164 | void ^?{}( string_sharectx & s ) with( s ) { |
---|
[0f781fb8] | 165 | if ( activeHeap ) delete( activeHeap ); |
---|
| 166 | |
---|
[681e12f] | 167 | // unlink s from older-list starting from ambient_string_sharectx |
---|
| 168 | // usually, s==ambient_string_sharectx and the loop runs zero times |
---|
[0f781fb8] | 169 | string_sharectx *& c = ambient_string_sharectx; |
---|
[681e12f] | 170 | while ( c != &s ) &c = &c->older; // find s |
---|
| 171 | c = s.older; // unlink |
---|
[0f781fb8] | 172 | } |
---|
| 173 | |
---|
| 174 | //######################### String Resource ######################### |
---|
| 175 | |
---|
| 176 | |
---|
| 177 | VbyteHeap * DEBUG_string_heap() { |
---|
| 178 | assert( ambient_string_sharectx->activeHeap && "No sharing context is active" ); |
---|
| 179 | return ambient_string_sharectx->activeHeap; |
---|
| 180 | } |
---|
[6cc87c0] | 181 | |
---|
| 182 | size_t DEBUG_string_bytes_avail_until_gc( VbyteHeap * heap ) { |
---|
[06280ad] | 183 | return ((char *)heap->ExtVbyte) - heap->EndVbyte; |
---|
[6cc87c0] | 184 | } |
---|
| 185 | |
---|
[7b0e8b7] | 186 | size_t DEBUG_string_bytes_in_heap( VbyteHeap * heap ) { |
---|
| 187 | return heap->CurrSize; |
---|
| 188 | } |
---|
| 189 | |
---|
[6cc87c0] | 190 | const char * DEBUG_string_heap_start( VbyteHeap * heap ) { |
---|
| 191 | return heap->StartVbyte; |
---|
| 192 | } |
---|
| 193 | |
---|
[f450f2f] | 194 | // Returns the size of the string in bytes |
---|
[681e12f] | 195 | size_t size(const string_res & s) with(s) { |
---|
[f450f2f] | 196 | return Handle.lnth; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | // Output operator |
---|
[681e12f] | 200 | ofstream & ?|?(ofstream & out, const string_res & s) { |
---|
[9ca5e56] | 201 | // CFA string is NOT null terminated, so print exactly lnth characters in a minimum width of 0. |
---|
| 202 | out | wd( 0, s.Handle.lnth, s.Handle.s ) | nonl; |
---|
[f450f2f] | 203 | return out; |
---|
| 204 | } |
---|
| 205 | |
---|
[681e12f] | 206 | void ?|?(ofstream & out, const string_res & s) { |
---|
[9ca5e56] | 207 | (ofstream &)(out | s); ends( out ); |
---|
[f450f2f] | 208 | } |
---|
| 209 | |
---|
[d32679d5] | 210 | // Input operator |
---|
[681e12f] | 211 | ifstream & ?|?(ifstream & in, string_res & s) { |
---|
[d32679d5] | 212 | // Reading into a temp before assigning to s is near zero overhead in typical cases because of sharing. |
---|
| 213 | // If s is a substring of something larger, simple assignment takes care of that case correctly. |
---|
| 214 | // But directly reading a variable amount of text into the middle of a larger context is not practical. |
---|
| 215 | string_res temp; |
---|
| 216 | |
---|
| 217 | // Read in chunks. Often, one chunk is enough. Keep the string that accumulates chunks last in the heap, |
---|
| 218 | // so available room is rest of heap. When a chunk fills the heap, force growth then take the next chunk. |
---|
[0860d9c] | 219 | for (bool cont = true; cont; ) { |
---|
| 220 | cont = false; |
---|
| 221 | |
---|
[d32679d5] | 222 | // Append dummy content to temp, forcing expansion when applicable (occurs always on subsequent loops) |
---|
| 223 | // length 2 ensures room for at least one real char, plus scanf/pipe-cstr's null terminator |
---|
| 224 | temp += "--"; |
---|
| 225 | assert( temp.Handle.ulink->EndVbyte == temp.Handle.s + temp.Handle.lnth ); // last in heap |
---|
| 226 | |
---|
| 227 | // reset, to overwrite the appended "--" |
---|
| 228 | temp.Handle.lnth -= 2; |
---|
| 229 | temp.Handle.ulink->EndVbyte -= 2; |
---|
| 230 | |
---|
[0860d9c] | 231 | // rest of heap is available to read into |
---|
[06280ad] | 232 | int lenReadable = (char *)temp.Handle.ulink->ExtVbyte - temp.Handle.ulink->EndVbyte; |
---|
[0860d9c] | 233 | assert (lenReadable >= 2); |
---|
[d32679d5] | 234 | |
---|
| 235 | // get bytes |
---|
[0860d9c] | 236 | try { |
---|
[37ceccb] | 237 | *(temp.Handle.ulink->EndVbyte) = '\0'; // pre-assign empty cstring |
---|
[0860d9c] | 238 | in | wdi( lenReadable, temp.Handle.ulink->EndVbyte ); |
---|
[681e12f] | 239 | } catch (cstring_length *) { |
---|
[0860d9c] | 240 | cont = true; |
---|
| 241 | } |
---|
[d32679d5] | 242 | int lenWasRead = strlen(temp.Handle.ulink->EndVbyte); |
---|
| 243 | |
---|
| 244 | // update metadata |
---|
| 245 | temp.Handle.lnth += lenWasRead; |
---|
| 246 | temp.Handle.ulink->EndVbyte += lenWasRead; |
---|
| 247 | } |
---|
| 248 | |
---|
[37ceccb] | 249 | if ( temp.Handle.lnth > 0 ) s = temp; |
---|
[d32679d5] | 250 | return in; |
---|
| 251 | } |
---|
| 252 | |
---|
[681e12f] | 253 | void ?|?( ifstream & in, string_res & s ) { |
---|
| 254 | (ifstream &)(in | s); |
---|
[ff56dd2e] | 255 | } |
---|
| 256 | |
---|
| 257 | ifstream & ?|?( ifstream & is, _Istream_Rstr f ) { |
---|
| 258 | // .---------------, |
---|
| 259 | // | | | | |...|0|0| null terminator and guard if missing |
---|
| 260 | // `---------------' |
---|
| 261 | enum { gwd = 128 + 1, wd = gwd - 1 }; // guard and unguard width |
---|
| 262 | char cstr[gwd]; // read in chunks |
---|
| 263 | bool cont = false; |
---|
| 264 | |
---|
[5764204] | 265 | _Istream_Cwidth cf = { cstr, (_Istream_str_base)f }; |
---|
[ff56dd2e] | 266 | if ( ! cf.flags.rwd ) cf.wd = wd; |
---|
| 267 | |
---|
| 268 | cstr[wd] = '\0'; // guard null terminate string |
---|
| 269 | try { |
---|
[37ceccb] | 270 | cstr[0] = '\0'; // pre-assign as empty cstring |
---|
[ff56dd2e] | 271 | is | cf; |
---|
| 272 | } catch( cstring_length * ) { |
---|
| 273 | cont = true; |
---|
| 274 | } finally { |
---|
[681e12f] | 275 | if ( ! cf.flags.ignore // ok to initialize string |
---|
| 276 | // && cstr[0] != '\0' // something was read |
---|
| 277 | ) { |
---|
[37ceccb] | 278 | *(f.s) = cstr; |
---|
| 279 | } |
---|
[ff56dd2e] | 280 | } // try |
---|
| 281 | for ( ; cont; ) { // overflow read ? |
---|
| 282 | cont = false; |
---|
| 283 | try { |
---|
[37ceccb] | 284 | cstr[0] = '\0'; // pre-assign as empty cstring |
---|
[ff56dd2e] | 285 | is | cf; |
---|
| 286 | } catch( cstring_length * ) { |
---|
| 287 | cont = true; // continue not allowed |
---|
| 288 | } finally { |
---|
[681e12f] | 289 | if ( ! cf.flags.ignore && cstr[0] != '\0' ) { // something was read |
---|
[37ceccb] | 290 | *(f.s) += cstr; // build string chunk at a time |
---|
| 291 | } |
---|
[ff56dd2e] | 292 | } // try |
---|
| 293 | } // for |
---|
| 294 | return is; |
---|
| 295 | } // ?|? |
---|
| 296 | |
---|
| 297 | void ?|?( ifstream & in, _Istream_Rstr f ) { |
---|
[f842032] | 298 | (ifstream &)(in | f); |
---|
[ff56dd2e] | 299 | } |
---|
| 300 | |
---|
[d32679d5] | 301 | |
---|
[f450f2f] | 302 | // Empty constructor |
---|
[681e12f] | 303 | void ?{}(string_res & s) with(s) { |
---|
[804bf677] | 304 | if( ambient_string_sharectx->activeHeap ) { |
---|
| 305 | (Handle){ * ambient_string_sharectx->activeHeap }; |
---|
| 306 | (shareEditSet_owns_ulink){ false }; |
---|
[2b30370] | 307 | verify( Handle.s == 0p && Handle.lnth == 0 ); |
---|
[804bf677] | 308 | } else { |
---|
| 309 | (Handle){ * new( (size_t) 10 ) }; // TODO: can I lazily avoid allocating for empty string |
---|
| 310 | (shareEditSet_owns_ulink){ true }; |
---|
[2b30370] | 311 | Handle.s = Handle.ulink->StartVbyte; |
---|
| 312 | verify( Handle.lnth == 0 ); |
---|
[804bf677] | 313 | } |
---|
[f450f2f] | 314 | s.shareEditSet_prev = &s; |
---|
| 315 | s.shareEditSet_next = &s; |
---|
| 316 | } |
---|
| 317 | |
---|
[06280ad] | 318 | static void eagerCopyCtorHelper(string_res & s, const char * rhs, size_t rhslnth) with(s) { |
---|
[804bf677] | 319 | if( ambient_string_sharectx->activeHeap ) { |
---|
| 320 | (Handle){ * ambient_string_sharectx->activeHeap }; |
---|
| 321 | (shareEditSet_owns_ulink){ false }; |
---|
| 322 | } else { |
---|
| 323 | (Handle){ * new( rhslnth ) }; |
---|
| 324 | (shareEditSet_owns_ulink){ true }; |
---|
| 325 | } |
---|
[0f781fb8] | 326 | Handle.s = VbyteAlloc(*Handle.ulink, rhslnth); |
---|
[f450f2f] | 327 | Handle.lnth = rhslnth; |
---|
[08ed947] | 328 | memmove( Handle.s, rhs, rhslnth ); |
---|
[f450f2f] | 329 | s.shareEditSet_prev = &s; |
---|
| 330 | s.shareEditSet_next = &s; |
---|
| 331 | } |
---|
| 332 | |
---|
[4b3b352] | 333 | // Constructor from a raw buffer and size |
---|
[06280ad] | 334 | void ?{}(string_res & s, const char * rhs, size_t rhslnth) with(s) { |
---|
[4b3b352] | 335 | eagerCopyCtorHelper(s, rhs, rhslnth); |
---|
| 336 | } |
---|
| 337 | |
---|
[f2898df] | 338 | void ?{}( string_res & s, ssize_t rhs ) { |
---|
| 339 | char buf[64]; |
---|
| 340 | int len; |
---|
| 341 | snprintf( buf, sizeof(buf)-1, "%zd%n", rhs, &len ); |
---|
| 342 | ( s ){ buf, len }; |
---|
| 343 | } |
---|
| 344 | void ?{}( string_res & s, size_t rhs ) { |
---|
| 345 | char buf[64]; |
---|
| 346 | int len; |
---|
| 347 | snprintf( buf, sizeof(buf)-1, "%zu%n", rhs, &len ); |
---|
| 348 | ( s ){ buf, len }; |
---|
| 349 | } |
---|
| 350 | void ?{}( string_res & s, double rhs ) { |
---|
| 351 | char buf[64]; |
---|
| 352 | int len; |
---|
| 353 | snprintf( buf, sizeof(buf)-1, "%g%n", rhs, &len ); |
---|
| 354 | ( s ){ buf, len }; |
---|
| 355 | } |
---|
| 356 | void ?{}( string_res & s, long double rhs ) { |
---|
| 357 | char buf[64]; |
---|
| 358 | int len; |
---|
| 359 | snprintf( buf, sizeof(buf)-1, "%Lg%n", rhs, &len ); |
---|
| 360 | ( s ){ buf, len }; |
---|
| 361 | } |
---|
| 362 | void ?{}( string_res & s, double _Complex rhs ) { |
---|
| 363 | char buf[64]; |
---|
| 364 | int len; |
---|
| 365 | snprintf( buf, sizeof(buf)-1, "%g+%gi%n", creal( rhs ), cimag( rhs ), &len ); |
---|
| 366 | ( s ){ buf, len }; |
---|
| 367 | } |
---|
| 368 | void ?{}( string_res & s, long double _Complex rhs ) { |
---|
| 369 | char buf[64]; |
---|
| 370 | int len; |
---|
| 371 | snprintf( buf, sizeof(buf)-1, "%Lg+%Lgi%n", creall( rhs ), cimagl( rhs ), &len ); |
---|
| 372 | ( s ){ buf, len }; |
---|
| 373 | } |
---|
| 374 | |
---|
[fe18b46] | 375 | // private ctor (not in header): use specified heap (ignore ambient) and copy chars in |
---|
[06280ad] | 376 | void ?{}( string_res & s, VbyteHeap & heap, const char * rhs, size_t rhslnth ) with(s) { |
---|
[fe18b46] | 377 | (Handle){ heap }; |
---|
| 378 | Handle.s = VbyteAlloc(*Handle.ulink, rhslnth); |
---|
| 379 | Handle.lnth = rhslnth; |
---|
| 380 | (s.shareEditSet_owns_ulink){ false }; |
---|
[08ed947] | 381 | memmove( Handle.s, rhs, rhslnth ); |
---|
[fe18b46] | 382 | s.shareEditSet_prev = &s; |
---|
| 383 | s.shareEditSet_next = &s; |
---|
| 384 | } |
---|
| 385 | |
---|
[e8b3717] | 386 | |
---|
[f450f2f] | 387 | // General copy constructor |
---|
[e8b3717] | 388 | void ?{}(string_res & s, const string_res & s2, StrResInitMode mode, size_t start, size_t len ) { |
---|
[f450f2f] | 389 | |
---|
[e8b3717] | 390 | size_t end = start + len; |
---|
[218096f] | 391 | verify( start <= end && end <= s2.Handle.lnth ); |
---|
| 392 | |
---|
[fe18b46] | 393 | if (s2.Handle.ulink != ambient_string_sharectx->activeHeap && mode == COPY_VALUE) { |
---|
| 394 | // crossing heaps (including private): copy eagerly |
---|
| 395 | eagerCopyCtorHelper(s, s2.Handle.s + start, end - start); |
---|
| 396 | verify(s.shareEditSet_prev == &s); |
---|
| 397 | verify(s.shareEditSet_next == &s); |
---|
| 398 | } else { |
---|
[4b3b352] | 399 | (s.Handle){}; |
---|
| 400 | s.Handle.s = s2.Handle.s + start; |
---|
| 401 | s.Handle.lnth = end - start; |
---|
[fe18b46] | 402 | s.Handle.ulink = s2.Handle.ulink; |
---|
| 403 | |
---|
[4b3b352] | 404 | AddThisAfter(s.Handle, s2.Handle ); // insert this handle after rhs handle |
---|
| 405 | // ^ bug? skip others at early point in string |
---|
[fe18b46] | 406 | |
---|
[4b3b352] | 407 | if (mode == COPY_VALUE) { |
---|
[fe18b46] | 408 | verify(s2.Handle.ulink == ambient_string_sharectx->activeHeap); |
---|
| 409 | // requested logical copy in same heap: defer copy until write |
---|
| 410 | |
---|
| 411 | (s.shareEditSet_owns_ulink){ false }; |
---|
| 412 | |
---|
[4b3b352] | 413 | // make s alone in its shareEditSet |
---|
| 414 | s.shareEditSet_prev = &s; |
---|
| 415 | s.shareEditSet_next = &s; |
---|
| 416 | } else { |
---|
| 417 | verify( mode == SHARE_EDITS ); |
---|
[fe18b46] | 418 | // sharing edits with source forces same heap as source (ignore context) |
---|
| 419 | |
---|
| 420 | (s.shareEditSet_owns_ulink){ s2.shareEditSet_owns_ulink }; |
---|
[f450f2f] | 421 | |
---|
[4b3b352] | 422 | // s2 is logically const but not implementation const |
---|
| 423 | string_res & s2mod = (string_res &) s2; |
---|
[f450f2f] | 424 | |
---|
[4b3b352] | 425 | // insert s after s2 on shareEditSet |
---|
| 426 | s.shareEditSet_next = s2mod.shareEditSet_next; |
---|
| 427 | s.shareEditSet_prev = &s2mod; |
---|
| 428 | s.shareEditSet_next->shareEditSet_prev = &s; |
---|
| 429 | s.shareEditSet_prev->shareEditSet_next = &s; |
---|
[6cc87c0] | 430 | } |
---|
[f450f2f] | 431 | } |
---|
[4b3b352] | 432 | } |
---|
[f450f2f] | 433 | |
---|
[681e12f] | 434 | static void assignEditSet(string_res & s, string_res * shareEditSetStartPeer, string_res * shareEditSetEndPeer, |
---|
[4b3b352] | 435 | char * resultSesStart, |
---|
| 436 | size_t resultSesLnth, |
---|
| 437 | HandleNode * resultPadPosition, size_t bsize ) { |
---|
[6cc87c0] | 438 | |
---|
| 439 | char * beforeBegin = shareEditSetStartPeer->Handle.s; |
---|
[681e12f] | 440 | size_t beforeLen = s.Handle.s - beforeBegin; |
---|
[f450f2f] | 441 | |
---|
[681e12f] | 442 | char * afterBegin = s.Handle.s + s.Handle.lnth; |
---|
[6cc87c0] | 443 | size_t afterLen = shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth - afterBegin; |
---|
[f450f2f] | 444 | |
---|
[681e12f] | 445 | size_t oldLnth = s.Handle.lnth; |
---|
[f450f2f] | 446 | |
---|
[681e12f] | 447 | s.Handle.s = resultSesStart + beforeLen; |
---|
| 448 | s.Handle.lnth = bsize; |
---|
[6f7aff3] | 449 | if (resultPadPosition) |
---|
[681e12f] | 450 | MoveThisAfter( s.Handle, *resultPadPosition ); |
---|
[f450f2f] | 451 | |
---|
| 452 | // adjust all substring string and handle locations, and check if any substring strings are outside the new base string |
---|
[4b3b352] | 453 | char *limit = resultSesStart + resultSesLnth; |
---|
[681e12f] | 454 | for ( string_res * p = s.shareEditSet_next; p != &s; p = p->shareEditSet_next ) { |
---|
[218096f] | 455 | verify (p->Handle.s >= beforeBegin); |
---|
[6cc87c0] | 456 | if ( p->Handle.s >= afterBegin ) { |
---|
[218096f] | 457 | verify ( p->Handle.s <= afterBegin + afterLen ); |
---|
| 458 | verify ( p->Handle.s + p->Handle.lnth <= afterBegin + afterLen ); |
---|
[6cc87c0] | 459 | // p starts after the edit |
---|
| 460 | // take start and end as end-anchored |
---|
| 461 | size_t startOffsetFromEnd = afterBegin + afterLen - p->Handle.s; |
---|
| 462 | p->Handle.s = limit - startOffsetFromEnd; |
---|
| 463 | // p->Handle.lnth unaffected |
---|
| 464 | } else if ( p->Handle.s <= beforeBegin + beforeLen ) { |
---|
| 465 | // p starts before, or at the start of, the edit |
---|
| 466 | if ( p->Handle.s + p->Handle.lnth <= beforeBegin + beforeLen ) { |
---|
[f450f2f] | 467 | // p ends before the edit |
---|
| 468 | // take end as start-anchored too |
---|
| 469 | // p->Handle.lnth unaffected |
---|
| 470 | } else if ( p->Handle.s + p->Handle.lnth < afterBegin ) { |
---|
[6cc87c0] | 471 | // p ends during the edit; p does not include the last character replaced |
---|
[f450f2f] | 472 | // clip end of p to end at start of edit |
---|
| 473 | p->Handle.lnth = beforeLen - ( p->Handle.s - beforeBegin ); |
---|
| 474 | } else { |
---|
| 475 | // p ends after the edit |
---|
[218096f] | 476 | verify ( p->Handle.s + p->Handle.lnth <= afterBegin + afterLen ); |
---|
[f450f2f] | 477 | // take end as end-anchored |
---|
| 478 | // stretch-shrink p according to the edit |
---|
[681e12f] | 479 | p->Handle.lnth += s.Handle.lnth; |
---|
[f450f2f] | 480 | p->Handle.lnth -= oldLnth; |
---|
| 481 | } |
---|
| 482 | // take start as start-anchored |
---|
| 483 | size_t startOffsetFromStart = p->Handle.s - beforeBegin; |
---|
[4b3b352] | 484 | p->Handle.s = resultSesStart + startOffsetFromStart; |
---|
[6cc87c0] | 485 | } else { |
---|
[218096f] | 486 | verify ( p->Handle.s < afterBegin ); |
---|
[f450f2f] | 487 | // p starts during the edit |
---|
[218096f] | 488 | verify( p->Handle.s + p->Handle.lnth >= beforeBegin + beforeLen ); |
---|
[f450f2f] | 489 | if ( p->Handle.s + p->Handle.lnth < afterBegin ) { |
---|
[6cc87c0] | 490 | // p ends during the edit; p does not include the last character replaced |
---|
[f450f2f] | 491 | // set p to empty string at start of edit |
---|
[681e12f] | 492 | p->Handle.s = s.Handle.s; |
---|
[f450f2f] | 493 | p->Handle.lnth = 0; |
---|
| 494 | } else { |
---|
[6cc87c0] | 495 | // p includes the end of the edit |
---|
[f450f2f] | 496 | // clip start of p to start at end of edit |
---|
[6cc87c0] | 497 | int charsToClip = afterBegin - p->Handle.s; |
---|
[681e12f] | 498 | p->Handle.s = s.Handle.s + s.Handle.lnth; |
---|
[6cc87c0] | 499 | p->Handle.lnth -= charsToClip; |
---|
[f450f2f] | 500 | } |
---|
| 501 | } |
---|
[6f7aff3] | 502 | if (resultPadPosition) |
---|
| 503 | MoveThisAfter( p->Handle, *resultPadPosition ); // move substring handle to maintain sorted order by string position |
---|
[4b3b352] | 504 | } |
---|
| 505 | } |
---|
| 506 | |
---|
[681e12f] | 507 | // traverse the share-edit set (SES) to recover the range of a base string to which `s` belongs |
---|
| 508 | static void locateInShareEditSet( string_res & s, string_res *& shareEditSetStartPeer, string_res *& shareEditSetEndPeer ) { |
---|
| 509 | shareEditSetStartPeer = & s; |
---|
| 510 | shareEditSetEndPeer = & s; |
---|
| 511 | for (string_res * editPeer = s.shareEditSet_next; editPeer != &s; editPeer = editPeer->shareEditSet_next) { |
---|
[4b3b352] | 512 | if ( editPeer->Handle.s < shareEditSetStartPeer->Handle.s ) { |
---|
| 513 | shareEditSetStartPeer = editPeer; |
---|
| 514 | } |
---|
| 515 | if ( shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth < editPeer->Handle.s + editPeer->Handle.lnth) { |
---|
| 516 | shareEditSetEndPeer = editPeer; |
---|
| 517 | } |
---|
[f450f2f] | 518 | } |
---|
[d32679d5] | 519 | } |
---|
| 520 | |
---|
[06280ad] | 521 | static string_res & assign_(string_res & s, const char * buffer, size_t bsize, const string_res & valSrc) { |
---|
[d32679d5] | 522 | |
---|
| 523 | string_res * shareEditSetStartPeer; |
---|
| 524 | string_res * shareEditSetEndPeer; |
---|
[681e12f] | 525 | locateInShareEditSet( s, shareEditSetStartPeer, shareEditSetEndPeer ); |
---|
[4b3b352] | 526 | |
---|
| 527 | verify( shareEditSetEndPeer->Handle.s >= shareEditSetStartPeer->Handle.s ); |
---|
[0ca15b7] | 528 | size_t origEditSetLength = shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth - shareEditSetStartPeer->Handle.s; |
---|
[681e12f] | 529 | verify( origEditSetLength >= s.Handle.lnth ); |
---|
[4b3b352] | 530 | |
---|
[681e12f] | 531 | if ( s.shareEditSet_owns_ulink ) { // assigning to private context |
---|
[6f7aff3] | 532 | // ok to overwrite old value within LHS |
---|
| 533 | char * prefixStartOrig = shareEditSetStartPeer->Handle.s; |
---|
[681e12f] | 534 | int prefixLen = s.Handle.s - prefixStartOrig; |
---|
| 535 | char * suffixStartOrig = s.Handle.s + s.Handle.lnth; |
---|
[6f7aff3] | 536 | int suffixLen = shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth - suffixStartOrig; |
---|
| 537 | |
---|
[681e12f] | 538 | int delta = bsize - s.Handle.lnth; |
---|
| 539 | if ( char * oldBytes = VbyteTryAdjustLast( *s.Handle.ulink, delta ) ) { |
---|
[6f7aff3] | 540 | // growing: copy from old to new |
---|
[681e12f] | 541 | char * dest = VbyteAlloc( *s.Handle.ulink, origEditSetLength + delta ); |
---|
[fe18b46] | 542 | char *destCursor = dest; memcpy(destCursor, prefixStartOrig, prefixLen); |
---|
| 543 | destCursor += prefixLen; memcpy(destCursor, buffer , bsize ); |
---|
| 544 | destCursor += bsize; memcpy(destCursor, suffixStartOrig, suffixLen); |
---|
[681e12f] | 545 | assignEditSet(s, shareEditSetStartPeer, shareEditSetEndPeer, |
---|
[6f7aff3] | 546 | dest, |
---|
[0ca15b7] | 547 | origEditSetLength + delta, |
---|
[6f7aff3] | 548 | 0p, bsize); |
---|
| 549 | free( oldBytes ); |
---|
| 550 | } else { |
---|
| 551 | // room is already allocated in-place: bubble suffix and overwite middle |
---|
| 552 | memmove( suffixStartOrig + delta, suffixStartOrig, suffixLen ); |
---|
[681e12f] | 553 | memcpy( s.Handle.s, buffer, bsize ); |
---|
[6f7aff3] | 554 | |
---|
[681e12f] | 555 | assignEditSet(s, shareEditSetStartPeer, shareEditSetEndPeer, |
---|
[6f7aff3] | 556 | shareEditSetStartPeer->Handle.s, |
---|
[0ca15b7] | 557 | origEditSetLength + delta, |
---|
[6f7aff3] | 558 | 0p, bsize); |
---|
| 559 | } |
---|
[4b3b352] | 560 | |
---|
[6f7aff3] | 561 | } else if ( // assigning to shared context |
---|
[681e12f] | 562 | s.Handle.lnth == origEditSetLength && // overwriting entire run of SES |
---|
[6f7aff3] | 563 | & valSrc && // sourcing from a managed string |
---|
[681e12f] | 564 | valSrc.Handle.ulink == s.Handle.ulink ) { // sourcing from same heap |
---|
[4b3b352] | 565 | |
---|
[6f7aff3] | 566 | // SES's result will only use characters from the source string => reuse source |
---|
[681e12f] | 567 | assignEditSet(s, shareEditSetStartPeer, shareEditSetEndPeer, |
---|
[4b3b352] | 568 | valSrc.Handle.s, |
---|
| 569 | valSrc.Handle.lnth, |
---|
| 570 | &((string_res&)valSrc).Handle, bsize); |
---|
| 571 | |
---|
| 572 | } else { |
---|
[6f7aff3] | 573 | // overwriting a proper substring of some string: mash characters from old and new together (copy on write) |
---|
| 574 | // OR we are importing characters: need to copy eagerly (can't refer to source) |
---|
[4b3b352] | 575 | |
---|
| 576 | // full string is from start of shareEditSetStartPeer thru end of shareEditSetEndPeer |
---|
[681e12f] | 577 | // `s` occurs in the middle of it, to be replaced |
---|
[4b3b352] | 578 | // build up the new text in `pasting` |
---|
| 579 | |
---|
| 580 | string_res pasting = { |
---|
[681e12f] | 581 | * s.Handle.ulink, // maintain same heap, regardless of context |
---|
[4b3b352] | 582 | shareEditSetStartPeer->Handle.s, // start of SES |
---|
[681e12f] | 583 | s.Handle.s - shareEditSetStartPeer->Handle.s }; // length of SES, before s |
---|
[4b3b352] | 584 | append( pasting, |
---|
[681e12f] | 585 | buffer, // start of replacement for s |
---|
| 586 | bsize ); // length of replacement for s |
---|
[4b3b352] | 587 | append( pasting, |
---|
[681e12f] | 588 | s.Handle.s + s.Handle.lnth, // start of SES after s |
---|
[4b3b352] | 589 | shareEditSetEndPeer->Handle.s + shareEditSetEndPeer->Handle.lnth - |
---|
[681e12f] | 590 | (s.Handle.s + s.Handle.lnth) ); // length of SES, after s |
---|
[4b3b352] | 591 | |
---|
| 592 | // The above string building can trigger compaction. |
---|
| 593 | // The reference points (that are arguments of the string building) may move during that building. |
---|
[681e12f] | 594 | // From s point on, they are stable. |
---|
[4b3b352] | 595 | |
---|
[681e12f] | 596 | assignEditSet(s, shareEditSetStartPeer, shareEditSetEndPeer, |
---|
[4b3b352] | 597 | pasting.Handle.s, |
---|
| 598 | pasting.Handle.lnth, |
---|
| 599 | &pasting.Handle, bsize); |
---|
| 600 | } |
---|
[1733184] | 601 | |
---|
[681e12f] | 602 | return s; |
---|
[4b3b352] | 603 | } |
---|
| 604 | |
---|
[e891349] | 605 | string_res & assign(string_res & s, const string_res & src, size_t maxlen) { |
---|
| 606 | return assign_(s, src.Handle.s, min(src.Handle.lnth, maxlen), *0p); |
---|
| 607 | } |
---|
| 608 | |
---|
[06280ad] | 609 | string_res & assign(string_res & s, const char * buffer, size_t bsize) { |
---|
[681e12f] | 610 | return assign_(s, buffer, bsize, *0p); |
---|
[f450f2f] | 611 | } |
---|
| 612 | |
---|
[681e12f] | 613 | string_res & ?=?(string_res & s, char c) { |
---|
| 614 | return assign(s, &c, 1); |
---|
[d8d512e] | 615 | } |
---|
| 616 | |
---|
[f2898df] | 617 | string_res & ?=?( string_res & s, ssize_t rhs ) { |
---|
| 618 | string_res rhs2 = rhs; |
---|
| 619 | s = rhs2; |
---|
| 620 | return s; |
---|
| 621 | } |
---|
| 622 | string_res & ?=?( string_res & s, size_t rhs ) { |
---|
| 623 | string_res rhs2 = rhs; |
---|
| 624 | s = rhs2; |
---|
| 625 | return s; |
---|
| 626 | } |
---|
| 627 | string_res & ?=?( string_res & s, double rhs ) { |
---|
| 628 | string_res rhs2 = rhs; |
---|
| 629 | s = rhs2; |
---|
| 630 | return s; |
---|
| 631 | } |
---|
| 632 | string_res & ?=?( string_res & s, long double rhs ) { |
---|
| 633 | string_res rhs2 = rhs; |
---|
| 634 | s = rhs2; |
---|
| 635 | return s; |
---|
| 636 | } |
---|
| 637 | string_res & ?=?( string_res & s, double _Complex rhs ) { |
---|
| 638 | string_res rhs2 = rhs; |
---|
| 639 | s = rhs2; |
---|
| 640 | return s; |
---|
| 641 | } |
---|
| 642 | string_res & ?=?( string_res & s, long double _Complex rhs ) { |
---|
| 643 | string_res rhs2 = rhs; |
---|
| 644 | s = rhs2; |
---|
| 645 | return s; |
---|
| 646 | } |
---|
| 647 | |
---|
[d8d512e] | 648 | // Copy assignment operator |
---|
[681e12f] | 649 | string_res & ?=?(string_res & s, const string_res & rhs) with( s ) { |
---|
| 650 | return assign_(s, rhs.Handle.s, rhs.Handle.lnth, rhs); |
---|
[d8d512e] | 651 | } |
---|
| 652 | |
---|
[681e12f] | 653 | string_res & ?=?(string_res & s, string_res & rhs) with( s ) { |
---|
[f450f2f] | 654 | const string_res & rhs2 = rhs; |
---|
[681e12f] | 655 | return s = rhs2; |
---|
[f450f2f] | 656 | } |
---|
| 657 | |
---|
| 658 | |
---|
| 659 | // Destructor |
---|
[681e12f] | 660 | void ^?{}(string_res & s) with(s) { |
---|
[f450f2f] | 661 | // much delegated to implied ^VbyteSM |
---|
| 662 | |
---|
| 663 | // sever s from its share-edit peers, if any (four no-ops when already solo) |
---|
| 664 | s.shareEditSet_prev->shareEditSet_next = s.shareEditSet_next; |
---|
| 665 | s.shareEditSet_next->shareEditSet_prev = s.shareEditSet_prev; |
---|
[218096f] | 666 | // s.shareEditSet_next = &s; |
---|
| 667 | // s.shareEditSet_prev = &s; |
---|
[804bf677] | 668 | |
---|
| 669 | if (shareEditSet_owns_ulink && s.shareEditSet_next == &s) { // last one out |
---|
| 670 | delete( s.Handle.ulink ); |
---|
| 671 | } |
---|
[f450f2f] | 672 | } |
---|
| 673 | |
---|
| 674 | |
---|
| 675 | // Returns the character at the given index |
---|
| 676 | // With unicode support, this may be different from just the byte at the given |
---|
| 677 | // offset from the start of the string. |
---|
[681e12f] | 678 | char ?[?](const string_res & s, size_t index) with(s) { |
---|
[f450f2f] | 679 | //TODO: Check if index is valid (no exceptions yet) |
---|
| 680 | return Handle.s[index]; |
---|
| 681 | } |
---|
| 682 | |
---|
[681e12f] | 683 | void assignAt(const string_res & s, size_t index, char val) { |
---|
[e8b3717] | 684 | // caution: not tested (not reachable by string-api-coverage interface) |
---|
| 685 | // equivalent form at string level is `s[index] = val`, |
---|
| 686 | // which uses the overload that returns a length-1 string |
---|
| 687 | string_res editZone = { s, SHARE_EDITS, index, 1 }; |
---|
[218096f] | 688 | assign(editZone, &val, 1); |
---|
| 689 | } |
---|
| 690 | |
---|
[d8d512e] | 691 | |
---|
[f450f2f] | 692 | /////////////////////////////////////////////////////////////////// |
---|
[d8d512e] | 693 | // Concatenation |
---|
[f450f2f] | 694 | |
---|
[681e12f] | 695 | void append(string_res & str1, const char * buffer, size_t bsize) { |
---|
[4e8df745] | 696 | size_t clnth = str1.Handle.lnth + bsize; |
---|
| 697 | if ( str1.Handle.s + str1.Handle.lnth == buffer ) { // already juxtapose ? |
---|
[d8d512e] | 698 | // no-op |
---|
[f450f2f] | 699 | } else { // must copy some text |
---|
[4e8df745] | 700 | if ( str1.Handle.s + str1.Handle.lnth == VbyteAlloc(*str1.Handle.ulink, 0) ) { // str1 at end of string area ? |
---|
[0f781fb8] | 701 | VbyteAlloc( *str1.Handle.ulink, bsize ); // create room for 2nd part at the end of string area |
---|
[f450f2f] | 702 | } else { // copy the two parts |
---|
[fe18b46] | 703 | char * str1newBuf = VbyteAlloc( *str1.Handle.ulink, clnth ); |
---|
| 704 | char * str1oldBuf = str1.Handle.s; // must read after VbyteAlloc call in case it gs's |
---|
| 705 | str1.Handle.s = str1newBuf; |
---|
[4e8df745] | 706 | memcpy( str1.Handle.s, str1oldBuf, str1.Handle.lnth ); |
---|
[f450f2f] | 707 | } // if |
---|
[4e8df745] | 708 | memcpy( str1.Handle.s + str1.Handle.lnth, buffer, bsize ); |
---|
[f450f2f] | 709 | } // if |
---|
| 710 | str1.Handle.lnth = clnth; |
---|
| 711 | } |
---|
| 712 | |
---|
[681e12f] | 713 | void ?+=?(string_res & str1, const string_res & str2) { |
---|
[d8d512e] | 714 | append( str1, str2.Handle.s, str2.Handle.lnth ); |
---|
[f450f2f] | 715 | } |
---|
| 716 | |
---|
[e891349] | 717 | void append(string_res & str1, const string_res & str2, size_t maxlen) { |
---|
| 718 | append( str1, str2.Handle.s, min(str2.Handle.lnth, maxlen) ); |
---|
| 719 | } |
---|
| 720 | |
---|
[681e12f] | 721 | void ?+=?(string_res & s, char c) { |
---|
| 722 | append( s, & c, 1 ); |
---|
[f450f2f] | 723 | } |
---|
[e891349] | 724 | void ?+=?(string_res & s, const char * c) { |
---|
| 725 | append( s, c, strlen(c) ); |
---|
| 726 | } |
---|
[f450f2f] | 727 | |
---|
[38951c31] | 728 | /////////////////////////////////////////////////////////////////// |
---|
| 729 | // Repetition |
---|
| 730 | |
---|
| 731 | void ?*=?(string_res & s, size_t factor) { |
---|
[e8b3717] | 732 | string_res s2 = { s, COPY_VALUE }; |
---|
[38951c31] | 733 | s = ""; |
---|
| 734 | for (factor) s += s2; |
---|
| 735 | } |
---|
| 736 | |
---|
[f450f2f] | 737 | ////////////////////////////////////////////////////////// |
---|
| 738 | // Comparisons |
---|
| 739 | |
---|
[681e12f] | 740 | int strcmp(const string_res & s1, const string_res & s2) { |
---|
[416b443] | 741 | // return 0; |
---|
| 742 | int ans1 = memcmp(s1.Handle.s, s2.Handle.s, min(s1.Handle.lnth, s2.Handle.lnth)); |
---|
| 743 | if (ans1 != 0) return ans1; |
---|
| 744 | return s1.Handle.lnth - s2.Handle.lnth; |
---|
[f450f2f] | 745 | } |
---|
| 746 | |
---|
[681e12f] | 747 | bool ?==?(const string_res & s1, const string_res & s2) { return strcmp(s1, s2) == 0; } |
---|
| 748 | bool ?!=?(const string_res & s1, const string_res & s2) { return strcmp(s1, s2) != 0; } |
---|
| 749 | bool ?>? (const string_res & s1, const string_res & s2) { return strcmp(s1, s2) > 0; } |
---|
| 750 | bool ?>=?(const string_res & s1, const string_res & s2) { return strcmp(s1, s2) >= 0; } |
---|
| 751 | bool ?<=?(const string_res & s1, const string_res & s2) { return strcmp(s1, s2) <= 0; } |
---|
| 752 | bool ?<? (const string_res & s1, const string_res & s2) { return strcmp(s1, s2) < 0; } |
---|
[416b443] | 753 | |
---|
[06280ad] | 754 | int strcmp (const string_res & s1, const char * s2) { |
---|
[416b443] | 755 | string_res s2x = s2; |
---|
[681e12f] | 756 | return strcmp(s1, s2x); |
---|
[f450f2f] | 757 | } |
---|
[416b443] | 758 | |
---|
[06280ad] | 759 | bool ?==?(const string_res & s1, const char * s2) { return strcmp(s1, s2) == 0; } |
---|
| 760 | bool ?!=?(const string_res & s1, const char * s2) { return strcmp(s1, s2) != 0; } |
---|
| 761 | bool ?>? (const string_res & s1, const char * s2) { return strcmp(s1, s2) > 0; } |
---|
| 762 | bool ?>=?(const string_res & s1, const char * s2) { return strcmp(s1, s2) >= 0; } |
---|
| 763 | bool ?<=?(const string_res & s1, const char * s2) { return strcmp(s1, s2) <= 0; } |
---|
| 764 | bool ?<? (const string_res & s1, const char * s2) { return strcmp(s1, s2) < 0; } |
---|
[416b443] | 765 | |
---|
[06280ad] | 766 | int strcmp (const char * s1, const string_res & s2) { |
---|
[416b443] | 767 | string_res s1x = s1; |
---|
[681e12f] | 768 | return strcmp(s1x, s2); |
---|
[f450f2f] | 769 | } |
---|
| 770 | |
---|
[06280ad] | 771 | bool ?==?(const char * s1, const string_res & s2) { return strcmp(s1, s2) == 0; } |
---|
| 772 | bool ?!=?(const char * s1, const string_res & s2) { return strcmp(s1, s2) != 0; } |
---|
| 773 | bool ?>? (const char * s1, const string_res & s2) { return strcmp(s1, s2) > 0; } |
---|
| 774 | bool ?>=?(const char * s1, const string_res & s2) { return strcmp(s1, s2) >= 0; } |
---|
| 775 | bool ?<=?(const char * s1, const string_res & s2) { return strcmp(s1, s2) <= 0; } |
---|
| 776 | bool ?<? (const char * s1, const string_res & s2) { return strcmp(s1, s2) < 0; } |
---|
[416b443] | 777 | |
---|
| 778 | |
---|
[f450f2f] | 779 | |
---|
| 780 | ////////////////////////////////////////////////////////// |
---|
| 781 | // Search |
---|
| 782 | |
---|
[681e12f] | 783 | bool contains(const string_res & s, char ch) { |
---|
[9ca5e56] | 784 | for ( i; size(s) ) { |
---|
[f450f2f] | 785 | if (s[i] == ch) return true; |
---|
| 786 | } |
---|
| 787 | return false; |
---|
| 788 | } |
---|
| 789 | |
---|
[681e12f] | 790 | int find(const string_res & s, char search) { |
---|
[08ed947] | 791 | return findFrom(s, 0, search); |
---|
[f450f2f] | 792 | } |
---|
| 793 | |
---|
[681e12f] | 794 | int findFrom(const string_res & s, size_t fromPos, char search) { |
---|
[08ed947] | 795 | // FIXME: This paricular overload (find of single char) is optimized to use memchr. |
---|
| 796 | // The general overload (find of string, memchr applying to its first character) and `contains` should be adjusted to match. |
---|
| 797 | char * searchFrom = s.Handle.s + fromPos; |
---|
| 798 | size_t searchLnth = s.Handle.lnth - fromPos; |
---|
| 799 | int searchVal = search; |
---|
| 800 | char * foundAt = (char *) memchr(searchFrom, searchVal, searchLnth); |
---|
| 801 | if (foundAt == 0p) return s.Handle.lnth; |
---|
| 802 | else return foundAt - s.Handle.s; |
---|
| 803 | } |
---|
[f450f2f] | 804 | |
---|
[681e12f] | 805 | int find(const string_res & s, const string_res & search) { |
---|
[08ed947] | 806 | return findFrom(s, 0, search); |
---|
| 807 | } |
---|
| 808 | |
---|
[681e12f] | 809 | int findFrom(const string_res & s, size_t fromPos, const string_res & search) { |
---|
[08ed947] | 810 | return findFrom(s, fromPos, search.Handle.s, search.Handle.lnth); |
---|
[f450f2f] | 811 | } |
---|
| 812 | |
---|
[06280ad] | 813 | int find(const string_res & s, const char * search) { |
---|
[08ed947] | 814 | return findFrom(s, 0, search); |
---|
| 815 | } |
---|
[06280ad] | 816 | int findFrom(const string_res & s, size_t fromPos, const char * search) { |
---|
[08ed947] | 817 | return findFrom(s, fromPos, search, strlen(search)); |
---|
[f450f2f] | 818 | } |
---|
| 819 | |
---|
[06280ad] | 820 | int find(const string_res & s, const char * search, size_t searchsize) { |
---|
[08ed947] | 821 | return findFrom(s, 0, search, searchsize); |
---|
| 822 | } |
---|
| 823 | |
---|
[06280ad] | 824 | int findFrom(const string_res & s, size_t fromPos, const char * search, size_t searchsize) { |
---|
[08ed947] | 825 | |
---|
| 826 | /* Remaining implementations essentially ported from Sunjay's work */ |
---|
| 827 | |
---|
| 828 | |
---|
[f450f2f] | 829 | // FIXME: This is a naive algorithm. We probably want to switch to someting |
---|
| 830 | // like Boyer-Moore in the future. |
---|
| 831 | // https://en.wikipedia.org/wiki/String_searching_algorithm |
---|
| 832 | |
---|
| 833 | // Always find the empty string |
---|
| 834 | if (searchsize == 0) { |
---|
| 835 | return 0; |
---|
| 836 | } |
---|
| 837 | |
---|
[9ca5e56] | 838 | for ( i; fromPos ~ s.Handle.lnth ) { |
---|
[f450f2f] | 839 | size_t remaining = s.Handle.lnth - i; |
---|
| 840 | // Never going to find the search string if the remaining string is |
---|
| 841 | // smaller than search |
---|
| 842 | if (remaining < searchsize) { |
---|
| 843 | break; |
---|
| 844 | } |
---|
| 845 | |
---|
| 846 | bool matched = true; |
---|
[9ca5e56] | 847 | for ( j; searchsize ) { |
---|
[f450f2f] | 848 | if (search[j] != s.Handle.s[i + j]) { |
---|
| 849 | matched = false; |
---|
| 850 | break; |
---|
| 851 | } |
---|
| 852 | } |
---|
| 853 | if (matched) { |
---|
| 854 | return i; |
---|
| 855 | } |
---|
| 856 | } |
---|
| 857 | |
---|
| 858 | return s.Handle.lnth; |
---|
| 859 | } |
---|
| 860 | |
---|
[681e12f] | 861 | bool includes(const string_res & s, const string_res & search) { |
---|
[f450f2f] | 862 | return includes(s, search.Handle.s, search.Handle.lnth); |
---|
| 863 | } |
---|
| 864 | |
---|
[06280ad] | 865 | bool includes(const string_res & s, const char * search) { |
---|
[f450f2f] | 866 | return includes(s, search, strlen(search)); |
---|
| 867 | } |
---|
| 868 | |
---|
[06280ad] | 869 | bool includes(const string_res & s, const char * search, size_t searchsize) { |
---|
[f450f2f] | 870 | return find(s, search, searchsize) < s.Handle.lnth; |
---|
| 871 | } |
---|
| 872 | |
---|
[681e12f] | 873 | bool startsWith(const string_res & s, const string_res & prefix) { |
---|
[f450f2f] | 874 | return startsWith(s, prefix.Handle.s, prefix.Handle.lnth); |
---|
| 875 | } |
---|
| 876 | |
---|
[06280ad] | 877 | bool startsWith(const string_res & s, const char * prefix) { |
---|
[f450f2f] | 878 | return startsWith(s, prefix, strlen(prefix)); |
---|
| 879 | } |
---|
| 880 | |
---|
[06280ad] | 881 | bool startsWith(const string_res & s, const char * prefix, size_t prefixsize) { |
---|
[f450f2f] | 882 | if (s.Handle.lnth < prefixsize) { |
---|
| 883 | return false; |
---|
| 884 | } |
---|
| 885 | return memcmp(s.Handle.s, prefix, prefixsize) == 0; |
---|
| 886 | } |
---|
| 887 | |
---|
[681e12f] | 888 | bool endsWith(const string_res & s, const string_res & suffix) { |
---|
[f450f2f] | 889 | return endsWith(s, suffix.Handle.s, suffix.Handle.lnth); |
---|
| 890 | } |
---|
| 891 | |
---|
[06280ad] | 892 | bool endsWith(const string_res & s, const char * suffix) { |
---|
[f450f2f] | 893 | return endsWith(s, suffix, strlen(suffix)); |
---|
| 894 | } |
---|
| 895 | |
---|
[06280ad] | 896 | bool endsWith(const string_res & s, const char * suffix, size_t suffixsize) { |
---|
[f450f2f] | 897 | if (s.Handle.lnth < suffixsize) { |
---|
| 898 | return false; |
---|
| 899 | } |
---|
| 900 | // Amount to offset the bytes pointer so that we are comparing the end of s |
---|
| 901 | // to suffix. s.bytes + offset should be the first byte to compare against suffix |
---|
| 902 | size_t offset = s.Handle.lnth - suffixsize; |
---|
| 903 | return memcmp(s.Handle.s + offset, suffix, suffixsize) == 0; |
---|
| 904 | } |
---|
| 905 | |
---|
| 906 | /* Back to Mike's work */ |
---|
| 907 | |
---|
| 908 | |
---|
| 909 | /////////////////////////////////////////////////////////////////////////// |
---|
| 910 | // charclass, include, exclude |
---|
| 911 | |
---|
[681e12f] | 912 | void ?{}( charclass_res & s, const string_res & chars) { |
---|
| 913 | (s){ chars.Handle.s, chars.Handle.lnth }; |
---|
[f450f2f] | 914 | } |
---|
| 915 | |
---|
[681e12f] | 916 | void ?{}( charclass_res & s, const char * chars ) { |
---|
| 917 | (s){ chars, strlen(chars) }; |
---|
[f450f2f] | 918 | } |
---|
| 919 | |
---|
[681e12f] | 920 | void ?{}( charclass_res & s, const char * chars, size_t charssize ) { |
---|
| 921 | (s.chars){ chars, charssize }; |
---|
[f450f2f] | 922 | // now sort it ? |
---|
| 923 | } |
---|
| 924 | |
---|
[681e12f] | 925 | void ^?{}( charclass_res & s ) { |
---|
| 926 | ^(s.chars){}; |
---|
[f450f2f] | 927 | } |
---|
| 928 | |
---|
| 929 | static bool test( const charclass_res & mask, char c ) { |
---|
| 930 | // instead, use sorted char list? |
---|
| 931 | return contains( mask.chars, c ); |
---|
| 932 | } |
---|
| 933 | |
---|
[681e12f] | 934 | int exclude(const string_res & s, const charclass_res & mask) { |
---|
[9ca5e56] | 935 | for ( i; size(s) ) { |
---|
[f450f2f] | 936 | if ( test(mask, s[i]) ) return i; |
---|
| 937 | } |
---|
| 938 | return size(s); |
---|
| 939 | } |
---|
| 940 | |
---|
[681e12f] | 941 | int include(const string_res & s, const charclass_res & mask) { |
---|
[9ca5e56] | 942 | for ( i; size(s) ) { |
---|
[f450f2f] | 943 | if ( ! test(mask, s[i]) ) return i; |
---|
| 944 | } |
---|
| 945 | return size(s); |
---|
| 946 | } |
---|
| 947 | |
---|
| 948 | //######################### VbyteHeap "implementation" ######################### |
---|
| 949 | |
---|
| 950 | |
---|
| 951 | // Add a new HandleNode node n after the current HandleNode node. |
---|
| 952 | |
---|
[681e12f] | 953 | static void AddThisAfter( HandleNode & s, HandleNode & n ) with(s) { |
---|
[f450f2f] | 954 | #ifdef VbyteDebug |
---|
[681e12f] | 955 | serr | "enter:AddThisAfter, s:" | &s | " n:" | &n; |
---|
[f450f2f] | 956 | #endif // VbyteDebug |
---|
[08ed947] | 957 | // Performance note: we are on the critical path here. MB has ensured that the verifies don't contribute to runtime (are compiled away, like they're supposed to be). |
---|
[0f781fb8] | 958 | verify( n.ulink != 0p ); |
---|
[681e12f] | 959 | verify( s.ulink == n.ulink ); |
---|
[f450f2f] | 960 | flink = n.flink; |
---|
| 961 | blink = &n; |
---|
[681e12f] | 962 | n.flink->blink = &s; |
---|
| 963 | n.flink = &s; |
---|
[f450f2f] | 964 | #ifdef VbyteDebug |
---|
| 965 | { |
---|
| 966 | serr | "HandleList:"; |
---|
| 967 | serr | nlOff; |
---|
| 968 | for ( HandleNode *ni = HeaderPtr->flink; ni != HeaderPtr; ni = ni->flink ) { |
---|
| 969 | serr | "\tnode:" | ni | " lnth:" | ni->lnth | " s:" | (void *)ni->s | ",\""; |
---|
[9ca5e56] | 970 | for ( i; ni->lnth ) { |
---|
[f450f2f] | 971 | serr | ni->s[i]; |
---|
| 972 | } // for |
---|
| 973 | serr | "\" flink:" | ni->flink | " blink:" | ni->blink | nl; |
---|
| 974 | } // for |
---|
| 975 | serr | nlOn; |
---|
| 976 | } |
---|
| 977 | serr | "exit:AddThisAfter"; |
---|
| 978 | #endif // VbyteDebug |
---|
| 979 | } // AddThisAfter |
---|
| 980 | |
---|
| 981 | |
---|
| 982 | // Delete the current HandleNode node. |
---|
| 983 | |
---|
[681e12f] | 984 | static void DeleteNode( HandleNode & s ) with(s) { |
---|
[f450f2f] | 985 | #ifdef VbyteDebug |
---|
[681e12f] | 986 | serr | "enter:DeleteNode, s:" | &s; |
---|
[f450f2f] | 987 | #endif // VbyteDebug |
---|
| 988 | flink->blink = blink; |
---|
| 989 | blink->flink = flink; |
---|
| 990 | #ifdef VbyteDebug |
---|
| 991 | serr | "exit:DeleteNode"; |
---|
| 992 | #endif // VbyteDebug |
---|
| 993 | } // DeleteNode |
---|
| 994 | |
---|
| 995 | |
---|
| 996 | // Allocates specified storage for a string from byte-string area. If not enough space remains to perform the |
---|
[6f7aff3] | 997 | // allocation, the garbage collection routine is called. |
---|
[f450f2f] | 998 | |
---|
[681e12f] | 999 | static char * VbyteAlloc( VbyteHeap & s, int size ) with(s) { |
---|
[f450f2f] | 1000 | #ifdef VbyteDebug |
---|
| 1001 | serr | "enter:VbyteAlloc, size:" | size; |
---|
| 1002 | #endif // VbyteDebug |
---|
| 1003 | uintptr_t NoBytes; |
---|
| 1004 | char *r; |
---|
| 1005 | |
---|
| 1006 | NoBytes = ( uintptr_t )EndVbyte + size; |
---|
[681e12f] | 1007 | if ( NoBytes > ( uintptr_t )ExtVbyte ) { // enough room for new byte-string ? |
---|
| 1008 | garbage( s, size ); // firer up the garbage collector |
---|
[4e8df745] | 1009 | verify( (( uintptr_t )EndVbyte + size) <= ( uintptr_t )ExtVbyte && "garbage run did not free up required space" ); |
---|
[f450f2f] | 1010 | } // if |
---|
| 1011 | r = EndVbyte; |
---|
| 1012 | EndVbyte += size; |
---|
| 1013 | #ifdef VbyteDebug |
---|
| 1014 | serr | "exit:VbyteAlloc, r:" | (void *)r | " EndVbyte:" | (void *)EndVbyte | " ExtVbyte:" | ExtVbyte; |
---|
| 1015 | #endif // VbyteDebug |
---|
| 1016 | return r; |
---|
| 1017 | } // VbyteAlloc |
---|
| 1018 | |
---|
| 1019 | |
---|
[6f7aff3] | 1020 | // Adjusts the last allocation in this heap by delta bytes, or resets this heap to be able to offer |
---|
| 1021 | // new allocations of its original size + delta bytes. Positive delta means bigger; |
---|
| 1022 | // negative means smaller. A null return indicates that the original heap location has room for |
---|
| 1023 | // the requested growth. A non-null return indicates that copying to a new location is required |
---|
| 1024 | // but has not been done; the returned value is the old heap storage location; `this` heap is |
---|
| 1025 | // modified to reference the new location. In the copy-requred case, the caller should use |
---|
| 1026 | // VbyteAlloc to claim the new space, while doing optimal copying from old to new, then free old. |
---|
| 1027 | |
---|
[681e12f] | 1028 | static char * VbyteTryAdjustLast( VbyteHeap & s, int delta ) with(s) { |
---|
[6f7aff3] | 1029 | if ( ( uintptr_t )EndVbyte + delta <= ( uintptr_t )ExtVbyte ) { |
---|
| 1030 | // room available |
---|
| 1031 | EndVbyte += delta; |
---|
| 1032 | return 0p; |
---|
| 1033 | } |
---|
| 1034 | |
---|
| 1035 | char *oldBytes = StartVbyte; |
---|
| 1036 | |
---|
| 1037 | NoOfExtensions += 1; |
---|
| 1038 | CurrSize *= 2; |
---|
[08ed947] | 1039 | StartVbyte = EndVbyte = TEMP_ALLOC(char, CurrSize); |
---|
[6f7aff3] | 1040 | ExtVbyte = StartVbyte + CurrSize; |
---|
| 1041 | |
---|
| 1042 | return oldBytes; |
---|
| 1043 | } |
---|
| 1044 | |
---|
| 1045 | |
---|
[f450f2f] | 1046 | // Move an existing HandleNode node h somewhere after the current HandleNode node so that it is in ascending order by |
---|
| 1047 | // the address in the byte string area. |
---|
| 1048 | |
---|
[681e12f] | 1049 | static void MoveThisAfter( HandleNode & s, const HandleNode & h ) with(s) { |
---|
[f450f2f] | 1050 | #ifdef VbyteDebug |
---|
[681e12f] | 1051 | serr | "enter:MoveThisAfter, s:" | & s | " h:" | & h; |
---|
[f450f2f] | 1052 | #endif // VbyteDebug |
---|
[0f781fb8] | 1053 | verify( h.ulink != 0p ); |
---|
[681e12f] | 1054 | verify( s.ulink == h.ulink ); |
---|
[f450f2f] | 1055 | if ( s < h.s ) { // check argument values |
---|
| 1056 | // serr | "VbyteSM: Error - Cannot move byte string starting at:" | s | " after byte string starting at:" |
---|
| 1057 | // | ( h->s ) | " and keep handles in ascending order"; |
---|
| 1058 | // exit(-1 ); |
---|
[218096f] | 1059 | verify( 0 && "VbyteSM: Error - Cannot move byte strings as requested and keep handles in ascending order"); |
---|
[f450f2f] | 1060 | } // if |
---|
| 1061 | |
---|
| 1062 | HandleNode *i; |
---|
| 1063 | for ( i = h.flink; i->s != 0 && s > ( i->s ); i = i->flink ); // find the position for this node after h |
---|
[681e12f] | 1064 | if ( & s != i->blink ) { |
---|
| 1065 | DeleteNode( s ); |
---|
| 1066 | AddThisAfter( s, *i->blink ); |
---|
[f450f2f] | 1067 | } // if |
---|
| 1068 | #ifdef VbyteDebug |
---|
| 1069 | { |
---|
| 1070 | serr | "HandleList:"; |
---|
| 1071 | serr | nlOff; |
---|
| 1072 | for ( HandleNode *n = HeaderPtr->flink; n != HeaderPtr; n = n->flink ) { |
---|
| 1073 | serr | "\tnode:" | n | " lnth:" | n->lnth | " s:" | (void *)n->s | ",\""; |
---|
[9ca5e56] | 1074 | for ( i; n->lnth ) { |
---|
| 1075 | serr | n->s[i]; |
---|
[f450f2f] | 1076 | } // for |
---|
[6cc87c0] | 1077 | serr | "\" flink:" | n->flink | " blink:" | n->blink | nl; |
---|
[f450f2f] | 1078 | } // for |
---|
| 1079 | serr | nlOn; |
---|
| 1080 | } |
---|
[6cc87c0] | 1081 | serr | "exit:MoveThisAfter"; |
---|
[f450f2f] | 1082 | #endif // VbyteDebug |
---|
| 1083 | } // MoveThisAfter |
---|
| 1084 | |
---|
| 1085 | |
---|
| 1086 | |
---|
| 1087 | |
---|
| 1088 | |
---|
| 1089 | //######################### VbyteHeap ######################### |
---|
| 1090 | |
---|
| 1091 | // Compare two byte strings in the byte-string area. The routine returns the following values: |
---|
| 1092 | // |
---|
| 1093 | // 1 => Src1-byte-string > Src2-byte-string |
---|
| 1094 | // 0 => Src1-byte-string = Src2-byte-string |
---|
| 1095 | // -1 => Src1-byte-string < Src2-byte-string |
---|
| 1096 | |
---|
[0f781fb8] | 1097 | int ByteCmp( char *Src1, int Src1Start, int Src1Lnth, char *Src2, int Src2Start, int Src2Lnth ) { |
---|
[f450f2f] | 1098 | #ifdef VbyteDebug |
---|
| 1099 | serr | "enter:ByteCmp, Src1Start:" | Src1Start | " Src1Lnth:" | Src1Lnth | " Src2Start:" | Src2Start | " Src2Lnth:" | Src2Lnth; |
---|
| 1100 | #endif // VbyteDebug |
---|
| 1101 | int cmp; |
---|
| 1102 | |
---|
| 1103 | CharZip: for ( int i = 0; ; i += 1 ) { |
---|
| 1104 | if ( i == Src2Lnth - 1 ) { |
---|
| 1105 | for ( ; ; i += 1 ) { |
---|
| 1106 | if ( i == Src1Lnth - 1 ) { |
---|
| 1107 | cmp = 0; |
---|
| 1108 | break CharZip; |
---|
| 1109 | } // exit |
---|
| 1110 | if ( Src1[Src1Start + i] != ' ') { |
---|
| 1111 | // SUSPECTED BUG: this could be be why Peter got the bug report about == " " (why is this case here at all?) |
---|
| 1112 | cmp = 1; |
---|
| 1113 | break CharZip; |
---|
| 1114 | } // exit |
---|
| 1115 | } // for |
---|
| 1116 | } // exit |
---|
| 1117 | if ( i == Src1Lnth - 1 ) { |
---|
| 1118 | for ( ; ; i += 1 ) { |
---|
| 1119 | if ( i == Src2Lnth - 1 ) { |
---|
| 1120 | cmp = 0; |
---|
| 1121 | break CharZip; |
---|
| 1122 | } // exit |
---|
| 1123 | if ( Src2[Src2Start + i] != ' ') { |
---|
| 1124 | cmp = -1; |
---|
| 1125 | break CharZip; |
---|
| 1126 | } // exit |
---|
| 1127 | } // for |
---|
| 1128 | } // exit |
---|
| 1129 | if ( Src2[Src2Start + i] != Src1[Src1Start+ i]) { |
---|
| 1130 | cmp = Src1[Src1Start + i] > Src2[Src2Start + i] ? 1 : -1; |
---|
| 1131 | break CharZip; |
---|
| 1132 | } // exit |
---|
| 1133 | } // for |
---|
| 1134 | #ifdef VbyteDebug |
---|
| 1135 | serr | "exit:ByteCmp, cmp:" | cmp; |
---|
| 1136 | #endif // VbyteDebug |
---|
| 1137 | return cmp; |
---|
| 1138 | } // ByteCmp |
---|
| 1139 | |
---|
| 1140 | |
---|
| 1141 | // The compaction moves all of the byte strings currently in use to the beginning of the byte-string area and modifies |
---|
| 1142 | // the handles to reflect the new positions of the byte strings. Compaction assumes that the handle list is in ascending |
---|
| 1143 | // order by pointers into the byte-string area. The strings associated with substrings do not have to be moved because |
---|
| 1144 | // the containing string has been moved. Hence, they only require that their string pointers be adjusted. |
---|
| 1145 | |
---|
[681e12f] | 1146 | void compaction(VbyteHeap & s) with(s) { |
---|
[f450f2f] | 1147 | HandleNode *h; |
---|
| 1148 | char *obase, *nbase, *limit; |
---|
| 1149 | |
---|
| 1150 | NoOfCompactions += 1; |
---|
| 1151 | EndVbyte = StartVbyte; |
---|
| 1152 | h = Header.flink; // ignore header node |
---|
[9ca5e56] | 1153 | for () { |
---|
[94647b0b] | 1154 | memmove( EndVbyte, h->s, h->lnth ); |
---|
[f450f2f] | 1155 | obase = h->s; |
---|
| 1156 | h->s = EndVbyte; |
---|
| 1157 | nbase = h->s; |
---|
| 1158 | EndVbyte += h->lnth; |
---|
| 1159 | limit = obase + h->lnth; |
---|
| 1160 | h = h->flink; |
---|
| 1161 | |
---|
| 1162 | // check if any substrings are allocated within a string |
---|
| 1163 | |
---|
[9ca5e56] | 1164 | for () { |
---|
[f450f2f] | 1165 | if ( h == &Header ) break; // end of header list ? |
---|
| 1166 | if ( h->s >= limit ) break; // outside of current string ? |
---|
| 1167 | h->s = nbase + (( uintptr_t )h->s - ( uintptr_t )obase ); |
---|
| 1168 | h = h->flink; |
---|
| 1169 | } // for |
---|
| 1170 | if ( h == &Header ) break; // end of header list ? |
---|
| 1171 | } // for |
---|
| 1172 | } // compaction |
---|
| 1173 | |
---|
| 1174 | |
---|
[08ed947] | 1175 | static double heap_expansion_freespace_threshold = 0.1; // default inherited from prior work: expand heap when less than 10% "free" (i.e. garbage) |
---|
| 1176 | // probably an unreasonable default, but need to assess early-round tests on changing it |
---|
| 1177 | |
---|
| 1178 | void TUNING_set_string_heap_liveness_threshold( double val ) { |
---|
| 1179 | heap_expansion_freespace_threshold = 1.0 - val; |
---|
| 1180 | } |
---|
| 1181 | |
---|
| 1182 | |
---|
[f450f2f] | 1183 | // Garbage determines the amount of free space left in the heap and then reduces, leave the same, or extends the size of |
---|
| 1184 | // the heap. The heap is then compacted in the existing heap or into the newly allocated heap. |
---|
| 1185 | |
---|
[681e12f] | 1186 | void garbage(VbyteHeap & s, int minreq ) with(s) { |
---|
[f450f2f] | 1187 | #ifdef VbyteDebug |
---|
| 1188 | serr | "enter:garbage"; |
---|
| 1189 | { |
---|
| 1190 | serr | "HandleList:"; |
---|
| 1191 | for ( HandleNode *n = Header.flink; n != &Header; n = n->flink ) { |
---|
| 1192 | serr | nlOff; |
---|
| 1193 | serr | "\tnode:" | n | " lnth:" | n->lnth | " s:" | (void *)n->s | ",\""; |
---|
[9ca5e56] | 1194 | for ( i; n->lnth ) { |
---|
[f450f2f] | 1195 | serr | n->s[i]; |
---|
| 1196 | } // for |
---|
| 1197 | serr | nlOn; |
---|
| 1198 | serr | "\" flink:" | n->flink | " blink:" | n->blink; |
---|
| 1199 | } // for |
---|
| 1200 | } |
---|
| 1201 | #endif // VbyteDebug |
---|
| 1202 | int AmountUsed, AmountFree; |
---|
| 1203 | |
---|
| 1204 | AmountUsed = 0; |
---|
| 1205 | for ( HandleNode *i = Header.flink; i != &Header; i = i->flink ) { // calculate amount of byte area used |
---|
| 1206 | AmountUsed += i->lnth; |
---|
| 1207 | } // for |
---|
| 1208 | AmountFree = ( uintptr_t )ExtVbyte - ( uintptr_t )StartVbyte - AmountUsed; |
---|
| 1209 | |
---|
[08ed947] | 1210 | if ( ( double ) AmountFree < ( CurrSize * heap_expansion_freespace_threshold ) || AmountFree < minreq ) { // free space less than threshold or not enough to serve cur request |
---|
[f450f2f] | 1211 | |
---|
[681e12f] | 1212 | extend( s, max( CurrSize, minreq ) ); // extend the heap |
---|
[f450f2f] | 1213 | |
---|
| 1214 | // Peter says, "This needs work before it should be used." |
---|
| 1215 | // } else if ( AmountFree > CurrSize / 2 ) { // free space greater than 3 times the initial allocation ? |
---|
| 1216 | // reduce(( AmountFree / CurrSize - 3 ) * CurrSize ); // reduce the memory |
---|
| 1217 | |
---|
[97c215f] | 1218 | // `extend` implies a `compaction` during the copy |
---|
| 1219 | |
---|
| 1220 | } else { |
---|
[681e12f] | 1221 | compaction(s); // in-place |
---|
[97c215f] | 1222 | }// if |
---|
[f450f2f] | 1223 | #ifdef VbyteDebug |
---|
| 1224 | { |
---|
| 1225 | serr | "HandleList:"; |
---|
| 1226 | for ( HandleNode *n = Header.flink; n != &Header; n = n->flink ) { |
---|
| 1227 | serr | nlOff; |
---|
| 1228 | serr | "\tnode:" | n | " lnth:" | n->lnth | " s:" | (void *)n->s | ",\""; |
---|
[9ca5e56] | 1229 | for ( i; n->lnth ) { |
---|
[f450f2f] | 1230 | serr | n->s[i]; |
---|
| 1231 | } // for |
---|
| 1232 | serr | nlOn; |
---|
| 1233 | serr | "\" flink:" | n->flink | " blink:" | n->blink; |
---|
| 1234 | } // for |
---|
| 1235 | } |
---|
| 1236 | serr | "exit:garbage"; |
---|
| 1237 | #endif // VbyteDebug |
---|
| 1238 | } // garbage |
---|
| 1239 | |
---|
| 1240 | #undef VbyteDebug |
---|
| 1241 | |
---|
| 1242 | |
---|
| 1243 | |
---|
| 1244 | // Extend the size of the byte-string area by creating a new area and copying the old area into it. The old byte-string |
---|
| 1245 | // area is deleted. |
---|
| 1246 | |
---|
[681e12f] | 1247 | void extend( VbyteHeap & s, int size ) with (s) { |
---|
[f450f2f] | 1248 | #ifdef VbyteDebug |
---|
| 1249 | serr | "enter:extend, size:" | size; |
---|
| 1250 | #endif // VbyteDebug |
---|
| 1251 | char *OldStartVbyte; |
---|
| 1252 | |
---|
| 1253 | NoOfExtensions += 1; |
---|
| 1254 | OldStartVbyte = StartVbyte; // save previous byte area |
---|
| 1255 | |
---|
| 1256 | CurrSize += size > InitSize ? size : InitSize; // minimum extension, initial size |
---|
[08ed947] | 1257 | StartVbyte = EndVbyte = TEMP_ALLOC(char, CurrSize); |
---|
[f450f2f] | 1258 | ExtVbyte = (void *)( StartVbyte + CurrSize ); |
---|
[681e12f] | 1259 | compaction(s); // copy from old heap to new & adjust pointers to new heap |
---|
[7b0e8b7] | 1260 | free( OldStartVbyte ); // release old heap |
---|
[f450f2f] | 1261 | #ifdef VbyteDebug |
---|
| 1262 | serr | "exit:extend, CurrSize:" | CurrSize; |
---|
| 1263 | #endif // VbyteDebug |
---|
| 1264 | } // extend |
---|
| 1265 | |
---|
[7b0e8b7] | 1266 | //WIP |
---|
| 1267 | #if 0 |
---|
[f450f2f] | 1268 | |
---|
| 1269 | // Extend the size of the byte-string area by creating a new area and copying the old area into it. The old byte-string |
---|
| 1270 | // area is deleted. |
---|
| 1271 | |
---|
| 1272 | void VbyteHeap::reduce( int size ) { |
---|
| 1273 | #ifdef VbyteDebug |
---|
| 1274 | serr | "enter:reduce, size:" | size; |
---|
| 1275 | #endif // VbyteDebug |
---|
| 1276 | char *OldStartVbyte; |
---|
| 1277 | |
---|
| 1278 | NoOfReductions += 1; |
---|
| 1279 | OldStartVbyte = StartVbyte; // save previous byte area |
---|
| 1280 | |
---|
| 1281 | CurrSize -= size; |
---|
| 1282 | StartVbyte = EndVbyte = new char[CurrSize]; |
---|
| 1283 | ExtVbyte = (void *)( StartVbyte + CurrSize ); |
---|
| 1284 | compaction(); // copy from old heap to new & adjust pointers to new heap |
---|
| 1285 | delete OldStartVbyte; // release old heap |
---|
| 1286 | #ifdef VbyteDebug |
---|
| 1287 | serr | "exit:reduce, CurrSize:" | CurrSize; |
---|
| 1288 | #endif // VbyteDebug |
---|
| 1289 | } // reduce |
---|
| 1290 | |
---|
| 1291 | |
---|
| 1292 | #endif |
---|