Skip to content

Commit

Permalink
Implement compile time type size checks.
Browse files Browse the repository at this point in the history
Signed-off-by: Rule Timothy (VM/EMT3) <Timothy.Rule@de.bosch.com>
  • Loading branch information
timrulebosch committed Jun 26, 2024
1 parent 507e49f commit b8bf551
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 15 deletions.
3 changes: 3 additions & 0 deletions dse/modelc/gateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ typedef struct ModelGatewayDesc {
char* name_arg;
/* Sync epsilon (i.e. clock tolerance). */
double clock_epsilon;

/* Reserved. */
uint64_t __reserved__[4];
} ModelGatewayDesc;


Expand Down
24 changes: 15 additions & 9 deletions dse/modelc/mcl.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,32 @@ typedef struct MclVTable {
MclMarshalOut marshal_out;
MclMarshalIn marshal_in;
MclUnload unload;

/* Reserved (space for 2 function pointers). */
void* __reserved__[2];
} MclVTable;


typedef struct MclDesc {
ModelDesc model;
ModelDesc model;
/* Extensions to base ModelDesc type. */
const char* adapter;
const char* version;
MclVTable vtable;
MclVTable vtable;
/* Model Time Calculation. */
double step_size;
double model_time;
double model_time_correction;
double step_size;
double model_time;
double model_time_correction;
/* References for Signal Vector marshalling. */
struct {
size_t* count;
const char** signal;
double** scalar;
size_t* count;
const char** signal;
double** scalar;
} source;
MarshalSignalMap* msm; // NULL terminated list
MarshalSignalMap* msm; // NULL terminated list

/* Reserved. */
uint64_t __reserved__[3];
} MclDesc;


Expand Down
16 changes: 13 additions & 3 deletions dse/modelc/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,21 +143,28 @@ typedef void (*ModelDestroy)(ModelDesc* m);
typedef ModelSignalIndex (*ModelIndex)(
ModelDesc* m, const char* vname, const char* sname);


typedef struct ModelVTable {
ModelCreate create;
ModelStep step;
ModelDestroy destroy;
ModelIndex index;

/* Reserved (space for 2 function pointers). */
void* __reserved__[2];
} ModelVTable;


typedef struct ModelDesc {
ModelVTable vtable;
ModelIndex index;
SimulationSpec* sim;
ModelInstanceSpec* mi;
SignalVector* sv;

/* Model Function Table. */
ModelVTable vtable;

/* Reserved. */
uint64_t __reserved__[2];
} ModelDesc;


Expand Down Expand Up @@ -206,6 +213,9 @@ typedef struct SignalVectorVTable {
SignalAnnotationGetFunc annotation;
BinarySignalCodecFunc codec;
SignalGroupAnnotationGetFunc group_annotation;

/* Reserved (space for 2 function pointers). */
void* __reserved__[2];
} SignalVectorVTable;


Expand Down Expand Up @@ -240,7 +250,7 @@ typedef struct SignalVector {
SignalVectorVTable vtable;

/* Reserved. */
uint64_t __reserved__[8];
uint64_t __reserved__[9];
} SignalVector;


Expand Down
16 changes: 15 additions & 1 deletion dse/modelc/model/gateway.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,23 @@
#define UNUSED(x) ((void)x)


__attribute__((unused)) static void __compile_time_checks(void)
{
/* Compile-time type size check. Get actual size with:
* char (*___)[sizeof(ModelGatewayDesc)] = 1;
*/
#if __SIZEOF_POINTER__ == 8
_Static_assert(sizeof(ModelGatewayDesc) == 80, "Compatibility FAIL!");
#else
_Static_assert(sizeof(ModelGatewayDesc) == 64, "Compatibility FAIL!");
#endif
}


/* Gateway Model Functions
* These represent the Model Interface of the Gateway. */
DLL_PRIVATE int __model_gw_step__(ModelDesc* model, double* model_time, double stop_time)
DLL_PRIVATE int __model_gw_step__(
ModelDesc* model, double* model_time, double stop_time)
{
UNUSED(model);
*model_time = stop_time;
Expand Down
16 changes: 16 additions & 0 deletions dse/modelc/model/mcl.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@
#include <dse/modelc/mcl.h>


__attribute__((unused)) static void __compile_time_checks(void)
{
/* Compile-time type size check. Get actual size with:
* char (*___)[sizeof(MclDesc)] = 1;
* char (*___)[sizeof(MarshalSignalMap)] = 1;
*/
#if __SIZEOF_POINTER__ == 8
_Static_assert(sizeof(MclDesc) == 256, "Compatibility FAIL!");
_Static_assert(sizeof(MarshalSignalMap) == 56, "Compatibility FAIL!");
#else
_Static_assert(sizeof(MclDesc) == 160, "Compatibility FAIL!");
_Static_assert(sizeof(MarshalSignalMap) == 28, "Compatibility FAIL!");
#endif
}


/**
mcl_create
==========
Expand Down
16 changes: 16 additions & 0 deletions dse/modelc/model/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@
#include <dse/modelc/schema.h>


__attribute__((unused)) static void __compile_time_checks(void)
{
/* Compile-time type size check. Get actual size with:
* char (*___)[sizeof(ModelInstanceSpec)] = 1;
* char (*___)[sizeof(ModelDesc)] = 1;
*/
#if __SIZEOF_POINTER__ == 8
_Static_assert(sizeof(ModelInstanceSpec) == 128, "Compatibility FAIL!");
_Static_assert(sizeof(ModelDesc) == 96, "Compatibility FAIL!");
#else
_Static_assert(sizeof(ModelInstanceSpec) == 80, "Compatibility FAIL!");
_Static_assert(sizeof(ModelDesc) == 56, "Compatibility FAIL!");
#endif
}


#define VECTOR_TYPE_BINARY_STR "binary"


Expand Down
15 changes: 14 additions & 1 deletion dse/modelc/model/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ extern void ncodec_trace_configure(NCODEC* nc, ModelInstanceSpec* mi);
extern void ncodec_trace_destroy(NCodecInstance* nc);


__attribute__((unused)) static void __compile_time_checks(void)
{
/* Compile-time type size check. Get actual size with:
* char (*___)[sizeof(SignalVector)] = 1;
*/
#if __SIZEOF_POINTER__ == 8
_Static_assert(sizeof(SignalVector) == 256, "Compatibility FAIL!");
#else
_Static_assert(sizeof(SignalVector) == 168, "Compatibility FAIL!");
#endif
}


/* Signal Annotation Functions. */

static SchemaSignalObject* __signal_match;
Expand Down Expand Up @@ -87,7 +100,7 @@ static int _sg_annotation_search_match_handler(
{
UNUSED(model_instance);

YamlNode* n = dse_yaml_find_node(object->doc, "metadata/annotations");
YamlNode* n = dse_yaml_find_node(object->doc, "metadata/annotations");
const char* value = dse_yaml_get_scalar(n, __signal_group_annotation_name);
if (value) {
/* Match found, return +ve to stop search. */
Expand Down
6 changes: 5 additions & 1 deletion dse/modelc/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef DSE_MODELC_RUNTIME_H_
#define DSE_MODELC_RUNTIME_H_

#include <limits.h>
#include <dse/modelc/model.h>


Expand Down Expand Up @@ -66,6 +67,9 @@ typedef struct ModelInstanceSpec {
void* yaml_doc_list;
/* Private data of the specific Model Instance. */
void* private;

/* Reserved. */
uint64_t __reserved__[4];
} ModelInstanceSpec;


Expand Down Expand Up @@ -120,7 +124,7 @@ DLL_PUBLIC int modelc_model_create(


/* modelc_debug.c - Debug Interface. */
DLL_PUBLIC int modelc_step(ModelInstanceSpec* mi, double step_size);
DLL_PUBLIC int modelc_step(ModelInstanceSpec* mi, double step_size);
DLL_PUBLIC void modelc_destroy(ModelInstanceSpec* mi);


Expand Down

0 comments on commit b8bf551

Please sign in to comment.