Skip to content

Commit

Permalink
fix(lib): instruction to stream
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasanchez committed Apr 19, 2022
1 parent 70cc768 commit cca94d4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
13 changes: 11 additions & 2 deletions lib/inc/instruction/instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ instruction_t *instruction_create(instcode_t icode, uint32_t param0, uint32_t pa
*
* @param instruction to be deleted
*/
void instruction_destroy(instruction_t *instruction);
void instruction_destroy(void *instruction);

/**
* @brief Maps an instruction to a Stream.
*
* @param instruction to be mapped
* @return a serialized stream containing an instruction.
*/
void *instruction_to_stream(instruction_t *instruction);
void *instruction_to_stream(void *instruction);

/**
* @brief Instantiates an instruction from a stream.
Expand All @@ -98,3 +98,12 @@ void *instruction_to_stream(instruction_t *instruction);
* @return instruction_t*
*/
instruction_t *instruction_from_stream(void *stream);

/**
* @brief Reduces an instruction into a buffer.
*
* @param buffer the accumulator
* @param next the next instruction
* @return the accumulator
*/
void *instruction_reduce(void *buffer, void *next);
20 changes: 16 additions & 4 deletions lib/src/instruction/instruction.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

#include "instruction.h"

// Global Variable for list operations
size_t _instruction_offset = 0lu;

// ============================================================================================================
// ***** Public Functions *****
// ============================================================================================================
Expand All @@ -40,23 +43,24 @@ instruction_t *instruction_create(instcode_t icode, uint32_t param0, uint32_t pa
return instance;
}

void instruction_destroy(instruction_t *instruction)
void instruction_destroy(void *instruction)
{
if (instruction)
free(instruction);
}

void *instruction_to_stream(instruction_t *this)
void *instruction_to_stream(void *instruction)
{
instruction_t *this = (instruction_t *)instruction;
void *stream = malloc(sizeof(instruction_t));

size_t offset = 0lu;

memcpy(stream + offset, &this->icode, sizeof(instcode_t));
offset += sizeof(instcode_t);
memcpy(stream + offset, &this->icode, sizeof(uint32_t));
memcpy(stream + offset, &this->param0, sizeof(uint32_t));
offset += sizeof(uint32_t);
memcpy(stream + offset, &this->icode, sizeof(uint32_t));
memcpy(stream + offset, &this->param1, sizeof(uint32_t));
offset += sizeof(uint32_t);

return stream;
Expand All @@ -77,3 +81,11 @@ instruction_t *instruction_from_stream(void *stream)

return instruction_create(_icode, _param0, _param1);
}

void *instruction_reduce(void *buffer, void *next)
{
size_t offset = sizeof(instruction_t);
memcpy(&buffer + _instruction_offset, next, offset);
_instruction_offset += offset;
return buffer;
}

0 comments on commit cca94d4

Please sign in to comment.