aboutsummaryrefslogtreecommitdiffstats
path: root/metis/ccnx/forwarder/metis/tlv/metis_TlvName.c
blob: 852978c5aa28d634fbe37310a0f925d956886dc4 (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
/*
 * 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.
 */

/**
 * A name built around the TLV representation.
 *
 * A common operation is to get a sub-string of the name, specifically prefixes.  Use
 * metisTlvName_Slice() for that.
 *
 * To be efficient about Slice(), the reference counters are pointers, so every allocated
 * copy shares the reference counter and all the allocated memory.  Each Slice() will do
 * one malloc for the new shell and do a shallow memcpy of the struct.  Destroy will always
 * free the shell, but will only free the guts when the shared reference count goes to zero.
 *
 */

#include <config.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>

#include <parc/algol/parc_Memory.h>
#include <parc/algol/parc_Hash.h>
#include <parc/algol/parc_BufferComposer.h>

#include <ccnx/forwarder/metis/tlv/metis_TlvName.h>
#include <ccnx/forwarder/metis/tlv/metis_Tlv.h>
#include <ccnx/forwarder/metis/tlv/metis_TlvNameCodec.h>

#include <LongBow/runtime.h>

struct metis_tlv_name {
    uint8_t *memory;
    size_t memoryLength;

    // the refcount is shared between all copies
    unsigned  *refCountPtr;

    // the memory extents of each path segment
    MetisTlvExtent *segmentArray;
    size_t segmentArrayLength;

    // hashes of the name through different prefix lengths
    // It is allocated out to the limit (same assegmentArrayLength),
    // but only computed so far through segmentCumulativeHashArrayLength
    // This is to avoid computing the hash over unnecessary suffix segments.
    size_t segmentCumulativeHashArrayLimit;

    // the cumulative hash array length is shared between all copies, so if
    // one copy extends the array, all copies see it
    size_t *segmentCumulativeHashArrayLengthPtr;
    uint32_t *segmentCumulativeHashArray;
};

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

static unsigned
_getRefCount(const MetisTlvName *name)
{
    return *name->refCountPtr;
}

static void
_incrementRefCount(MetisTlvName *name)
{
    assertTrue(*name->refCountPtr > 0, "Illegal State: Trying to increment a 0 refcount!");
    (*name->refCountPtr)++;
}

static void
_decrementRefCount(MetisTlvName *name)
{
    assertTrue(*name->refCountPtr > 0, "Illegal State: Trying to decrement a 0 refcount!");
    (*name->refCountPtr)--;
}

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

/**
 * Common parts of setting up a MetisTlvName after the backing memory has been allocated and copied in to.
 *
 * PRECONDITIONS: name->memory and name->memoryLength set
 */
static void
_setup(MetisTlvName *name)
{
    name->refCountPtr = parcMemory_Allocate(sizeof(unsigned));
    assertNotNull(name->refCountPtr, "parcMemory_Allocate(%zu) returned NULL", sizeof(unsigned));
    *name->refCountPtr = 1;

    metisTlv_NameSegments(name->memory, name->memoryLength, &name->segmentArray, &name->segmentArrayLength);

    name->segmentCumulativeHashArray = parcMemory_Allocate(name->segmentArrayLength * sizeof(uint32_t));
    assertNotNull(name->segmentCumulativeHashArray, "parcMemory_Allocate(%zu) returned NULL", name->segmentArrayLength * sizeof(uint32_t));

    name->segmentCumulativeHashArrayLengthPtr = parcMemory_Allocate(sizeof(size_t));
    assertNotNull(name->segmentCumulativeHashArrayLengthPtr, "parcMemory_Allocate(%zu) returned NULL", sizeof(size_t));

    *name->segmentCumulativeHashArrayLengthPtr = 1;
    name->segmentCumulativeHashArrayLimit = name->segmentArrayLength;


    if (name->segmentArrayLength > 0) {
        // always hash the 1st name component.  This is needed as the initial case
        // to do the cumulative hashes in metisTlvName_HashCode
        name->segmentCumulativeHashArray[0] = parcHash32_Data(&name->memory[name->segmentArray[0].offset], name->segmentArray[0].length);
    }
}

MetisTlvName *
metisTlvName_Create(const uint8_t *memory, size_t memoryLength)
{
    MetisTlvName *name = parcMemory_AllocateAndClear(sizeof(MetisTlvName));
    assertNotNull(name, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(MetisTlvName));

    name->memory = parcMemory_Allocate(memoryLength);
    assertNotNull(name->memory, "parcMemory_Allocate(%zu) returned NULL", memoryLength);

    memcpy(name->memory, memory, memoryLength);
    name->memoryLength = memoryLength;

    _setup(name);

    return name;
}

MetisTlvName *
metisTlvName_CreateFromCCNxName(const CCNxName *ccnxName)
{
    // to avoid reallocs, calculate the exact size we need
    size_t memoryLength = 0;
    for (size_t i = 0; i < ccnxName_GetSegmentCount(ccnxName); i++) {
        CCNxNameSegment *segment = ccnxName_GetSegment(ccnxName, i);
        memoryLength += 4 + ccnxNameSegment_Length(segment);
    }

    MetisTlvName *name = parcMemory_AllocateAndClear(sizeof(MetisTlvName));
    assertNotNull(name, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(MetisTlvName));

    name->memoryLength = memoryLength;
    name->memory = parcMemory_Allocate(memoryLength);
    assertNotNull(name->memory, "parcMemory_Allocate(%zu) returned NULL", memoryLength);

    uint8_t *p = name->memory;
    uint8_t *end = p + memoryLength;
    for (size_t i = 0; i < ccnxName_GetSegmentCount(ccnxName); i++) {
        CCNxNameSegment *segment = ccnxName_GetSegment(ccnxName, i);
        uint16_t type = ccnxNameSegment_GetType(segment);
        uint16_t length = ccnxNameSegment_Length(segment);

        *(uint16_t *) p = htons(type);
        p += 2;

        *(uint16_t *) p = htons(length);
        p += 2;

        if (length >0) {
            PARCBuffer *buffer = ccnxNameSegment_GetValue(segment);
            uint8_t *overlay = parcBuffer_Overlay(buffer, 0);
            memcpy(p, overlay, length);

            p += length;
        }

        // sanity check
        assertTrue(p <= end, "Wrote past the end of buffer, now at %p end at %p", p, end);
    }

    _setup(name);
    return name;
}

void
metisTlvName_Release(MetisTlvName **namePtr)
{
    assertNotNull(namePtr, "Parameter must be non-null double pointer");
    assertNotNull(*namePtr, "Parameter must dereference to non-null pointer");

    MetisTlvName *name = *namePtr;
    _decrementRefCount(name);
    if (_getRefCount(name) == 0) {
        parcMemory_Deallocate((void **) &(name->refCountPtr));
        parcMemory_Deallocate((void **) &(name->segmentArray));
        parcMemory_Deallocate((void **) &(name->segmentCumulativeHashArray));
        parcMemory_Deallocate((void **) &(name->segmentCumulativeHashArrayLengthPtr));
        parcMemory_Deallocate((void **) &(name->memory));
    }
    parcMemory_Deallocate((void **) &name);
    *namePtr = NULL;
}

MetisTlvName *
metisTlvName_Acquire(const MetisTlvName *original)
{
    return metisTlvName_Slice(original, UINT_MAX);
}

MetisTlvName *
metisTlvName_Slice(const MetisTlvName *original, size_t segmentCount)
{
    assertNotNull(original, "Parameter must be non-null");
    MetisTlvName *copy = parcMemory_AllocateAndClear(sizeof(MetisTlvName));
    assertNotNull(copy, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(MetisTlvName));

    memcpy(copy, original, sizeof(MetisTlvName));
    _incrementRefCount(copy);

    copy->segmentArrayLength = (copy->segmentArrayLength < segmentCount) ? copy->segmentArrayLength : segmentCount;

    // for equality to work, we need to shorten the MemoryLength to the amount
    // actually used by the number of segments.
    size_t startOfLastSegment = copy->segmentArray[ copy->segmentArrayLength - 1 ].offset;
    size_t lengthOfLastSegment = copy->segmentArray[ copy->segmentArrayLength - 1 ].length;

    copy->memoryLength = startOfLastSegment + lengthOfLastSegment;

    return copy;
}

uint32_t
metisTlvName_HashCode(const MetisTlvName *name)
{
    if ((name == NULL) || (name->segmentArrayLength == 0)) {
        return 0;
    }

    size_t lastSegment = name->segmentArrayLength - 1;

    if (lastSegment >= *name->segmentCumulativeHashArrayLengthPtr) {
        // we have not yet computed this, so lets do it now!
        // Note that we go up to and including lastSegment in the for loop.  lastSegment is not a "length", it is
        // the actual array index.
        for (size_t i = (*name->segmentCumulativeHashArrayLengthPtr); i <= lastSegment; i++) {
            // the -1 is ok in the 3rd argument, because segmentCumulativeHashArrayLength always has length at least 1
            // if there are any name components to hash.
            name->segmentCumulativeHashArray[i] = parcHash32_Data_Cumulative(&name->memory[ name->segmentArray[i].offset ],
                                                                             name->segmentArray[i].length,
                                                                             name->segmentCumulativeHashArray[i - 1]);
        }
        *name->segmentCumulativeHashArrayLengthPtr = lastSegment + 1;
    }

    return name->segmentCumulativeHashArray[lastSegment];
}

bool
metisTlvName_Equals(const MetisTlvName *a, const MetisTlvName *b)
{
    assertNotNull(a, "Parameter a must be non-null");
    assertNotNull(b, "Parameter b must be non-null");

    if (a->memoryLength == b->memoryLength) {
        return (memcmp(a->memory, b->memory, a->memoryLength) == 0);
    }
    return false;
}

int
metisTlvName_Compare(const MetisTlvName *a, const MetisTlvName *b)
{
    if (a == NULL && b == NULL) {
        return 0;
    }
    if (a == NULL) {
        return -1;
    }
    if (b == NULL) {
        return +1;
    }

    if (a->memoryLength < b->memoryLength) {
        return -1;
    }

    if (a->memoryLength > b->memoryLength) {
        return +1;
    }

    return memcmp(a->memory, b->memory, a->memoryLength);
}

bool
metisTlvName_StartsWith(const MetisTlvName *name, const MetisTlvName *prefix)
{
    assertNotNull(name, "Parameter name must be non-null");
    assertNotNull(prefix, "Parameter prefix must be non-null");

    if (prefix->memoryLength <= name->memoryLength) {
        return (memcmp(prefix->memory, name->memory, prefix->memoryLength) == 0);
    }

    return false;
}

size_t
metisTlvName_SegmentCount(const MetisTlvName *name)
{
    assertNotNull(name, "Parameter name must be non-null");
    return name->segmentArrayLength;
}

CCNxName *
metisTlvName_ToCCNxName(const MetisTlvName *name)
{
    return metisTlvNameCodec_Decode(name->memory, 0, name->memoryLength);
}