From: Florian Forster Date: Mon, 20 Dec 2010 08:34:55 +0000 (+0100) Subject: src/sn_comparator.[ch]: Add a user data member. X-Git-Tag: v1.0.0~16 X-Git-Url: https://git.octo.it/?p=sort-networks.git;a=commitdiff_plain;h=664f1e7c27a7bcea7ebb5a4329087bad22a87a01 src/sn_comparator.[ch]: Add a user data member. --- diff --git a/src/sn_comparator.c b/src/sn_comparator.c index 18361e4..a819576 100644 --- a/src/sn_comparator.c +++ b/src/sn_comparator.c @@ -42,12 +42,17 @@ sn_comparator_t *sn_comparator_create (int min, int max) c->min = min; c->max = max; + c->user_data = NULL; + c->free_func = NULL; return (c); } /* sn_comparator_t *sn_comparator_create */ void sn_comparator_destroy (sn_comparator_t *c) { + if (c->free_func != NULL) + c->free_func (c->user_data); + if (c != NULL) free (c); } /* void sn_comparator_destroy */ diff --git a/src/sn_comparator.h b/src/sn_comparator.h index db46982..6e3f7f5 100644 --- a/src/sn_comparator.h +++ b/src/sn_comparator.h @@ -35,6 +35,8 @@ struct sn_comparator_s { int min; /**< Index of the line onto which the smaller element will be put. */ int max; /**< Index of the line onto which the larger element will be put. */ + void *user_data; /**< Pointer to user data. */ + void (*free_func) (void *); /**< Pointer to a function used to free the user data pointer. */ }; typedef struct sn_comparator_s sn_comparator_t; @@ -47,6 +49,11 @@ typedef struct sn_comparator_s sn_comparator_t; /** Returns the index of the line onto which the larger element will be put. */ #define SN_COMP_MAX(c) (c)->max +/** Expands to the user data pointer. */ +#define SN_COMP_USER_DATA(c) (c)->user_data +/** Expands to the free-function pointer. */ +#define SN_COMP_FREE_FUNC(c) (c)->free_func + /** * Allocates, initializes and returns a new comparator object. The returned * pointer must be freed by sn_comparator_destroy(). diff --git a/src/sn_stage.c b/src/sn_stage.c index 8288415..ae61578 100644 --- a/src/sn_stage.c +++ b/src/sn_stage.c @@ -166,8 +166,13 @@ sn_stage_t *sn_stage_clone (const sn_stage_t *s) return (NULL); } - memcpy (s_copy->comparators, s->comparators, - s->comparators_num * sizeof (sn_comparator_t)); + for (i = 0; i < s->comparators_num; i++) + { + SN_COMP_MIN (s_copy->comparators + i) = SN_COMP_MIN (s->comparators + i); + SN_COMP_MAX (s_copy->comparators + i) = SN_COMP_MAX (s->comparators + i); + SN_COMP_USER_DATA (s_copy->comparators + i) = NULL; + SN_COMP_FREE_FUNC (s_copy->comparators + i) = NULL; + } s_copy->comparators_num = s->comparators_num; return (s_copy);