2007-07-13 19:37:47 +00:00
|
|
|
#define _FILE_OFFSET_BITS 64
|
|
|
|
#include "ybt.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2007-11-29 15:17:46 +00:00
|
|
|
DBT *toku_init_dbt (DBT *ybt) {
|
2007-07-13 19:37:47 +00:00
|
|
|
memset(ybt, 0, sizeof(*ybt));
|
2007-07-24 01:32:03 +00:00
|
|
|
return ybt;
|
2007-07-13 19:37:47 +00:00
|
|
|
}
|
|
|
|
|
2007-11-29 15:17:46 +00:00
|
|
|
DBT *toku_fill_dbt(DBT *dbt, bytevec k, ITEMLEN len) {
|
|
|
|
toku_init_dbt(dbt);
|
2007-07-23 20:10:16 +00:00
|
|
|
dbt->size=len;
|
|
|
|
dbt->data=(char*)k;
|
|
|
|
return dbt;
|
|
|
|
}
|
|
|
|
|
2007-11-29 15:17:46 +00:00
|
|
|
int toku_dbt_set_value (DBT *ybt, bytevec val, ITEMLEN vallen, void **staticptrp) {
|
2008-03-27 21:22:57 +00:00
|
|
|
int r = ENOSYS;
|
2007-07-13 19:37:47 +00:00
|
|
|
if (ybt->flags==DB_DBT_MALLOC) {
|
|
|
|
domalloc:
|
2008-04-02 23:40:36 +00:00
|
|
|
ybt->data = toku_malloc((size_t)vallen);
|
2008-03-27 21:22:57 +00:00
|
|
|
if (!ybt->data && vallen > 0) { r = errno; goto cleanup; }
|
2007-07-13 19:37:47 +00:00
|
|
|
} else if (ybt->flags==DB_DBT_REALLOC) {
|
|
|
|
if (ybt->data==0) goto domalloc;
|
2008-03-27 21:22:57 +00:00
|
|
|
/* tmp is used to prevent a memory leak if realloc fails */
|
2008-04-02 23:40:36 +00:00
|
|
|
void* tmp = toku_realloc(ybt->data, (size_t)vallen);
|
2008-03-27 21:22:57 +00:00
|
|
|
if (!tmp && vallen > 0) { r = errno; goto cleanup; }
|
|
|
|
ybt->data = tmp;
|
2007-07-13 19:37:47 +00:00
|
|
|
} else if (ybt->flags==DB_DBT_USERMEM) {
|
2007-12-10 17:04:02 +00:00
|
|
|
ybt->size = vallen;
|
2008-03-27 21:22:57 +00:00
|
|
|
if (ybt->ulen < vallen) { r = DB_BUFFER_SMALL; goto cleanup; }
|
2007-07-13 19:37:47 +00:00
|
|
|
} else {
|
|
|
|
if (staticptrp==0) return -1;
|
|
|
|
void *staticptr=*staticptrp;
|
2007-07-24 03:28:48 +00:00
|
|
|
//void *old=staticptr;
|
2008-03-27 21:22:57 +00:00
|
|
|
if (staticptr==0) {
|
2008-04-02 23:40:36 +00:00
|
|
|
staticptr = toku_malloc((size_t)vallen);
|
2008-03-27 21:22:57 +00:00
|
|
|
if (!staticptr && vallen > 0) { r = errno; goto cleanup; }
|
2008-04-02 23:40:36 +00:00
|
|
|
} else {
|
2008-03-27 21:22:57 +00:00
|
|
|
/* tmp is used to prevent a memory leak if realloc fails */
|
|
|
|
void* tmp = toku_realloc(staticptr, vallen);
|
|
|
|
if (!tmp && vallen > 0) { r = errno; goto cleanup; }
|
|
|
|
staticptr = tmp;
|
|
|
|
}
|
2007-07-24 03:28:48 +00:00
|
|
|
//if (old!=staticptr) printf("%s:%d MALLOC --> %p\n", __FILE__, __LINE__, staticptr);
|
2007-07-13 19:37:47 +00:00
|
|
|
*staticptrp = staticptr;
|
2008-02-08 22:53:08 +00:00
|
|
|
ybt->data = vallen > 0 ? staticptr : 0;
|
2007-07-13 19:37:47 +00:00
|
|
|
}
|
|
|
|
ybt->size = vallen;
|
2007-12-10 17:04:02 +00:00
|
|
|
if (ybt->size>0) {
|
2008-04-02 23:40:36 +00:00
|
|
|
memcpy(ybt->data, val, (size_t)vallen);
|
2007-07-13 19:37:47 +00:00
|
|
|
}
|
2008-03-27 21:22:57 +00:00
|
|
|
r = 0;
|
|
|
|
cleanup:
|
|
|
|
return r;
|
2007-07-13 19:37:47 +00:00
|
|
|
}
|
2008-03-27 21:22:57 +00:00
|
|
|
|