FreeRTOS C-Addons  1.1.0
C-Addon functionality to FreeRTOS
zero_copy_queue.c
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2017, Michael Becker (michael.f.becker@gmail.com)
4  *
5  * This file is part of the FreeRTOS Add-ons project.
6  *
7  * Source Code:
8  * https://github.com/michaelbecker/freertos-addons
9  *
10  * Project Page:
11  * http://michaelbecker.github.io/freertos-addons/
12  *
13  * On-line Documentation:
14  * http://michaelbecker.github.io/freertos-addons/docs/html/index.html
15  *
16  * Permission is hereby granted, free of charge, to any person obtaining a
17  * copy of this software and associated documentation files
18  * (the "Software"), to deal in the Software without restriction, including
19  * without limitation the rights to use, copy, modify, merge, publish,
20  * distribute, sublicense, and/or sell copies of the Software, and to
21  * permit persons to whom the Software is furnished to do so,subject to the
22  * following conditions:
23  *
24  * + The above copyright notice and this permission notice shall be included
25  * in all copies or substantial portions of the Software.
26  * + Credit is appreciated, but not required, if you find this project
27  * useful enough to include in your application, product, device, etc.
28  *
29  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
30  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
32  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
33  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
34  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
35  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36  *
37  ***************************************************************************/
38 
39 
40 #include <stdlib.h>
41 #include "FreeRTOS.h"
42 #include "queue.h"
43 #include "mem_pool.h"
44 #include "zero_copy_queue.h"
45 
46 
47 typedef struct PvtZeroCopyQueue_t_ {
48 
50  QueueHandle_t Handle;
51 
53 
54 
56  int ItemCount,
57  int Alignment)
58 {
59  /******************************/
60  PvtZeroCopyQueue_t *zcq;
61  /******************************/
62 
63  zcq = (PvtZeroCopyQueue_t *)malloc(sizeof(PvtZeroCopyQueue_t));
64  if (zcq == NULL) {
65  return NULL;
66  }
67 
68  zcq->Handle = xQueueCreate(ItemCount, sizeof(void *));
69  if (zcq->Handle == NULL) {
70  free(zcq);
71  return NULL;
72  }
73 
74  zcq->Pool = CreateMemoryPool(ItemSize, ItemCount, Alignment);
75  if (zcq->Pool == NULL) {
76  free(zcq);
77  vQueueDelete(zcq->Handle);
78  return NULL;
79  }
80 
81  return (ZeroCopyQueue_t)zcq;
82 }
83 
84 
86 {
87  /******************************/
88  PvtZeroCopyQueue_t *zcq;
89  void *Item;
90  /******************************/
91 
92  zcq = (PvtZeroCopyQueue_t *)z;
93 
94  Item = MemoryPoolAllocate(zcq->Pool);
95 
96  return Item;
97 }
98 
99 
101  void *Item)
102 {
103  /******************************/
104  PvtZeroCopyQueue_t *zcq;
105  /******************************/
106 
107  zcq = (PvtZeroCopyQueue_t *)z;
108 
109  MemoryPoolFree(zcq->Pool, Item);
110 }
111 
112 
114  void *Item,
115  TickType_t Timeout)
116 {
117  /******************************/
118  PvtZeroCopyQueue_t *zcq;
119  BaseType_t success;
120  /******************************/
121 
122  zcq = (PvtZeroCopyQueue_t *)z;
123 
124  success = xQueueSendToBack(zcq->Handle, &Item, Timeout);
125 
126  return success == pdTRUE ? 1 : 0;
127 }
128 
129 
131  TickType_t Timeout)
132 {
133  /******************************/
134  PvtZeroCopyQueue_t *zcq;
135  BaseType_t success;
136  void *Item;
137  /******************************/
138 
139  zcq = (PvtZeroCopyQueue_t *)z;
140 
141  success = xQueueReceive(zcq->Handle, &Item, Timeout);
142 
143  return success == pdTRUE ? Item : NULL;
144 }
145 
146 
void * MemoryPoolAllocate(MemoryPool_t pool)
Definition: mem_pool.c:311
struct PvtZeroCopyQueue_t_ PvtZeroCopyQueue_t
void * ZeroCopyQueue_t
MemoryPool_t CreateMemoryPool(int itemSize, int itemCount, int Alignment)
Definition: mem_pool.c:140
QueueHandle_t Handle
void * ZcqDequeueItem(ZeroCopyQueue_t z, TickType_t Timeout)
void ZcqFreeItem(ZeroCopyQueue_t z, void *Item)
void * MemoryPool_t
Definition: mem_pool.h:49
void MemoryPoolFree(MemoryPool_t pool, void *memory)
Definition: mem_pool.c:338
ZeroCopyQueue_t ZcqCreateQueue(int ItemSize, int ItemCount, int Alignment)
void * ZcqAllocateItem(ZeroCopyQueue_t z)
int ZcqEnqueueItem(ZeroCopyQueue_t z, void *Item, TickType_t Timeout)