From 4e571e82b5183d1b372d469918f78b952f272149 Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Sun, 21 Aug 2022 12:40:55 +0100 Subject: [PATCH] Add a basic ring queue impl --- src/queue.c | 42 ++++++++++++++++++++++++++++++++++++++ src/queue.h | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/queue.c create mode 100644 src/queue.h diff --git a/src/queue.c b/src/queue.c new file mode 100644 index 0000000..964ef86 --- /dev/null +++ b/src/queue.c @@ -0,0 +1,42 @@ +/* + * Feynman -- Wayland compositor for GNU Emacs + * + * Copyright (c) 2022 Sameer Rahmani + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "queue.h" + +#include "utils.h" + +queue_t * +init_queue (unsigned size) +{ + event_t *data = (event_t *)malloc (sizeof (event_t) * size); + queue_t *e = (queue_t *)malloc (sizeof (queue_t)); + + e->size = size; + e->events = data; + e->head = NULL; + e->tail = NULL; + + return e; +}; + +void +deinit_queue (queue_t *q) +{ + free (q->events); + free (q); +}; diff --git a/src/queue.h b/src/queue.h new file mode 100644 index 0000000..571bf81 --- /dev/null +++ b/src/queue.h @@ -0,0 +1,59 @@ +/* + * Feynman -- Wayland compositor for GNU Emacs + * + * Copyright (c) 2022 Sameer Rahmani + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef FEYNMAN_QUEUE_H +#define FEYNMAN_QUEUE_H + +#include +#include + +typedef enum event_enum +{ + echo = 0, + exit = 1, +} event_type_t; + +typedef struct arguments +{ + unsigned count; + void **args; +} arguments_t; + +typedef struct event +{ + unsigned id; + event_type_t event_type; + arguments_t *args; +} event_t; + +typedef struct queue +{ + unsigned *head; + unsigned *tail; + unsigned size; + event_t *events; +} queue_t; + +queue_t *init_queue (unsigned size); +void deinit_queue (queue_t *q); + +int enqueue_event (queue_t *q, event_t *event); +event_t *pop_event (queue_t *q); +bool is_queue_empty (queue_t *q); + +#endif