Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "linkedlist.h"
- typedef struct netobj
- {
- int id;
- int socket;
- int status;
- in_addr_t ip_addr;
- }netobj;
- typedef struct node
- {
- netobj obj;
- struct node *next;
- }node;
- node *head;
- node *tail;
- pthread_mutex_t llist_mutex = PTHREAD_MUTEX_INITIALIZER;
- void init_llist()
- {
- head = (node*)malloc(sizeof(node));
- (head->obj).id = 0;
- (head->obj).socket = 0;
- (head->obj).status = 0;
- (head->obj).ip_addr = 0;
- tail = (node*)malloc(sizeof(node));
- (tail->obj).id = 0;
- (tail->obj).socket = 0;
- (tail->obj).status = 0;
- (tail->obj).ip_addr = 0;
- tail->next = 0;
- head->next = tail;
- }
- node *init_node(netobj obj)
- {
- node *ptr;
- ptr = (node*)calloc(1, sizeof(node));
- if(ptr == 0)
- {
- return (node*)0;
- }
- else
- {
- (ptr->obj).id = obj.id;
- (ptr->obj).socket = obj.socket;
- (ptr->obj).status = obj.status;
- (ptr->obj).ip_addr = obj.ip_addr;
- }
- return ptr;
- }
- void add_llist(netobj obj)
- {
- pthread_mutex_lock(&llist_mutex);
- node *new = init_node(obj);
- node *it = head;
- if(head == 0)
- {
- head = new;
- }
- else if(head->next == 0)
- {
- head->next = new;
- }
- else
- {
- while(1)
- {
- if(it->next == 0)
- {
- new->next = tail;
- it->next = new;
- break;
- }
- it = it->next;
- }
- }
- pthread_mutex_unlock(&llist_mutex);
- }
- node *find_node_llist(netobj obj)
- {
- pthread_mutex_lock(&llist_mutex);
- node *it = head;
- while((it->obj).id != obj.id && (it->obj).ip_addr == obj.ip_addr)
- {
- it = it->next;
- if(it == 0)
- {
- break;
- }
- }
- pthread_mutex_unlock(&llist_mutex);
- return it;
- }
- int find_llist(netobj obj)
- {
- pthread_mutex_lock(&llist_mutex);
- node *it = head;
- while(it != 0)
- {
- if((it->obj).id == obj.id && (it->obj).ip_addr == obj.ip_addr
- && (it->obj).socket == obj.socket)
- {
- pthread_mutex_unlock(&llist_mutex);
- return 1;
- }
- it = it->next;
- }
- pthread_mutex_unlock(&llist_mutex);
- return 0;
- }
- int rm_llist(netobj obj)
- {
- pthread_mutex_lock(&llist_mutex);
- node *rmnode = find_node_llist(obj);
- if(rmnode == 0)
- {
- pthread_mutex_unlock(&llist_mutex);
- return -1;
- }
- else
- {
- node *tmp, *prev;
- tmp = rmnode;
- prev = head;
- if(tmp == prev)
- {
- head = head->next;
- if(tail == tmp)
- {
- tail = tail->next;
- }
- free(tmp);
- }
- else
- {
- while(prev->next != tmp)
- {
- prev = prev->next;
- }
- prev->next = tmp->next;
- if(tail == tmp)
- {
- tail = prev;
- }
- }
- }
- pthread_mutex_unlock(&llist_mutex);
- return 0;
- }
- int cnt_llist()
- {
- pthread_mutex_lock(&llist_mutex);
- node *it = head;
- int i = 0;
- while(it != 0)
- {
- it = it->next;
- i++;
- }
- //printf("list count: %d\n", i - 1);
- pthread_mutex_unlock(&llist_mutex);
- return i - 1;
- }
- void print_llist()
- {
- pthread_mutex_lock(&llist_mutex);
- node *it = head;
- printf("printing list:\n");
- printf("----------\n");
- while(it != 0)
- {
- printf("%d\n", (it->obj).id);
- it = it->next;
- }
- printf("----------\n");
- pthread_mutex_unlock(&llist_mutex);
- }
Advertisement
Add Comment
Please, Sign In to add comment