From 45606ddf562f4124140b5fc68b54dabd09e16b5b Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 17 Dec 2008 14:45:39 -0700 Subject: [PATCH] Change binhelper to avoid stupid user errors; now grows the buffer on demand for the specific # of bytes; fix bugs in get_data_dirs/0 (driver and erlang caller) --- c_src/bdberl_drv.c | 23 ++++++++++------------- c_src/bin_helper.c | 10 +++++++--- c_src/bin_helper.h | 2 +- src/bdberl.erl | 2 +- test/bdberl_SUITE.erl | 7 +++++-- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/c_src/bdberl_drv.c b/c_src/bdberl_drv.c index dae7ae1..961239d 100644 --- a/c_src/bdberl_drv.c +++ b/c_src/bdberl_drv.c @@ -122,7 +122,7 @@ static TPool* G_TPOOL_TXNS; #define RETURN_INT(val, outbuf) { \ BinHelper bh; \ - bin_helper_init(&bh, 4); \ + bin_helper_init(&bh); \ bin_helper_push_int32(&bh, val); \ RETURN_BH(bh, outbuf); } @@ -351,7 +351,7 @@ static int bdberl_drv_control(ErlDrvData handle, unsigned int cmd, // Pack the status and dbref (or errno) into a binary and return it // Outbuf is: <> BinHelper bh; - bin_helper_init(&bh, 5); + bin_helper_init(&bh); bin_helper_push_byte(&bh, status); bin_helper_push_int32(&bh, dbref); RETURN_BH(bh, outbuf); @@ -748,6 +748,7 @@ static int delete_database(const char* name) } // Good, database doesn't seem to be open -- attempt the delete + DBG("Attempting to delete database: %s\n", name); int rc = G_DB_ENV->dbremove(G_DB_ENV, 0, name, 0, DB_AUTO_COMMIT); WRITE_UNLOCK(G_DATABASES_RWLOCK); return rc; @@ -766,7 +767,7 @@ static void tune_system(int target, void* values, BinHelper* bh) unsigned int bytes = 0; int caches = 0; int rc = G_DB_ENV->get_cachesize(G_DB_ENV, &gbytes, &bytes, &caches); - bin_helper_init(bh, 16); + bin_helper_init(bh); bin_helper_push_int32(bh, rc); bin_helper_push_int32(bh, gbytes); bin_helper_push_int32(bh, bytes); @@ -777,7 +778,7 @@ static void tune_system(int target, void* values, BinHelper* bh) { unsigned int timeout = UNPACK_INT(values, 0); int rc = G_DB_ENV->set_timeout(G_DB_ENV, timeout, DB_SET_TXN_TIMEOUT); - bin_helper_init(bh, 4); + bin_helper_init(bh); bin_helper_push_int32(bh, rc); break; } @@ -785,7 +786,7 @@ static void tune_system(int target, void* values, BinHelper* bh) { unsigned int timeout = 0; int rc = G_DB_ENV->get_timeout(G_DB_ENV, &timeout, DB_SET_TXN_TIMEOUT); - bin_helper_init(bh, 8); + bin_helper_init(bh); bin_helper_push_int32(bh, rc); bin_helper_push_int32(bh, timeout); break; @@ -794,16 +795,12 @@ static void tune_system(int target, void* values, BinHelper* bh) { const char** dirs = 0; int rc = G_DB_ENV->get_data_dirs(G_DB_ENV, &dirs); - printf("DATA DIR: %d\n", rc); - bin_helper_init(bh, 64); + bin_helper_init(bh); bin_helper_push_int32(bh, rc); - if (dirs) + while (dirs && *dirs) { - while (*dirs != 0) - { - bin_helper_push_string(bh, *dirs); - dirs++; - } + bin_helper_push_string(bh, *dirs); + dirs++; } break; } diff --git a/c_src/bin_helper.c b/c_src/bin_helper.c index 9f05205..beedafb 100644 --- a/c_src/bin_helper.c +++ b/c_src/bin_helper.c @@ -12,15 +12,19 @@ static void bin_helper_check_size(BinHelper* bh, int space_needed) { - if (bh->offset + space_needed > bh->bin->orig_size) + if (bh->bin && (bh->offset + space_needed > bh->bin->orig_size)) { bh->bin = driver_realloc_binary(bh->bin, bh->offset + space_needed); } + else if (!bh->bin) + { + bh->bin = driver_alloc_binary(space_needed); + } } -void bin_helper_init(BinHelper* bh, unsigned int size) +void bin_helper_init(BinHelper* bh) { - bh->bin = driver_alloc_binary(size); + bh->bin = 0; bh->offset = 0; } diff --git a/c_src/bin_helper.h b/c_src/bin_helper.h index 053509a..195d341 100644 --- a/c_src/bin_helper.h +++ b/c_src/bin_helper.h @@ -15,7 +15,7 @@ typedef struct unsigned int offset; } BinHelper; -void bin_helper_init(BinHelper* bh, unsigned int size); +void bin_helper_init(BinHelper* bh); void bin_helper_push_byte(BinHelper* bh, int value); void bin_helper_push_int32(BinHelper* bh, int value); void bin_helper_push_string(BinHelper* bh, const char* string); diff --git a/src/bdberl.erl b/src/bdberl.erl index abb33da..c8d9eaf 100644 --- a/src/bdberl.erl +++ b/src/bdberl.erl @@ -239,7 +239,7 @@ cursor_close() -> end. delete_database(Filename) -> - Cmd = list_to_binary(Filename), + Cmd = <<(list_to_binary(Filename))/binary, 0:8>>, <> = erlang:port_control(get_port(), ?CMD_REMOVE_DB, Cmd), case decode_rc(Rc) of ok -> diff --git a/test/bdberl_SUITE.erl b/test/bdberl_SUITE.erl index 2f6c665..52f9b8d 100644 --- a/test/bdberl_SUITE.erl +++ b/test/bdberl_SUITE.erl @@ -30,9 +30,11 @@ all() -> delete_should_remove_file, delete_should_fail_if_db_inuse]. + dbconfig(Config) -> Cfg = [{set_data_dir, ?config(priv_dir, Config)}, - {set_flags, 'DB_TXN_NOSYNC'}], + {set_flags, 'DB_TXN_NOSYNC'}, + {set_log_config, 'DB_LOG_IN_MEMORY'}], list_to_binary(lists:flatten([io_lib:format("~s ~s\n", [K,V]) || {K, V} <- Cfg])). @@ -45,7 +47,8 @@ init_per_suite(Config) -> end_per_suite(_Config) -> ok. -init_per_testcase(_TestCase, Config) -> +init_per_testcase(TestCase, Config) -> + ct:print("~p", [TestCase]), {ok, Db} = bdberl:open("api_test.db", btree, [create, exclusive]), [{db, Db}|Config].