db_fld_t* db_fld_copy(db_fld_t* fld) { int i, n; db_fld_t* newp; for(n = 0; fld[n].name; n++); n++; /* We need to copy the terminating element too */ newp = (db_fld_t*)pkg_malloc(sizeof(db_fld_t) * n); if (newp == NULL) { ERR("db_fld: No memory left\n"); return NULL; } memcpy(newp, fld, sizeof(db_fld_t) * n); for(i = 0; i < n; i++) { if (db_gen_init(&newp[i].gen) < 0) goto error; } return newp; error: ERR("db_fld_copy() failed\n"); if (newp) { /* Free everything allocated in this function so far */ while(i >= 0) { db_gen_free(&newp[i].gen); i--; } pkg_free(newp); } return NULL; }
db_fld_t* db_fld(size_t n) { int i; db_fld_t* newp; newp = (db_fld_t*)pkg_malloc(sizeof(db_fld_t) * n); if (newp == NULL) { ERR("db_fld: No memory left\n"); return NULL; } memset(newp, '\0', sizeof(db_fld_t) * n); for(i = 0; i < n; i++) { if (db_gen_init(&newp[i].gen) < 0) goto error; } return newp; error: if (newp) { while(i >= 0) { db_gen_free(&newp[i].gen); i--; } pkg_free(newp); } return NULL; }
/* * Create a new generic db_con structure representing a * database connection and call the driver specific function * in the driver that is associated with the structure based * on the scheme of uri parameter */ db_con_t* db_con(db_ctx_t* ctx, db_uri_t* uri) { db_con_t* newp; newp = (db_con_t*)pkg_malloc(sizeof(db_con_t)); if (newp == NULL) { ERR("db_con: No memory left\n"); goto error; } memset(newp, '\0', sizeof(db_con_t)); if (db_gen_init(&newp->gen) < 0) goto error; newp->uri = uri; newp->ctx = ctx; newp->connect = db_con_connect; newp->disconnect = db_con_disconnect; /* Call db_ctx function if the driver has it */ if (db_drv_call(&uri->scheme, "db_con", newp, ctx->con_n) < 0) { goto error; } return newp; error: if (newp) { db_gen_free(&newp->gen); pkg_free(newp); } return NULL; }
/* * Releaase all memory used by the structure */ void db_con_free(db_con_t* con) { if (con == NULL) return; db_gen_free(&con->gen); if (con->uri) db_uri_free(con->uri); pkg_free(con); }
void db_fld_close(db_fld_t* fld) { int i; for(i = 0; !DB_FLD_LAST(fld[i]); i++) { db_gen_free(&fld[i].gen); } }
void db_fld_free(db_fld_t* fld) { int i; if (DB_FLD_EMPTY(fld)) return; for(i = 0; !DB_FLD_LAST(fld[i]); i++) { db_gen_free(&fld[i].gen); } pkg_free(fld); }