30 #include <oc_stdlib.h> 41 #define _________________________________________TYPES_SECTION______________________________________________________________________________ 46 oC_MemorySize_t GetIndex;
47 oC_MemorySize_t PutIndex;
50 oC_MemorySize_t BufferSize;
54 #undef _________________________________________TYPES_SECTION______________________________________________________________________________ 61 #define _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________ 64 #undef _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________ 72 #define _________________________________________FUNCTIONS_SECTION__________________________________________________________________________ 94 oC_SaveIfFalse(
"Allocator" , isaddresscorrect(Allocator), oC_ErrorCode_WrongAddress )
95 && oC_SaveIfFalse(
"BufferSize", BufferSize > 0 , oC_ErrorCode_SizeNotCorrect )
98 queue = kmalloc(
sizeof(
struct Queue_t) + BufferSize, Allocator, AllocationFlags_ZeroFill );
100 if( oC_SaveIfFalse(
"queue" , queue !=
NULL, oC_ErrorCode_AllocationError ) )
102 queue->UsedSlotsSemaphore = oC_Semaphore_New(BufferSize, 0, Allocator, AllocationFlags_Default);
103 queue->FreeSlotsSemaphore = oC_Semaphore_New(BufferSize, BufferSize, Allocator, AllocationFlags_Default);
106 oC_SaveIfFalse(
"used slots semaphore", queue->UsedSlotsSemaphore !=
NULL, oC_ErrorCode_AllocationError )
107 && oC_SaveIfFalse(
"free slots semaphore", queue->FreeSlotsSemaphore !=
NULL, oC_ErrorCode_AllocationError )
111 queue->BufferSize = BufferSize;
117 if(queue->UsedSlotsSemaphore !=
NULL)
119 oC_SaveIfFalse(
"used slots", oC_Semaphore_Delete(&queue->UsedSlotsSemaphore,0), oC_ErrorCode_ReleaseError );
121 if(queue->FreeSlotsSemaphore !=
NULL)
123 oC_SaveIfFalse(
"free slots", oC_Semaphore_Delete(&queue->FreeSlotsSemaphore,0), oC_ErrorCode_ReleaseError );
125 oC_SaveIfFalse(
"queue" , kfree(queue,AllocationFlags_Default), oC_ErrorCode_ReleaseError );
136 bool oC_Queue_Delete( oC_Queue_t * Queue )
138 bool deleted =
false;
141 oC_SaveIfFalse(
"Queue", isram(Queue) , oC_ErrorCode_AddressNotInRam )
142 && oC_SaveIfFalse(
"Queue", oC_Queue_IsCorrect(*Queue) , oC_ErrorCode_ObjectNotCorrect )
146 bool semaphore1deleted = oC_Semaphore_Delete(&queue->FreeSlotsSemaphore, 0);
147 bool semaphore2deleted = oC_Semaphore_Delete(&queue->UsedSlotsSemaphore, 0);
148 bool mainReleased = kfree( queue, AllocationFlags_Default );
150 deleted = oC_SaveIfFalse(
"Free slots semaphore", semaphore1deleted , oC_ErrorCode_ReleaseError)
151 && oC_SaveIfFalse(
"Used slots semaphore", semaphore2deleted , oC_ErrorCode_ReleaseError)
152 && oC_SaveIfFalse(
"Main object memory" , mainReleased , oC_ErrorCode_ReleaseError);
160 bool oC_Queue_IsCorrect( oC_Queue_t Queue )
166 bool oC_Queue_Put( oC_Queue_t Queue ,
const void * Data , uint32_t Size , oC_Time_t Timeout )
168 bool dataPushed =
false;
172 oC_SaveIfFalse(
"Queue" , oC_Queue_IsCorrect(Queue), oC_ErrorCode_ObjectNotCorrect )
173 && oC_SaveIfFalse(
"data" , isaddresscorrect(Data) , oC_ErrorCode_WrongAddress )
174 && oC_SaveIfFalse(
"Size" , Size > 0 , oC_ErrorCode_SizeNotCorrect )
175 && oC_SaveIfFalse(
"Timeout", Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
178 const uint8_t * buffer = Data;
180 if(oC_Semaphore_TakeCounting(queue->FreeSlotsSemaphore,Size,Timeout))
182 oC_IntMan_EnterCriticalSection();
184 for(oC_MemorySize_t i = 0; i < Size; i++)
186 queue->Buffer[queue->PutIndex++] = buffer[i];
187 if(queue->PutIndex >= queue->BufferSize)
192 oC_Semaphore_GiveCounting(queue->UsedSlotsSemaphore,Size);
194 oC_IntMan_ExitCriticalSection();
203 bool oC_Queue_Get( oC_Queue_t Queue ,
void * outData , uint32_t Size , oC_Time_t Timeout )
205 bool dataReady =
false;
209 oC_SaveIfFalse(
"Queue" , oC_Queue_IsCorrect(Queue) , oC_ErrorCode_ObjectNotCorrect )
210 && oC_SaveIfFalse(
"outData", isram(outData) || outData ==
NULL , oC_ErrorCode_OutputAddressNotInRAM )
211 && oC_SaveIfFalse(
"Size" , Size > 0 , oC_ErrorCode_SizeNotCorrect )
212 && oC_SaveIfFalse(
"Timeout", Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
215 uint8_t * outBuffer = outData;
217 if(oC_Semaphore_TakeCounting(queue->UsedSlotsSemaphore,Size,Timeout))
219 oC_IntMan_EnterCriticalSection();
221 for(oC_MemorySize_t i = 0; i < Size; i++)
223 if(outBuffer !=
NULL)
225 outBuffer[i] = queue->Buffer[queue->GetIndex];
228 if(queue->GetIndex >= queue->BufferSize)
233 oC_Semaphore_GiveCounting(queue->FreeSlotsSemaphore,Size);
235 oC_IntMan_ExitCriticalSection();
244 #undef _________________________________________FUNCTIONS_SECTION__________________________________________________________________________ The file with interface for thread managing.
identifier for allocations
The file with helper macros for managing objects.
uint32_t oC_ObjectControl_t
stores object control value
oC_Queue_t oC_Queue_New(Allocator_t Allocator, oC_MemorySize_t BufferSize)
allocates memory for a queue
The file with list library.
The file with interface for interrupt manager.
static oC_ObjectControl_t oC_CountObjectControl(void *ObjectPointer, oC_ObjectId_t ObjectId)
counts object control for object
static bool oC_CheckObjectControl(void *ObjectPointer, oC_ObjectId_t ObjectId, oC_ObjectControl_t ObjectControl)
checks if object control is correct
The file with interface for Queue module.
Definition of the null pointer.
static const oC_Allocator_t Allocator
The file with interface for semaphores.
#define NULL
pointer to a zero