Blame source/vdo/base/pointerMap.c

Packit b55c50
/*
Packit b55c50
 * Copyright (c) 2020 Red Hat, Inc.
Packit b55c50
 *
Packit b55c50
 * This program is free software; you can redistribute it and/or
Packit b55c50
 * modify it under the terms of the GNU General Public License
Packit b55c50
 * as published by the Free Software Foundation; either version 2
Packit b55c50
 * of the License, or (at your option) any later version.
Packit b55c50
 * 
Packit b55c50
 * This program is distributed in the hope that it will be useful,
Packit b55c50
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit b55c50
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit b55c50
 * GNU General Public License for more details.
Packit b55c50
 * 
Packit b55c50
 * You should have received a copy of the GNU General Public License
Packit b55c50
 * along with this program; if not, write to the Free Software
Packit b55c50
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit b55c50
 * 02110-1301, USA. 
Packit b55c50
 *
Packit b55c50
 * $Id: //eng/vdo-releases/aluminum/src/c++/vdo/base/pointerMap.c#1 $
Packit b55c50
 */
Packit b55c50
Packit b55c50
/**
Packit b55c50
 * Hash table implementation of a map from integers to pointers, implemented
Packit b55c50
 * using the Hopscotch Hashing algorithm by Herlihy, Shavit, and Tzafrir (see
Packit b55c50
 * http://en.wikipedia.org/wiki/Hopscotch_hashing). This implementation does
Packit b55c50
 * not contain any of the locking/concurrency features of the algorithm, just
Packit b55c50
 * the collision resolution scheme.
Packit b55c50
 *
Packit b55c50
 * Hopscotch Hashing is based on hashing with open addressing and linear
Packit b55c50
 * probing. All the entries are stored in a fixed array of buckets, with no
Packit b55c50
 * dynamic allocation for collisions. Unlike linear probing, all the entries
Packit b55c50
 * that hash to a given bucket are stored within a fixed neighborhood starting
Packit b55c50
 * at that bucket. Chaining is effectively represented as a bit vector
Packit b55c50
 * relative to each bucket instead of as pointers or explicit offsets.
Packit b55c50
 *
Packit b55c50
 * When an empty bucket cannot be found within a given neighborhood,
Packit b55c50
 * subsequent neighborhoods are searched, and one or more entries will "hop"
Packit b55c50
 * into those neighborhoods. When this process works, an empty bucket will
Packit b55c50
 * move into the desired neighborhood, allowing the entry to be added. When
Packit b55c50
 * that process fails (typically when the buckets are around 90% full), the
Packit b55c50
 * table must be resized and the all entries rehashed and added to the
Packit b55c50
 * expanded table.
Packit b55c50
 *
Packit b55c50
 * Unlike linear probing, the number of buckets that must be searched in the
Packit b55c50
 * worst case has a fixed upper bound (the size of the neighborhood). Those
Packit b55c50
 * entries occupy a small number of memory cache lines, leading to improved
Packit b55c50
 * use of the cache (fewer misses on both successful and unsuccessful
Packit b55c50
 * searches). Hopscotch hashing outperforms linear probing at much higher load
Packit b55c50
 * factors, so even with the increased memory burden for maintaining the hop
Packit b55c50
 * vectors, less memory is needed to achieve that performance. Hopscotch is
Packit b55c50
 * also immune to "contamination" from deleting entries since entries are
Packit b55c50
 * genuinely removed instead of being replaced by a placeholder.
Packit b55c50
 *
Packit b55c50
 * The published description of the algorithm used a bit vector, but the paper
Packit b55c50
 * alludes to an offset scheme which is used by this implementation. Since the
Packit b55c50
 * entries in the neighborhood are within N entries of the hash bucket at the
Packit b55c50
 * start of the neighborhood, a pair of small offset fields each log2(N) bits
Packit b55c50
 * wide is all that's needed to maintain the hops as a linked list. In order
Packit b55c50
 * to encode "no next hop" (i.e. NULL) as the natural initial value of zero,
Packit b55c50
 * the offsets are biased by one (i.e. 0 => NULL, 1 => offset=0, 2 =>
Packit b55c50
 * offset=1, etc.) We can represent neighborhoods of up to 255 entries with
Packit b55c50
 * just 8+8=16 bits per entry. The hop list is sorted by hop offset so the
Packit b55c50
 * first entry in the list is always the bucket closest to the start of the
Packit b55c50
 * neighborhood.
Packit b55c50
 *
Packit b55c50
 * While individual accesses tend to be very fast, the table resize operations
Packit b55c50
 * are very very expensive. If an upper bound on the latency of adding an
Packit b55c50
 * entry to the table is needed, we either need to ensure the table is
Packit b55c50
 * pre-sized to be large enough so no resize is ever needed, or we'll need to
Packit b55c50
 * develop an approach to incrementally resize the table.
Packit b55c50
 **/
Packit b55c50
Packit b55c50
#include "pointerMap.h"
Packit b55c50
Packit b55c50
#include "errors.h"
Packit b55c50
#include "logger.h"
Packit b55c50
#include "memoryAlloc.h"
Packit b55c50
#include "numeric.h"
Packit b55c50
#include "permassert.h"
Packit b55c50
Packit b55c50
enum {
Packit b55c50
  DEFAULT_CAPACITY = 16,    // the number of neighborhoods in a new table
Packit b55c50
  NEIGHBORHOOD     = 255,   // the number of buckets in each neighborhood
Packit b55c50
  MAX_PROBES       = 1024,  // limit on the number of probes for a free bucket
Packit b55c50
  NULL_HOP_OFFSET  = 0,     // the hop offset value terminating the hop list
Packit b55c50
  DEFAULT_LOAD     = 75     // a compromise between memory use and performance
Packit b55c50
};
Packit b55c50
Packit b55c50
/**
Packit b55c50
 * Buckets are packed together to reduce memory usage and improve cache
Packit b55c50
 * efficiency. It would be tempting to encode the hop offsets separately and
Packit b55c50
 * maintain alignment of key/value pairs, but it's crucial to keep the hop
Packit b55c50
 * fields near the buckets that they use them so they'll tend to share cache
Packit b55c50
 * lines.
Packit b55c50
 **/
Packit b55c50
typedef struct __attribute__((packed)) bucket {
Packit b55c50
  uint8_t   firstHop;  // the biased offset of the first entry in the hop list
Packit b55c50
                       // of the neighborhood that hashes to this bucket
Packit b55c50
  uint8_t   nextHop;   // the biased offset of the next bucket in the hop list
Packit b55c50
Packit b55c50
  const void *key;     // the key stored in this bucket
Packit b55c50
  void       *value;   // the value stored in this bucket (NULL if empty)
Packit b55c50
} Bucket;
Packit b55c50
Packit b55c50
/**
Packit b55c50
 * The concrete definition of the opaque PointerMap type. To avoid having to
Packit b55c50
 * wrap the neighborhoods of the last entries back around to the start of the
Packit b55c50
 * bucket array, we allocate a few more buckets at the end of the array
Packit b55c50
 * instead, which is why capacity and bucketCount are different.
Packit b55c50
 **/
Packit b55c50
struct pointerMap {
Packit b55c50
  /** the number of entries stored in the map */
Packit b55c50
  size_t                size;
Packit b55c50
  /** the number of neighborhoods in the map */
Packit b55c50
  size_t                capacity;
Packit b55c50
  /** the number of buckets in the bucket array */
Packit b55c50
  size_t                bucketCount;
Packit b55c50
  /** the array of hash buckets */
Packit b55c50
  Bucket               *buckets;
Packit b55c50
  /** the function for comparing keys for equality */
Packit b55c50
  PointerKeyComparator *comparator;
Packit b55c50
  /** the function for getting a hash code from a key */
Packit b55c50
  PointerKeyHasher     *hasher;
Packit b55c50
};
Packit b55c50
Packit b55c50
/**
Packit b55c50
 * Initialize a PointerMap.
Packit b55c50
 *
Packit b55c50
 * @param map       the map to initialize
Packit b55c50
 * @param capacity  the initial capacity of the map
Packit b55c50
 *
Packit b55c50
 * @return UDS_SUCCESS or an error code
Packit b55c50
 **/
Packit b55c50
static int allocateBuckets(PointerMap *map, size_t capacity)
Packit b55c50
{
Packit b55c50
  map->size     = 0;
Packit b55c50
  map->capacity = capacity;
Packit b55c50
Packit b55c50
  // Allocate NEIGHBORHOOD - 1 extra buckets so the last bucket can have a
Packit b55c50
  // full neighborhood without have to wrap back around to element zero.
Packit b55c50
  map->bucketCount = capacity + (NEIGHBORHOOD - 1);
Packit b55c50
  return ALLOCATE(map->bucketCount, Bucket, "PointerMap buckets",
Packit b55c50
                  &map->buckets);
Packit b55c50
}
Packit b55c50
Packit b55c50
/**********************************************************************/
Packit b55c50
int makePointerMap(size_t                 initialCapacity,
Packit b55c50
                   unsigned int           initialLoad,
Packit b55c50
                   PointerKeyComparator   comparator,
Packit b55c50
                   PointerKeyHasher       hasher,
Packit b55c50
                   PointerMap           **mapPtr)
Packit b55c50
{
Packit b55c50
  // Use the default initial load if the caller did not specify one.
Packit b55c50
  if (initialLoad == 0) {
Packit b55c50
    initialLoad = DEFAULT_LOAD;
Packit b55c50
  }
Packit b55c50
  if (initialLoad > 100) {
Packit b55c50
    return UDS_INVALID_ARGUMENT;
Packit b55c50
  }
Packit b55c50
Packit b55c50
  PointerMap *map;
Packit b55c50
  int result = ALLOCATE(1, PointerMap, "PointerMap", &map);
Packit b55c50
  if (result != UDS_SUCCESS) {
Packit b55c50
    return result;
Packit b55c50
  }
Packit b55c50
Packit b55c50
  map->hasher     = hasher;
Packit b55c50
  map->comparator = comparator;
Packit b55c50
Packit b55c50
  // Use the default capacity if the caller did not specify one.
Packit b55c50
  size_t capacity = (initialCapacity > 0) ? initialCapacity : DEFAULT_CAPACITY;
Packit b55c50
Packit b55c50
  // Scale up the capacity by the specified initial load factor.
Packit b55c50
  // (i.e to hold 1000 entries at 80% load we need a capacity of 1250)
Packit b55c50
  capacity = capacity * 100 / initialLoad;
Packit b55c50
Packit b55c50
  result = allocateBuckets(map, capacity);
Packit b55c50
  if (result != UDS_SUCCESS) {
Packit b55c50
    freePointerMap(&map);
Packit b55c50
    return result;
Packit b55c50
  }
Packit b55c50
Packit b55c50
  *mapPtr = map;
Packit b55c50
  return UDS_SUCCESS;
Packit b55c50
}
Packit b55c50
Packit b55c50
/**
Packit b55c50
 * Free the bucket array for the map.
Packit b55c50
 *
Packit b55c50
 * @param map  the map whose bucket array is to be freed
Packit b55c50
 **/
Packit b55c50
static void freeBuckets(PointerMap *map)
Packit b55c50
{
Packit b55c50
  FREE(map->buckets);
Packit b55c50
  map->buckets = NULL;
Packit b55c50
}
Packit b55c50
Packit b55c50
/**********************************************************************/
Packit b55c50
void freePointerMap(PointerMap **mapPtr)
Packit b55c50
{
Packit b55c50
  if (*mapPtr != NULL) {
Packit b55c50
    freeBuckets(*mapPtr);
Packit b55c50
    FREE(*mapPtr);
Packit b55c50
    *mapPtr = NULL;
Packit b55c50
  }
Packit b55c50
}
Packit b55c50
Packit b55c50
/**********************************************************************/
Packit b55c50
size_t pointerMapSize(const PointerMap *map)
Packit b55c50
{
Packit b55c50
  return map->size;
Packit b55c50
}
Packit b55c50
Packit b55c50
/**
Packit b55c50
 * Convert a biased hop offset within a neighborhood to a pointer to the
Packit b55c50
 * bucket it references.
Packit b55c50
 *
Packit b55c50
 * @param neighborhood  the first bucket in the neighborhood
Packit b55c50
 * @param hopOffset     the biased hop offset to the desired bucket
Packit b55c50
 *
Packit b55c50
 * @return NULL if hopOffset is zero, otherwise a pointer to
Packit b55c50
 *         the bucket in the neighborhood at hopOffset - 1
Packit b55c50
 **/
Packit b55c50
static Bucket *dereferenceHop(Bucket *neighborhood, unsigned int hopOffset)
Packit b55c50
{
Packit b55c50
  if (hopOffset == NULL_HOP_OFFSET) {
Packit b55c50
    return NULL;
Packit b55c50
  }
Packit b55c50
Packit b55c50
  STATIC_ASSERT(NULL_HOP_OFFSET == 0);
Packit b55c50
  return &neighborhood[hopOffset - 1];
Packit b55c50
}
Packit b55c50
Packit b55c50
/**
Packit b55c50
 * Add a bucket into the hop list for the neighborhood, inserting it into the
Packit b55c50
 * list so the hop list remains sorted by hop offset.
Packit b55c50
 *
Packit b55c50
 * @param neighborhood  the first bucket in the neighborhood
Packit b55c50
 * @param newBucket     the bucket to add to the hop list
Packit b55c50
 **/
Packit b55c50
static void insertInHopList(Bucket *neighborhood, Bucket *newBucket)
Packit b55c50
{
Packit b55c50
  // Zero indicates a NULL hop offset, so bias the hop offset by one.
Packit b55c50
  int hopOffset = 1 + (newBucket - neighborhood);
Packit b55c50
Packit b55c50
  // Handle the special case of adding a bucket at the start of the list.
Packit b55c50
  int nextHop = neighborhood->firstHop;
Packit b55c50
  if ((nextHop == NULL_HOP_OFFSET) || (nextHop > hopOffset)) {
Packit b55c50
    newBucket->nextHop = nextHop;
Packit b55c50
    neighborhood->firstHop = hopOffset;
Packit b55c50
    return;
Packit b55c50
  }
Packit b55c50
Packit b55c50
  // Search the hop list for the insertion point that maintains the sort
Packit b55c50
  // order.
Packit b55c50
  for (;;) {
Packit b55c50
    Bucket *bucket = dereferenceHop(neighborhood, nextHop);
Packit b55c50
    nextHop = bucket->nextHop;
Packit b55c50
Packit b55c50
    if ((nextHop == NULL_HOP_OFFSET) || (nextHop > hopOffset)) {
Packit b55c50
      newBucket->nextHop = nextHop;
Packit b55c50
      bucket->nextHop = hopOffset;
Packit b55c50
      return;
Packit b55c50
    }
Packit b55c50
  }
Packit b55c50
}
Packit b55c50
Packit b55c50
/**
Packit b55c50
 * Select and return the hash bucket for a given search key.
Packit b55c50
 *
Packit b55c50
 * @param map  the map to search
Packit b55c50
 * @param key  the mapping key
Packit b55c50
 **/
Packit b55c50
static Bucket *selectBucket(const PointerMap *map, const void *key)
Packit b55c50
{
Packit b55c50
  /*
Packit b55c50
   * Scale the 32-bit hash to a bucket index by treating it as a binary
Packit b55c50
   * fraction and multiplying that by the capacity. If the hash is uniformly
Packit b55c50
   * distributed over [0 .. 2^32-1], then (hash * capacity / 2^32) should be
Packit b55c50
   * uniformly distributed over [0 .. capacity-1]. The multiply and shift is
Packit b55c50
   * much faster than a divide (modulus) on X86 CPUs.
Packit b55c50
   */
Packit b55c50
  uint64_t hash = map->hasher(key);
Packit b55c50
  return &map->buckets[(hash * map->capacity) >> 32];
Packit b55c50
}
Packit b55c50
Packit b55c50
/**
Packit b55c50
 * Search the hop list associated with given hash bucket for a given search
Packit b55c50
 * key. If the key is found, returns a pointer to the entry (bucket or
Packit b55c50
 * collision), otherwise returns NULL.
Packit b55c50
 *
Packit b55c50
 * @param [in]  map          the map being searched
Packit b55c50
 * @param [in]  bucket       the map bucket to search for the key
Packit b55c50
 * @param [in]  key          the mapping key
Packit b55c50
 * @param [out] previousPtr  if not NULL, a pointer in which to
Packit b55c50
 *                           store the bucket in the list preceding the one
Packit b55c50
 *                           that had the matching key
Packit b55c50
 *
Packit b55c50
 * @return an entry that matches the key, or NULL if not found
Packit b55c50
 **/
Packit b55c50
static Bucket *searchHopList(PointerMap  *map,
Packit b55c50
                             Bucket      *bucket,
Packit b55c50
                             const void  *key,
Packit b55c50
                             Bucket     **previousPtr)
Packit b55c50
{
Packit b55c50
  Bucket *previous = NULL;
Packit b55c50
  unsigned int nextHop = bucket->firstHop;
Packit b55c50
  while (nextHop != NULL_HOP_OFFSET) {
Packit b55c50
    // Check the neighboring bucket indexed by the offset for the desired key.
Packit b55c50
    Bucket *entry = dereferenceHop(bucket, nextHop);
Packit b55c50
    if ((entry->value != NULL) && map->comparator(key, entry->key)) {
Packit b55c50
      if (previousPtr != NULL) {
Packit b55c50
        *previousPtr = previous;
Packit b55c50
      }
Packit b55c50
      return entry;
Packit b55c50
    }
Packit b55c50
    nextHop = entry->nextHop;
Packit b55c50
    previous = entry;
Packit b55c50
  }
Packit b55c50
  return NULL;
Packit b55c50
}
Packit b55c50
Packit b55c50
/**********************************************************************/
Packit b55c50
void *pointerMapGet(PointerMap *map, const void *key)
Packit b55c50
{
Packit b55c50
  Bucket *match = searchHopList(map, selectBucket(map, key), key, NULL);
Packit b55c50
  return ((match != NULL) ? match->value : NULL);
Packit b55c50
}
Packit b55c50
Packit b55c50
/**
Packit b55c50
 * Increase the number of hash buckets and rehash all the existing entries,
Packit b55c50
 * storing them in the new buckets.
Packit b55c50
 *
Packit b55c50
 * @param map  the map to resize
Packit b55c50
 **/
Packit b55c50
static int resizeBuckets(PointerMap *map)
Packit b55c50
{
Packit b55c50
  // Copy the top-level map data to the stack.
Packit b55c50
  PointerMap oldMap = *map;
Packit b55c50
Packit b55c50
  // Re-initialize the map to be empty and 50% larger.
Packit b55c50
  size_t newCapacity = map->capacity / 2 * 3;
Packit b55c50
  logInfo("%s: attempting resize from %zu to %zu, current size=%zu",
Packit b55c50
          __func__, map->capacity, newCapacity, map->size);
Packit b55c50
  int result = allocateBuckets(map, newCapacity);
Packit b55c50
  if (result != UDS_SUCCESS) {
Packit b55c50
    *map = oldMap;
Packit b55c50
    return result;
Packit b55c50
  }
Packit b55c50
Packit b55c50
  // Populate the new hash table from the entries in the old bucket array.
Packit b55c50
  for (size_t i = 0; i < oldMap.bucketCount; i++) {
Packit b55c50
    Bucket *entry = &oldMap.buckets[i];
Packit b55c50
    if (entry->value == NULL) {
Packit b55c50
      continue;
Packit b55c50
    }
Packit b55c50
Packit b55c50
    result = pointerMapPut(map, entry->key, entry->value, true, NULL);
Packit b55c50
    if (result != UDS_SUCCESS) {
Packit b55c50
      // Destroy the new partial map and restore the map from the stack.
Packit b55c50
      freeBuckets(map);
Packit b55c50
      *map = oldMap;
Packit b55c50
      return result;
Packit b55c50
    }
Packit b55c50
  }
Packit b55c50
Packit b55c50
  // Destroy the old bucket array.
Packit b55c50
  freeBuckets(&oldMap);
Packit b55c50
  return UDS_SUCCESS;
Packit b55c50
}
Packit b55c50
Packit b55c50
/**
Packit b55c50
 * Probe the bucket array starting at the given bucket for the next empty
Packit b55c50
 * bucket, returning a pointer to it. NULL will be returned if
Packit b55c50
 * the search reaches the end of the bucket array or if the number of linear
Packit b55c50
 * probes exceeds a specified limit.
Packit b55c50
 *
Packit b55c50
 * @param map        the map containing the buckets to search
Packit b55c50
 * @param bucket     the bucket at which to start probing
Packit b55c50
 * @param maxProbes  the maximum number of buckets to search
Packit b55c50
 *
Packit b55c50
 * @return the next empty bucket, or NULL if the search failed
Packit b55c50
 **/
Packit b55c50
static Bucket *findEmptyBucket(PointerMap   *map,
Packit b55c50
                               Bucket       *bucket,
Packit b55c50
                               unsigned int  maxProbes)
Packit b55c50
{
Packit b55c50
  // Limit the search to either the nearer of the end of the bucket array or a
Packit b55c50
  // fixed distance beyond the initial bucket.
Packit b55c50
  size_t remaining = &map->buckets[map->bucketCount] - bucket;
Packit b55c50
  Bucket *sentinel = &bucket[minSizeT(remaining, maxProbes)];
Packit b55c50
Packit b55c50
  for (Bucket *entry = bucket; entry < sentinel; entry++) {
Packit b55c50
    if (entry->value == NULL) {
Packit b55c50
      return entry;
Packit b55c50
    }
Packit b55c50
  }
Packit b55c50
  return NULL;
Packit b55c50
}
Packit b55c50
Packit b55c50
/**
Packit b55c50
 * Move an empty bucket closer to the start of the bucket array. This searches
Packit b55c50
 * the neighborhoods that contain the empty bucket for a non-empty bucket
Packit b55c50
 * closer to the start of the array. If such a bucket is found, this swaps the
Packit b55c50
 * two buckets by moving the entry to the empty bucket.
Packit b55c50
 *
Packit b55c50
 * @param map   the map containing the bucket
Packit b55c50
 * @param hole  the empty bucket to fill with an entry that precedes it in one
Packit b55c50
 *              of its enclosing neighborhoods
Packit b55c50
 *
Packit b55c50
 * @return the bucket that was vacated by moving its entry to the provided
Packit b55c50
 *         hole, or NULL if no entry could be moved
Packit b55c50
 **/
Packit b55c50
static Bucket *moveEmptyBucket(PointerMap *map __attribute__((unused)),
Packit b55c50
                               Bucket     *hole)
Packit b55c50
{
Packit b55c50
  /*
Packit b55c50
   * Examine every neighborhood that the empty bucket is part of, starting
Packit b55c50
   * with the one in which it is the last bucket. No boundary check is needed
Packit b55c50
   * for the negative array arithmetic since this function is only called when
Packit b55c50
   * hole is at least NEIGHBORHOOD cells deeper into the array than a valid
Packit b55c50
   * bucket.
Packit b55c50
   */
Packit b55c50
  for (Bucket *bucket = &hole[1 - NEIGHBORHOOD]; bucket < hole; bucket++) {
Packit b55c50
    // Find the entry that is nearest to the bucket, which means it will be
Packit b55c50
    // nearest to the hash bucket whose neighborhood is full.
Packit b55c50
    Bucket *newHole = dereferenceHop(bucket, bucket->firstHop);
Packit b55c50
    if (newHole == NULL) {
Packit b55c50
      // There are no buckets in this neighborhood that are in use by this one
Packit b55c50
      // (they must all be owned by overlapping neighborhoods).
Packit b55c50
      continue;
Packit b55c50
    }
Packit b55c50
Packit b55c50
    // Skip this bucket if its first entry is actually further away than the
Packit b55c50
    // hole that we're already trying to fill.
Packit b55c50
    if (hole < newHole) {
Packit b55c50
      continue;
Packit b55c50
    }
Packit b55c50
Packit b55c50
    /*
Packit b55c50
     * We've found an entry in this neighborhood that we can "hop" further
Packit b55c50
     * away, moving the hole closer to the hash bucket, if not all the way
Packit b55c50
     * into its neighborhood.
Packit b55c50
     */
Packit b55c50
Packit b55c50
    // The entry that will be the new hole is the first bucket in the list,
Packit b55c50
    // so setting firstHop is all that's needed remove it from the list.
Packit b55c50
    bucket->firstHop = newHole->nextHop;
Packit b55c50
    newHole->nextHop = NULL_HOP_OFFSET;
Packit b55c50
Packit b55c50
    // Move the entry into the original hole.
Packit b55c50
    hole->key      = newHole->key;
Packit b55c50
    hole->value    = newHole->value;
Packit b55c50
    newHole->value = NULL;
Packit b55c50
Packit b55c50
    // Insert the filled hole into the hop list for the neighborhood.
Packit b55c50
    insertInHopList(bucket, hole);
Packit b55c50
    return newHole;
Packit b55c50
  }
Packit b55c50
Packit b55c50
  // We couldn't find an entry to relocate to the hole.
Packit b55c50
  return NULL;
Packit b55c50
}
Packit b55c50
Packit b55c50
/**
Packit b55c50
 * Find and update any existing mapping for a given key, returning the value
Packit b55c50
 * associated with the key in the provided pointer.
Packit b55c50
 *
Packit b55c50
 * @param [in]  map           the PointerMap to attempt to modify
Packit b55c50
 * @param [in]  neighborhood  the first bucket in the neighborhood that
Packit b55c50
 *                            would contain the search key
Packit b55c50
 * @param [in]  key           the key with which to associate the new value
Packit b55c50
 * @param [in]  newValue      the value to be associated with the key
Packit b55c50
 * @param [in]  update        whether to overwrite an existing value
Packit b55c50
 * @param [out] oldValuePtr   a pointer in which to store the old value
Packit b55c50
 *                            (unmodified if no mapping was found)
Packit b55c50
 *
Packit b55c50
 * @return true if the map contains a mapping for the key
Packit b55c50
 *         false if it does not
Packit b55c50
 **/
Packit b55c50
static bool updateMapping(PointerMap  *map,
Packit b55c50
                          Bucket      *neighborhood,
Packit b55c50
                          const void  *key,
Packit b55c50
                          void        *newValue,
Packit b55c50
                          bool         update,
Packit b55c50
                          void       **oldValuePtr)
Packit b55c50
{
Packit b55c50
  Bucket *bucket = searchHopList(map, neighborhood, key, NULL);
Packit b55c50
  if (bucket == NULL) {
Packit b55c50
    // There is no bucket containing the key in the neighborhood.
Packit b55c50
    return false;
Packit b55c50
  }
Packit b55c50
Packit b55c50
  // Return the value of the current mapping (if desired) and update the
Packit b55c50
  // mapping with the new value (if desired).
Packit b55c50
  if (oldValuePtr != NULL) {
Packit b55c50
    *oldValuePtr = bucket->value;
Packit b55c50
  }
Packit b55c50
  if (update) {
Packit b55c50
    // We're dropping the old key pointer on the floor here, assuming it's a
Packit b55c50
    // property of the value or that it's otherwise safe to just forget.
Packit b55c50
    bucket->key   = key;
Packit b55c50
    bucket->value = newValue;
Packit b55c50
  }
Packit b55c50
  return true;
Packit b55c50
}
Packit b55c50
Packit b55c50
/**
Packit b55c50
 * Find an empty bucket in a specified neighborhood for a new mapping or
Packit b55c50
 * attempt to re-arrange mappings so there is such a bucket. This operation
Packit b55c50
 * may fail (returning NULL) if an empty bucket is not available or could not
Packit b55c50
 * be relocated to the neighborhood.
Packit b55c50
 *
Packit b55c50
 * @param map           the PointerMap to search or modify
Packit b55c50
 * @param neighborhood  the first bucket in the neighborhood in which
Packit b55c50
 *                      an empty bucket is needed for a new mapping
Packit b55c50
 *
Packit b55c50
 * @return a pointer to an empty bucket in the desired neighborhood, or
Packit b55c50
 *         NULL if a vacancy could not be found or arranged
Packit b55c50
 **/
Packit b55c50
static Bucket *findOrMakeVacancy(PointerMap *map, Bucket *neighborhood)
Packit b55c50
{
Packit b55c50
  // Probe within and beyond the neighborhood for the first empty bucket.
Packit b55c50
  Bucket *hole = findEmptyBucket(map, neighborhood, MAX_PROBES);
Packit b55c50
Packit b55c50
  // Keep trying until the empty bucket is in the bucket's neighborhood or we
Packit b55c50
  // are unable to move it any closer by swapping it with a filled bucket.
Packit b55c50
  while (hole != NULL) {
Packit b55c50
    int distance = hole - neighborhood;
Packit b55c50
    if (distance < NEIGHBORHOOD) {
Packit b55c50
      // We've found or relocated an empty bucket close enough to the initial
Packit b55c50
      // hash bucket to be referenced by its hop vector.
Packit b55c50
      return hole;
Packit b55c50
    }
Packit b55c50
Packit b55c50
    // The nearest empty bucket isn't within the neighborhood that must
Packit b55c50
    // contain the new entry, so try to swap it with bucket that is closer.
Packit b55c50
    hole = moveEmptyBucket(map, hole);
Packit b55c50
  }
Packit b55c50
Packit b55c50
  return NULL;
Packit b55c50
}
Packit b55c50
Packit b55c50
/**********************************************************************/
Packit b55c50
int pointerMapPut(PointerMap  *map,
Packit b55c50
                  const void  *key,
Packit b55c50
                  void        *newValue,
Packit b55c50
                  bool         update,
Packit b55c50
                  void       **oldValuePtr)
Packit b55c50
{
Packit b55c50
  if (newValue == NULL) {
Packit b55c50
    return UDS_INVALID_ARGUMENT;
Packit b55c50
  }
Packit b55c50
Packit b55c50
  // Select the bucket at the start of the neighborhood that must contain any
Packit b55c50
  // entry for the provided key.
Packit b55c50
  Bucket *neighborhood = selectBucket(map, key);
Packit b55c50
Packit b55c50
  // Check whether the neighborhood already contains an entry for the key, in
Packit b55c50
  // which case we optionally update it, returning the old value.
Packit b55c50
  if (updateMapping(map, neighborhood, key, newValue, update, oldValuePtr)) {
Packit b55c50
    return UDS_SUCCESS;
Packit b55c50
  }
Packit b55c50
Packit b55c50
  /*
Packit b55c50
   * Find an empty bucket in the desired neighborhood for the new entry or
Packit b55c50
   * re-arrange entries in the map so there is such a bucket. This operation
Packit b55c50
   * will usually succeed; the loop body will only be executed on the rare
Packit b55c50
   * occasions that we have to resize the map.
Packit b55c50
   */
Packit b55c50
  Bucket *bucket;
Packit b55c50
  while ((bucket = findOrMakeVacancy(map, neighborhood)) == NULL) {
Packit b55c50
    /*
Packit b55c50
     * There is no empty bucket in which to put the new entry in the current
Packit b55c50
     * map, so we're forced to allocate a new bucket array with a larger
Packit b55c50
     * capacity, re-hash all the entries into those buckets, and try again (a
Packit b55c50
     * very expensive operation for large maps).
Packit b55c50
     */
Packit b55c50
    int result = resizeBuckets(map);
Packit b55c50
    if (result != UDS_SUCCESS) {
Packit b55c50
      return result;
Packit b55c50
    }
Packit b55c50
Packit b55c50
    // Resizing the map invalidates all pointers to buckets, so recalculate
Packit b55c50
    // the neighborhood pointer.
Packit b55c50
    neighborhood = selectBucket(map, key);
Packit b55c50
  }
Packit b55c50
Packit b55c50
  // Put the new entry in the empty bucket, adding it to the neighborhood.
Packit b55c50
  bucket->key   = key;
Packit b55c50
  bucket->value = newValue;
Packit b55c50
  insertInHopList(neighborhood, bucket);
Packit b55c50
  map->size += 1;
Packit b55c50
Packit b55c50
  // There was no existing entry, so there was no old value to be returned.
Packit b55c50
  if (oldValuePtr != NULL) {
Packit b55c50
    *oldValuePtr = NULL;
Packit b55c50
  }
Packit b55c50
  return UDS_SUCCESS;
Packit b55c50
}
Packit b55c50
Packit b55c50
/**********************************************************************/
Packit b55c50
void *pointerMapRemove(PointerMap *map, const void *key)
Packit b55c50
{
Packit b55c50
  // Select the bucket to search and search it for an existing entry.
Packit b55c50
  Bucket *bucket = selectBucket(map, key);
Packit b55c50
  Bucket *previous;
Packit b55c50
  Bucket *victim = searchHopList(map, bucket, key, &previous);
Packit b55c50
Packit b55c50
  if (victim == NULL) {
Packit b55c50
    // There is no matching entry to remove.
Packit b55c50
    return NULL;
Packit b55c50
  }
Packit b55c50
Packit b55c50
  // We found an entry to remove. Save the mapped value to return later and
Packit b55c50
  // empty the bucket.
Packit b55c50
  map->size -= 1;
Packit b55c50
  void *value   = victim->value;
Packit b55c50
  victim->value = NULL;
Packit b55c50
  victim->key   = 0;
Packit b55c50
Packit b55c50
  // The victim bucket is now empty, but it still needs to be spliced out of
Packit b55c50
  // the hop list.
Packit b55c50
  if (previous == NULL) {
Packit b55c50
    // The victim is the head of the list, so swing firstHop.
Packit b55c50
    bucket->firstHop  = victim->nextHop;
Packit b55c50
  } else {
Packit b55c50
    previous->nextHop = victim->nextHop;
Packit b55c50
  }
Packit b55c50
  victim->nextHop = NULL_HOP_OFFSET;
Packit b55c50
Packit b55c50
  return value;
Packit b55c50
}