rem: size = sizeof(presentity_t) + _uri->len + 1; presentity = (presentity_t*)shm_malloc(size); if (!presentity) { paerrno = PA_NO_MEMORY; LOG(L_ERR, "new_presentity(): No memory left: size=%d\n", size); return -1; } memset(presentity, 0, sizeof(presentity_t));  presentity->uri.s = ((char*)presentity) + sizeof(presentity_t); strncpy(presentity->uri.s, _uri->s, _uri->len); presentity->uri.s[_uri->len] = 0; presentity->uri.len = _uri->len; presentity->pdomain = pdomain;  if (use_db) { db_key_t query_cols[4]; db_op_t  query_ops[4]; db_val_t query_vals[4];  db_key_t result_cols[4]; db_res_t *res; int n_query_cols = 0; int n_result_cols = 0; int presid_col; int presid = 0;  query_cols[0] = "uri"; query_ops[0] = OP_EQ; query_vals[0].type = DB_STR; query_vals[0].nul = 0; query_vals[0].val.str_val = presentity->uri; n_query_cols++;  query_cols[n_query_cols] = "pdomain"; query_ops[n_query_cols] = OP_EQ; query_vals[n_query_cols].type = DB_STR; query_vals[n_query_cols].nul = 0; query_vals[n_query_cols].val.str_val = *presentity->pdomain->name; n_query_cols++;  result_cols[presid_col = n_result_cols++] = "presid";  if (pa_dbf.use_table(pa_db, presentity_table) < 0) { LOG(L_ERR, "new_presentity: Error in use_table\n"); return -1; }  while (!presid) { if (pa_dbf.query (pa_db, query_cols, query_ops, query_vals, result_cols, n_query_cols, n_result_cols, 0, &res) < 0) { LOG(L_ERR, "new_presentity: Error while querying presentity\n"); return -1; } if (res && res->n > 0) {  db_row_t *row = &res->rows[0]; db_val_t *row_vals = ROW_VALUES(row); presid = presentity->presid = row_vals[presid_col].val.int_val;  LOG(L_INFO, "  presid=%d\n", presid);  } else {  LOG(L_INFO, "new_presentity: inserting %d cols into table\n", n_query_cols); if (pa_dbf.insert(pa_db, query_cols, query_vals, n_query_cols) < 0) { LOG(L_ERR, "new_presentity: Error while inserting tuple\n"); return -1; } } pa_dbf.free_result(pa_db, res); } }  *_p = presentity;  LOG(L_ERR, "new_presentity=%p for uri=%.*s\n", presentity, presentity->uri.len, presentity->uri.s);  return 0;
add: return res;
context: int new_presentity(struct pdomain *pdomain, str* _uri, presentity_t** _p){     presentity_t* presentity;     int size = 0;     if (!_uri || !_p) {	  paerrno = PA_INTERNAL_ERROR;	  LOG(L_ERR, "new_presentity(): Invalid parameter value\n");	  return -1;     }     size = sizeof(presentity_t) + _uri->len + 1;     presentity = (presentity_t*)shm_malloc(size);     if (!presentity) {	  paerrno = PA_NO_MEMORY;	  LOG(L_ERR, "new_presentity(): No memory left: size=%d\n", size);	  return -1;     }     memset(presentity, 0, sizeof(presentity_t));     presentity->uri.s = ((char*)presentity) + sizeof(presentity_t);     strncpy(presentity->uri.s, _uri->s, _uri->len);     presentity->uri.s[_uri->len] = 0;     presentity->uri.len = _uri->len;     presentity->pdomain = pdomain;     if (use_db) {	  db_key_t query_cols[4];	  db_op_t  query_ops[4];	  db_val_t query_vals[4];	  db_key_t result_cols[4];	  db_res_t *res;	  int n_query_cols = 0;	  int n_result_cols = 0;	  int presid_col;	  int presid = 0;	  query_cols[0] = "uri";	  query_ops[0] = OP_EQ;	  query_vals[0].type = DB_STR;	  query_vals[0].nul = 0;	  query_vals[0].val.str_val = presentity->uri;	  n_query_cols++;	  query_cols[n_query_cols] = "pdomain";	  query_ops[n_query_cols] = OP_EQ;	  query_vals[n_query_cols].type = DB_STR;	  query_vals[n_query_cols].nul = 0;	  query_vals[n_query_cols].val.str_val = *presentity->pdomain->name;	  n_query_cols++;	  result_cols[presid_col = n_result_cols++] = "presid";	  if (pa_dbf.use_table(pa_db, presentity_table) < 0) {		  LOG(L_ERR, "new_presentity: Error in use_table\n");		  return -1;	  }	  while (!presid) {	       if (pa_dbf.query (pa_db, query_cols, query_ops, query_vals,			     result_cols, n_query_cols, n_result_cols, 0, &res) < 0) {		    LOG(L_ERR, "new_presentity: Error while querying presentity\n");		    return -1;	       }	       if (res && res->n > 0) {		    /* fill in tuple structure from database query result */		    db_row_t *row = &res->rows[0];		    db_val_t *row_vals = ROW_VALUES(row);		    presid = presentity->presid = row_vals[presid_col].val.int_val;		    LOG(L_INFO, "  presid=%d\n", presid);	       } else {		    /* insert new record into database */		    LOG(L_INFO, "new_presentity: inserting %d cols into table\n", n_query_cols);		    if (pa_dbf.insert(pa_db, query_cols, query_vals, n_query_cols)					< 0) {			 LOG(L_ERR, "new_presentity: Error while inserting tuple\n");			 return -1;		    }	       }	       pa_dbf.free_result(pa_db, res);	  }     }     *_p = presentity;     LOG(L_ERR, "new_presentity=%p for uri=%.*s\n", 	 presentity, presentity->uri.len, presentity->uri.s);     return 0;}