2016-03-21 14 views
0

私は、いくつかの重要な自動車用モジュールでコードを実行しています。 「メインモジュール」、揮発性資格の喪失

Main Module.c 

//The structure x contains the major data that needs to be stored in case of a crash event. i.e. a real car crash 
// x contains data such a vehicle speed, environment data, sensor data, CAN related data,etc. Basically x itself has lot of structures inside it. 


typedef struct x x_tst; 
volatile x_tst x_ast[5]; 
//x_ast is being used in realtime and background functions, considering proper interrupt disabling and enabling. 

は、以下のコードである「x_ast」揮発性変数/配列を所有している - 以下のコードは、モジュールの一部である

:次は、コードの概算ですモジュール "DependentModule"の一部で、バッファ "x_ast"の共有を可能にします。

DependentModule.c 

extern volatile x_tst x_ast[5]; 


//x_ast is owned by a separate module 
//Now I need to share this buffer "x_ast" to a different module that will inturn fill data in it for some conditions. Say for a particular condition that is activated, the buffer "x_ast" will be shared across to a module "Conditional Data Record". 
//The base address of first indexed buffer "x_ast[1]" is provided to "Conditional Data Record" for access. 
//The main module will not access the buffer that is shared, once the "Conditional Data Record" is activated/requested. 

// Below is a mechanism to share the buffer "x_ast" to module "Conditional Data Record". 

// This API will be called by module - "Conditional Data Record" once it is activated. It is ensured that this takes place only during intialization of whole system, and no other time. 

boolean GetConditionalBuffer(uint8 **PtrToBuffer) 
{ 
    boolean BufferShared = FALSE; 
    void* Temp_ptr; 
    *PtrToBuffer = NULL; 

// if module "Conditional Data Record" is activated? then share the buffer "x_ast", else do not share the buffer. 

if(Conditional Data Record == activated) { 
    Temp_ptr = (x_tst*)&x_ast[1]; 
    *PtrToBuffer = Temp_ptr; 
    BufferShared = TRUE; 
} 

return BufferShared; 

} 



Referring to the line of code: 
Temp_ptr = (x_tst*)&x_ast[1]; 

コード(Tempptr =(x_tstの*)& x_ast [1];)の上の行警告 "メッセージ(7:0312)スロー。揮発性資格の損失危険ポインタキャスト結果を " 上記の警告は必須の警告ですので、解決する必要があります。

volatile変数のアドレスをvoidポインターに割り当てて、volatile修飾を失うことを理解しています。 私は警告を解決しようとするさまざまな方法を試みましたが、決定的な方法を得ることはできませんでした。

コードを修正してこの警告を削除したり、この警告を回避する方法はありますか。

+1

代わりに、関数は 'volatile void ** PtrToBuffer'をとるようにしてください。もう一つのオプションは、あなたの仕事を別のバッファで行い、必要に応じて揮発性バッファに/からコピーすることです。後者は危険性がより低い。バッファーをロックする必要があることを覚えておいてください。これは、ユーザー自身と、これらのバックグラウンド関数の1つが並行してアクセスするのを防ぐためです。 –

+0

同意条件は、モジュール "条件付きデータレコード"で処理されます。競合状態が回避されることが保証される。 –

+0

関数がバッファをロックしていることを意味している場合は、その関数を 'volatile void ** PtrToBuffer'にします。 (揮発性でないuint8_t)。バッファをロックしても、volatileとwriteをキャストすると、未定義の動作が発生します。代入演算子を使用して、一度にバッファ1の値を読み書きする必要があります。 –

答えて

1

揮発性オブジェクトに値を割り当てるか、volatile修飾されたポインタを使用せずに揮発性オブジェクトの値を読み取ると、未定義の動作が発生します。

コンパイラは、不揮発性オブジェクト(不揮発性オブジェクトをあたかも揮発性であるかのように扱うことを意味し、不揮発性オブジェクトをあたかも不揮発性であるかのように扱うことを意味する)よりも厳しい方法で、悪い結果)。

揮発性オブジェクトのアドレスを不揮発性ポインタにキャストすると、は重大な危険にさらされます。しないでください。そのvoid *ポインターを使用する人は、未定義のビヘイビアを呼び出す危険があります。たとえば、memcpyを使用してvolatile配列をコピーすると、未定義の動作になります。何か、そして通常は悪いことが起こる可能性があります。揮発性x_tst *は、それがPtrToBufferに格納するものであるため、

boolean GetConditionalBuffer(volatile x_tst **PtrToBuffer) 

としてその関数を宣言します。なぜあなたは型情報を捨てますか?私は実際に貧しい開発者の脳にそれが容易になり、かつ使用する機能が容易になり

volatile x_tst* GetConditionalBuffer (void); 

に変更したいです。

+0

もう1つの依存関係は、モジュール "条件付きデータレコード"が共有バッファ "x_ast"にバイト単位でアクセスすることです。モジュール「条件付データレコード」は、その操作のためにサイズ7KBのバッファを必要とする。 x_ast [1] ... x_ast [5]:合計サイズは約8KBです。したがって、モジュール "条件付きデータレコード"は、バッファをx_astとして動作させたり使用したりすることはありませんが、単純に8KBの配列として使用します –