From c5706db3a74d24ebf614489470fe9a14cade1306 Mon Sep 17 00:00:00 2001 From: XerolySkinner <63062392+XerolySkinner@users.noreply.github.com> Date: Sat, 6 Aug 2022 02:29:55 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=9F=E5=88=97=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 加入了快捷队列驱动,还有仿FIFO函数处理 --- bsp_Device/bsp_Queue.c | 93 ++++++++++++++++++++++++++++++++++++++++++ bsp_Device/bsp_Queue.h | 43 +++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 bsp_Device/bsp_Queue.c create mode 100644 bsp_Device/bsp_Queue.h diff --git a/bsp_Device/bsp_Queue.c b/bsp_Device/bsp_Queue.c new file mode 100644 index 0000000..0e1c69d --- /dev/null +++ b/bsp_Device/bsp_Queue.c @@ -0,0 +1,93 @@ +/*---------------------------------------------------------------------------------------------------- +# THIS FILE IS A PART OF XerolySkinner's PROJECT +# +# THIS PROGRAM IS FREE SOFTWARE +# +# E-mail:ZABBCCCBBAZ@163.com +# QQ:2715099320 +# +# Copyright (c) 2022 XerolySkinner +# All rights reserved. +*/ + +/*---------------------------------------------------------------------------------------------------- + :bsp_Queue + ʱ:20220806-0226 + Ա:Դ + ˵:FIFO +*/ +#include "stdio.h" +#include "stdlib.h" +#include "string.h" +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +struct _Queue { + struct _Queue* front; + void* member; + struct _Queue* rear; +}; +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +struct _Queue* bsp_QueueInit(void) { + struct _Queue* list = malloc(sizeof(struct _Queue)); + if (list == NULL)return NULL; + list->front = NULL; + list->rear = NULL; + list->member = NULL; + return list;} +//////////////////////////////////////////////////////////////////////////////////////////////////// +// пռ +char bsp_QueueEmpty(struct _Queue *list) { + return (list->front == NULL && list->rear == NULL);} +//////////////////////////////////////////////////////////////////////////////////////////////////// +// βʹѹԱ +char bsp_QueuePush(struct _Queue *list,void* member) { + struct _Queue* listAdd = malloc(sizeof(struct _Queue)); + struct _Queue* listFront = list->front; + struct _Queue* listRear = list->rear; + if (list == NULL)return 1; //ֹը + if (listAdd == NULL)return 1; //ֹը + + listAdd->member = member; // + + // յλʼ + if (bsp_QueueEmpty(list)) { + list->front = list->rear = listAdd; + listAdd->front = listAdd->rear = listAdd;} + // λ + else { + listAdd->front = listRear; //½ڵβ + listRear->rear = listAdd; //βͽ½ڵ + listAdd->rear = listAdd; //βԽ + list->rear = listAdd; //β + } + return 0;} +//-------------------------------------------------------------------------------------------------- +char bsp_QueuePushCharPtr(struct _Queue* list, char* member, unsigned int len) { + char* enter = malloc(len+1); + if (enter == NULL)return 1; + enter[len] = '\0'; + strcpy_s(enter,len+1, member); + bsp_QueuePush(list,enter);} + +void bsp_QueuePushCharPtrFree(char* member) { + free(member);} +//////////////////////////////////////////////////////////////////////////////////////////////////// +// ͷԱ +void* bsp_QueuePop(struct _Queue* list) { + struct _Queue* listFront = list->front; + struct _Queue* listRear = list->rear; + void* result=NULL; + if (list == NULL)return NULL; //ֹը + if (bsp_QueueEmpty(list))return NULL; //Ѹ + + // Ѹڵ + if (listFront== listRear) { + list->front = list->rear = NULL; + result= listFront->member;} + else { + list->front = listFront->rear; + result= listFront->member;} + free(listFront); //Ĩ + return result;} +//////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/bsp_Device/bsp_Queue.h b/bsp_Device/bsp_Queue.h new file mode 100644 index 0000000..0e6dc8d --- /dev/null +++ b/bsp_Device/bsp_Queue.h @@ -0,0 +1,43 @@ +/*---------------------------------------------------------------------------------------------------- +# THIS FILE IS A PART OF XerolySkinner's PROJECT +# +# THIS PROGRAM IS FREE SOFTWARE +# +# E-mail:ZABBCCCBBAZ@163.com +# QQ:2715099320 +# +# Copyright (c) 2022 XerolySkinner +# All rights reserved. +*/ + +/*---------------------------------------------------------------------------------------------------- + :bsp_Queue + ʱ:20220806-0226 + Ա:Դ + ˵:FIFO +*/ +#ifndef _BSP_QUEUE +#define _BSP_QUEUE +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// ѭֱִȫ +#define bsp_QueuePopRunLoopVoid(Queue) \ + do{ \ + while(!bsp_QueueEmpty(Queue)) \ + ((void (*)(void))bsp_QueuePop(Queue))(); \ + }while(0) +// ִе +#define bsp_QueuePopRunIfVoid(Queue) \ + do{ \ + if(!bsp_QueueEmpty(Queue)) \ + ((void (*)(void))bsp_QueuePop(Queue))(); \ + }while(0) +// öд +struct _Queue* bsp_QueueInit(void); +char bsp_QueueEmpty(struct _Queue* list); +char bsp_QueuePush(struct _Queue* list, void* member); +void* bsp_QueuePop(struct _Queue* list); +void bsp_QueuePushCharPtrFree(char* member); +char bsp_QueuePushCharPtr(struct _Queue* list, char* member, unsigned int len); +//////////////////////////////////////////////////////////////////////////////////////////////////// +#endif