File: normSegment.h

package info (click to toggle)
norm 1.5.9%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,680 kB
  • sloc: cpp: 123,494; xml: 7,536; tcl: 5,460; makefile: 3,442; python: 1,898; java: 1,750; ansic: 642; sh: 21; csh: 8
file content (409 lines) | stat: -rw-r--r-- 14,517 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#ifndef _NORM_SEGMENT
#define _NORM_SEGMENT

#include "normMessage.h"
#include "protoBitmask.h"

#define USE_PROTO_TREE 1  // for more better performing NormBlockBuffer?


// Norm uses preallocated (or dynamically allocated) pools of 
// segments (vectors) for different buffering purposes

class NormSegmentPool
{
    public:
        NormSegmentPool();
        ~NormSegmentPool();
        
        bool Init(unsigned int count, unsigned int size);
        void Destroy();        
        char* Get();
        void Put(char* segment)
        {
            ASSERT(seg_count < seg_total);
            *((char**)((void*)segment)) = seg_list;  // this might make a warning on Solaris
            seg_list = segment;
            seg_count++;
        }
        bool IsEmpty() const {return (NULL == seg_list);}
        
        unsigned int CurrentUsage() const 
            {return (seg_total - seg_count);}
        unsigned long PeakUsage() const {return peak_usage;}
        unsigned long OverunCount() const {return overruns;}
        unsigned int GetSegmentSize() {return seg_size;}
        
    private: 
        unsigned int    seg_size;
        unsigned int    seg_count;  
        unsigned int    seg_total;
        char*           seg_list;
		char**          seg_pool;
        
        unsigned long   peak_usage;
        unsigned long   overruns;
        bool            overrun_flag;
};  // end class NormSegmentPool

#ifdef USE_PROTO_TREE
class NormBlock : public ProtoSortedTree::Item
#else
class NormBlock
#endif // if/else USE_PROTO_TREE
{
    friend class NormBlockPool;
    friend class NormBlockBuffer;
    
    public:
        enum Flag 
        {
            IN_REPAIR    = 0x01
        };
            
        NormBlock();
        ~NormBlock();
        const NormBlockId& GetId() const {return blk_id;}
        void SetId(NormBlockId& x) {blk_id = x;}
        bool Init(UINT16 totalSize);
        void Destroy();   
        
        void SetFlag(NormBlock::Flag flag) {flags |= flag;}
        void ClearFlag(NormBlock::Flag flag) {flags &= ~flag;}
        bool InRepair() {return (0 != (flags & IN_REPAIR));}
        bool ParityReady(UINT16 ndata) {return (erasure_count == ndata);}
        UINT16 ParityReadiness() {return erasure_count;}
        void IncreaseParityReadiness() {erasure_count++;}
        void SetParityReadiness(UINT16 ndata) {erasure_count = ndata;}
        
        char** SegmentList(UINT16 index = 0) {return &segment_table[index];}
        char* GetSegment(NormSegmentId sid)
        {
            ASSERT(sid < size);
            return segment_table[sid];
        }
        void AttachSegment(NormSegmentId sid, char* segment)
        {
            ASSERT(sid < size);
            ASSERT(!segment_table[sid]);
            segment_table[sid] = segment;
        }    
        char* DetachSegment(NormSegmentId sid)
        {
            ASSERT(sid < size);
            char* segment = segment_table[sid];
            segment_table[sid] = (char*)NULL;
            return segment;
        }
        void SetSegment(NormSegmentId sid, char* segment)
        {
            ASSERT(sid < size);
            ASSERT(!segment_table[sid]);
            segment_table[sid] = segment;
        }    
        
        // Sender routines
        void TxInit(NormBlockId& blockId, UINT16 ndata, UINT16 autoParity)
        {
            blk_id = blockId;
            pending_mask.Clear();
            pending_mask.SetBits(0, ndata+autoParity);
            repair_mask.Clear();
            erasure_count = 0;
            parity_count = 0; 
            parity_offset = autoParity;  
            flags = 0;
            seg_size_max = 0;
            last_nack_time.GetCurrentTime();
        }
        void TxRecover(NormBlockId& blockId, UINT16 ndata, UINT16 nparity)
        {
            blk_id = blockId;
            pending_mask.Clear();
            repair_mask.Clear();
            erasure_count = 0;
            parity_count = nparity;  // force recovered blocks to 
            parity_offset = nparity; // explicit repair mode ???  
            flags = IN_REPAIR;
            seg_size_max = 0;
        }
        bool TxReset(UINT16 ndata, UINT16 nparity, UINT16 autoParity, 
                     UINT16 segmentSize);
        bool TxUpdate(NormSegmentId nextId, NormSegmentId lastId,
                      UINT16 ndata, UINT16 nparity, UINT16 erasureCount);
        void UpdateSegSizeMax(UINT16 segSize)
            {seg_size_max = (segSize > seg_size_max) ? segSize : seg_size_max;}
        UINT16 GetSegSizeMax() {return seg_size_max;}
        
        bool HandleSegmentRequest(NormSegmentId nextId, NormSegmentId lastId,
                                  UINT16 ndata, UINT16 nparity, 
                                  UINT16 erasureCount);
        bool ActivateRepairs(UINT16 nparity);
        void ResetParityCount(UINT16 nparity) 
        {
            parity_offset += parity_count;
            parity_offset = MIN(parity_offset, nparity);
            parity_count = 0;
        }
        bool AppendRepairAdv(NormCmdRepairAdvMsg& cmd, 
                             NormObjectId         objectId,
                             bool                 repairInfo,
                             UINT8                fecId,
                             UINT8                fecM,
                             UINT16               numData,
                             UINT16               payloadMax);
        
        // Receiver routines
        void RxInit(NormBlockId& blockId, UINT16 ndata, UINT16 nparity)
        {
            blk_id = blockId;
            pending_mask.Clear();
            pending_mask.SetBits(0, ndata+nparity);
            repair_mask.Clear();
            erasure_count = ndata;
            parity_count = 0;
            parity_offset = 0;
            flags = 0;
        }
        // Note: This invalidates the repair_mask state.
        bool IsRepairPending(UINT16 ndata, UINT16 nparity); 
        void DecrementErasureCount() {erasure_count--;}
        void IncrementErasureCount() {erasure_count++;}
        UINT16 ErasureCount() const {return erasure_count;}
        void IncrementParityCount() {parity_count++;}
        UINT16 ParityCount() const {return parity_count;}
        
        bool GetFirstPending(NormSymbolId& symbolId) const
        {
            UINT32 index;
            bool result = pending_mask.GetFirstSet(index);
            symbolId = (UINT16)index;
            return result;
        }
        bool GetNextPending(NormSymbolId& symbolId) const
        {
            UINT32 index = (UINT32)symbolId;
            bool result = pending_mask.GetNextSet(index);
            symbolId = (UINT16)index;
            return result;
        }
        NormSymbolId GetFirstRepair(NormSymbolId& symbolId)  const
        {
            UINT32 index;
            bool result = repair_mask.GetFirstSet(index);
            symbolId = (UINT16)index;
            return result;
        }
        
        bool GetNextRepair(NormSymbolId& symbolId) const
        {
            UINT32 index = (UINT32)symbolId;
            bool result = repair_mask.GetNextSet(index);
            symbolId = (UINT16)index;
            return result;
        }
        
        bool SetPending(NormSymbolId s) 
            {return pending_mask.Set(s);}
        bool SetPending(NormSymbolId firstId, UINT16 count)
            {return pending_mask.SetBits(firstId, count);}
        void UnsetPending(NormSymbolId s) 
            {pending_mask.Unset(s);}
        void ClearPending()
            {pending_mask.Clear();}
        bool SetRepair(NormSymbolId s) 
            {return repair_mask.Set(s);}
        bool SetRepairs(NormSymbolId first, NormSymbolId last)
        {
            if (first == last)
                return repair_mask.Set(first);
            else
                return (repair_mask.SetBits(first, last-first+1));   
        }
        void UnsetRepair(NormSymbolId s)
            {repair_mask.Unset(s);}
        void ClearRepairs()
            {repair_mask.Clear();}
        bool IsPending(NormSymbolId s) const
            {return pending_mask.Test(s);}
        bool IsPending() const
            {return pending_mask.IsSet();}   
        bool IsRepairPending() const
            {return repair_mask.IsSet();}
        bool IsTransmitPending() const
            {return (pending_mask.IsSet() || repair_mask.IsSet());}
        
        NormObjectSize GetBytesPending(UINT16      numData,
                                       UINT16      segmentSize,
                                       NormBlockId finalBlockId,
                                       UINT16      finalSegmentSize) const;
        
        bool AppendRepairRequest(NormNackMsg&   nack, 
                                 UINT8          fecId,
                                 UINT8          fecM,
                                 UINT16         numData, 
                                 UINT16         numParity,
                                 NormObjectId   objectId,
                                 bool           pendingInfo,
                                 UINT16         payloadMax);
        
        
        void SetLastNackTime(const ProtoTime& theTime)
            {last_nack_time = theTime;}
        const ProtoTime& GetLastNackTime() const
            {return last_nack_time;}
        double GetNackAge() const
            {return ProtoTime::Delta(ProtoTime().GetCurrentTime(), last_nack_time);}
        
        //void DisplayPendingMask(FILE* f) {pending_mask.Display(f);}
        
        //bool IsEmpty() const;
        void EmptyToPool(NormSegmentPool& segmentPool);
            
    private:
#ifdef USE_PROTO_TREE
        const char* GetKey() const
            {return blk_id.GetValuePtr();}
        unsigned int GetKeysize() const
            {return (8*sizeof(UINT32));} 
        ProtoTree::Endian GetEndian() const
            {return ProtoTree::GetNativeEndian();}    
#endif  // USE_PROTO_TREE
            
        NormBlockId  blk_id;
        UINT16       size;
        char**       segment_table;
        
        int          flags;
        UINT16       erasure_count;
        UINT16       parity_count;  // how many fresh parity we are currently planning to send
        UINT16       parity_offset; // offset from where our fresh parity will be sent
        UINT16       seg_size_max;
        
        ProtoBitmask pending_mask;
        ProtoBitmask repair_mask;
        ProtoTime    last_nack_time;  // for stream flow control
        NormBlock*   next;            // used for NormBlockPool
};  // end class NormBlock

class NormBlockPool
{
    public:
        NormBlockPool();
        ~NormBlockPool();
        bool Init(UINT32 numBlocks, UINT16 totalSize);
        void Destroy();
        bool IsEmpty() const {return (NULL == head);}
        NormBlock* Get()
        {
            NormBlock* b = head;
            head = b ? b->next : NULL;
            if (b) 
            {
                blk_count--;
                overrun_flag = false;
            }
            else if (!overrun_flag)
            {
                PLOG(PL_DEBUG, "NormBlockPool::Get() warning: operating with constrained buffering resources\n");
                overruns++;
                overrun_flag = true;   
            }
            return b;
        }
        void Put(NormBlock* b)
        {
            b->next = head;
            head = b;
            blk_count++;
        }
        unsigned long OverrunCount() const {return overruns;}
        UINT32 GetCount() {return blk_count;}
        UINT32 GetTotal() {return blk_total;}
        
    private:
        NormBlock*      head;
        UINT32          blk_total;
        UINT32          blk_count;
        unsigned long   overruns;
        bool            overrun_flag;
};  // end class NormBlockPool

#ifdef USE_PROTO_TREE
class NormBlockTree : public ProtoSortedTreeTemplate<NormBlock> {};
#endif // USE_PROTO_TREE

class NormBlockBuffer
{
    public:
        class Iterator;
        friend class NormBlockBuffer::Iterator;
            
        NormBlockBuffer();
        ~NormBlockBuffer();
        bool Init(unsigned long rangeMax, unsigned long tableSize, UINT32 fecBlockMask);
        void Destroy();
        
        bool Insert(NormBlock* theBlock);
        bool Remove(NormBlock* theBlock);
        NormBlock* Find(const NormBlockId& blockId) const;
        
        NormBlockId RangeLo() const {return range_lo;}
        NormBlockId RangeHi() const {return range_hi;}
        NormBlockId RangeMin() const;
        bool IsEmpty() const {return (0 == range);}
        bool CanInsert(NormBlockId blockId) const;

#ifdef USE_PROTO_TREE
        class Iterator
        {
            public:
                Iterator(NormBlockBuffer& blockBuffer);
                NormBlock* GetNextBlock();
                void Reset();
                
            private:
                NormBlockBuffer&           buffer;
                NormBlockTree::Iterator    iterator;
                NormBlock*                 next_block;
        }; 
        
#else        
        class Iterator
        {
            public:
                Iterator(const NormBlockBuffer& blockBuffer);
                NormBlock* GetNextBlock();
                void Reset() {reset = true;}
                
            private:
                const NormBlockBuffer&  buffer;
                bool                    reset;
                NormBlockId             index;
        }; 
#endif // if/else USE_PROTO_TREE            
    private:
        int Compare(NormBlockId a, NormBlockId b) const
            {return NormBlockId::Compare(a, b, fec_block_mask);}
        INT32 Difference(NormBlockId a, NormBlockId b) const
            {return NormBlockId::Difference(a, b, fec_block_mask);}
        void Increment(NormBlockId& b, UINT32 i = 1) const
            {b.Increment(i, fec_block_mask);}
        void Decrement(NormBlockId& b, UINT32 i = 1) const
            {b.Decrement(i, fec_block_mask);}

#ifdef USE_PROTO_TREE
        NormBlockTree   tree;
#else    
        static NormBlock* Next(NormBlock* b) {return b->next;}    
        NormBlock**     table;
        unsigned long   hash_mask; 
#endif // if/else USE_PROTO_TREE      
        unsigned long   range_max;  // max range of blocks that can be buffered
        unsigned long   range;      // zero if "block buffer" is empty
        UINT32          fec_block_mask;
        NormBlockId     range_lo;
        NormBlockId     range_hi;
};  // end class NormBlockBuffer

#endif // _NORM_SEGMENT