[music-dsp] conventional c++ .dll memory practice

Payan, Remi rem at ti.com
Sun Mar 30 01:03:01 EST 2003


Hi Toto,
further comments on general programming in the example below.

In a true multi-thread system, this would not be safe because 
a task making a call to the init funtion could be preempted 
while checking/modifying the "tableInit" variable.

A safer way to write it would be to disable interrupts while 
you are checking/modifying this variable, as shown below, assuming 
you have available macros to disable/enable interrupts.
Alternatively to enabling/disabling ALL interrupts, you could also 
enable/disable task scheduling if your system allows you to do so.

Cheers,
Remi

=============================================
int initTable() {
  int status=0;

  // Just to make sure init is done once only
  DISABLE_INTERRUPTS();
  if (tableInit) {
    status = 1;
    tableInit = 0;
  }
  ENABLE_INTERRUPTS();

  if (status) {
    //
    //Initialize your tables here
    //
    return 0;
  else {
    return -1;
  }
}

==============================================


> -----Original Message-----
> From: Payan, Remi 
> Sent: Sunday, March 30, 2003 10:32 AM
> To: 'music-dsp at aulos.calarts.edu'
> Subject: RE: [music-dsp] conventional c++ .dll memory practice
> 
> 
> 
> Hi JAE,
> I don't know much about C++ and dlls, but a good way to do this in C 
> would be to declare your tables as static in a separate source file 
> and provide APIs to initialize them and get a pointer to them 
> wherever you need.
> The example below also uses a static variable in order to signal 
> whether tables are initialized or not.
> A more advanced version would declare an array of pointers to tables 
> and allocate memory for the tables within the init function, 
> just before 
> initializing.
> 
> Hope this helps,
> Remi
> 
> ===============================================
> #define TAB_NUM 2 //Number of tables
> #define TAB_LEN 2 //Length of tables
> 
> static tableInit = 1;
> static float table[TAB_NUM][TAB_LEN];
> 
> int initTable() {
>   // Just to make sure init is done once only
>   if (tableInit) {
>     tableInit = 0;
>     //
>     //Initialize your tables here
>     //
>     return 0;
>   }
>   else {
>     return -1;
>   }
> }
>  
> float *getTablePtr(int index) {
>   // Return 0 if tables not initialized
>   if (!tableInit) {
>     return table[index];
>   }
>   else {
>     return 0;
>   }
> }
> 
> ================================================
> 
> 
> dupswapdrop -- the music-dsp mailing list and website: 
> subscription info, FAQ, source code archive, list archive, 
> book reviews, dsp links http://shoko.calarts.edu/musicdsp/ 
> http://aulos.calarts.edu/mailman/listinfo/music-dsp
> 



More information about the music-dsp mailing list