source: libcfa/src/collections/string_res.cfa@ 831b2ec

Last change on this file since 831b2ec was 9018dcf, checked in by Peter A. Buhr <pabuhr@…>, 9 months ago

updates to string type

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