- Timestamp:
- May 25, 2026, 4:40:18 PM (40 hours ago)
- Branches:
- master
- Children:
- 893fb47
- Parents:
- bc547d3
- Location:
- libcfa/src/collections
- Files:
-
- 2 edited
-
string_res.cfa (modified) (36 diffs)
-
string_res.hfa (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/collections/string_res.cfa
rbc547d3 rb7caceb 10 10 // Created On : Fri Sep 03 11:00:00 2021 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu May 7 13:14:45202613 // Update Count : 13512 // Last Modified On : Mon May 25 16:22:58 2026 13 // Update Count : 316 14 14 // 15 15 … … 29 29 //######################### VbyteHeap "header" ######################### 30 30 31 #ifdef VbyteDebug 32 HandleNode *HeaderPtr; 31 //#define VbyteDebug 32 #ifdef VbyteDebug 33 HandleNode * HeaderPtr; 34 35 void printHandleList( HandleNode & s ) with(s) { 36 printf( "HandleList: Header %p\n", HeaderPtr ); 37 for ( HandleNode * ni = HeaderPtr;; ) { // print header node 38 printf( "\tnode:%p lnth:%u s:%p, \"", ni, ni->lnth, ni->s ); 39 for ( i; ni->lnth ) { 40 printf( "%c", ni->s[i] ); 41 } // for 42 printf( "\" flink:%p blink:%p\n", ni->flink, ni->blink ); 43 ni = ni->flink; 44 if ( ni == HeaderPtr ) break; 45 } // for 46 } 33 47 #endif // VbyteDebug 34 48 … … 40 54 int InitSize; // initial number of bytes in the byte-string area 41 55 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 area43 char * EndVbyte;// pointer to the next byte after the end of the currently used portion of byte-string area44 void * ExtVbyte;// pointer to the next byte after the end of the byte-string area56 char * StartVbyte; // pointer to the `st byte of the start of the byte-string area 57 char * EndVbyte; // pointer to the next byte after the end of the currently used portion of byte-string area 58 void * ExtVbyte; // pointer to the next byte after the end of the byte-string area 45 59 46 60 HandleNode Header; // header node for handle list … … 62 76 static void AddThisAfter( HandleNode &, HandleNode & ); 63 77 static void DeleteNode( HandleNode & ); 64 static void MoveThisAfter( HandleNode &, constHandleNode & ); // move current handle after parameter handle78 static void MoveThisAfter( HandleNode &, HandleNode & ); // move current handle after parameter handle 65 79 66 80 … … 69 83 static void ?{}( VbyteHeap & s, size_t Size ) with(s) { 70 84 #ifdef VbyteDebug 71 serr | "enter:VbyteHeap::VbyteHeap, s:" | &s | " Size:" | Size;85 printf( "enter:VbyteHeap::VbyteHeap, s:%p Size:%zd\n", &s, Size ); 72 86 #endif // VbyteDebug 73 87 NoOfCompactions = NoOfExtensions = NoOfReductions = 0; … … 77 91 Header.flink = Header.blink = &Header; 78 92 Header.ulink = &s; 93 // Should be unnecessary to initialize. 94 // Header.s = 0p; 95 // Header.lnth = 0; 79 96 #ifdef VbyteDebug 80 97 HeaderPtr = &Header; 81 serr | "exit:VbyteHeap::VbyteHeap, s:" | &s;98 printf( "exit:VbyteHeap::VbyteHeap, s:%p\n", &s ); 82 99 #endif // VbyteDebug 83 100 } // VbyteHeap … … 99 116 static void ?{}( HandleNode & s ) with(s) { 100 117 #ifdef VbyteDebug 101 serr | "enter:HandleNode::HandleNode, s:" | &s; 102 #endif // VbyteDebug 103 s = 0; 104 lnth = 0; 105 #ifdef VbyteDebug 106 serr | "exit:HandleNode::HandleNode, s:" | &s; 118 printf( "enter:HandleNode::HandleNode, s:%p\n", &s ); 119 #endif // VbyteDebug 120 // Should be unnecessary to initialize. 121 // s = 0p; 122 // lnth = 0; 123 #ifdef VbyteDebug 124 printf( "exit:HandleNode::HandleNode, s:%p\n", &s ); 107 125 #endif // VbyteDebug 108 126 } // HandleNode … … 114 132 static void ?{}( HandleNode & s, VbyteHeap & vh ) with(s) { 115 133 #ifdef VbyteDebug 116 serr | "enter:HandleNode::HandleNode, s:" | &s;134 printf( "enter:HandleNode::HandleNode, s:%p\n", &s ); 117 135 #endif // VbyteDebug 118 136 s = 0; … … 121 139 AddThisAfter( s, *vh.Header.blink ); 122 140 #ifdef VbyteDebug 123 serr | "exit:HandleNode::HandleNode, s:" | &s;141 printf( "exit:HandleNode::HandleNode, s:%p\n", &s ); 124 142 #endif // VbyteDebug 125 143 } // HandleNode … … 131 149 static void ^?{}( HandleNode & s ) with(s) { 132 150 #ifdef VbyteDebug 133 serr | "enter:HandleNode::~HandleNode, s:" | & s;151 printf( "enter:HandleNode::~HandleNode, s:%p\n", &s ); 134 152 { 135 serr | nlOff; 136 serr | " lnth:" | lnth | " s:" | (void *)s | ",\""; 153 printf( " lnth:%u s:%p,\"", lnth, (void *)s ); 137 154 for ( i; lnth ) { 138 serr | s[i];155 printf( "%c", s[i] ); 139 156 } // for 140 serr | "\" flink:" | flink | " blink:" | blink | nl; 141 serr | nlOn; 157 printf( "\" flink:%p blink:%p\n", flink, blink ); 142 158 } 143 159 #endif // VbyteDebug … … 176 192 177 193 VbyteHeap * DEBUG_string_heap() { 178 assert( ambient_string_sharectx->activeHeap && "No sharing context is active" );194 // assert( ambient_string_sharectx->activeHeap && "No sharing context is active" ); 179 195 return ambient_string_sharectx->activeHeap; 180 196 } … … 330 346 s.shareSet_prev = &s; 331 347 s.shareSet_next = &s; 332 }348 } 333 349 334 350 static void eagerCopyCtorHelper(string_res & s, const char * rhs, size_t rhslnth) with(s) { … … 340 356 (shareSet_owns_ulink){ true }; 341 357 } 342 Handle.s = VbyteAlloc( *Handle.ulink, rhslnth);358 Handle.s = VbyteAlloc( *Handle.ulink, rhslnth ); 343 359 Handle.lnth = rhslnth; 344 360 memmove( Handle.s, rhs, rhslnth ); … … 392 408 void ?{}( string_res & s, VbyteHeap & heap, const char * rhs, size_t rhslnth ) with(s) { 393 409 (Handle){ heap }; 394 Handle.s = VbyteAlloc( *Handle.ulink, rhslnth);410 Handle.s = VbyteAlloc( *Handle.ulink, rhslnth ); 395 411 Handle.lnth = rhslnth; 396 412 (s.shareSet_owns_ulink){ false }; … … 398 414 s.shareSet_prev = &s; 399 415 s.shareSet_next = &s; 416 #ifdef VbyteDebug 417 printHandleList( s.Handle ); 418 printf( "exit:constructor, s:%p\n", &s ); 419 #endif // VbyteDebug 400 420 } 401 421 … … 535 555 536 556 static string_res & assign_(string_res & s, const char * buffer, size_t bsize, const string_res & valSrc) { 557 #ifdef VbyteDebug 558 printf( "enter:assign_\n" ); 559 printHandleList( s.Handle ); 560 #endif // VbyteDebug 561 537 562 string_res * shareSetStartPeer; 538 563 string_res * shareSetEndPeer; … … 614 639 } 615 640 641 #ifdef VbyteDebug 642 printHandleList( s.Handle ); 643 printf( "exit:assign_\n" ); 644 #endif // VbyteDebug 645 616 646 return s; 617 647 } … … 708 738 709 739 void append(string_res & str1, const char * buffer, size_t bsize) { 740 #ifdef VbyteDebug 741 printf( "enter append %p %p %zd\n", &str1, buffer, bsize ); 742 printHandleList( str1.Handle ); 743 #endif // VbyteDebug 710 744 size_t clnth = str1.Handle.lnth + bsize; 711 745 if ( str1.Handle.s + str1.Handle.lnth == buffer ) { // already juxtapose ? 712 746 // no-op 713 747 } else { // must copy some text 714 if ( str1.Handle.s + str1.Handle.lnth == VbyteAlloc(*str1.Handle.ulink, 0) ) { // str1 at end of string area ? 748 // if ( str1.Handle.s + str1.Handle.lnth == VbyteAlloc(*str1.Handle.ulink, 0) ) { // str1 at end of string area ? 749 if ( str1.Handle.s + str1.Handle.lnth == str1.Handle.ulink->EndVbyte ) { // str1 at end of string area ? 715 750 VbyteAlloc( *str1.Handle.ulink, bsize ); // create room for 2nd part at the end of string area 716 751 } else { // copy the two parts … … 723 758 } // if 724 759 str1.Handle.lnth = clnth; 760 #ifdef VbyteDebug 761 printHandleList( str1.Handle ); 762 printf( "exit append\n" ); 763 #endif // VbyteDebug 725 764 } 726 765 … … 938 977 static void AddThisAfter( HandleNode & s, HandleNode & n ) with(s) { 939 978 #ifdef VbyteDebug 940 serr | "enter:AddThisAfter, s:" | &s | " n:" | &n;979 printf( "enter:AddThisAfter, s:%p n:%p\n", &s, &n ); 941 980 #endif // VbyteDebug 942 981 // 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). … … 948 987 n.flink = &s; 949 988 #ifdef VbyteDebug 950 { 951 serr | "HandleList:"; 952 serr | nlOff; 953 for ( HandleNode *ni = HeaderPtr->flink; ni != HeaderPtr; ni = ni->flink ) { 954 serr | "\tnode:" | ni | " lnth:" | ni->lnth | " s:" | (void *)ni->s | ",\""; 955 for ( i; ni->lnth ) { 956 serr | ni->s[i]; 957 } // for 958 serr | "\" flink:" | ni->flink | " blink:" | ni->blink | nl; 959 } // for 960 serr | nlOn; 961 } 962 serr | "exit:AddThisAfter"; 989 printHandleList( s ); 990 printf( "exit:AddThisAfter\n" ); 963 991 #endif // VbyteDebug 964 992 } // AddThisAfter … … 969 997 static void DeleteNode( HandleNode & s ) with(s) { 970 998 #ifdef VbyteDebug 971 serr | "enter:DeleteNode, s:" | &s; 999 printf( "enter:DeleteNode, s:%p\n", &s ); 1000 printHandleList( s ); 972 1001 #endif // VbyteDebug 973 1002 flink->blink = blink; 974 1003 blink->flink = flink; 975 1004 #ifdef VbyteDebug 976 serr | "exit:DeleteNode"; 1005 printHandleList( s ); 1006 printf( "exit:DeleteNode\n" ); 977 1007 #endif // VbyteDebug 978 1008 } // DeleteNode … … 984 1014 static char * VbyteAlloc( VbyteHeap & s, int size ) with(s) { 985 1015 #ifdef VbyteDebug 986 serr | "enter:VbyteAlloc, size:" | size;1016 printf( "enter:VbyteAlloc, size:%d\n", size ); 987 1017 #endif // VbyteDebug 988 1018 uintptr_t NoBytes; … … 997 1027 EndVbyte += size; 998 1028 #ifdef VbyteDebug 999 serr | "exit:VbyteAlloc, r:" | (void *)r | " EndVbyte:" | (void *)EndVbyte | " ExtVbyte:" | ExtVbyte;1029 printf( "exit:VbyteAlloc, r:%p EndVbyte:%p ExtVbyte:%p\n", (void *)r, (void *)EndVbyte, ExtVbyte ); 1000 1030 #endif // VbyteDebug 1001 1031 return r; … … 1029 1059 1030 1060 1031 // Move an existing HandleNode node h somewhere after the current HandleNode node so that it is in ascending order by1032 // the address in thebyte string area.1033 1034 static void MoveThisAfter( HandleNode & s, const HandleNode& h ) with(s) {1035 #ifdef VbyteDebug 1036 serr | "enter:MoveThisAfter, s:" | & s | " h:" | & h;1061 // Move HandleNode node s somewhere after the HandleNode node h so that it is in ascending order by the address in the 1062 // byte string area. 1063 1064 static void MoveThisAfter( HandleNode & s, HandleNode & h ) with(s) { 1065 #ifdef VbyteDebug 1066 printf( "enter:MoveThisAfter, s:%p h:%p\n", &s, &h ); 1037 1067 #endif // VbyteDebug 1038 1068 verify( h.ulink != 0p ); 1039 1069 verify( s.ulink == h.ulink ); 1040 if ( s < h.s ) { // check argument values 1041 // serr | "VbyteSM: Error - Cannot move byte string starting at:" | s | " after byte string starting at:" 1042 // | ( h->s ) | " and keep handles in ascending order"; 1043 // exit(-1 ); 1044 verify( 0 && "VbyteSM: Error - Cannot move byte strings as requested and keep handles in ascending order"); 1045 } // if 1070 verify( s < h.s && "VbyteSM: Error - Cannot move byte strings as requested and keep handles in ascending order" ); 1046 1071 1047 1072 HandleNode *i; 1048 for ( i = h.flink; i ->s != 0 && s > ( i->s ); i = i->flink ); // find the position for this node after h1049 if ( & s != i->blink ) {1073 for ( i = h.flink; i != &s.ulink->Header && s > i->s; i = i->flink ); // find the position for this node after h 1074 if ( &s != i->blink ) { // s just after h ? => nothing to move 1050 1075 DeleteNode( s ); 1051 1076 AddThisAfter( s, *i->blink ); 1052 1077 } // if 1053 1078 #ifdef VbyteDebug 1054 { 1055 serr | "HandleList:"; 1056 serr | nlOff; 1057 for ( HandleNode *n = HeaderPtr->flink; n != HeaderPtr; n = n->flink ) { 1058 serr | "\tnode:" | n | " lnth:" | n->lnth | " s:" | (void *)n->s | ",\""; 1059 for ( i; n->lnth ) { 1060 serr | n->s[i]; 1061 } // for 1062 serr | "\" flink:" | n->flink | " blink:" | n->blink | nl; 1063 } // for 1064 serr | nlOn; 1065 } 1066 serr | "exit:MoveThisAfter"; 1079 printHandleList( s ); 1080 printf( "exit:MoveThisAfter\n" ); 1067 1081 #endif // VbyteDebug 1068 1082 } // MoveThisAfter … … 1079 1093 int ByteCmp( char *Src1, int Src1Start, int Src1Lnth, char *Src2, int Src2Start, int Src2Lnth ) { 1080 1094 #ifdef VbyteDebug 1081 serr | "enter:ByteCmp, Src1Start:" | Src1Start | " Src1Lnth:" | Src1Lnth | " Src2Start:" | Src2Start | " Src2Lnth:" | Src2Lnth;1095 printf( "enter:ByteCmp, Src1Start:%d Src1Lnth:%d Src2Start:%d Src2Lnth:%d", Src1Start, Src1Lnth, Src2Start, Src2Lnth ); 1082 1096 #endif // VbyteDebug 1083 1097 int cmp; … … 1115 1129 } // for 1116 1130 #ifdef VbyteDebug 1117 serr | "exit:ByteCmp, cmp:" | cmp;1131 printf( "exit:ByteCmp, cmp:%d\n", cmp ); 1118 1132 #endif // VbyteDebug 1119 1133 return cmp; … … 1126 1140 // the containing string has been moved. Hence, they only require that their string pointers be adjusted. 1127 1141 1128 void compaction(VbyteHeap & s) with(s) { 1129 HandleNode *h; 1130 char *obase, *nbase, *limit; 1142 void compaction( VbyteHeap & s ) with(s) { 1143 #ifdef VbyteDebug 1144 printf( "enter compaction\n" ); 1145 printHandleList( Header ); 1146 #endif // VbyteDebug 1147 char * obase, * nbase, * limit; 1131 1148 1132 1149 NoOfCompactions += 1; 1133 1150 EndVbyte = StartVbyte; 1134 h = Header.flink; // ignore header node 1135 for () { 1151 for ( HandleNode * h = Header.flink; h != &Header; ) { 1136 1152 memmove( EndVbyte, h->s, h->lnth ); 1137 1153 obase = h->s; … … 1141 1157 limit = obase + h->lnth; 1142 1158 h = h->flink; 1143 1144 1159 // check if any substrings are allocated within a string 1145 1146 for () { 1147 if ( h == &Header ) break; // end of header list ? 1148 if ( h->s >= limit ) break; // outside of current string ? 1160 for ( ; h != &Header; h = h->flink ) { 1161 if ( h->s >= limit ) break; // outside of current string ? 1149 1162 h->s = nbase + (( uintptr_t )h->s - ( uintptr_t )obase ); 1150 h = h->flink;1151 1163 } // for 1152 if ( h == &Header ) break; // end of header list ?1153 1164 } // for 1165 #ifdef VbyteDebug 1166 printHandleList( Header ); 1167 printf( "exit compaction\n" ); 1168 #endif // VbyteDebug 1154 1169 } // compaction 1155 1170 … … 1166 1181 // the heap. The heap is then compacted in the existing heap or into the newly allocated heap. 1167 1182 1168 void garbage(VbyteHeap & s, int minreq ) with(s) { 1169 #ifdef VbyteDebug 1170 serr | "enter:garbage"; 1171 { 1172 serr | "HandleList:"; 1173 for ( HandleNode *n = Header.flink; n != &Header; n = n->flink ) { 1174 serr | nlOff; 1175 serr | "\tnode:" | n | " lnth:" | n->lnth | " s:" | (void *)n->s | ",\""; 1176 for ( i; n->lnth ) { 1177 serr | n->s[i]; 1178 } // for 1179 serr | nlOn; 1180 serr | "\" flink:" | n->flink | " blink:" | n->blink; 1181 } // for 1182 } 1183 void garbage( VbyteHeap & s, int minreq ) with(s) { 1184 #ifdef VbyteDebug 1185 printf( "enter:garbage\n" ); 1186 printHandleList( Header ); 1183 1187 #endif // VbyteDebug 1184 1188 int AmountUsed, AmountFree; … … 1204 1208 }// if 1205 1209 #ifdef VbyteDebug 1206 { 1207 serr | "HandleList:"; 1208 for ( HandleNode *n = Header.flink; n != &Header; n = n->flink ) { 1209 serr | nlOff; 1210 serr | "\tnode:" | n | " lnth:" | n->lnth | " s:" | (void *)n->s | ",\""; 1211 for ( i; n->lnth ) { 1212 serr | n->s[i]; 1213 } // for 1214 serr | nlOn; 1215 serr | "\" flink:" | n->flink | " blink:" | n->blink; 1216 } // for 1217 } 1218 serr | "exit:garbage"; 1210 printHandleList( Header ); 1211 printf( "exit:garbage\n" ); 1219 1212 #endif // VbyteDebug 1220 1213 } // garbage 1221 1222 #undef VbyteDebug1223 1224 1214 1225 1215 … … 1229 1219 void extend( VbyteHeap & s, int size ) with (s) { 1230 1220 #ifdef VbyteDebug 1231 serr | "enter:extend, size:" | size;1221 printf( "enter:extend, size:%d", size ); 1232 1222 #endif // VbyteDebug 1233 1223 char *OldStartVbyte; … … 1242 1232 free( OldStartVbyte ); // release old heap 1243 1233 #ifdef VbyteDebug 1244 serr | "exit:extend, CurrSize:" | CurrSize;1234 printf( "exit:extend, CurrSize: %d\n", CurrSize ); 1245 1235 #endif // VbyteDebug 1246 1236 } // extend … … 1248 1238 //WIP 1249 1239 #if 0 1250 1251 1240 // Extend the size of the byte-string area by creating a new area and copying the old area into it. The old byte-string 1252 1241 // area is deleted. … … 1254 1243 void VbyteHeap::reduce( int size ) { 1255 1244 #ifdef VbyteDebug 1256 serr | "enter:reduce, size:" | size;1245 printf( "enter:reduce, size: %d", size ); 1257 1246 #endif // VbyteDebug 1258 1247 char *OldStartVbyte; … … 1267 1256 delete OldStartVbyte; // release old heap 1268 1257 #ifdef VbyteDebug 1269 !serr | "exit:reduce, CurrSize:" | CurrSize;1258 printf( "exit:reduce, CurrSize: %d", CurrSize ); 1270 1259 #endif // VbyteDebug 1271 1260 } // reduce 1272 1273 1274 #endif 1261 #endif // 0 -
libcfa/src/collections/string_res.hfa
rbc547d3 rb7caceb 10 10 // Created On : Fri Sep 03 11:00:00 2021 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu May 7 13:14:04202613 // Update Count : 8 312 // Last Modified On : Mon May 11 18:56:08 2026 13 // Update Count : 84 14 14 // 15 15 … … 31 31 32 32 char * s; // pointer to byte string 33 unsigned int lnth;// length of byte string33 size_t lnth; // length of byte string 34 34 }; // HandleNode 35 35
Note:
See TracChangeset
for help on using the changeset viewer.