Changeset 0f781fb8
- Timestamp:
- Sep 29, 2021, 10:46:52 PM (3 years ago)
- Branches:
- ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
- Children:
- 4b3b352
- Parents:
- 218096f
- Files:
-
- 3 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/Makefile.am
r218096f r0f781fb8 63 63 containers/queueLockFree.hfa \ 64 64 containers/stackLockFree.hfa \ 65 containers/string_sharectx.hfa \ 65 66 containers/vector2.hfa \ 66 67 vec/vec.hfa \ -
libcfa/src/containers/string_res.cfa
r218096f r0f781fb8 15 15 16 16 #include "string_res.hfa" 17 #include "string_sharectx.hfa" 18 17 19 #include <stdlib.hfa> // e.g. malloc 18 20 #include <string.h> // e.g. strlen … … 20 22 21 23 //######################### VbyteHeap "header" ######################### 22 23 24 25 26 27 28 29 30 // DON'T COMMIT:31 // #define VbyteDebug32 33 34 35 36 24 37 25 #ifdef VbyteDebug … … 62 50 static inline void ?{}( VbyteHeap &, int = 1000 ); 63 51 static inline void ^?{}( VbyteHeap & ); 64 static inline void ByteCopy( VbyteHeap &,char *, int, int, char *, int, int ); // copy a block of bytes from one location in the heap to another65 static inline int ByteCmp( VbyteHeap &,char *, int, int, char *, int, int ); // compare 2 blocks of bytes52 static inline void ByteCopy( char *, int, int, char *, int, int ); // copy a block of bytes from one location in the heap to another 53 static inline int ByteCmp( char *, int, int, char *, int, int ); // compare 2 blocks of bytes 66 54 static inline char *VbyteAlloc( VbyteHeap &, int ); // allocate a block bytes in the heap 67 55 … … 83 71 ExtVbyte = (void *)( StartVbyte + CurrSize ); 84 72 Header.flink = Header.blink = &Header; 73 Header.ulink = & this; 85 74 #ifdef VbyteDebug 86 75 HeaderPtr = &Header; … … 124 113 s = 0; 125 114 lnth = 0; 115 ulink = &vh; 126 116 AddThisAfter( this, *vh.Header.blink ); 127 117 #ifdef VbyteDebug … … 150 140 } // ~HandleNode 151 141 142 143 //######################### String Sharing Context ######################### 144 145 static string_sharectx * ambient_string_sharectx; // fickle top of stack 146 static string_sharectx default_string_sharectx = {NEW_SHARING}; // stable bottom of stack 147 148 void ?{}( string_sharectx & this, StringSharectx_Mode mode ) with( this ) { 149 (older){ ambient_string_sharectx }; 150 if ( mode == NEW_SHARING ) { 151 (activeHeap){ new( 1000 ) }; 152 } else { 153 verify( mode == NO_SHARING ); 154 (activeHeap){ 0p }; 155 } 156 ambient_string_sharectx = & this; 157 } 158 159 void ^?{}( string_sharectx & this ) with( this ) { 160 if ( activeHeap ) delete( activeHeap ); 161 162 // unlink this from older-list starting from ambient_string_sharectx 163 // usually, this==ambient_string_sharectx and the loop runs zero times 164 string_sharectx *& c = ambient_string_sharectx; 165 while ( c != &this ) &c = &c->older; // find this 166 c = this.older; // unlink 167 } 168 152 169 //######################### String Resource ######################### 153 170 154 171 155 VbyteHeap HeapArea; 156 157 VbyteHeap * DEBUG_string_heap = & HeapArea; 172 VbyteHeap * DEBUG_string_heap() { 173 assert( ambient_string_sharectx->activeHeap && "No sharing context is active" ); 174 return ambient_string_sharectx->activeHeap; 175 } 158 176 159 177 size_t DEBUG_string_bytes_avail_until_gc( VbyteHeap * heap ) { … … 199 217 // Empty constructor 200 218 void ?{}(string_res &s) with(s) { 201 (Handle){ HeapArea }; 219 assert( ambient_string_sharectx->activeHeap && "Need to implement private contexts" ); 220 (Handle){ * ambient_string_sharectx->activeHeap }; 202 221 s.shareEditSet_prev = &s; 203 222 s.shareEditSet_next = &s; … … 206 225 // Constructor from a raw buffer and size 207 226 void ?{}(string_res &s, const char* rhs, size_t rhslnth) with(s) { 208 (Handle){ HeapArea }; 209 Handle.s = VbyteAlloc(HeapArea, rhslnth); 227 assert( ambient_string_sharectx->activeHeap && "Need to implement private contexts" ); 228 (Handle){ * ambient_string_sharectx->activeHeap }; 229 Handle.s = VbyteAlloc(*Handle.ulink, rhslnth); 210 230 Handle.lnth = rhslnth; 211 231 for ( int i = 0; i < rhslnth; i += 1 ) { // copy characters … … 224 244 void ?{}(string_res &s, const string_res & s2, StrResInitMode mode, size_t start, size_t end ) { 225 245 246 assert( ambient_string_sharectx->activeHeap && "Need to implement private contexts" ); 247 assert( s2.Handle.ulink == ambient_string_sharectx->activeHeap && "need to implement context crossing"); 248 226 249 verify( start <= end && end <= s2.Handle.lnth ); 227 250 … … 229 252 s.Handle.s = s2.Handle.s + start; 230 253 s.Handle.lnth = end - start; 254 s.Handle.ulink = ambient_string_sharectx->activeHeap; 231 255 AddThisAfter(s.Handle, s2.Handle ); // insert this handle after rhs handle 232 256 // ^ bug? skip others at early point in string … … 404 428 // no-op 405 429 } else { // must copy some text 406 if ( str1.Handle.s + size(str1) == VbyteAlloc( HeapArea, 0) ) { // str1 at end of string area ?407 VbyteAlloc( HeapArea, bsize); // create room for 2nd part at the end of string area430 if ( str1.Handle.s + size(str1) == VbyteAlloc(*str1.Handle.ulink, 0) ) { // str1 at end of string area ? 431 VbyteAlloc( *str1.Handle.ulink, bsize ); // create room for 2nd part at the end of string area 408 432 } else { // copy the two parts 409 433 char * str1oldBuf = str1.Handle.s; 410 str1.Handle.s = VbyteAlloc( HeapArea, clnth );411 ByteCopy( HeapArea,str1.Handle.s, 0, str1.Handle.lnth, str1oldBuf, 0, str1.Handle.lnth);434 str1.Handle.s = VbyteAlloc( *str1.Handle.ulink, clnth ); 435 ByteCopy( str1.Handle.s, 0, str1.Handle.lnth, str1oldBuf, 0, str1.Handle.lnth); 412 436 } // if 413 ByteCopy( HeapArea,str1.Handle.s, str1.Handle.lnth, bsize, (char*)buffer, 0, (int)bsize);437 ByteCopy( str1.Handle.s, str1.Handle.lnth, bsize, (char*)buffer, 0, (int)bsize); 414 438 // VbyteHeap & this, char *Dst, int DstStart, int DstLnth, char *Src, int SrcStart, int SrcLnth 415 439 } // if … … 437 461 438 462 bool ?==?(const string_res &s1, const string_res &s2) { 439 return ByteCmp( HeapArea,s1.Handle.s, 0, s1.Handle.lnth, s2.Handle.s, 0, s2.Handle.lnth) == 0;463 return ByteCmp( s1.Handle.s, 0, s1.Handle.lnth, s2.Handle.s, 0, s2.Handle.lnth) == 0; 440 464 } 441 465 … … 608 632 serr | "enter:AddThisAfter, this:" | &this | " n:" | &n; 609 633 #endif // VbyteDebug 634 verify( n.ulink != 0p ); 635 verify( this.ulink == n.ulink ); 610 636 flink = n.flink; 611 637 blink = &n; … … 682 708 serr | "enter:MoveThisAfter, this:" | & this | " h:" | & h; 683 709 #endif // VbyteDebug 710 verify( h.ulink != 0p ); 711 verify( this.ulink == h.ulink ); 684 712 if ( s < h.s ) { // check argument values 685 713 // serr | "VbyteSM: Error - Cannot move byte string starting at:" | s | " after byte string starting at:" … … 723 751 // if the |Dst| > |Src| => pad Dst with blanks 724 752 725 void ByteCopy( VbyteHeap & this,char *Dst, int DstStart, int DstLnth, char *Src, int SrcStart, int SrcLnth ) {753 void ByteCopy( char *Dst, int DstStart, int DstLnth, char *Src, int SrcStart, int SrcLnth ) { 726 754 for ( int i = 0; i < DstLnth; i += 1 ) { 727 755 if ( i == SrcLnth ) { // |Dst| > |Src| … … 741 769 // -1 => Src1-byte-string < Src2-byte-string 742 770 743 int ByteCmp( VbyteHeap & this, char *Src1, int Src1Start, int Src1Lnth, char *Src2, int Src2Start, int Src2Lnth ) with(this){771 int ByteCmp( char *Src1, int Src1Start, int Src1Lnth, char *Src2, int Src2Start, int Src2Lnth ) { 744 772 #ifdef VbyteDebug 745 773 serr | "enter:ByteCmp, Src1Start:" | Src1Start | " Src1Lnth:" | Src1Lnth | " Src2Start:" | Src2Start | " Src2Lnth:" | Src2Lnth; … … 798 826 h = Header.flink; // ignore header node 799 827 for (;;) { 800 ByteCopy( this,EndVbyte, 0, h->lnth, h->s, 0, h->lnth );828 ByteCopy( EndVbyte, 0, h->lnth, h->s, 0, h->lnth ); 801 829 obase = h->s; 802 830 h->s = EndVbyte; -
libcfa/src/containers/string_res.hfa
r218096f r0f781fb8 27 27 HandleNode *flink; // forward link 28 28 HandleNode *blink; // backward link 29 VbyteHeap *ulink; // upward link 29 30 30 31 char *s; // pointer to byte string … … 32 33 }; // HandleNode 33 34 34 extern VbyteHeap * DEBUG_string_heap;35 VbyteHeap * DEBUG_string_heap(); 35 36 size_t DEBUG_string_bytes_avail_until_gc( VbyteHeap * heap ); 36 37 const char * DEBUG_string_heap_start( VbyteHeap * heap ); -
tests/collections/string-gc.cfa
r218096f r0f781fb8 2 2 3 3 size_t bytesRemaining() { 4 return DEBUG_string_bytes_avail_until_gc( DEBUG_string_heap );4 return DEBUG_string_bytes_avail_until_gc( DEBUG_string_heap() ); 5 5 } 6 6 7 7 size_t heapOffsetStart( string_res & s ) { 8 const char * startByte = DEBUG_string_heap_start( DEBUG_string_heap );8 const char * startByte = DEBUG_string_heap_start( DEBUG_string_heap() ); 9 9 assert( s.Handle.s >= startByte ); 10 10 return s.Handle.s - startByte;
Note: See TracChangeset
for help on using the changeset viewer.