source: tests/collections/string-ctx-manage.cfa @ 7b0e8b7

ADTast-experimentalenumpthread-emulationqualifiedEnum
Last change on this file since 7b0e8b7 was 7b0e8b7, checked in by Michael Brooks <mlbrooks@…>, 3 years ago

String heap growth implemented

  • Property mode set to 100644
File size: 1.5 KB
Line 
1#include <string.hfa>
2#include <string_sharectx.hfa>
3#include <string_res.hfa>
4
5void baseline() {
6    string x = "hi";
7
8    string y = x; // construct y in same context, no write yet => no copy yet
9    assert( y.inner->Handle.s == x.inner->Handle.s);
10    sout | y; // hi
11
12    x = "bye";
13    y = x; // y in same context, no write yet => no copy yet
14    assert( y.inner->Handle.s == x.inner->Handle.s);
15    sout | y; // bye
16}
17
18void eagerCopy() {
19    string x = "hi";
20    string_sharectx c = { NEW_SHARING };
21
22    string y = x; // construct y in different context => eager copy
23    assert( y.inner->Handle.s != x.inner->Handle.s);
24    sout | y; // hi
25
26    x = "bye";
27    y = x; // y was already in different context => eager copy
28    assert( y.inner->Handle.s != x.inner->Handle.s);
29    sout | y; // bye
30}
31
32void soloAlloc() {
33    string x = "hi";
34    string_sharectx c = { NO_SHARING };
35   
36    string y = x; // y allocates into private pad, implying eager copy
37    assert( y.inner->Handle.s != x.inner->Handle.s);
38    assert( DEBUG_string_bytes_in_heap(y.inner->Handle.ulink) == y.inner->Handle.lnth );  // y is in a perfectly fitting heap
39    sout | y; // hi
40
41    x = "bye";
42    y = x; // into private y => eager copy
43    assert( y.inner->Handle.s != x.inner->Handle.s);
44//    assert( DEBUG_string_bytes_in_heap(y.inner->Handle.ulink) == y.inner->Handle.lnth );  // optimization required
45    sout | y; // bye
46}
47
48
49int main() {
50    baseline();
51    eagerCopy();
52    soloAlloc();
53    printf("done\n");
54}
Note: See TracBrowser for help on using the repository browser.