summaryrefslogtreecommitdiffstats
path: root/libccnx-common/ccnx/common/codec/ccnxCodec_NetworkBuffer.c
blob: ea1868b6100eb901bc69601656a3cf18db48a59b (plain)
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * We maintain a linked list of memory blocks.  We fill each memory block to capacity, then
 * allocate another memory block, putting it at the tail of the list.
 *
 * We maintain a "current" pointer to the memory block that holds "position".  Insertions always go
 * current block.
 *
 * Each memory block has a capacity and a limit.  The capacity is the maximum number of bytes available.
 * The limit is the furthest byte written.  It will not exceed the capacity..
 *
 * Once a memory block has a "next" block, the limit is fixed.  One cannot shrink or expand the limit.
 * When the "next" pointer is set, the capacity is shrunk to the limit and the buffer is called "frozen".
 *
 *    (always in ABSOLUTE bytes)
 *                                                                         position = 4036
 *    begin = 0                  begin = 1536               begin = 3536   |
 *    |                          |                          |              |
 *   +--------------------------+--------------------------+--------------------------+
 *   |         block 0          |         block 1          |         block 2          |
 *   +--------------------------+--------------------------+--------------------------+
 *                             |                       |                  |           |
 *                          capacity = 1536        capacity = 2000        |       capacity = 2046
 *                          limit = 1536           limit = 2000       limit = 500
 *    (always in RELATIVE bytes)
 *
 *  Block 0 was allocated at 1536 bytes and filled to capacity before it was frozen.
 *
 *  Block 1 was allocated at 2046 but only filled to 2000 bytes when it was frozen.  The last 46 bytes
 *          of the block are permanently lost.
 *
 *  Block 2 is still in use.  500 bytes have been written out of the 2046 capacity.
 *
 * The "begin" of a memory block is equal to the previous's memory block's "begin" plus
 * the previous blocks "limit" when it is frozen.  The "begin" value is absolute byte position
 * and it will never change because all prior blocks must be frozen.
 *
 * The total "limit" of the entire chain is the tail's "begin" plus tail's "limit".
 *
 *
 */

#include <config.h>
#include <stdio.h>
#include <parc/algol/parc_Memory.h>
#include <LongBow/runtime.h>

#include <ccnx/common/codec/ccnxCodec_NetworkBuffer.h>

struct ccnx_codec_network_buffer_memory;
typedef struct ccnx_codec_network_buffer_memory CCNxCodecNetworkBufferMemory;

struct ccnx_codec_network_buffer_memory {
    CCNxCodecNetworkBufferMemory *next;

    size_t begin;      /**< Absolute position of begining */
    size_t limit;      /**< Bytes used */
    size_t capacity;   /**< maximum bytes available (end - begin) */

    uint8_t *memory;
};

struct ccnx_codec_network_buffer_iovec {
    CCNxCodecNetworkBuffer *networkBuffer;
    unsigned refcount;
    size_t totalBytes;
    int iovcnt;
    struct iovec array[];
};

struct ccnx_codec_network_buffer {
    size_t position;
    size_t capacity;         /**< Bytes allocated */

    CCNxCodecNetworkBufferMemory *current;
    CCNxCodecNetworkBufferMemory *head;
    CCNxCodecNetworkBufferMemory *tail;

    void *userarg;
    CCNxCodecNetworkBufferMemoryBlockFunctions memoryFunctions;
    unsigned refcount;
};

// ================================================================================

#define INLINE_POSITION(block) ((uint8_t *) block + sizeof(CCNxCodecNetworkBufferMemory))

static CCNxCodecNetworkBufferMemory *
_ccnxCodecNetworkBufferMemory_Allocate(CCNxCodecNetworkBuffer *buffer, size_t bytes)
{
    assertNotNull(buffer->memoryFunctions.allocator, "Allocator must be non-null to allocate memory!");

    CCNxCodecNetworkBufferMemory *block;
    size_t totalAllocation = bytes + sizeof(CCNxCodecNetworkBufferMemory);
    size_t actual = buffer->memoryFunctions.allocator(buffer->userarg, totalAllocation, (void **) &block);

    if (actual > sizeof(CCNxCodecNetworkBufferMemory)) {
        block->next = NULL;
        block->begin = 0;
        block->capacity = actual - sizeof(CCNxCodecNetworkBufferMemory);
        block->limit = 0;

        block->memory = INLINE_POSITION(block);
        return block;
    }

    // Need a de-allocator, see case 1006
    trapOutOfMemory("Wanted %zu got %zu, minimum required %zu", totalAllocation, actual, sizeof(CCNxCodecNetworkBufferMemory));
    return NULL;
}

/**
 * Wrap a user-provided buffer.  It will be de-allocated with the buffer memory functions.
 *
 * The capacity = limit = length of the user provided memory.
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * @return <#value#> <#explanation#>
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
static CCNxCodecNetworkBufferMemory *
_ccnxCodecNetworkBufferMemory_Wrap(CCNxCodecNetworkBuffer *buffer, size_t length, uint8_t memory[length])
{
    CCNxCodecNetworkBufferMemory *block = parcMemory_AllocateAndClear(sizeof(CCNxCodecNetworkBufferMemory));
    if (block) {
        block->next = NULL;
        block->begin = 0;
        block->capacity = length;
        block->limit = length;
        block->memory = memory;

        return block;
    }
    trapOutOfMemory("Could not allocate a CCNxCodecNetworkBufferMemory");
}

/**
 * Releases a memory block
 *
 * The memory block must not be in a linked list (i.e memory->next must be NULL)
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * @return <#value#> <#explanation#>
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
static void
_ccnxCodecNetworkBufferMemory_Release(CCNxCodecNetworkBuffer *buffer, CCNxCodecNetworkBufferMemory **memoryPtr)
{
    assertNotNull(memoryPtr, "Parameter must be non-null");
    assertNotNull(*memoryPtr, "Parameter must dereference to non-null");

    CCNxCodecNetworkBufferMemory *memory = *memoryPtr;

    assertNull(memory->next, "memory->next is not null");

    // If the memory is not in-line, free it with the deallocator
    if (memory->memory == INLINE_POSITION(memory)) {
        if (buffer->memoryFunctions.deallocator) {
            buffer->memoryFunctions.deallocator(buffer->userarg, (void **) memoryPtr);
        }
    } else {
        if (buffer->memoryFunctions.deallocator) {
            buffer->memoryFunctions.deallocator(buffer->userarg, (void **) &memory->memory);
        }
        parcMemory_Deallocate((void **) &memory);
    }


    *memoryPtr = NULL;
}

static void
_ccnxCodecNetworkBufferMemory_Display(const CCNxCodecNetworkBufferMemory *block, unsigned indent)
{
    assertNotNull(block, "Parameter block must be non-null");

    printf("Memory block %p next %p offset %zu limit %zu capacity %zu\n",
           (void *) block, (void *) block->next, block->begin, block->limit, block->capacity);

    longBowDebug_MemoryDump((const char *) block->memory, block->capacity);
}

static bool
_ccnxCodecNetworkBufferMemory_ContainsPosition(CCNxCodecNetworkBufferMemory *memory, size_t position)
{
    return (memory->begin <= position && position < memory->begin + memory->limit);
}

// ================================================================================

static void
_ccnxCodecNetworkBuffer_Expand(CCNxCodecNetworkBuffer *buffer)
{
    size_t allocationSize = 2048;
    CCNxCodecNetworkBufferMemory *memory = _ccnxCodecNetworkBufferMemory_Allocate(buffer, allocationSize);

    buffer->capacity += memory->capacity;

    memory->begin = buffer->tail->begin + buffer->tail->limit;

    // this will free the tail buffer.  We will drop its capacity to its limit.
    buffer->tail->next = memory;
    buffer->tail->capacity = buffer->tail->limit;

    buffer->tail = memory;
}

static size_t
_ccnxCodecNetworkBuffer_RemainingCurrentBlock(CCNxCodecNetworkBuffer *buffer)
{
    size_t remaining = buffer->current->begin + buffer->current->capacity - buffer->position;
    return remaining;
}

static size_t
_ccnxCodecNetworkBuffer_BlockCount(CCNxCodecNetworkBuffer *buffer)
{
    // we should store this count for faster access
    size_t count = 0;
    CCNxCodecNetworkBufferMemory *block = buffer->head;
    while (block) {
        count++;
        block = block->next;
    }
    return count;
}

static void
_ccnxCodecNetworkBuffer_AllocateIfNeeded(CCNxCodecNetworkBuffer *buffer)
{
    if (buffer->position == buffer->current->begin + buffer->current->capacity) {
        if (buffer->current->next) {
            buffer->current = buffer->current->next;
        } else {
            // we are at the end of the current buffer and there's nothing beyond,
            // so allocate another memory block
            _ccnxCodecNetworkBuffer_Expand(buffer);
            buffer->current = buffer->tail;
        }
    }
}

/**
 * Check if we can fit 'length' bytes in contiguous memory.
 *
 * If we cannot, and the remaining buffer space in the current buffer is small, freeze it out
 * and allocate a new buffer.  Otherwise if the difference is large, do not freeze it and the
 * write will span memory blocks.
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * @return <#value#> <#explanation#>
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
static void
_ccnxCodecNetworkBuffer_EnsureRemaining(CCNxCodecNetworkBuffer *buffer, size_t length)
{
    // If the current block as a next pointer, then the remaining is from
    // the position to the limit.  Otherwise it is from the position to
    // the end.

    size_t remaining;
    remaining = buffer->current->begin + buffer->current->capacity - buffer->position;


    if (remaining < length) {
        // If its a small amount of memory to waste, we'll freeze the curent buffer and
        // make a new one.
        if (length < 32 && buffer->current->next == NULL) {
            _ccnxCodecNetworkBuffer_Expand(buffer);
            buffer->current = buffer->tail;
            return;
        }

        // otherwise, thre is still space in the current buffer, even though it is not
        // long enough.  The writer will just need to span the two memory blocks.
        _ccnxCodecNetworkBuffer_AllocateIfNeeded(buffer);
    }
}


// ================================================================================

static size_t
_ccnxCodecNetworkBuffer_ParcMemoryAllocator(void *userarg, size_t bytes, void **output)
{
    *output = parcMemory_Allocate(bytes);
    if (*output) {
        return bytes;
    }
    return 0;
}

static void
_ccnxCodecNetworkBuffer_ParcMemoryDeallocator(void *userarg, void **memoryPtr)
{
    void *memory = *memoryPtr;
    parcMemory_Deallocate((void **) &memory);
    *memoryPtr = NULL;
}

const CCNxCodecNetworkBufferMemoryBlockFunctions ParcMemoryMemoryBlock = {
    .allocator   = &_ccnxCodecNetworkBuffer_ParcMemoryAllocator,
    .deallocator = &_ccnxCodecNetworkBuffer_ParcMemoryDeallocator
};

CCNxCodecNetworkBuffer *
ccnxCodecNetworkBuffer_Allocate(const CCNxCodecNetworkBufferMemoryBlockFunctions *memoryFunctions, void *userarg)
{
    CCNxCodecNetworkBuffer *buffer = parcMemory_Allocate(sizeof(CCNxCodecNetworkBuffer));
    assertNotNull(buffer, "parcMemory_Allocate(%zu) returned NULL", sizeof(CCNxCodecNetworkBuffer));
    buffer->refcount = 1;
    buffer->position = 0;
    memcpy(&buffer->memoryFunctions, memoryFunctions, sizeof(CCNxCodecNetworkBufferMemoryBlockFunctions));
    buffer->userarg = userarg;
    return buffer;
}

CCNxCodecNetworkBuffer *
ccnxCodecNetworkBuffer_Create(const CCNxCodecNetworkBufferMemoryBlockFunctions *memoryFunctions, void *userarg)
{
    CCNxCodecNetworkBuffer *buffer = ccnxCodecNetworkBuffer_Allocate(memoryFunctions, userarg);

    buffer->head = _ccnxCodecNetworkBufferMemory_Allocate(buffer, 1536);
    buffer->tail = buffer->head;
    buffer->current = buffer->head;
    buffer->capacity = buffer->head->capacity;

    return buffer;
}

CCNxCodecNetworkBuffer *
ccnxCodecNetworkBuffer_CreateFromArray(const CCNxCodecNetworkBufferMemoryBlockFunctions *memoryFunctions, void *userarg, size_t length, uint8_t memory[length])
{
    CCNxCodecNetworkBuffer *buffer = ccnxCodecNetworkBuffer_Allocate(memoryFunctions, userarg);

    buffer->head = _ccnxCodecNetworkBufferMemory_Wrap(buffer, length, memory);
    buffer->tail = buffer->head;
    buffer->current = buffer->head;
    buffer->capacity = buffer->head->capacity;

    return buffer;
}



CCNxCodecNetworkBuffer *
ccnxCodecNetworkBuffer_Acquire(CCNxCodecNetworkBuffer *original)
{
    assertNotNull(original, "Parameter must be non-null");
    assertTrue(original->refcount > 0, "Refcount must be positive, got 0");

    original->refcount++;
    return original;
}

void
ccnxCodecNetworkBuffer_Release(CCNxCodecNetworkBuffer **bufferPtr)
{
    assertNotNull(bufferPtr, "Parameter must be non-null");
    assertNotNull(*bufferPtr, "Parameter must dereference to non-null");

    CCNxCodecNetworkBuffer *buffer = *bufferPtr;
    assertTrue(buffer->refcount > 0, "refcount must be positive");

    buffer->refcount--;
    if (buffer->refcount == 0) {
        while (buffer->head) {
            CCNxCodecNetworkBufferMemory *next = buffer->head->next;
            buffer->head->next = NULL;
            _ccnxCodecNetworkBufferMemory_Release(buffer, &buffer->head);
            buffer->head = next;
        }
        parcMemory_Deallocate((void **) &buffer);
    }
    *bufferPtr = NULL;
}

// ================================================================================

static inline size_t
_ccnxCodecNetworkBuffer_Limit(const CCNxCodecNetworkBuffer *buffer)
{
    return buffer->tail->begin + buffer->tail->limit;
}

size_t
ccnxCodecNetworkBuffer_Position(const CCNxCodecNetworkBuffer *buffer)
{
    assertNotNull(buffer, "Parameter must be non-null");
    return buffer->position;
}

size_t
ccnxCodecNetworkBuffer_Limit(const CCNxCodecNetworkBuffer *buffer)
{
    assertNotNull(buffer, "Parameter must be non-null");
    return _ccnxCodecNetworkBuffer_Limit(buffer);
}

void
ccnxCodecNetworkBuffer_SetPosition(CCNxCodecNetworkBuffer *buffer, size_t position)
{
    assertNotNull(buffer, "Parameter buffer must be non-null");
    assertTrue(position <= _ccnxCodecNetworkBuffer_Limit(buffer), "Position must not exceed limit, got %zu limit %zu",
               position, _ccnxCodecNetworkBuffer_Limit(buffer));

    // We allow the position to be set to the end (just past the last written byte) of the buffer.
    // This is the "next"  position to be written
    if (position == _ccnxCodecNetworkBuffer_Limit(buffer)) {
        buffer->current = buffer->tail;
    } else {
        // Is the new position within the current memory block?
        if (_ccnxCodecNetworkBufferMemory_ContainsPosition(buffer->current, position)) {
            // we're ok, new position is in this buffer, we're done :)
        } else {
            // we need to find the right buffer
            CCNxCodecNetworkBufferMemory *memory = buffer->head;
            while (!_ccnxCodecNetworkBufferMemory_ContainsPosition(memory, position)) {
                memory = memory->next;
                assertNotNull(memory, "Illegal state: position < buffer->limit, but we ran off end of linked list");
            }

            buffer->current = memory;
        }
    }

    buffer->position = position;
}

void
ccnxCodecNetworkBuffer_Finalize(CCNxCodecNetworkBuffer *buffer)
{
    assertNotNull(buffer, "Parameter buffer must be non-null");

    // if we're at the limit, we're done
    if (buffer->position < _ccnxCodecNetworkBuffer_Limit(buffer)) {
        // begin at the tail and free memory blocks until we've found the current position
        size_t position = buffer->position;

        // Is the new position within the current memory block?
        if (_ccnxCodecNetworkBufferMemory_ContainsPosition(buffer->current, position)) {
            // we're ok, new position is in this buffer, we're done :)
        } else {
            // we need to find the right buffer
            CCNxCodecNetworkBufferMemory *memory = buffer->head;
            while (!_ccnxCodecNetworkBufferMemory_ContainsPosition(memory, position)) {
                memory = memory->next;
                assertNotNull(memory, "Illegal state: position < buffer->limit, but we ran off end of linked list");
            }

            buffer->current = memory;
        }

        // discard any memory blocks after this

        CCNxCodecNetworkBufferMemory *current = buffer->current->next;
        while (current) {
            CCNxCodecNetworkBufferMemory *next = current->next;

            // this is a requirement for _Release to not throw an assertion
            current->next = NULL;
            _ccnxCodecNetworkBufferMemory_Release(buffer, &current);
            current = next;
        }

        // Set the limit of the current block so buffer->position is the end
        buffer->current->next = NULL;
        size_t relativePosition = buffer->position - buffer->current->begin;
        buffer->current->limit = relativePosition;
        buffer->tail = buffer->current;
    }
}

static inline void
_ccnxCodecNetworkBuffer_PutUint8(CCNxCodecNetworkBuffer *buffer, uint8_t value)
{
    _ccnxCodecNetworkBuffer_AllocateIfNeeded(buffer);

    size_t relativePosition = buffer->position - buffer->current->begin;
    buffer->current->memory[relativePosition++] = value;
    if (relativePosition > buffer->current->limit) {
        buffer->current->limit = relativePosition;
    }

    buffer->position++;
}

void
ccnxCodecNetworkBuffer_PutUint8(CCNxCodecNetworkBuffer *buffer, uint8_t value)
{
    assertNotNull(buffer, "Parameter buffer must be non-null");
    _ccnxCodecNetworkBuffer_PutUint8(buffer, value);
}

void
ccnxCodecNetworkBuffer_PutUint16(CCNxCodecNetworkBuffer *buffer, uint16_t value)
{
    _ccnxCodecNetworkBuffer_EnsureRemaining(buffer, 2);

    _ccnxCodecNetworkBuffer_PutUint8(buffer, value >> 8);
    _ccnxCodecNetworkBuffer_PutUint8(buffer, value & 0xFF);
}

void
ccnxCodecNetworkBuffer_PutUint32(CCNxCodecNetworkBuffer *buffer, uint32_t value)
{
    _ccnxCodecNetworkBuffer_EnsureRemaining(buffer, 4);

    for (int i = sizeof(uint32_t) - 1; i > 0; i--) {
        uint8_t b = value >> (i * 8) & 0xFF;
        _ccnxCodecNetworkBuffer_PutUint8(buffer, b);
    }

    _ccnxCodecNetworkBuffer_PutUint8(buffer, value & 0xFF);
}

void
ccnxCodecNetworkBuffer_PutUint64(CCNxCodecNetworkBuffer *buffer, uint64_t value)
{
    _ccnxCodecNetworkBuffer_EnsureRemaining(buffer, 8);

    for (int i = sizeof(uint64_t) - 1; i > 0; i--) {
        uint8_t b = value >> (i * 8) & 0xFF;
        _ccnxCodecNetworkBuffer_PutUint8(buffer, b);
    }

    _ccnxCodecNetworkBuffer_PutUint8(buffer, value & 0xFF);
}

void
ccnxCodecNetworkBuffer_PutArray(CCNxCodecNetworkBuffer *buffer, size_t length, const uint8_t array[length])
{
    _ccnxCodecNetworkBuffer_AllocateIfNeeded(buffer);

    size_t offset = 0;
    while (offset < length) {
        size_t available = _ccnxCodecNetworkBuffer_RemainingCurrentBlock(buffer);
        if (available == 0) {
            _ccnxCodecNetworkBuffer_AllocateIfNeeded(buffer);
        } else {
            if (available > (length - offset)) {
                available = length - offset;
            }

            size_t relativePosition = buffer->position - buffer->current->begin;
            void *dest = &buffer->current->memory[relativePosition];
            const void *src = &array[offset];
            memcpy(dest, src, available);

            relativePosition += available;
            if (relativePosition > buffer->current->limit) {
                buffer->current->limit = relativePosition;
            }

            buffer->position += available;
            offset += available;
        }
    }
}

void
ccnxCodecNetworkBuffer_PutBuffer(CCNxCodecNetworkBuffer *buffer, PARCBuffer *value)
{
    size_t length = parcBuffer_Remaining(value);
    if (length > 0) {
        void *ptr = parcBuffer_Overlay(value, 0);
        ccnxCodecNetworkBuffer_PutArray(buffer, length, ptr);
    }
}

PARCBuffer *
ccnxCodecNetworkBuffer_CreateParcBuffer(CCNxCodecNetworkBuffer *buffer)
{
    // We don't have the idea of Flip here yet, so we go from 0 .. position

    size_t length = _ccnxCodecNetworkBuffer_Limit(buffer);
    PARCBuffer *output = parcBuffer_Allocate(length);
    CCNxCodecNetworkBufferMemory *block = buffer->head;
    while (block) {
        size_t available = (length > block->limit) ? block->limit : length;
        if (available > 0) {
            parcBuffer_PutArray(output, available, block->memory);
        }
        length -= available;
        block = block->next;
    }
    parcBuffer_Flip(output);
    return output;
}

PARCSignature *
ccnxCodecNetworkBuffer_ComputeSignature(CCNxCodecNetworkBuffer *buffer, size_t start, size_t end, PARCSigner *signer)
{
    // Most positions (start, end, position, roof) below are in **absolute** coordinates
    // The position relativePosition is relative to the memory block start
    assertNotNull(buffer, "Parameter buffer must be non-null");
    assertTrue(end >= start, "End is less than start: start %zu end %zu", start, end);

    PARCSignature *signature = NULL;
    if (signer) {
        // compute the signature over the specified area

        PARCCryptoHasher *hasher = parcSigner_GetCryptoHasher(signer);
        parcCryptoHasher_Init(hasher);

        size_t position = start;
        CCNxCodecNetworkBufferMemory *block = buffer->head;
        while (block && position < block->begin + block->limit) {
            if (_ccnxCodecNetworkBufferMemory_ContainsPosition(block, position)) {
                // determine if we're going all the way to the block's end or are we
                // stopping early because that's the end of the designated area
                size_t roof = (end > block->begin + block->limit) ? block->limit : end;
                size_t length = roof - position;

                // now calculate the relative offset in the block so we can update the hash
                size_t relativePosition = position - block->begin;

                parcCryptoHasher_UpdateBytes(hasher, &block->memory[relativePosition], length);

                position += length;
            }

            block = block->next;
        }

        PARCCryptoHash *hash = parcCryptoHasher_Finalize(hasher);

        signature = parcSigner_SignDigest(signer, hash);
        parcCryptoHash_Release(&hash);
    }

    return signature;
}

uint8_t
ccnxCodecNetworkBuffer_GetUint8(const CCNxCodecNetworkBuffer *netbuff, size_t position)
{
    assertNotNull(netbuff, "Parameter buffer must be non-null");
    assertTrue(position < _ccnxCodecNetworkBuffer_Limit(netbuff), "Position %zu beyond limit %zu", position, _ccnxCodecNetworkBuffer_Limit(netbuff));

    CCNxCodecNetworkBufferMemory *block = netbuff->head;
    while (block && !_ccnxCodecNetworkBufferMemory_ContainsPosition(block, position)) {
        block = block->next;
    }

    trapUnexpectedStateIf(block == NULL,
                          "Could not find position %zu that is less than limit %zu",
                          position, _ccnxCodecNetworkBuffer_Limit(netbuff));

    size_t relativeOffset = position - block->begin;
    return block->memory[relativeOffset];
}

void
ccnxCodecNetworkBuffer_Display(const CCNxCodecNetworkBuffer *netbuff, unsigned indent)
{
    printf("CCNxCodecNetworkBuffer %p head %p current %p tail %p\n",
           (void *) netbuff, (void *) netbuff->head, (void *) netbuff->current, (void *) netbuff->tail);
    printf(" position %zu limit %zu capacity %zu refcount %u userarg %p\n",
           netbuff->position, _ccnxCodecNetworkBuffer_Limit(netbuff), netbuff->capacity, netbuff->refcount, netbuff->userarg);

    CCNxCodecNetworkBufferMemory *block = netbuff->head;
    while (block) {
        _ccnxCodecNetworkBufferMemory_Display(block, 6);
        block = block->next;
    }
}

// ======================================================================================


CCNxCodecNetworkBufferIoVec *
ccnxCodecNetworkBuffer_CreateIoVec(CCNxCodecNetworkBuffer *buffer)
{
    size_t blockCount = _ccnxCodecNetworkBuffer_BlockCount(buffer);
    size_t allocationSize = sizeof(CCNxCodecNetworkBufferIoVec) + sizeof(struct iovec) * blockCount;

    CCNxCodecNetworkBufferIoVec *vec = parcMemory_Allocate(allocationSize);
    assertNotNull(vec, "parcMemory_Allocate(%zu) returned NULL", allocationSize);
    vec->refcount = 1;
    vec->networkBuffer = ccnxCodecNetworkBuffer_Acquire(buffer);
    vec->iovcnt = (int) blockCount;
    vec->totalBytes = 0;

    CCNxCodecNetworkBufferMemory *block = buffer->head;
    for (int i = 0; i < vec->iovcnt; i++) {
        vec->array[i].iov_base = block->memory;
        vec->array[i].iov_len = block->limit;
        vec->totalBytes += block->limit;
        block = block->next;
    }

    return vec;
}

CCNxCodecNetworkBufferIoVec *
ccnxCodecNetworkBufferIoVec_Acquire(CCNxCodecNetworkBufferIoVec *vec)
{
    assertNotNull(vec, "Parameter vec must be non-null");
    assertTrue(vec->refcount > 0, "Existing reference count is 0");
    vec->refcount++;
    return vec;
}

void
ccnxCodecNetworkBufferIoVec_Release(CCNxCodecNetworkBufferIoVec **vecPtr)
{
    assertNotNull(vecPtr, "Parameter must be non-null");
    assertNotNull(*vecPtr, "Parameter must dereference to non-null");
    CCNxCodecNetworkBufferIoVec *vec = *vecPtr;
    assertTrue(vec->refcount > 0, "object has 0 refcount!");

    vec->refcount--;
    if (vec->refcount == 0) {
        ccnxCodecNetworkBuffer_Release(&vec->networkBuffer);
        parcMemory_Deallocate((void **) &vec);
    }
    *vecPtr = NULL;
}

int
ccnxCodecNetworkBufferIoVec_GetCount(CCNxCodecNetworkBufferIoVec *vec)
{
    assertNotNull(vec, "Parameter vec must be non-null");
    return vec->iovcnt;
}

const struct iovec *
ccnxCodecNetworkBufferIoVec_GetArray(CCNxCodecNetworkBufferIoVec *vec)
{
    assertNotNull(vec, "Parameter vec must be non-null");
    return vec->array;
}

void
ccnxCodecNetworkBufferIoVec_Display(const CCNxCodecNetworkBufferIoVec *vec, int indent)
{
    printf("\nCCNxCodecNetworkBufferIoVec %p refcount %u totalBytes %zu iovcnt %d NetworkBuffer %p\n",
           (void *) vec, vec->refcount, vec->totalBytes, vec->iovcnt, (void *) vec->networkBuffer);

    size_t total = 0;
    for (int i = 0; i < vec->iovcnt; i++) {
        total += vec->array[i].iov_len;
        int nwritten = printf("   vec %3d base %p length %5zu total %5zu\n", i, (void *)  vec->array[i].iov_base, vec->array[i].iov_len, total);
        assertTrue(nwritten >= 0, "Error calling printf");
        longBowDebug_MemoryDump(vec->array[i].iov_base, vec->array[i].iov_len);
    }
}

size_t
ccnxCodecNetworkBufferIoVec_Length(const CCNxCodecNetworkBufferIoVec *vec)
{
    assertNotNull(vec, "Parameter vec must be non-null");
    return vec->totalBytes;
}

bool
ccnxCodecNetworkBufferIoVec_Equals(const CCNxCodecNetworkBufferIoVec *a, const CCNxCodecNetworkBufferIoVec *b)
{
    if (a == NULL && b == NULL) {
        return true;
    }

    if (a == NULL || b == NULL) {
        return false;
    }

    // both are non-null
    bool equals = false;
    if (a->totalBytes == b->totalBytes) {
        PARCBuffer *abuffer = ccnxCodecNetworkBuffer_CreateParcBuffer(a->networkBuffer);
        PARCBuffer *bbuffer = ccnxCodecNetworkBuffer_CreateParcBuffer(b->networkBuffer);

        equals = parcBuffer_Equals(abuffer, bbuffer);

        parcBuffer_Release(&abuffer);
        parcBuffer_Release(&bbuffer);
    }
    return equals;
}