static gboolean nv_table_reserve_table_entry(NVTable *self, NVHandle handle, NVDynValue **dyn_slot) { if (G_UNLIKELY(!(*dyn_slot) && handle > self->num_static_entries)) { /* this is a dynamic value */ NVDynValue *dyn_entries = nv_table_get_dyn_entries(self);; gint l, h, m, ndx; gboolean found = FALSE; if (!nv_table_alloc_check(self, sizeof(dyn_entries[0]))) return FALSE; l = 0; h = self->num_dyn_entries - 1; ndx = -1; while (l <= h) { guint16 mv; m = (l+h) >> 1; mv = NV_TABLE_DYNVALUE_HANDLE(dyn_entries[m]); if (mv == handle) { ndx = m; found = TRUE; break; } else if (mv > handle) { h = m - 1; } else { l = m + 1; } } /* if we find the proper slot we set that, if we don't, we insert a new entry */ if (!found) ndx = l; g_assert(ndx >= 0 && ndx <= self->num_dyn_entries); if (ndx < self->num_dyn_entries) { memmove(&dyn_entries[ndx + 1], &dyn_entries[ndx], (self->num_dyn_entries - ndx) * sizeof(dyn_entries[0])); } *dyn_slot = &dyn_entries[ndx]; /* we set ofs to zero here, which means that the NVEntry won't be found even if the slot is present in dyn_entries */ (**dyn_slot).handle = handle; (**dyn_slot).ofs = 0; if (!found) self->num_dyn_entries++; }
static gboolean _read_header(SerializeArchive *sa, NVTable **nvtable) { NVTable *res = NULL; guint32 size; g_assert(*nvtable == NULL); if (!serialize_read_uint32(sa, &size)) goto error; if (size > NV_TABLE_MAX_BYTES) goto error; res = (NVTable *) g_malloc(size); res->size = size; if (!serialize_read_uint32(sa, &res->used)) goto error; if (!serialize_read_uint16(sa, &res->index_size)) goto error; if (!serialize_read_uint8(sa, &res->num_static_entries)) goto error; /* static entries has to be known by this syslog-ng, if they are over * LM_V_MAX, that means we have no clue how an entry is called, as static * entries don't contain names. If there are less static entries, that * can be ok. */ if (res->num_static_entries > LM_V_MAX) goto error; /* validates self->used and self->index_size value as compared to "size" */ if (!nv_table_alloc_check(res, 0)) goto error; res->borrowed = FALSE; res->ref_cnt = 1; *nvtable = res; return TRUE; error: if (res) g_free(res); return FALSE; }
/* return the offset to a newly allocated payload string */ static inline NVEntry * nv_table_alloc_value(NVTable *self, gsize alloc_size) { NVEntry *entry; alloc_size = NV_TABLE_BOUND(alloc_size); /* alloc error, NVTable should be realloced */ if (!nv_table_alloc_check(self, alloc_size)) return NULL; self->used += alloc_size; entry = (NVEntry *) (nv_table_get_top(self) - (self->used)); entry->alloc_len = alloc_size; entry->indirect = FALSE; entry->referenced = FALSE; return entry; }