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
|
/* State lists and hash tables, for libreswan
*
* Copyright (C) 2015-2019 Andrew Cagney <cagney@gnu.org>
* Copyright (C) 2019 D. Hugh Redelmeier <hugh@mimosa.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <https://www.gnu.org/licenses/gpl2.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include <stdint.h>
#include "passert.h"
#include "defs.h"
#include "log.h"
#include "hash_table.h"
#define passert_entry(ENTRY, ASSERTION) \
{ \
bool a_ = ASSERTION; /* evaluate once */ \
if (!a_) { \
LLOG_PASSERT_JAMBUF(&global_logger, HERE, buf) { \
jam_string(buf, (ENTRY)->info->name); \
jam_string(buf, ": "); \
jam_list_entry(buf, (ENTRY)); \
jam_string(buf, ": "); \
jam_string(buf, #ASSERTION); \
} \
} \
}
#define passert_info(INFO, ASSERTION) \
{ \
bool a_ = ASSERTION; /* evaluate once */ \
if (!a_) { \
LLOG_PASSERT_JAMBUF(&global_logger, HERE, buf) { \
jam_string(buf, (INFO)->name); \
jam_string(buf, ": "); \
jam_string(buf, #ASSERTION); \
} \
} \
}
void jam_list_entry(struct jambuf *buf, const struct list_entry *entry)
{
if (entry == NULL) {
jam(buf, "(null)");
} else {
if (entry->data == NULL) {
jam(buf, "HEAD");
} else {
entry->info->jam(buf, entry->data);
}
jam(buf, " %p<-%p->%p", entry->next[NEW2OLD], entry, entry->next[OLD2NEW]);
}
}
static void log_entry(const char *op, struct list_entry *entry)
{
passert(entry != NULL);
LDBGP_JAMBUF(DBG_TMI, &global_logger, buf) {
jam(buf, "%s: %s ", entry->info->name, op);
jam_list_entry(buf, entry);
}
if (entry->next[OLD2NEW] != NULL || entry->next[NEW2OLD] != NULL) {
passert_entry(entry, entry->next[OLD2NEW] != NULL);
passert_entry(entry, entry->next[OLD2NEW]->next[NEW2OLD] == entry);
passert_entry(entry, entry->next[NEW2OLD] != NULL);
passert_entry(entry, entry->next[NEW2OLD]->next[OLD2NEW] == entry);
}
}
void init_list_entry(const struct list_info *info, void *data, struct list_entry *entry)
{
/* something to do? */
passert_info(info, data != NULL);
passert_info(info, entry != NULL);
/* not initialized */
passert_info(info, entry->info == NULL);
passert_info(info, entry->data == NULL);
passert_info(info, entry->next[OLD2NEW] == NULL);
passert_info(info, entry->next[NEW2OLD] == NULL);
#if 0
/* cross-check */
passert_info(info, entry == data_list_entry(info, data));
#endif
/* initialize */
*entry = (struct list_entry) {
.info = info,
.data = data,
};
}
#if 0
struct list_entry *data_list_entry(const struct list_info *info, void *data)
{
uint8_t *ptr = data;
struct list_entry *entry = (void *)(ptr + info->offset);
return entry;
}
#endif
#if 0
void *list_entry_data(const struct list_entry *entry)
{
passert(entry->info != NULL);
uint8_t *offptr = (void*)entry;
return offptr - entry->info->offset;
}
#endif
bool detached_list_entry(const struct list_entry *entry)
{
passert_entry(entry, entry->data != NULL); /* entry is not a list head */
passert_entry(entry, (entry->next[OLD2NEW] == NULL) == (entry->next[OLD2NEW] == NULL));
return entry->next[OLD2NEW] == NULL;
}
void insert_list_entry(struct list_head *list,
struct list_entry *entry)
{
passert_entry(entry, entry->info != NULL);
passert_entry(entry, entry->data != NULL);
LDBGP_JAMBUF(DBG_TMI, &global_logger, buf) {
jam(buf, "%s: inserting ",
entry->info->name);
jam_list_entry(buf, entry);
jam(buf, " into list ");
jam_list_entry(buf, &list->head);
}
passert_entry(entry, list->head.info == entry->info);
passert_entry(entry, entry->data != NULL);
passert_entry(entry, entry->next[NEW2OLD] == NULL && entry->next[OLD2NEW] == NULL);
passert_entry(entry, list->head.next[NEW2OLD] != NULL && list->head.next[OLD2NEW] != NULL);
/* insert at the front */
entry->next[OLD2NEW] = &list->head;
entry->next[NEW2OLD] = list->head.next[NEW2OLD];
entry->next[NEW2OLD]->next[OLD2NEW] = entry;
entry->next[OLD2NEW]->next[NEW2OLD] = entry;
/* list->next[OLD2NEW] = list->next[OLD2NEW]; */
LDBGP_JAMBUF(DBG_TMI, &global_logger, buf) {
jam(buf, "%s: inserted ",
entry->info->name);
jam_list_entry(buf, entry);
jam(buf, " into list ");
jam_list_entry(buf, &list->head);
}
}
void remove_list_entry(struct list_entry *entry)
{
log_entry("removing", entry);
/* entry is not a list head */
passert_entry(entry, entry->data != NULL);
/* unlink: older - entry - newer */
struct list_entry *newer = entry->next[OLD2NEW];
struct list_entry *older = entry->next[NEW2OLD];
passert_entry(entry, older != NULL && newer != NULL);
entry->next[NEW2OLD] = NULL; /* detach from list */
entry->next[OLD2NEW] = NULL;
newer->next[NEW2OLD] = older; /* seal the rift */
older->next[OLD2NEW] = newer;
if (older == newer) {
ldbgf(DBG_TMI, &global_logger, "%s: empty", entry->info->name);
} else {
log_entry("updated older", older);
log_entry("updated newer ", newer);
}
}
|