Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
devcoons committed Mar 23, 2024
1 parent 91366a5 commit cd5be66
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions lib/lib_iqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,67 +47,71 @@
* Definition | Static Functions
******************************************************************************/


/******************************************************************************
* Definition | Public Functions
******************************************************************************/

i_status iqueue_init(iqueue_t* _queue, uint32_t _max_elements, size_t _element_size, void* _storage)
{
if (_queue != (void*)NULL)
{
memset(_storage, 0x00, _element_size * (size_t)_max_elements);
if ((_queue != NULL) && (_storage != NULL))
{
(void)memset(_storage, 0, _element_size * _max_elements);
_queue->element_size = _element_size;
_queue->max_elements = _max_elements;
_queue->first = 0;
_queue->first = (uintptr_t)NULL;
_queue->next = (uintptr_t)_storage;
_queue->storage = _storage;
return I_OK;
}
return I_ERROR;
return I_OK;
}
else
{
return I_ERROR;
}
}

void* iqueue_get_next_enqueue(iqueue_t* _queue)
{
return (void*)_queue->next;
return (_queue != NULL) ? (void *)(_queue->next) : NULL;
}

i_status iqueue_advance_next(iqueue_t* _queue)
{
if (_queue->first != _queue->next)
{
_queue->first = _queue->first == 0 ? _queue->next : _queue->first;
_queue->next = _queue->next + _queue->element_size == (uintptr_t)_queue->storage + (_queue->element_size * _queue->max_elements)
? (uintptr_t)_queue->storage : _queue->next + _queue->element_size;
uintptr_t storage_end = (uintptr_t)(_queue->storage) + (_queue->element_size * _queue->max_elements);
_queue->first = (_queue->first == 0U) ? _queue->next : _queue->first;
_queue->next = ((_queue->next + _queue->element_size) == storage_end) ? (uintptr_t)_queue->storage : (_queue->next + _queue->element_size);
return I_OK;
}
return I_FULL;
else
{
return I_FULL;
}
}

i_status iqueue_enqueue(iqueue_t* _queue, void* _element)
{
if (_queue->first != _queue->next)
{
memmove((void*)_queue->next, (void*)_element, _queue->element_size);
_queue->first = _queue->first == 0 ? _queue->next : _queue->first;
_queue->next = _queue->next + _queue->element_size == (uintptr_t)_queue->storage + (_queue->element_size * _queue->max_elements)
? (uintptr_t)_queue->storage : _queue->next + _queue->element_size;
return I_OK;
(void)memmove((void*)_queue->next, (void*)_element, _queue->element_size);
return iqueue_advance_next(_queue);
}
return I_FULL;
}

i_status iqueue_dequeue(iqueue_t* _queue, void* _element)
{
if (_queue->first != 0)
void* x = iqueue_dequeue_fast(_queue);
if (x != NULL)
{
memmove((void*)_element, (void*)_queue->first, _queue->element_size);
_queue->first = _queue->first + _queue->element_size == (uintptr_t)_queue->storage + (_queue->element_size * _queue->max_elements)
? (uintptr_t)_queue->storage : _queue->first + _queue->element_size;
_queue->first = _queue->first == _queue->next ? 0 : _queue->first;

(void)memmove((void*)_element, x, _queue->element_size);
return I_OK;
}
return I_EMPTY;
else
{
return I_EMPTY;
}
}

void* iqueue_dequeue_fast(iqueue_t* _queue)
Expand Down Expand Up @@ -137,4 +141,4 @@ i_status iqueue_size(iqueue_t* _queue, size_t* _size)

/******************************************************************************
* EOF - NO CODE AFTER THIS LINE
******************************************************************************/
******************************************************************************/

0 comments on commit cd5be66

Please sign in to comment.