Browse Source

queue: Fix memory leaks and reduce allocations.

Georgi Chorbadzhiyski 9 years ago
parent
commit
0dc8d87080
2 changed files with 21 additions and 26 deletions
  1. 19
    24
      queue.c
  2. 2
    2
      queue.h

+ 19
- 24
queue.c View File

16
 #include "queue.h"
16
 #include "queue.h"
17
 
17
 
18
 QUEUE *queue_new(void) {
18
 QUEUE *queue_new(void) {
19
-	pthread_mutex_t *mutex = malloc(sizeof(pthread_mutex_t));
20
-	if (pthread_mutex_init(mutex,NULL) != 0) {
19
+	QUEUE *q = calloc(1, sizeof(QUEUE));
20
+	if (!q)
21
+		return NULL;
22
+	if (pthread_mutex_init(&q->mutex,NULL) != 0) {
21
 		perror("queue_new: mutex_init");
23
 		perror("queue_new: mutex_init");
24
+		free(q);
22
 		return NULL;
25
 		return NULL;
23
 	}
26
 	}
24
-	pthread_cond_t *cond = malloc(sizeof(pthread_cond_t));
25
-	if (pthread_cond_init(cond,NULL)!=0){
27
+	if (pthread_cond_init(&q->cond,NULL)!=0){
26
 		perror("queue_new: cond_init");
28
 		perror("queue_new: cond_init");
29
+		free(q);
27
 		return NULL;
30
 		return NULL;
28
 	}
31
 	}
29
-    QUEUE *q = calloc(1, sizeof(QUEUE));
30
-	if (!q)
31
-		return NULL;
32
-    // initialize queue 
33
-    q->mutex = mutex;
34
-    q->cond  = cond;
35
-    return q;
32
+	return q;
36
 }
33
 }
37
 
34
 
38
 void queue_free(QUEUE **pq) {
35
 void queue_free(QUEUE **pq) {
42
 	while (q->items > 0) {
39
 	while (q->items > 0) {
43
 		queue_get(q);
40
 		queue_get(q);
44
 	}
41
 	}
45
-	pthread_mutex_destroy(q->mutex);
46
-	FREE(q->mutex);
47
-	pthread_cond_destroy(q->cond);
48
-	FREE(q->cond);
42
+	pthread_mutex_destroy(&q->mutex);
43
+	pthread_cond_destroy(&q->cond);
49
 	FREE(*pq);
44
 	FREE(*pq);
50
 }
45
 }
51
 
46
 
52
 void queue_add(QUEUE *q, void *data) {
47
 void queue_add(QUEUE *q, void *data) {
53
 	if (!q)
48
 	if (!q)
54
 		return;
49
 		return;
55
-	QNODE *a_msg = malloc(sizeof(QNODE));
50
+	QNODE *a_msg = calloc(1, sizeof(QNODE));
56
 	if (!a_msg) {
51
 	if (!a_msg) {
57
 		perror("queue_enqueue, malloc:");
52
 		perror("queue_enqueue, malloc:");
58
 		return;
53
 		return;
59
 	}
54
 	}
60
 	a_msg->data = data;
55
 	a_msg->data = data;
61
 	a_msg->next = NULL;
56
 	a_msg->next = NULL;
62
-	pthread_mutex_lock(q->mutex);
57
+	pthread_mutex_lock(&q->mutex);
63
 	if (q->items == 0) { 			// special case - queue is empty 
58
 	if (q->items == 0) { 			// special case - queue is empty 
64
 		q->head = a_msg;
59
 		q->head = a_msg;
65
 		q->tail = a_msg;
60
 		q->tail = a_msg;
68
 		q->tail = a_msg;
63
 		q->tail = a_msg;
69
 	}
64
 	}
70
 	q->items++;
65
 	q->items++;
71
-	pthread_cond_signal(q->cond);
72
-	pthread_mutex_unlock(q->mutex);
66
+	pthread_cond_signal(&q->cond);
67
+	pthread_mutex_unlock(&q->mutex);
73
 }
68
 }
74
 
69
 
75
 void *queue_get(QUEUE *q) {
70
 void *queue_get(QUEUE *q) {
76
 	if (!q)
71
 	if (!q)
77
 		return NULL;
72
 		return NULL;
78
-	pthread_mutex_lock(q->mutex);
73
+	pthread_mutex_lock(&q->mutex);
79
 	while (q->items==0) {
74
 	while (q->items==0) {
80
-		pthread_cond_wait(q->cond, q->mutex);
75
+		pthread_cond_wait(&q->cond, &q->mutex);
81
 		if (q->items == 0)
76
 		if (q->items == 0)
82
 			return NULL;
77
 			return NULL;
83
 	}
78
 	}
89
 	    q->tail = NULL;
84
 	    q->tail = NULL;
90
 	}
85
 	}
91
 	q->items--;
86
 	q->items--;
92
-	pthread_cond_signal(q->cond);
93
-	pthread_mutex_unlock(q->mutex);
87
+	pthread_cond_signal(&q->cond);
88
+	pthread_mutex_unlock(&q->mutex);
94
 	void *data = a_msg->data;
89
 	void *data = a_msg->data;
95
 	FREE(a_msg);
90
 	FREE(a_msg);
96
     return data;
91
     return data;
104
 
99
 
105
 void queue_wakeup(QUEUE *q) {
100
 void queue_wakeup(QUEUE *q) {
106
 	if (q) {
101
 	if (q) {
107
-		pthread_cond_signal(q->cond);
102
+		pthread_cond_signal(&q->cond);
108
 	}
103
 	}
109
 }
104
 }
110
 
105
 

+ 2
- 2
queue.h View File

22
 typedef struct QUEUE {
22
 typedef struct QUEUE {
23
 	QNODE *head;
23
 	QNODE *head;
24
 	QNODE *tail;
24
 	QNODE *tail;
25
-	pthread_mutex_t *mutex;	// queue's mutex.
26
-	pthread_cond_t  *cond;	// queue's condition variable.
25
+	pthread_mutex_t mutex;	// queue's mutex.
26
+	pthread_cond_t cond;	// queue's condition variable.
27
 	int items;				// number of messages in queue
27
 	int items;				// number of messages in queue
28
 } QUEUE;
28
 } QUEUE;
29
 
29
 

Loading…
Cancel
Save