source: src/examples/strings/src/internal/VbyteSM.h @ 9ea58ca

string
Last change on this file since 9ea58ca was 9ea58ca, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

finished translating VbyteSM from C++ to cfa

  • Property mode set to 100644
File size: 1.9 KB
Line 
1#pragma once
2
3#include <stdlib>
4
5typedef char char_t;
6
7//######################### HandleNode #########################
8struct VbyteHeap_t;
9
10struct HandleNode_t
11{
12        char_t* string;
13        unsigned short int length;
14
15        HandleNode_t* next;
16        HandleNode_t* previous;
17};
18
19void DeleteNode(HandleNode_t* const this);
20void AddThisAfter(HandleNode_t* const this, HandleNode_t* other);
21
22void MoveThisAfter(HandleNode_t* const this, const HandleNode_t* rhs);
23void MoveThisBefore(HandleNode_t* const this, const HandleNode_t* rhs);
24
25//######################### VbyteHeap #########################
26struct VbyteHeap
27{
28        int NoOfCompactions;
29        int NoOfExtensions;
30        int NoOfReductions;
31
32        int InitSize;
33        int CurrSize;
34        char_t* StartVbyte;
35        char_t* EndVbyte;
36        void* ExtVbyte;
37
38        HandleNode_t Header;
39};
40
41void compaction(VbyteHeap* const this);
42void garbage(VbyteHeap* const this);
43void extend(VbyteHeap* const this, int);
44void reduce(VbyteHeap* const this, int);
45
46//######################### HandleNode #########################
47static inline ctor(HandleNode_t* const this)
48{
49        this->string = (void*)0;
50        this->length = 0;
51}
52
53static inline ctor(HandleNode_t* const this, VbyteHeap* vh)
54{
55        this->string = (void*)0;
56        this->length = 0;
57        HandleNode_t* const node = &vh->Header;
58        AddThisAfter(this, node->previous);
59}
60
61static inline dtor(HandleNode_t* const this)
62{
63        DeleteNode(this);
64}
65
66//######################### VbyteHeap #########################
67static inline ctor(VbyteHeap* const this, int size)
68{
69        this->NoOfCompactions = 0;
70        this->NoOfExtensions = 0;
71        this->NoOfReductions = 0;
72
73        this->InitSize = size;
74        this->CurrSize = size;
75
76        this->StartVbyte = calloc(size);
77        this->EndVbyte = this->StartVbyte;
78        this->ExtVbyte = (void*)( this->StartVbyte + this->CurrSize );
79
80        ctor((HandleNode_t* const)&this->Header);
81        this->Header.next = &this->Header;
82        this->Header.previous = &this->Header;
83}
84
85static inline dtor(VbyteHeap* const this)
86{
87        free(this->StartVbyte);
88}
Note: See TracBrowser for help on using the repository browser.