LibJuno 1.0.1
LibJuno is a lightweight C11 library designed specifically for embedded systems.
Loading...
Searching...
No Matches
minimal_queue_example.c File Reference
#include "juno/ds/array_api.h"
#include "juno/ds/queue_api.h"
#include "juno/macros.h"
#include "juno/memory/pointer_api.h"
#include "juno/module.h"
#include "juno/status.h"
#include <stddef.h>
#include <stdio.h>
#include <stdalign.h>
#include <string.h>
Include dependency graph for minimal_queue_example.c:

Data Structures

struct  USER_DATA_T
 

Typedefs

typedef struct USER_DATA_T USER_DATA_T
 

Functions

struct USER_DATA_BUFFER_T JUNO_MODULE_DERIVE (JUNO_DS_ARRAY_ROOT_T, USER_DATA_T tBuffer;[100])
 
static JUNO_STATUS_T UserDataCopy (JUNO_POINTER_T tDest, JUNO_POINTER_T tSrc)
 
static JUNO_STATUS_T UserDataReset (JUNO_POINTER_T tPointer)
 Reset the memory at the pointer. This could mean zero-initialization.
 
static JUNO_STATUS_T UserDataSetAt (JUNO_DS_ARRAY_ROOT_T *ptArray, JUNO_POINTER_T tItem, size_t iIndex)
 Set the value at an index.
 
static JUNO_RESULT_POINTER_T UserDataGetAt (JUNO_DS_ARRAY_ROOT_T *ptArray, size_t iIndex)
 Get the value at an index.
 
static JUNO_STATUS_T UserDataRemoveAt (JUNO_DS_ARRAY_ROOT_T *ptArray, size_t iIndex)
 Remove a value at an index.
 
void ErrorHandler (JUNO_STATUS_T tStatus, const char *pcMsg, JUNO_USER_DATA_T *pvUserData)
 
int main (void)
 

Variables

const JUNO_DS_ARRAY_API_T gtUserDataArrayApi
 

Typedef Documentation

◆ USER_DATA_T

typedef struct USER_DATA_T USER_DATA_T

DOC

The Queue Example

In this example we will implement a queue. Queue's are intended to be memory safe in LibJuno so we need to implement an array for our type that tell's the queue how to get, set, and remove values. We also need to implement a pointer API for this type to tell the queue how to safely access the memory.

For Context: The pointer API answers the question: "How should LibJuno access my type safely?" The array API answers the question: "How should I index into my array safely?"

The Queue uses the answers to these two questions to implement a queue. A similar paradigm is used on the stack, map, and heap APIs.

Typically users will use the scrips/create_array.py script to generate the boilerplate for arrays and pointers.

First we will define out type, and derive an array.

Function Documentation

◆ ErrorHandler()

void ErrorHandler ( JUNO_STATUS_T  tStatus,
const char *  pcMsg,
JUNO_USER_DATA_T pvUserData 
)

DOC We will also implement an error handler. This will automatically get called when LibJuno encounters an error. In this case we will print the message and handle the case when the queue is simple empty or full.

◆ JUNO_MODULE_DERIVE()

struct USER_DATA_BUFFER_T JUNO_MODULE_DERIVE ( JUNO_DS_ARRAY_ROOT_T  ,
USER_DATA_T tBuffer;  [100] 
)

◆ main()

int main ( void  )

DOC Next we need to init the array and queue

DOC Finally we will enqueue the array until it is full. We know it's full when it returns an error.

DOC Then we will dequeue the array.

◆ UserDataCopy()

static JUNO_STATUS_T UserDataCopy ( JUNO_POINTER_T  tDest,
JUNO_POINTER_T  tSrc 
)
static

DOC We then need to implement the Copy and Reset functions for the pointer. We will verify the type matches and safely dereference the pointer to perform the copy and reset.

◆ UserDataGetAt()

static JUNO_RESULT_POINTER_T UserDataGetAt ( JUNO_DS_ARRAY_ROOT_T ptArray,
size_t  iIndex 
)
static

Get the value at an index.

◆ UserDataRemoveAt()

static JUNO_STATUS_T UserDataRemoveAt ( JUNO_DS_ARRAY_ROOT_T ptArray,
size_t  iIndex 
)
static

Remove a value at an index.

◆ UserDataReset()

static JUNO_STATUS_T UserDataReset ( JUNO_POINTER_T  tPointer)
static

Reset the memory at the pointer. This could mean zero-initialization.

◆ UserDataSetAt()

static JUNO_STATUS_T UserDataSetAt ( JUNO_DS_ARRAY_ROOT_T ptArray,
JUNO_POINTER_T  tItem,
size_t  iIndex 
)
static

Set the value at an index.

DOC Then we need to define the array access. Again we will verify the type and perform the actions using the pointer API we defined.

Variable Documentation

◆ gtUserDataArrayApi

const JUNO_DS_ARRAY_API_T gtUserDataArrayApi
Initial value:
= {
}
static JUNO_STATUS_T UserDataRemoveAt(JUNO_DS_ARRAY_ROOT_T *ptArray, size_t iIndex)
Remove a value at an index.
Definition minimal_queue_example.c:142
static JUNO_RESULT_POINTER_T UserDataGetAt(JUNO_DS_ARRAY_ROOT_T *ptArray, size_t iIndex)
Get the value at an index.
Definition minimal_queue_example.c:128
static JUNO_STATUS_T UserDataSetAt(JUNO_DS_ARRAY_ROOT_T *ptArray, JUNO_POINTER_T tItem, size_t iIndex)
Set the value at an index.
Definition minimal_queue_example.c:115