Blame dict.h

Packit 0021fb
/*
Packit 0021fb
 * This file is part of ltrace.
Packit 0021fb
 * Copyright (C) 2012, 2013 Petr Machata, Red Hat Inc.
Packit 0021fb
 *
Packit 0021fb
 * This program is free software; you can redistribute it and/or
Packit 0021fb
 * modify it under the terms of the GNU General Public License as
Packit 0021fb
 * published by the Free Software Foundation; either version 2 of the
Packit 0021fb
 * License, or (at your option) any later version.
Packit 0021fb
 *
Packit 0021fb
 * This program is distributed in the hope that it will be useful, but
Packit 0021fb
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 0021fb
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 0021fb
 * General Public License for more details.
Packit 0021fb
 *
Packit 0021fb
 * You should have received a copy of the GNU General Public License
Packit 0021fb
 * along with this program; if not, write to the Free Software
Packit 0021fb
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
Packit 0021fb
 * 02110-1301 USA
Packit 0021fb
 */
Packit 0021fb
Packit 0021fb
#ifndef _DICT_H_
Packit 0021fb
#define _DICT_H_
Packit 0021fb
Packit 0021fb
#include <stddef.h>
Packit 0021fb
#include <assert.h>
Packit 0021fb
#include "vect.h"
Packit 0021fb
Packit 0021fb
struct dict {
Packit 0021fb
	/* The invariant is that KEYS, VALUES and STATUS are of the
Packit 0021fb
	 * same size.  */
Packit 0021fb
	struct vect keys;
Packit 0021fb
	struct vect values;
Packit 0021fb
	struct vect status;
Packit 0021fb
	size_t size;
Packit 0021fb
Packit 0021fb
	size_t (*hash1)(const void *);
Packit 0021fb
	int (*eq)(const void *, const void *);
Packit 0021fb
	size_t (*hash2)(size_t);
Packit 0021fb
};
Packit 0021fb
Packit 0021fb
/* Initialize a dictionary DICT.  The dictionary will hold keys of the
Packit 0021fb
 * size KEY_SIZE and values of the size VALUE_SIZE.  HASH1 and HASH2
Packit 0021fb
 * are, respectively, primary and secondary hashing functions.  The
Packit 0021fb
 * latter may be NULL, in which case a default internal hash is used.
Packit 0021fb
 * EQ is a callback for comparing two keys.  */
Packit 0021fb
void dict_init(struct dict *dict,
Packit 0021fb
	       size_t key_size, size_t value_size,
Packit 0021fb
	       size_t (*hash1)(const void *),
Packit 0021fb
	       int (*eq)(const void *, const void *),
Packit 0021fb
	       size_t (*hash2)(size_t));
Packit 0021fb
Packit 0021fb
/* Wrapper around dict_init.  Initializes a dictionary DITCP which
Packit 0021fb
 * will hold keys of type KEY_TYPE and values of type VALUE_TYPE.
Packit 0021fb
 * Other arguments as above.  */
Packit 0021fb
#define DICT_INIT(DICTP, KEY_TYPE, VALUE_TYPE, HASH1, EQ, HASH2)	\
Packit 0021fb
	({								\
Packit 0021fb
		/* Check that callbacks are typed properly.  */		\
Packit 0021fb
		size_t (*_hash1_callback)(const KEY_TYPE *) = HASH1;	\
Packit 0021fb
		int (*_eq_callback)(const KEY_TYPE *, const KEY_TYPE *) = EQ; \
Packit 0021fb
		dict_init(DICTP, sizeof(KEY_TYPE), sizeof(VALUE_TYPE),	\
Packit 0021fb
			  (size_t (*)(const void *))_hash1_callback,	\
Packit 0021fb
			  (int (*)(const void *, const void *))_eq_callback, \
Packit 0021fb
			  HASH2);					\
Packit 0021fb
	})
Packit 0021fb
Packit 0021fb
/* Clone SOURCE to TARGET.  For cloning slots, CLONE_KEY and
Packit 0021fb
 * CLONE_VALUE are called.  These callbacks return 0 on success or a
Packit 0021fb
 * negative value on failure.  If any of the callbacks is NULL, the
Packit 0021fb
 * default action is simple memmove.  Returns 0 on success.  If the
Packit 0021fb
 * cloning fails for any reason, already-cloned keys and values are
Packit 0021fb
 * destroyed again by DTOR_KEY and DTOR_VALUE callbacks (if non-NULL),
Packit 0021fb
 * and the function returns a negative value.  DATA is passed to all
Packit 0021fb
 * callbacks verbatim.  */
Packit 0021fb
int dict_clone(struct dict *target, const struct dict *source,
Packit 0021fb
	       int (*clone_key)(void *tgt, const void *src, void *data),
Packit 0021fb
	       void (*dtor_key)(void *tgt, void *data),
Packit 0021fb
	       int (*clone_value)(void *tgt, const void *src, void *data),
Packit 0021fb
	       void (*dtor_value)(void *tgt, void *data),
Packit 0021fb
	       void *data);
Packit 0021fb
Packit 0021fb
/* Clone SRC_DICTP, which holds KEY_TYPE-VALUE_TYPE pairs, into
Packit 0021fb
 * TGT_DICTP.  Other arguments and return codes as above.  */
Packit 0021fb
#define DICT_CLONE(TGT_DICTP, SRC_DICTP, KEY_TYPE, VALUE_TYPE,		\
Packit 0021fb
		   CLONE_KEY, DTOR_KEY, CLONE_VALUE, DTOR_VALUE, DATA)	\
Packit 0021fb
	/* xxx GCC-ism necessary to get in the safety latches.  */	\
Packit 0021fb
	({								\
Packit 0021fb
		const struct dict *_source_d = (SRC_DICTP);		\
Packit 0021fb
		assert(_source_d->keys.elt_size == sizeof(KEY_TYPE));	\
Packit 0021fb
		assert(_source_d->values.elt_size == sizeof(VALUE_TYPE)); \
Packit 0021fb
		/* Check that callbacks are typed properly.  */		\
Packit 0021fb
		void (*_key_dtor_cb)(KEY_TYPE *, void *) = DTOR_KEY;	\
Packit 0021fb
		int (*_key_clone_cb)(KEY_TYPE *, const KEY_TYPE *,	\
Packit 0021fb
				     void *) = CLONE_KEY;		\
Packit 0021fb
		void (*_value_dtor_cb)(VALUE_TYPE *, void *) = DTOR_VALUE; \
Packit 0021fb
		int (*_value_clone_cb)(VALUE_TYPE *, const VALUE_TYPE *, \
Packit 0021fb
				       void *) = CLONE_VALUE;		\
Packit 0021fb
		dict_clone((TGT_DICTP), _source_d,			\
Packit 0021fb
			   (int (*)(void *, const void *,		\
Packit 0021fb
				    void *))_key_clone_cb,		\
Packit 0021fb
			   (void (*)(void *, void *))_key_dtor_cb,	\
Packit 0021fb
			   (int (*)(void *, const void *,		\
Packit 0021fb
				    void *))_value_clone_cb,		\
Packit 0021fb
			   (void (*)(void *, void *))_value_dtor_cb,	\
Packit 0021fb
			   (DATA));					\
Packit 0021fb
	})
Packit 0021fb
Packit 0021fb
/* Return number of key-value pairs stored in DICT.  */
Packit 0021fb
size_t dict_size(const struct dict *dict);
Packit 0021fb
Packit 0021fb
/* Emptiness predicate.  */
Packit 0021fb
int dict_empty(const struct dict *dict);
Packit 0021fb
Packit 0021fb
/* Insert into DICT a pair of KEY and VALUE.  Returns 0 if insertion
Packit 0021fb
 * was successful, a negative value on error, or a positive value if
Packit 0021fb
 * this key is already present in the table.  */
Packit 0021fb
int dict_insert(struct dict *dict, void *key, void *value);
Packit 0021fb
Packit 0021fb
/* Insert into DICT a pair of KEY and VALUE.  See dict_insert for
Packit 0021fb
 * details.  In addition, make a check whether DICTP holds elements of
Packit 0021fb
 * the right size.  */
Packit 0021fb
#define DICT_INSERT(DICTP, KEYP, VALUEP)				\
Packit 0021fb
	(assert((DICTP)->keys.elt_size == sizeof(*(KEYP))),		\
Packit 0021fb
	 assert((DICTP)->values.elt_size == sizeof(*(VALUEP))),		\
Packit 0021fb
	 dict_insert((DICTP), (KEYP), (VALUEP)))
Packit 0021fb
Packit 0021fb
/* Find in DICT a value corresponding to KEY and return a pointer to
Packit 0021fb
 * it.  Returns NULL if the key was not found.  */
Packit 0021fb
void *dict_find(struct dict *dict, const void *key);
Packit 0021fb
Packit 0021fb
/* Look into DICTP for a key *KEYP.  Return a boolean indicating
Packit 0021fb
 * whether the key was found.  */
Packit 0021fb
#define DICT_HAS_KEY(DICTP, KEYP)				\
Packit 0021fb
	(assert((DICTP)->keys.elt_size == sizeof(*(KEYP))),	\
Packit 0021fb
	 dict_find((DICTP), (KEYP)) != NULL)
Packit 0021fb
Packit 0021fb
/* Find in DICTP a value of type VALUE_TYPE corresponding to KEYP and
Packit 0021fb
 * return a pointer (VALUE_TYPE *) to it.  Returns NULL if the key was
Packit 0021fb
 * not found.  */
Packit 0021fb
#define DICT_FIND_REF(DICTP, KEYP, VALUE_TYPE)			\
Packit 0021fb
	(assert((DICTP)->keys.elt_size == sizeof(*(KEYP))),	\
Packit 0021fb
	 (VALUE_TYPE *)dict_find((DICTP), (KEYP)))
Packit 0021fb
Packit 0021fb
/* Find in DICTP a value of type VALUE_TYPE corresponding to KEYP and
Packit 0021fb
 * copy it to the memory pointed-to by VAR.  Returns 0 on success, or
Packit 0021fb
 * a negative value if the key was not found.  */
Packit 0021fb
#define DICT_FIND_VAL(DICTP, KEYP, VAR)					\
Packit 0021fb
	({								\
Packit 0021fb
		assert((DICTP)->keys.elt_size == sizeof(*(KEYP)));	\
Packit 0021fb
		assert((DICTP)->values.elt_size == sizeof((VAR)));	\
Packit 0021fb
		void *_ptr = dict_find((DICTP), (KEYP));		\
Packit 0021fb
		if (_ptr != NULL)					\
Packit 0021fb
			memcpy((VAR), _ptr, (DICTP)->values.elt_size);	\
Packit 0021fb
		_ptr != NULL ? 0 : -1;					\
Packit 0021fb
	})
Packit 0021fb
Packit 0021fb
/* Erase from DICT the entry corresponding to KEY.  Returns a negative
Packit 0021fb
 * value if the key was not found, or 0 on success.  DTOR_KEY and
Packit 0021fb
 * DTOR_VALUE, if non-NULL, are called to destroy the erased
Packit 0021fb
 * value.  */
Packit 0021fb
int dict_erase(struct dict *dict, const void *key,
Packit 0021fb
	       void (*dtor_key)(void *tgt, void *data),
Packit 0021fb
	       void (*dtor_value)(void *tgt, void *data),
Packit 0021fb
	       void *data);
Packit 0021fb
Packit 0021fb
/* Erase from DICTP a value of type VALUE_TYPE corresponding to
Packit 0021fb
 * KEYP.  */
Packit 0021fb
#define DICT_ERASE(DICTP, KEYP, VALUE_TYPE, DTOR_KEY, DTOR_VALUE, DATA) \
Packit 0021fb
	({								\
Packit 0021fb
		struct dict *_d = (DICTP);				\
Packit 0021fb
		assert(_d->keys.elt_size == sizeof(*KEYP));		\
Packit 0021fb
		assert(_d->values.elt_size == sizeof(VALUE_TYPE));	\
Packit 0021fb
		/* Check that callbacks are typed properly.  */		\
Packit 0021fb
		void (*_value_dtor_cb)(VALUE_TYPE *, void *) = DTOR_VALUE; \
Packit 0021fb
		dict_erase(_d, (KEYP), (DTOR_KEY),			\
Packit 0021fb
			   (void (*)(void *, void *))_value_dtor_cb,	\
Packit 0021fb
			   (DATA));					\
Packit 0021fb
	})
Packit 0021fb
Packit 0021fb
/* Destroy DICT.  If KEY_DTOR is non-NULL, then it's called on each
Packit 0021fb
 * key stored in DICT.  Similarly for VALUE_DTOR.  DATA is passed to
Packit 0021fb
 * DTOR's verbatim.  The memory pointed-to by DICT is not freed.  */
Packit 0021fb
void dict_destroy(struct dict *dict,
Packit 0021fb
		  void (*dtor_key)(void *tgt, void *data),
Packit 0021fb
		  void (*dtor_value)(void *tgt, void *data),
Packit 0021fb
		  void *data);
Packit 0021fb
Packit 0021fb
/* Destroy DICTP, which holds keys of type KEY_TYPE and values of type
Packit 0021fb
 * VALUE_TYPE, using DTOR.  */
Packit 0021fb
#define DICT_DESTROY(DICTP, KEY_TYPE, VALUE_TYPE, DTOR_KEY, DTOR_VALUE, DATA) \
Packit 0021fb
	do {								\
Packit 0021fb
		struct dict *_d = (DICTP);				\
Packit 0021fb
		assert(_d->keys.elt_size == sizeof(KEY_TYPE));		\
Packit 0021fb
		assert(_d->values.elt_size == sizeof(VALUE_TYPE));	\
Packit 0021fb
		/* Check that callbacks are typed properly.  */		\
Packit 0021fb
		void (*_key_dtor_cb)(KEY_TYPE *, void *) = DTOR_KEY;	\
Packit 0021fb
		void (*_value_dtor_cb)(VALUE_TYPE *, void *) = DTOR_VALUE; \
Packit 0021fb
		dict_destroy(_d, (void (*)(void *, void *))_key_dtor_cb, \
Packit 0021fb
			     (void (*)(void *, void *))_value_dtor_cb,	\
Packit 0021fb
			     (DATA));					\
Packit 0021fb
	} while (0)
Packit 0021fb
Packit 0021fb
/* Iterate through DICT.  See callback.h for notes on iteration
Packit 0021fb
 * interfaces.  Callback arguments are key, value, DATA.  Note that
Packit 0021fb
 * the iteration over DICT is more expensive than in other containers:
Packit 0021fb
 * while CB is only called for items present in the table, and is
Packit 0021fb
 * therefore O(number of elements), the iterator needs to go through
Packit 0021fb
 * all the table, which is proportional to O(size of table).
Packit 0021fb
 * START_AFTER and the returned iterator are key where the iteration
Packit 0021fb
 * stopped.  */
Packit 0021fb
void *dict_each(struct dict *dict, void *start_after,
Packit 0021fb
		enum callback_status (*cb)(void *, void *, void *), void *data);
Packit 0021fb
Packit 0021fb
#define DICT_EACH(DICTP, KEY_TYPE, VALUE_TYPE, START_AFTER, CB, DATA)	\
Packit 0021fb
	/* xxx GCC-ism necessary to get in the safety latches.  */	\
Packit 0021fb
	({								\
Packit 0021fb
		assert((DICTP)->keys.elt_size == sizeof(KEY_TYPE));	\
Packit 0021fb
		assert((DICTP)->values.elt_size == sizeof(VALUE_TYPE));	\
Packit 0021fb
		/* Check that CB is typed properly.  */			\
Packit 0021fb
		enum callback_status (*_cb)(KEY_TYPE *, VALUE_TYPE *,	\
Packit 0021fb
					    void *) = CB;		\
Packit 0021fb
		KEY_TYPE *_start_after = (START_AFTER);			\
Packit 0021fb
		(KEY_TYPE *)dict_each((DICTP), _start_after,		\
Packit 0021fb
				      (enum callback_status		\
Packit 0021fb
				       (*)(void *, void *, void *))_cb,	\
Packit 0021fb
				      (DATA));				\
Packit 0021fb
	})
Packit 0021fb
Packit 0021fb
/* A callback for hashing integers.  */
Packit 0021fb
size_t dict_hash_int(const int *key);
Packit 0021fb
Packit 0021fb
/* An equality predicate callback for integers.  */
Packit 0021fb
int dict_eq_int(const int *key1, const int *key2);
Packit 0021fb
Packit 0021fb
/* A callback for hashing NULL-terminated strings.  */
Packit 0021fb
size_t dict_hash_string(const char **key);
Packit 0021fb
Packit 0021fb
/* An equality predicate callback for strings.  */
Packit 0021fb
int dict_eq_string(const char **key1, const char **key2);
Packit 0021fb
Packit 0021fb
/* A dtor which calls 'free' on keys in a table.  */
Packit 0021fb
void dict_dtor_string(const char **key, void *data);
Packit 0021fb
Packit 0021fb
/* A cloner that calls 'strdup' on keys in a table.  */
Packit 0021fb
int dict_clone_string(const char **tgt, const char **src, void *data);
Packit 0021fb
Packit 0021fb
#endif /* _DICT_H_ */