Changeset fe18b46
- Timestamp:
- Oct 19, 2021, 2:55:36 PM (3 years ago)
- Branches:
- ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
- Children:
- 0ca15b7
- Parents:
- 6f7aff3
- Files:
-
- 2 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/containers/string_res.cfa
r6f7aff3 rfe18b46 258 258 } 259 259 260 // private ctor (not in header): use specified heap (ignore ambient) and copy chars in 261 void ?{}( string_res &s, VbyteHeap & heap, const char* rhs, size_t rhslnth ) with(s) { 262 (Handle){ heap }; 263 Handle.s = VbyteAlloc(*Handle.ulink, rhslnth); 264 Handle.lnth = rhslnth; 265 (s.shareEditSet_owns_ulink){ false }; 266 for ( int i = 0; i < rhslnth; i += 1 ) { // copy characters 267 Handle.s[i] = rhs[i]; 268 } // for 269 s.shareEditSet_prev = &s; 270 s.shareEditSet_next = &s; 271 } 272 260 273 // General copy constructor 261 274 void ?{}(string_res &s, const string_res & s2, StrResInitMode mode, size_t start, size_t end ) { … … 263 276 verify( start <= end && end <= s2.Handle.lnth ); 264 277 265 if (s2.Handle.ulink == ambient_string_sharectx->activeHeap) { 266 // same heap: allow overlap 278 if (s2.Handle.ulink != ambient_string_sharectx->activeHeap && mode == COPY_VALUE) { 279 // crossing heaps (including private): copy eagerly 280 eagerCopyCtorHelper(s, s2.Handle.s + start, end - start); 281 verify(s.shareEditSet_prev == &s); 282 verify(s.shareEditSet_next == &s); 283 } else { 267 284 (s.Handle){}; 268 285 s.Handle.s = s2.Handle.s + start; 269 286 s.Handle.lnth = end - start; 270 s.Handle.ulink = ambient_string_sharectx->activeHeap;271 (s.shareEditSet_owns_ulink){ false }; 287 s.Handle.ulink = s2.Handle.ulink; 288 272 289 AddThisAfter(s.Handle, s2.Handle ); // insert this handle after rhs handle 273 290 // ^ bug? skip others at early point in string 274 291 275 292 if (mode == COPY_VALUE) { 293 verify(s2.Handle.ulink == ambient_string_sharectx->activeHeap); 294 // requested logical copy in same heap: defer copy until write 295 296 (s.shareEditSet_owns_ulink){ false }; 297 276 298 // make s alone in its shareEditSet 277 299 s.shareEditSet_prev = &s; … … 279 301 } else { 280 302 verify( mode == SHARE_EDITS ); 303 // sharing edits with source forces same heap as source (ignore context) 304 305 (s.shareEditSet_owns_ulink){ s2.shareEditSet_owns_ulink }; 281 306 282 307 // s2 is logically const but not implementation const … … 289 314 s.shareEditSet_prev->shareEditSet_next = &s; 290 315 } 291 } else {292 // crossing heaps: eager copy293 assert( mode == COPY_VALUE && "need to solidify context-crossing rules for requesting shared edits");294 eagerCopyCtorHelper(s, s2.Handle.s + start, end - start);295 verify(s.shareEditSet_prev == &s);296 verify(s.shareEditSet_next == &s);297 316 } 298 317 } … … 400 419 // growing: copy from old to new 401 420 char * dest = VbyteAlloc( *this.Handle.ulink, editSetLength ); 402 char *destCursor = dest; 403 destCursor += prefixLen; 404 destCursor += this.Handle.lnth;memcpy(destCursor, suffixStartOrig, suffixLen);421 char *destCursor = dest; memcpy(destCursor, prefixStartOrig, prefixLen); 422 destCursor += prefixLen; memcpy(destCursor, buffer , bsize ); 423 destCursor += bsize; memcpy(destCursor, suffixStartOrig, suffixLen); 405 424 assignEditSet(this, shareEditSetStartPeer, shareEditSetEndPeer, 406 425 dest, … … 437 456 // `this` occurs in the middle of it, to be replaced 438 457 // build up the new text in `pasting` 439 440 // super private string ctor: ignore ambient context441 void ?{}( string_res &s, VbyteHeap & heap, const char* rhs, size_t rhslnth ) with(s) {442 (Handle){ heap };443 Handle.s = VbyteAlloc(*Handle.ulink, rhslnth);444 Handle.lnth = rhslnth;445 (s.shareEditSet_owns_ulink){ false };446 for ( int i = 0; i < rhslnth; i += 1 ) { // copy characters447 Handle.s[i] = rhs[i];448 } // for449 s.shareEditSet_prev = &s;450 s.shareEditSet_next = &s;451 }452 458 453 459 string_res pasting = { … … 538 544 VbyteAlloc( *str1.Handle.ulink, bsize ); // create room for 2nd part at the end of string area 539 545 } else { // copy the two parts 540 char * str1oldBuf = str1.Handle.s; 541 str1.Handle.s = VbyteAlloc( *str1.Handle.ulink, clnth ); 546 char * str1newBuf = VbyteAlloc( *str1.Handle.ulink, clnth ); 547 char * str1oldBuf = str1.Handle.s; // must read after VbyteAlloc call in case it gs's 548 str1.Handle.s = str1newBuf; 542 549 ByteCopy( str1.Handle.s, 0, str1.Handle.lnth, str1oldBuf, 0, str1.Handle.lnth); 543 550 } // if -
tests/collections/string-api-coverage.cfa
r6f7aff3 rfe18b46 28 28 29 29 #ifdef STRING_SHARING_OFF 30 sout | "string sharing disabled";31 30 string_sharectx c = { NO_SHARING }; 32 31 #endif
Note: See TracChangeset
for help on using the changeset viewer.