source: libcfa/src/collections/string_res.cfa @ b1eefe50

Last change on this file since b1eefe50 was e8b3717, checked in by Michael Brooks <mlbrooks@…>, 6 months ago

Modify substring interface from start-end to start-len, and add a missing test.

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