Blame source/vdo/base/pointerMap.h

Packit Service d40955
/*
Packit Service d40955
 * Copyright (c) 2020 Red Hat, Inc.
Packit Service d40955
 *
Packit Service d40955
 * This program is free software; you can redistribute it and/or
Packit Service d40955
 * modify it under the terms of the GNU General Public License
Packit Service d40955
 * as published by the Free Software Foundation; either version 2
Packit Service d40955
 * of the License, or (at your option) any later version.
Packit Service d40955
 * 
Packit Service d40955
 * This program is distributed in the hope that it will be useful,
Packit Service d40955
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service d40955
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service d40955
 * GNU General Public License for more details.
Packit Service d40955
 * 
Packit Service d40955
 * You should have received a copy of the GNU General Public License
Packit Service d40955
 * along with this program; if not, write to the Free Software
Packit Service d40955
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit Service d40955
 * 02110-1301, USA. 
Packit Service d40955
 *
Packit Service d40955
 * $Id: //eng/vdo-releases/aluminum/src/c++/vdo/base/pointerMap.h#1 $
Packit Service d40955
 */
Packit Service d40955
Packit Service d40955
#ifndef POINTER_MAP_H
Packit Service d40955
#define POINTER_MAP_H
Packit Service d40955
Packit Service d40955
#include "common.h"
Packit Service d40955
Packit Service d40955
/**
Packit Service d40955
 * PointerMap associates pointer values (void *) with the data
Packit Service d40955
 * referenced by pointer keys (void *). NULL pointer
Packit Service d40955
 * values are not supported. A NULL key value is supported when
Packit Service d40955
 * the instance's key comparator and hasher functions support it.
Packit Service d40955
 *
Packit Service d40955
 * The map is implemented as hash table, which should provide constant-time
Packit Service d40955
 * insert, query, and remove operations, although the insert may occasionally
Packit Service d40955
 * grow the table, which is linear in the number of entries in the map. The
Packit Service d40955
 * table will grow as needed to hold new entries, but will not shrink as
Packit Service d40955
 * entries are removed.
Packit Service d40955
 *
Packit Service d40955
 * The key and value pointers passed to the map are retained and used by the
Packit Service d40955
 * map, but are not owned by the map. Freeing the map does not attempt to free
Packit Service d40955
 * the pointers. The client is entirely responsible for the memory managment
Packit Service d40955
 * of the keys and values. The current interface and implementation assume
Packit Service d40955
 * that keys will be properties of the values, or that keys will not be memory
Packit Service d40955
 * managed, or that keys will not need to be freed as a result of being
Packit Service d40955
 * replaced when a key is re-mapped.
Packit Service d40955
 **/
Packit Service d40955
Packit Service d40955
typedef struct pointerMap PointerMap;
Packit Service d40955
Packit Service d40955
/**
Packit Service d40955
 * The prototype of functions that compare the referents of two pointer keys
Packit Service d40955
 * for equality. If two keys are equal, then both keys must have the same the
Packit Service d40955
 * hash code associated with them by the hasher function defined below.
Packit Service d40955
Packit Service d40955
 * @param thisKey  The first element to compare
Packit Service d40955
 * @param thatKey  The second element to compare
Packit Service d40955
 *
Packit Service d40955
 * @return true if and only if the referents of the two
Packit Service d40955
 *         key pointers are to be treated as the same key by the map
Packit Service d40955
 **/
Packit Service d40955
typedef bool PointerKeyComparator(const void *thisKey, const void *thatKey);
Packit Service d40955
Packit Service d40955
/**
Packit Service d40955
 * The prototype of functions that get or calculate a hash code associated
Packit Service d40955
 * with the referent of pointer key. The hash code must be uniformly
Packit Service d40955
 * distributed over all uint32_t values. The hash code associated with a given
Packit Service d40955
 * key must not change while the key is in the map. If the comparator function
Packit Service d40955
 * says two keys are equal, then this function must return the same hash code
Packit Service d40955
 * for both keys. This function may be called many times for a key while an
Packit Service d40955
 * entry is stored for it in the map.
Packit Service d40955
 *
Packit Service d40955
 * @param key  The pointer key to hash
Packit Service d40955
 *
Packit Service d40955
 * @return the hash code for the key
Packit Service d40955
 **/
Packit Service d40955
typedef uint32_t PointerKeyHasher(const void *key);
Packit Service d40955
Packit Service d40955
/**
Packit Service d40955
 * Allocate and initialize a PointerMap.
Packit Service d40955
 *
Packit Service d40955
 * @param [in]  initialCapacity  The number of entries the map should
Packit Service d40955
 *                               initially be capable of holding (zero tells
Packit Service d40955
 *                               the map to use its own small default)
Packit Service d40955
 * @param [in]  initialLoad      The load factor of the map, expressed as an
Packit Service d40955
 *                               integer percentage (typically in the range
Packit Service d40955
 *                               50 to 90, with zero telling the map to use
Packit Service d40955
 *                               its own default)
Packit Service d40955
 * @param [in]  comparator       The function to use to compare the referents
Packit Service d40955
 *                               of two pointer keys for equality
Packit Service d40955
 * @param [in]  hasher           The function to use obtain the hash code
Packit Service d40955
 *                               associated with each pointer key
Packit Service d40955
 * @param [out] mapPtr           A pointer to hold the new PointerMap
Packit Service d40955
 *
Packit Service d40955
 * @return UDS_SUCCESS or an error code
Packit Service d40955
 **/
Packit Service d40955
int makePointerMap(size_t                 initialCapacity,
Packit Service d40955
                   unsigned int           initialLoad,
Packit Service d40955
                   PointerKeyComparator   comparator,
Packit Service d40955
                   PointerKeyHasher       hasher,
Packit Service d40955
                   PointerMap           **mapPtr)
Packit Service d40955
  __attribute__((warn_unused_result));
Packit Service d40955
Packit Service d40955
/**
Packit Service d40955
 * Free a PointerMap and null out the reference to it. NOTE: The map does not
Packit Service d40955
 * own the pointer keys and values stored in the map and they are not freed by
Packit Service d40955
 * this call.
Packit Service d40955
 *
Packit Service d40955
 * @param [in,out] mapPtr  The reference to the PointerMap to free
Packit Service d40955
 **/
Packit Service d40955
void freePointerMap(PointerMap **mapPtr);
Packit Service d40955
Packit Service d40955
/**
Packit Service d40955
 * Get the number of entries stored in a PointerMap.
Packit Service d40955
 *
Packit Service d40955
 * @param map  The PointerMap to query
Packit Service d40955
 *
Packit Service d40955
 * @return the number of entries in the map
Packit Service d40955
 **/
Packit Service d40955
size_t pointerMapSize(const PointerMap *map);
Packit Service d40955
Packit Service d40955
/**
Packit Service d40955
 * Retrieve the value associated with a given key from the PointerMap.
Packit Service d40955
 *
Packit Service d40955
 * @param map  The PointerMap to query
Packit Service d40955
 * @param key  The key to look up (may be NULL if the
Packit Service d40955
 *             comparator and hasher functions support it)
Packit Service d40955
 *
Packit Service d40955
 * @return the value associated with the given key, or NULL
Packit Service d40955
 *         if the key is not mapped to any value
Packit Service d40955
 **/
Packit Service d40955
void *pointerMapGet(PointerMap *map, const void *key);
Packit Service d40955
Packit Service d40955
/**
Packit Service d40955
 * Try to associate a value (a pointer) with an integer in a PointerMap.
Packit Service d40955
 * If the map already contains a mapping for the provided key, the old value is
Packit Service d40955
 * only replaced with the specified value if update is true. In either case
Packit Service d40955
 * the old value is returned. If the map does not already contain a value for
Packit Service d40955
 * the specified key, the new value is added regardless of the value of update.
Packit Service d40955
 *
Packit Service d40955
 * If the value stored in the map is updated, then the key stored in the map
Packit Service d40955
 * will also be updated with the key provided by this call. The old key will
Packit Service d40955
 * not be returned due to the memory managment assumptions described in the
Packit Service d40955
 * interface header comment.
Packit Service d40955
 *
Packit Service d40955
 * @param [in]  map          The PointerMap to attempt to modify
Packit Service d40955
 * @param [in]  key          The key with which to associate the new value
Packit Service d40955
 *                           (may be NULL if the comparator and
Packit Service d40955
 *                           hasher functions support it)
Packit Service d40955
 * @param [in]  newValue     The value to be associated with the key
Packit Service d40955
 * @param [in]  update       Whether to overwrite an existing value
Packit Service d40955
 * @param [out] oldValuePtr  A pointer in which to store either the old value
Packit Service d40955
 *                           (if the key was already mapped) or
Packit Service d40955
 *                           NULL if the map did not contain the
Packit Service d40955
 *                           key; NULL may be provided if the
Packit Service d40955
 *                           caller does not need to know the old value
Packit Service d40955
 *
Packit Service d40955
 * @return UDS_SUCCESS or an error code
Packit Service d40955
 **/
Packit Service d40955
int pointerMapPut(PointerMap  *map,
Packit Service d40955
                  const void  *key,
Packit Service d40955
                  void        *newValue,
Packit Service d40955
                  bool         update,
Packit Service d40955
                  void       **oldValuePtr)
Packit Service d40955
  __attribute__((warn_unused_result));
Packit Service d40955
Packit Service d40955
/**
Packit Service d40955
 * Remove the mapping for a given key from the PointerMap.
Packit Service d40955
 *
Packit Service d40955
 * @param map  The PointerMap from which to remove the mapping
Packit Service d40955
 * @param key  The key whose mapping is to be removed (may be NULL
Packit Service d40955
 *             if the comparator and hasher functions support it)
Packit Service d40955
 *
Packit Service d40955
 * @return the value that was associated with the key, or
Packit Service d40955
 *         NULL if it was not mapped
Packit Service d40955
 **/
Packit Service d40955
void *pointerMapRemove(PointerMap *map, const void *key);
Packit Service d40955
Packit Service d40955
#endif /* POINTER_MAP_H */