Browse Source

queue: Fix memory leaks and reduce allocations.

Georgi Chorbadzhiyski 6 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,23 +16,20 @@
16 16
 #include "queue.h"
17 17
 
18 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 23
 		perror("queue_new: mutex_init");
24
+		free(q);
22 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 28
 		perror("queue_new: cond_init");
29
+		free(q);
27 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 35
 void queue_free(QUEUE **pq) {
@@ -42,24 +39,22 @@ void queue_free(QUEUE **pq) {
42 39
 	while (q->items > 0) {
43 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 44
 	FREE(*pq);
50 45
 }
51 46
 
52 47
 void queue_add(QUEUE *q, void *data) {
53 48
 	if (!q)
54 49
 		return;
55
-	QNODE *a_msg = malloc(sizeof(QNODE));
50
+	QNODE *a_msg = calloc(1, sizeof(QNODE));
56 51
 	if (!a_msg) {
57 52
 		perror("queue_enqueue, malloc:");
58 53
 		return;
59 54
 	}
60 55
 	a_msg->data = data;
61 56
 	a_msg->next = NULL;
62
-	pthread_mutex_lock(q->mutex);
57
+	pthread_mutex_lock(&q->mutex);
63 58
 	if (q->items == 0) { 			// special case - queue is empty 
64 59
 		q->head = a_msg;
65 60
 		q->tail = a_msg;
@@ -68,16 +63,16 @@ void queue_add(QUEUE *q, void *data) {
68 63
 		q->tail = a_msg;
69 64
 	}
70 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 70
 void *queue_get(QUEUE *q) {
76 71
 	if (!q)
77 72
 		return NULL;
78
-	pthread_mutex_lock(q->mutex);
73
+	pthread_mutex_lock(&q->mutex);
79 74
 	while (q->items==0) {
80
-		pthread_cond_wait(q->cond, q->mutex);
75
+		pthread_cond_wait(&q->cond, &q->mutex);
81 76
 		if (q->items == 0)
82 77
 			return NULL;
83 78
 	}
@@ -89,8 +84,8 @@ void *queue_get(QUEUE *q) {
89 84
 	    q->tail = NULL;
90 85
 	}
91 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 89
 	void *data = a_msg->data;
95 90
 	FREE(a_msg);
96 91
     return data;
@@ -104,7 +99,7 @@ void *queue_get_nowait(QUEUE* q) {
104 99
 
105 100
 void queue_wakeup(QUEUE *q) {
106 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,8 +22,8 @@ typedef struct QNODE {
22 22
 typedef struct QUEUE {
23 23
 	QNODE *head;
24 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 27
 	int items;				// number of messages in queue
28 28
 } QUEUE;
29 29
 

Loading…
Cancel
Save