| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <proton/object.h> |
| 23 |
|
| 24 |
#include "core/memory.h" |
| 25 |
|
| 26 |
#include <stddef.h> |
| 27 |
#include <assert.h> |
| 28 |
|
| 29 |
typedef struct { |
| 30 |
pn_handle_t key; |
| 31 |
const pn_class_t *clazz; |
| 32 |
void *value; |
| 33 |
} pni_field_t; |
| 34 |
|
| 35 |
struct pn_record_t { |
| 36 |
size_t size; |
| 37 |
size_t capacity; |
| 38 |
pni_field_t *fields; |
| 39 |
}; |
| 40 |
|
| 41 |
static void pn_record_initialize(void *object) |
| 42 |
{ |
| 43 |
pn_record_t *record = (pn_record_t *) object; |
| 44 |
record->size = 0; |
| 45 |
record->capacity = 0; |
| 46 |
record->fields = NULL; |
| 47 |
} |
| 48 |
|
| 49 |
static void pn_record_finalize(void *object) |
| 50 |
{ |
| 51 |
pn_record_t *record = (pn_record_t *) object; |
| 52 |
for (size_t i = 0; i < record->size; i++) { |
| 53 |
pni_field_t *v = &record->fields[i]; |
| 54 |
pn_class_decref(v->clazz, v->value); |
| 55 |
} |
| 56 |
pni_mem_subdeallocate(pn_class(record), record, record->fields); |
| 57 |
} |
| 58 |
|
| 59 |
#define pn_record_hashcode NULL |
| 60 |
#define pn_record_compare NULL |
| 61 |
#define pn_record_inspect NULL |
| 62 |
|
| 63 |
pn_record_t *pn_record(void) |
| 64 |
{ |
| 65 |
static const pn_class_t clazz = PN_CLASS(pn_record); |
| 66 |
pn_record_t *record = (pn_record_t *) pn_class_new(&clazz, sizeof(pn_record_t)); |
| 67 |
pn_record_def(record, PN_LEGCTX, PN_VOID); |
| 68 |
return record; |
| 69 |
} |
| 70 |
|
| 71 |
static pni_field_t *pni_record_find(pn_record_t *record, pn_handle_t key) { |
| 72 |
for (size_t i = 0; i < record->size; i++) { |
| 73 |
pni_field_t *field = &record->fields[i]; |
| 74 |
if (field->key == key) { |
| 75 |
return field; |
| 76 |
} |
| 77 |
} |
| 78 |
return NULL; |
| 79 |
} |
| 80 |
|
| 81 |
static pni_field_t *pni_record_create(pn_record_t *record) { |
| 82 |
record->size++; |
| 83 |
if (record->size > record->capacity) { |
| 84 |
record->fields = (pni_field_t *) pni_mem_subreallocate(pn_class(record), record, record->fields, record->size * sizeof(pni_field_t)); |
| 85 |
record->capacity = record->size; |
| 86 |
} |
| 87 |
pni_field_t *field = &record->fields[record->size - 1]; |
| 88 |
field->key = 0; |
| 89 |
field->clazz = NULL; |
| 90 |
field->value = NULL; |
| 91 |
return field; |
| 92 |
} |
| 93 |
|
| 94 |
void pn_record_def(pn_record_t *record, pn_handle_t key, const pn_class_t *clazz) |
| 95 |
{ |
| 96 |
assert(record); |
| 97 |
assert(clazz); |
| 98 |
|
| 99 |
pni_field_t *field = pni_record_find(record, key); |
| 100 |
if (field) { |
| 101 |
assert(field->clazz == clazz); |
| 102 |
} else { |
| 103 |
field = pni_record_create(record); |
| 104 |
field->key = key; |
| 105 |
field->clazz = clazz; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
bool pn_record_has(pn_record_t *record, pn_handle_t key) |
| 110 |
{ |
| 111 |
assert(record); |
| 112 |
pni_field_t *field = pni_record_find(record, key); |
| 113 |
if (field) { |
| 114 |
return true; |
| 115 |
} else { |
| 116 |
return false; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
void *pn_record_get(pn_record_t *record, pn_handle_t key) |
| 121 |
{ |
| 122 |
assert(record); |
| 123 |
pni_field_t *field = pni_record_find(record, key); |
| 124 |
if (field) { |
| 125 |
return field->value; |
| 126 |
} else { |
| 127 |
return NULL; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
void pn_record_set(pn_record_t *record, pn_handle_t key, void *value) |
| 132 |
{ |
| 133 |
assert(record); |
| 134 |
|
| 135 |
pni_field_t *field = pni_record_find(record, key); |
| 136 |
if (field) { |
| 137 |
void *old = field->value; |
| 138 |
field->value = value; |
| 139 |
pn_class_incref(field->clazz, value); |
| 140 |
pn_class_decref(field->clazz, old); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
void pn_record_clear(pn_record_t *record) |
| 145 |
{ |
| 146 |
assert(record); |
| 147 |
for (size_t i = 0; i < record->size; i++) { |
| 148 |
pni_field_t *field = &record->fields[i]; |
| 149 |
pn_class_decref(field->clazz, field->value); |
| 150 |
field->key = 0; |
| 151 |
field->clazz = NULL; |
| 152 |
field->value = NULL; |
| 153 |
} |
| 154 |
record->size = 0; |
| 155 |
pn_record_def(record, PN_LEGCTX, PN_VOID); |
| 156 |
} |