]> code.citadel.org Git - citadel.git/blobdiff - citadel/server/backends/berkeley_db/berkeley_db.c
cdb_fetch() and cdb_next_item() now return a struct cdbdata instead of a pointer...
[citadel.git] / citadel / server / backends / berkeley_db / berkeley_db.c
index 60063099e47cd3453a053b1fab5935ec563f0e03..7656e4f8be0bfb30a71e0c14bf7a7063cc5afa24 100644 (file)
@@ -522,13 +522,15 @@ static DBC *bdb_localcursor(int cdb) {
 // 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;
 
@@ -556,22 +558,13 @@ struct cdbdata *bdb_fetch(int cdb, const void *key, int keylen) {
                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);
 }
 
 
@@ -612,10 +605,12 @@ void bdb_rewind(int cdb) {
 
 // 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;
@@ -628,14 +623,13 @@ struct cdbdata *bdb_next_item(int cdb) {
                        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);
 }