sachikong is my 2d queue_t

This commit is contained in:
mallocnull 2017-10-17 22:01:05 +00:00
parent b1b96ba3e9
commit db39759d42
8 changed files with 152 additions and 10 deletions

View file

@ -9,19 +9,18 @@ struct {
static void ui_draw_borders();
void ui_init() {
}
void ui_refresh() {
clear();
ui_draw_borders();
}
static void ui_draw_borders() {
}
void ui_free() {

95
src/queue.c Normal file
View file

@ -0,0 +1,95 @@
#include "queue.h"
queue_t* queue_init() {
queue_t *queue = (queue_t*)malloc(sizeof(queue_t));
queue->front = NULL;
queue->back = NULL;
return queue;
}
static queue_node_t* queue_node_init() {
queue_node_t *node = (queue_node_t*)malloc(sizeof(queue_node_t));
node->next = NULL;
node->data = NULL;
return node;
}
void queue_push(queue_t *queue, void *data) {
queue_node_t *node = queue_node_init();
node->data = data;
if(queue->front == NULL)
queue->front = queue->back = node;
else
queue->back = queue->back->next = node;
}
void* queue_pop(queue_t *queue) {
if(queue_can_pop(queue) == FALSE)
return NULL;
queue_node_t *this = queue->front;
void *data = this->data;
if(queue->front == queue->back)
queue->front = queue->back = NULL;
else
queue->front = queue->front->next;
free(this);
return data;
}
BOOL queue_can_pop(queue_t *queue) {
return queue->front != NULL;
}
int queue_depth(queue_t *queue) {
queue_node_t *ptr = queue->front;
int i;
for(i = 0; ptr != NULL; ++i)
ptr = ptr->next;
return i;
}
void queue_free(queue_t *queue) {
queue_node_t *ptr = queue->front, *next;
while(ptr != NULL) {
next = ptr->next;
free(ptr);
ptr = next;
}
free(queue);
}
void queue_nodes_free(queue_t *queue) {
queue_node_t *ptr = queue->front, *next;
while(ptr != NULL) {
next = ptr->next;
if(ptr->data != NULL)
free(ptr->data);
free(ptr);
ptr = next;
}
free(queue);
}
void queue_nodes_func_free(queue_t *queue, queueffptr ffptr) {
queue_node_t *ptr = queue->front, *next;
while(ptr != NULL) {
next = ptr->next;
if(ptr->data != NULL)
(*ffptr)(ptr->data);
free(ptr);
ptr = next;
}
free(queue);
}

32
src/queue.h Normal file
View file

@ -0,0 +1,32 @@
#ifndef QUEUE_H
#define QUEUE_H
#include <stdlib.h>
#include "common.h"
typedef void(*queueffptr)(void*);
typedef struct queue_node_t queue_node_t;
struct queue_node_t {
queue_node_t *next;
void *data;
};
typedef struct queue_t queue_t;
struct queue_t {
queue_node_t *front;
queue_node_t *back;
};
queue_t* queue_init();
void queue_push(queue_t*, void*);
void* queue_pop(queue_t*);
BOOL queue_can_pop(queue_t*);
int queue_depth(queue_t*);
void queue_free(queue_t*);
void queue_nodes_free(queue_t*);
void queue_nodes_func_free(queue_t*, queueffptr);
#endif

View file

@ -2,24 +2,26 @@
struct {
BOOL running;
pthread_t *thread;
pthread_t thread;
} ctx;
void server_context() {
void* server_context(void *params) {
while(ctx.running == TRUE) {
}
return NULL;
}
void server_context_start() {
if(ctx.running == TRUE)
return;
pthread_create(ctx.thread, NULL, server_context, NULL);
pthread_create(&ctx.thread, NULL, &server_context, NULL);
ctx.running = TRUE;
}
void server_context_stop() {
ctx.running = FALSE;
pthread_join(ctx.thread, NULL);
}

View file

@ -1,6 +1,7 @@
#include "server.h"
void server() {
void server() {
server_context_start();
socket_t *sock = sock_server_init("6770");
sock_start(sock);
@ -18,6 +19,7 @@ void server() {
sock_recv(conn, in, 10);
printf("got %s\r\n", in);
server_context_stop();
sock_stop(sock);
sock_free(sock);
}

View file

@ -3,6 +3,7 @@
#include <stdio.h>
#include "sock.h"
#include "queue.h"
void server();

View file

@ -31,6 +31,16 @@ BOOL stack_can_pop(stack_t *stack) {
return stack->next != NULL;
}
int stack_depth(stack_t *stack) {
stack_t *ptr = stack;
int i;
for(i = -1; ptr != NULL; ++i)
ptr = ptr->next;
return i;
}
void stack_free(stack_t *stack) {
stack_t *ptr = stack, *next;
while(ptr != NULL) {

View file

@ -17,6 +17,7 @@ stack_t* stack_init();
void stack_push(stack_t*, void*);
void* stack_pop(stack_t*);
BOOL stack_can_pop(stack_t*);
int stack_depth(stack_t*);
void stack_free(stack_t*);
void stack_nodes_free(stack_t*);