LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Tips for malloc()/reentrancy in CLFN DLL?

I want to make my malloc()-calling DLL reentrant.  Should I declare my pointer as a local variable, or is it better to use a pointer within a structure with a pointer that get's passed back and forth to LabView?

To clarify, which one is better for LabView:

/* Typedefs */   
typedef struct {
    MYSQL mysql;
    MYSQL_RES *query_results;
    unsigned short int odbc_driver;
    unsigned short int db_type;
    } SQL_LV_REF;

MSEXPORT long sql_open(DB_LOGIN *login, unsigned long  *db, long *driver, LStrHandle debug)
{
   db_ref = (SQL_LV_REF *) malloc(sizeof(SQL_LV_REF));
   *db = (unsigned long)db_ref;
    if (mysql_query(&(db_ref->mysql), sql_query) != 0) {}  //NOTE:  mysql_query() is likely calling malloc()


or:

/* Typedefs */   
typedef struct {
    MYSQL_RES *query_results;
    unsigned short int odbc_driver;
    unsigned short int db_type;
    } SQL_LV_REF;

MSEXPORT long sql_open(DB_LOGIN *login, unsigned long  *db, long *driver, LStrHandle debug)
{
    MYSQL mysql;
   db_ref = (SQL_LV_REF *) malloc(sizeof(SQL_LV_REF));
   *db = (unsigned long)db_ref;
    if (mysql_query(&mysql, sql_query) != 0) {}

In order to continue using an open connection, I need to have the first solution but when there's a choice, what's best?  The programming is easier when the variables are local, but is that universally reentrant?

   ...Dan
0 Kudos
Message 1 of 2
(2,408 Views)
dgholstein-

In terms of making the function call reentrant, I don't think that either solution will cause a problem. the only difference is wether you have mysql as a local or part of a struct that you crate a new instance of for each function call. As you pointed out, the first example maintains the mysql variable as part of the struct after the open function executes (and thus allows you to reference it in subsequent calls to other functions) where the second implementation would destrruct the mysql variable after the function executes.

For that reason, and the fact that both are equally reentrant (neither are using globals), I would have to say that the first implemenation is your best bet.

Xaq
Message 2 of 2
(2,387 Views)