Skip to content

Commit

Permalink
add init and deinit generic function for the peer structure
Browse files Browse the repository at this point in the history
They are defined as global variable initialized as:
peer_deinit = peer_deinit_data;
peer_init = peer_init_data;
  • Loading branch information
lucabaldesi committed Sep 11, 2017
1 parent 0c6efcd commit b79c8e3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
4 changes: 3 additions & 1 deletion include/peer.h
Expand Up @@ -2,8 +2,8 @@
* @file: peer.h
* @brief Peer structure definition.
* @author: Alessandro Russo <russo@disi.unitn.it>
* @author: Luca Baldesi <luca.baldesi@unitn.it>
*
* @date December 15, 2009, 2:09 PM
*/

#ifndef _PEER_H
Expand All @@ -25,5 +25,7 @@ struct peer {
void * user_data;
};

typedef void (*peer_deinit_f)(struct peer *p);
typedef void (*peer_init_f)(struct peer *p);

#endif /* _PEER_H */
35 changes: 25 additions & 10 deletions src/PeerSet/peerset_ops.c
Expand Up @@ -20,6 +20,26 @@

#define DEFAULT_SIZE_INCREMENT 32

void peer_init_data(struct peer *p)
{
if (p)
{
p->metadata = NULL;
p->user_data = NULL;
}
}

void peer_deinit_data(struct peer *p)
{
if(p && p->metadata)
free(p->metadata);
if(p && p->metadata)
free(p->user_data);
}

peer_deinit_f peer_deinit = peer_deinit_data;
peer_init_f peer_init = peer_init_data;

static int nodeid_peer_cmp(const void *id, const void *p)
{
const struct peer *peer = *(struct peer *const *)p;
Expand Down Expand Up @@ -156,8 +176,7 @@ int peerset_add_peer(struct peerset *h,const struct nodeID *id)
h->elements[pos] = e;
gettimeofday(&e->creation_timestamp, NULL);
e->id = nodeid_dup(id);
e->metadata = NULL;
e->user_data = NULL;
peer_init(e);

return h->n_elements;
}
Expand Down Expand Up @@ -203,10 +222,8 @@ int peerset_remove_peer(struct peerset *h, const struct nodeID *id){
if (i >= 0) {
struct peer *e = h->elements[i];
nodeid_free(e->id);
if (e->metadata)
free(e->metadata);
if (e->user_data)
free(e->user_data);
if (peer_deinit)
peer_deinit(e);
memmove(&h->elements[i], &h->elements[i+1], ((h->n_elements--) - (i+1)) * sizeof(struct peer *));
free(e);

Expand All @@ -231,10 +248,8 @@ void peerset_clear(struct peerset *h, int size)
for (i = 0; i < h->n_elements; i++) {
struct peer *e = h->elements[i];
nodeid_free(e->id);
if (e->metadata)
free(e->metadata);
if (e->user_data)
free(e->user_data);
if (peer_deinit)
peer_deinit(e);
free(e);
}

Expand Down

0 comments on commit b79c8e3

Please sign in to comment.