Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalise zqueue to non-string data pointers, fix memory leaks #806

Merged
merged 1 commit into from Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 4 additions & 6 deletions lib/queue.c
Expand Up @@ -34,12 +34,12 @@ zqueue_t *queue_init(void)

int is_empty(zqueue_t *queue) { return queue->size == 0; }

void push_back(char *data, zqueue_t *queue)
void push_back(void *data, zqueue_t *queue)
{
znode_t *new_node = xmalloc(sizeof(znode_t));
new_node->prev = NULL;
new_node->next = NULL;
new_node->data = strdup(data);
new_node->data = data;

pthread_mutex_lock(&queue->lock);
if (is_empty(queue)) {
Expand Down Expand Up @@ -86,8 +86,7 @@ znode_t *get_front(zqueue_t *queue)
pthread_cond_wait(&queue->empty, &queue->lock);
}

znode_t *temp = xmalloc(sizeof(znode_t));
temp = queue->front;
znode_t *temp = queue->front;
pthread_mutex_unlock(&queue->lock);
return temp;
}
Expand All @@ -100,8 +99,7 @@ znode_t *get_back(zqueue_t *queue)
pthread_cond_wait(&queue->empty, &queue->lock);
}

znode_t *temp = xmalloc(sizeof(znode_t));
temp = queue->back;
znode_t *temp = queue->back;
pthread_mutex_unlock(&queue->lock);
return temp;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/queue.h
Expand Up @@ -23,7 +23,7 @@
#include <pthread.h>

typedef struct zqueue_node {
char *data;
void *data;
struct zqueue_node *prev;
struct zqueue_node *next;
} znode_t;
Expand All @@ -39,7 +39,7 @@ typedef struct zqueue {

zqueue_t *queue_init(void);
int is_empty(zqueue_t *queue);
void push_back(char *data, zqueue_t *queue);
void push_back(void *data, zqueue_t *queue);
znode_t *pop_front(zqueue_t *queue);
znode_t *pop_front_unsafe(zqueue_t *queue);
znode_t *get_front(zqueue_t *queue);
Expand Down
8 changes: 4 additions & 4 deletions src/ztee.c
Expand Up @@ -334,7 +334,7 @@ void *process_queue(void *arg)
pthread_mutex_unlock(&queue->lock);

// Write raw data to output file
fprintf(output_file, "%s", node->data);
fprintf(output_file, "%s", (char *)node->data);
fflush(output_file);
if (ferror(output_file)) {
log_fatal("ztee", "Error writing to output file");
Expand All @@ -346,11 +346,11 @@ void *process_queue(void *arg)
log_fatal("ztee", "JSON input format unimplemented");
break;
case FORMAT_CSV:
print_from_csv(node->data);
print_from_csv((char *)node->data);
break;
default:
// Handle raw
fprintf(stdout, "%s", node->data);
fprintf(stdout, "%s", (char *)node->data);
break;
}

Expand Down Expand Up @@ -383,7 +383,7 @@ void *read_in(void *arg)

// Read in from stdin and add to back of linked list
while (getline(&input, &length, stdin) > 0) {
push_back(input, queue);
push_back((void *)strdup(input), queue);

total_read_in++;
read_in_last_sec++;
Expand Down