Design Pattern Examples
Overview of object-oriented design patterns
Adapter_BackEnd.c File Reference

Implementation of the memory read/write functions as used in the Adapter Pattern. More...

#include "pch.h"
#include "Adapter_BackEnd.h"
Include dependency graph for Adapter_BackEnd.c:

Go to the source code of this file.

Classes

struct  MemoryBlock
 Represents a single memory block. More...
 

Macros

#define BLOCK_0_NAME   "gorp"
 
#define BLOCK_1_NAME   "baba"
 
#define BLOCK_2_NAME   "yaga"
 
#define MAX_DATA_SIZE   32
 Maximum number of 32-bit chunks in a single memory block.
 

Functions

static int _FindBlock (const char *blockName)
 Retrieve the index of the requested memory block.
 
static bool _ConvertHandleToBlockIndex (int dataHandle, int *memoryIndex)
 Convert the given data handle to an index into the memory blocks.
 
static bool _IsValidOffset (int chunkOffset)
 Determine if the given offset is valid.
 
DDR_ErrorCode DDR_OpenMemoryBlock (const char *blockName, int *dataHandle)
 Open access to a memory block for exclusive use, given the name of the memory block.
 
DDR_ErrorCode DDR_CloseMemoryBlock (int dataHandle)
 Close access to a previously opened memory block, thus releasing it for others to open.
 
DDR_ErrorCode DDR_GetMemorySize (int dataHandle, int *memorySizeInChunks)
 Retrieve the number of chunks in the memory block indicated by the handle to the successfully opened memory block.
 
DDR_ErrorCode DDR_GetDataChunk (int dataHandle, int chunkOffset, uint32_t *value)
 Read a single 32-bit value at the given offset in the memory block indicated by the specified handle.
 
DDR_ErrorCode DDR_SetDataChunk (int dataHandle, int chunkOffset, uint32_t value)
 Writes a single 32-bit value to the given offset in the memory block indicated by the specified handle.
 

Variables

const char * BLOCK_NAME_0 = BLOCK_0_NAME
 Name of the first block.
 
const char * BLOCK_NAME_1 = BLOCK_1_NAME
 Name of the second block.
 
const char * BLOCK_NAME_2 = BLOCK_2_NAME
 Name of the third block.
 
static MemoryBlock memory_blocks []
 Predefined set of memory blocks that can be accessed by name. Data for each memory block is stored as a set of 32-bit values and can only be accessed as a 32-bit quantity.
 
static size_t memory_block_count = _countof(memory_blocks)
 

Detailed Description

Implementation of the memory read/write functions as used in the Adapter Pattern.

Definition in file Adapter_BackEnd.c.

Macro Definition Documentation

◆ BLOCK_0_NAME

#define BLOCK_0_NAME   "gorp"

Definition at line 9 of file Adapter_BackEnd.c.

◆ BLOCK_1_NAME

#define BLOCK_1_NAME   "baba"

Definition at line 10 of file Adapter_BackEnd.c.

◆ BLOCK_2_NAME

#define BLOCK_2_NAME   "yaga"

Definition at line 11 of file Adapter_BackEnd.c.

◆ MAX_DATA_SIZE

#define MAX_DATA_SIZE   32

Maximum number of 32-bit chunks in a single memory block.

Definition at line 20 of file Adapter_BackEnd.c.

Function Documentation

◆ _ConvertHandleToBlockIndex()

static bool _ConvertHandleToBlockIndex ( int  dataHandle,
int *  memoryIndex 
)
static

Convert the given data handle to an index into the memory blocks.

Parameters
dataHandleData handle as returned from the DDR_OpenMemoryBlock() function.
memoryIndexReturns the index into the memory blocks if the data handle is valid.
Returns
Returns true if the data handle is valid and was converted to an index; otherwise, returns false.

Definition at line 89 of file Adapter_BackEnd.c.

References memory_block_count.

Referenced by DDR_CloseMemoryBlock(), DDR_GetDataChunk(), DDR_GetMemorySize(), and DDR_SetDataChunk().

◆ _FindBlock()

static int _FindBlock ( const char *  blockName)
static

Retrieve the index of the requested memory block.

Parameters
blockNameName of the block to find
Returns
Returns the index to the memory block if found; otherwise, returns -1, indicating the block was not found.

Definition at line 65 of file Adapter_BackEnd.c.

References memory_block_count, and memory_blocks.

Referenced by DDR_OpenMemoryBlock().

◆ _IsValidOffset()

static bool _IsValidOffset ( int  chunkOffset)
static

Determine if the given offset is valid.

Parameters
chunkOffsetOffset into a memory block to verify.
Returns
Returns true if the offset is valid; otherwise, returns false, the offset is out of bounds.

Definition at line 113 of file Adapter_BackEnd.c.

References DDR_MAX_OFFSET.

Referenced by DDR_GetDataChunk(), and DDR_SetDataChunk().

◆ DDR_CloseMemoryBlock()

DDR_ErrorCode DDR_CloseMemoryBlock ( int  dataHandle)

Close access to a previously opened memory block, thus releasing it for others to open.

Parameters
dataHandleHandle to a previously opened memory block as obtained from the DDR_OpenMemoryBlock() function.
Returns
Returns a value from the DDR_ErrorCode enumeration indicating success or failure.

Definition at line 150 of file Adapter_BackEnd.c.

References _ConvertHandleToBlockIndex(), DDR_ErrorCode_Block_Not_Opened, DDR_ErrorCode_Invalid_Handle, DDR_ErrorCode_Success, MemoryBlock::locked, and memory_blocks.

Referenced by Adapter_CloseMemory(), and DataReaderWriter::~DataReaderWriter().

◆ DDR_GetDataChunk()

DDR_ErrorCode DDR_GetDataChunk ( int  dataHandle,
int  chunkOffset,
uint32_t *  value 
)

Read a single 32-bit value at the given offset in the memory block indicated by the specified handle.

Parameters
dataHandleHandle to a previously opened memory block as obtained from the DDR_OpenMemoryBlock() function.
chunkOffsetOffset into the memory block from which to get the value (range is 0 to DDR_MAX_OFFSET-1).
valueReturns the requested value.
Returns
Returns a value from the DDR_ErrorCode enumeration indicating success or failure.

Definition at line 191 of file Adapter_BackEnd.c.

References _ConvertHandleToBlockIndex(), _IsValidOffset(), MemoryBlock::data, DDR_ErrorCode_Block_Not_Opened, DDR_ErrorCode_Invalid_Offset, DDR_ErrorCode_Null_Argument, DDR_ErrorCode_Success, and memory_blocks.

Referenced by Adapter_ReadMemory(), Adapter_WriteMemory(), DataReaderWriter::Read(), and DataReaderWriter::Write().

◆ DDR_GetMemorySize()

DDR_ErrorCode DDR_GetMemorySize ( int  dataHandle,
int *  memorySizeInChunks 
)

Retrieve the number of chunks in the memory block indicated by the handle to the successfully opened memory block.

Parameters
dataHandleHandle to a previously opened memory block as obtained from the DDR_OpenMemoryBlock() function.
memorySizeInChunksReturns the number of 32-bit chunks in the memory block.
Returns
Returns a value from the DDR_ErrorCode enumeration indicating success or failure.

Definition at line 171 of file Adapter_BackEnd.c.

References _ConvertHandleToBlockIndex(), DDR_ErrorCode_Invalid_Handle, DDR_ErrorCode_Null_Argument, DDR_ErrorCode_Success, and MAX_DATA_SIZE.

Referenced by Adapter_GetMemorySize(), and DataReaderWriter::DataReaderWriter().

◆ DDR_OpenMemoryBlock()

DDR_ErrorCode DDR_OpenMemoryBlock ( const char *  blockName,
int *  dataHandle 
)

Open access to a memory block for exclusive use, given the name of the memory block.

Parameters
blockNameName of the block to access (one of the predefined names, BLOCK_NAME_0, BLOCK_NAME_1, or BLOCK_NAME_2)
dataHandleReturns a handle to be used for accessing the specific memory block.
Returns
Returns a value from the DDR_ErrorCode enumeration indicating success or failure.

Definition at line 121 of file Adapter_BackEnd.c.

References _FindBlock(), DDR_ErrorCode_Block_Already_Opened, DDR_ErrorCode_Invalid_Block_Name, DDR_ErrorCode_Null_Argument, DDR_ErrorCode_Success, DDR_INVALID_HANDLE, MemoryBlock::locked, and memory_blocks.

Referenced by Adapter_OpenMemory(), and DataReaderWriter::DataReaderWriter().

◆ DDR_SetDataChunk()

DDR_ErrorCode DDR_SetDataChunk ( int  dataHandle,
int  chunkOffset,
uint32_t  value 
)

Writes a single 32-bit value to the given offset in the memory block indicated by the specified handle.

Parameters
dataHandleHandle to a previously opened memory block as obtained from the DDR_OpenMemoryBlock() function.
chunkOffsetOffset into the memory block to which to set the value (range is 0 to DDR_MAX_OFFSET-1).
valueThe value to write to the memory block
Returns
Returns a value from the DDR_ErrorCode enumeration indicating success or failure.

Definition at line 219 of file Adapter_BackEnd.c.

References _ConvertHandleToBlockIndex(), _IsValidOffset(), MemoryBlock::data, DDR_ErrorCode_Block_Not_Opened, DDR_ErrorCode_Invalid_Handle, DDR_ErrorCode_Invalid_Offset, DDR_ErrorCode_Success, and memory_blocks.

Referenced by Adapter_WriteMemory(), and DataReaderWriter::Write().

Variable Documentation

◆ BLOCK_NAME_0

const char* BLOCK_NAME_0 = BLOCK_0_NAME

Name of the first block.

Definition at line 13 of file Adapter_BackEnd.c.

Referenced by DataReaderWriter::_GetBlockNameForBlockNumber(), and _GetBlockNameForBlockNumber().

◆ BLOCK_NAME_1

const char* BLOCK_NAME_1 = BLOCK_1_NAME

Name of the second block.

Definition at line 14 of file Adapter_BackEnd.c.

Referenced by DataReaderWriter::_GetBlockNameForBlockNumber(), and _GetBlockNameForBlockNumber().

◆ BLOCK_NAME_2

const char* BLOCK_NAME_2 = BLOCK_2_NAME

Name of the third block.

Definition at line 15 of file Adapter_BackEnd.c.

Referenced by DataReaderWriter::_GetBlockNameForBlockNumber(), and _GetBlockNameForBlockNumber().

◆ memory_block_count

size_t memory_block_count = _countof(memory_blocks)
static

Definition at line 57 of file Adapter_BackEnd.c.

Referenced by _ConvertHandleToBlockIndex(), and _FindBlock().

◆ memory_blocks

MemoryBlock memory_blocks[]
static
Initial value:
= {
{ BLOCK_0_NAME, false, { 0 } },
{ BLOCK_1_NAME, false, { 0 } },
{ BLOCK_2_NAME, false, { 0 } }
}
#define BLOCK_2_NAME
#define BLOCK_0_NAME
#define BLOCK_1_NAME

Predefined set of memory blocks that can be accessed by name. Data for each memory block is stored as a set of 32-bit values and can only be accessed as a 32-bit quantity.

Definition at line 50 of file Adapter_BackEnd.c.

Referenced by _FindBlock(), DDR_CloseMemoryBlock(), DDR_GetDataChunk(), DDR_OpenMemoryBlock(), and DDR_SetDataChunk().