From 11da511c784eca003deb90c23570f0873954e0de Mon Sep 17 00:00:00 2001 From: Duncan Wilkie Date: Sat, 18 Nov 2023 06:11:09 -0600 Subject: Initial commit. --- ic-reals-6.3/base/nodeId.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 ic-reals-6.3/base/nodeId.c (limited to 'ic-reals-6.3/base/nodeId.c') diff --git a/ic-reals-6.3/base/nodeId.c b/ic-reals-6.3/base/nodeId.c new file mode 100644 index 0000000..035fa49 --- /dev/null +++ b/ic-reals-6.3/base/nodeId.c @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2000, Imperial College + * + * This file is part of the Imperial College Exact Real Arithmetic Library. + * See the copyright notice included in the distribution for conditions + * of use. + */ + +#include +#include "real.h" +#include "real-impl.h" + +/* + * For debugging purposes, we assign a unique identifier to each object in + * the heap. This identifier is invariant under copying in garbage collection. + * Also the names of the nodes in daVinci are strings formed from the + * assigned nodeId. + */ +int nodeId = 1; + +/* + * For debugging purposes, nodes are assigned id's which (unlike the address + * of the node) is invariant under garbage collection. + * + * This file allocates ids and provides a function for mapping ids to + * heap addresses. + */ +void +newNodeId(Generic *node) +{ + void addHashTableEntry(Generic *); + + node->tag.nodeId = nodeId++; + addHashTableEntry(node); +} + +/* + * We use a naive hash table to map node identifiers to heap cells. + */ +typedef struct HTE { + struct HTE *next; + Generic *node; +} HashTableEntry; + +/* + * The following must be an integer power of 2 + */ +#define HASH_TABLE_SIZE 512 +#define HASH_TABLE_MASK (HASH_TABLE_SIZE - 1) + +static HashTableEntry *hashTable[HASH_TABLE_SIZE] = {NULL}; + +void +addHashTableEntry(Generic *node) +{ + HashTableEntry *new; + + if ((new = (HashTableEntry *) malloc(sizeof(HashTableEntry))) == NULL) + Error(FATAL, E_INT, "addHashTableEntry", "malloc failed"); + + new->node = node; + new->next = hashTable[node->tag.nodeId & HASH_TABLE_MASK]; + hashTable[node->tag.nodeId & HASH_TABLE_MASK] = new; +} + +Generic * +mapNodeIdToHeapCell(int nodeId) +{ + HashTableEntry *p; + + p = hashTable[nodeId & HASH_TABLE_MASK]; + + while (p != NULL) { + if (p->node->tag.nodeId == nodeId) + return p->node; + else + p = p->next; + } + Error(FATAL, E_INT, "mapNodeIdToHeapCell", "failed to find nodeId %d", nodeId); + return NULL; +} -- cgit v1.2.3