// Fetch a piece of data. If not found, returns NULL. Otherwise, it returns
// a struct cdbdata which it is the caller's responsibility to free later on
// using the bdb_free() routine.
-struct cdbdata *bdb_fetch(int cdb, const void *key, int keylen) {
+struct cdbdata bdb_fetch(int cdb, const void *key, int keylen) {
+
+ struct cdbdata returned_data;
+ memset(&returned_data, 0, sizeof(struct cdbdata));
if (keylen == 0) { // key length zero is impossible
- return(NULL);
+ return(returned_data);
}
- struct cdbdata *tempcdb;
DBT dkey;
int ret;
bdb_abort();
}
- if (ret != 0) {
- return NULL;
+ if (ret == 0) {
+ bdb_decompress_if_necessary(&TSD->dbdata[cdb]);
+ returned_data.len = TSD->dbdata[cdb].size;
+ returned_data.ptr = TSD->dbdata[cdb].data;
}
- bdb_decompress_if_necessary(&TSD->dbdata[cdb]);
-
- tempcdb = (struct cdbdata *) malloc(sizeof(struct cdbdata));
- if (tempcdb == NULL) {
- syslog(LOG_ERR, "bdb: bdb_fetch() cannot allocate memory for tempcdb: %m");
- bdb_abort();
- }
- else {
- tempcdb->len = TSD->dbdata[cdb].size;
- tempcdb->ptr = TSD->dbdata[cdb].data;
- return (tempcdb);
- }
+ return(returned_data);
}
// Fetch the next item in a sequential search. Returns a pointer to a
// cdbdata structure, or NULL if we've hit the end.
-struct cdbdata *bdb_next_item(int cdb) {
- struct cdbdata *cdbret;
+struct cdbdata bdb_next_item(int cdb) {
+ struct cdbdata cdbret;
int ret = 0;
+ memset(&cdbret, 0, sizeof(struct cdbdata));
+
// reuse memory from the previous call.
TSD->dbkey[cdb].flags = DB_DBT_MALLOC;
TSD->dbdata[cdb].flags = DB_DBT_MALLOC;
bdb_abort();
}
bdb_close_cursor(cdb);
- return NULL; // presumably, we are at the end
+ return(cdbret); // presumably, we are at the end
}
bdb_decompress_if_necessary(&TSD->dbdata[cdb]);
- cdbret = (struct cdbdata *) malloc(sizeof(struct cdbdata));
- cdbret->len = TSD->dbdata[cdb].size;
- cdbret->ptr = TSD->dbdata[cdb].data;
+ cdbret.len = TSD->dbdata[cdb].size;
+ cdbret.ptr = TSD->dbdata[cdb].data;
return (cdbret);
}