From ccac8cbcac795c1e3fffb8d796b01b104ba30eb4 Mon Sep 17 00:00:00 2001 From: "Bradley C. Kuszmaul" Date: Tue, 16 Apr 2013 23:57:34 -0400 Subject: [PATCH] Make cxx compile. Addresses #1032, #1343, #1328. git-svn-id: file:///svn/toku/tokudb.1032b+1343@8459 c7de825b-a66e-492c-adef-691d508d4ae1 --- cxx/tests/db_dump.cpp | 5 +- cxx/tests/db_dump_e.cpp | 5 +- cxx/tests/db_load.cpp | 7 +-- cxx/tests/exceptions.cpp | 5 +- cxx/tests/test_cursor_count.cpp | 65 +++++++++++++------------- cxx/tests/test_db_assoc3.cpp | 27 +++++------ cxx/tests/test_reverse_compare_fun.cpp | 5 +- include/memory.h | 8 ++++ 8 files changed, 71 insertions(+), 56 deletions(-) diff --git a/cxx/tests/db_dump.cpp b/cxx/tests/db_dump.cpp index e6fa4cb0078..bbd31243a41 100644 --- a/cxx/tests/db_dump.cpp +++ b/cxx/tests/db_dump.cpp @@ -1,6 +1,7 @@ #include #include #include +#include static void hexdump(Dbt *d) { unsigned char *cp = (unsigned char *) d->get_data(); @@ -55,8 +56,8 @@ static int dbdump(char *dbfile, char *dbname) { // printf("%.*s\n", val.get_size(), (char *)val.get_data()); hexdump(&val); } - if (key.get_data()) free(key.get_data()); - if (val.get_data()) free(val.get_data()); + if (key.get_data()) toku_free(key.get_data()); + if (val.get_data()) toku_free(val.get_data()); r = cursor->close(); assert(r == 0); r = db.close(0); assert(r == 0); diff --git a/cxx/tests/db_dump_e.cpp b/cxx/tests/db_dump_e.cpp index 9fa9fcbcbdb..b65f0ffbf05 100644 --- a/cxx/tests/db_dump_e.cpp +++ b/cxx/tests/db_dump_e.cpp @@ -2,6 +2,7 @@ #include #include #include +#include static void hexdump(Dbt *d) { unsigned char *cp = (unsigned char *) d->get_data(); @@ -51,8 +52,8 @@ static int dbdump(char *dbfile, char *dbname) { } catch (DbException ) { /* Nothing, that's just how we got out of the loop. */ } - free(key.get_data()); - free(val.get_data()); + toku_free(key.get_data()); + toku_free(val.get_data()); r = cursor->close(); assert(r == 0); r = db.close(0); assert(r == 0); diff --git a/cxx/tests/db_load.cpp b/cxx/tests/db_load.cpp index 04ede3bfe26..636b4e71e31 100644 --- a/cxx/tests/db_load.cpp +++ b/cxx/tests/db_load.cpp @@ -1,6 +1,7 @@ #include #include #include +#include static inline void hexdump(Dbt *d) { unsigned char *cp = (unsigned char *) d->get_data(); @@ -17,7 +18,7 @@ static void hexput(Dbt *d, int c) { int ulen = d->get_ulen(); if (n+1 >= ulen) { int newulen = ulen == 0 ? 1 : ulen*2; - cp = (unsigned char *) realloc(cp, newulen); assert(cp); + cp = (unsigned char *) toku_realloc(cp, newulen); assert(cp); d->set_data(cp); d->set_ulen(newulen); } @@ -77,8 +78,8 @@ static int dbload(char *dbfile, char *dbname) { r = db.put(0, &key, &val, 0); assert(r == 0); } - if (key.get_data()) free(key.get_data()); - if (val.get_data()) free(val.get_data()); + if (key.get_data()) toku_free(key.get_data()); + if (val.get_data()) toku_free(val.get_data()); r = db.close(0); assert(r == 0); #if defined(USE_ENV) && USE_ENV diff --git a/cxx/tests/exceptions.cpp b/cxx/tests/exceptions.cpp index 52d103c72b1..a1da4c75db3 100644 --- a/cxx/tests/exceptions.cpp +++ b/cxx/tests/exceptions.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #define FNAME __FILE__ ".tdb" #define FNAME2 __FILE__ "2.tdb" @@ -162,8 +163,8 @@ static void test_dbc_exceptions () { Dbt key; key.set_flags(DB_DBT_MALLOC); Dbt val; val.set_flags(DB_DBT_MALLOC); TC(curs->get(&key, &val, DB_FIRST), 0); - free(key.get_data()); - free(val.get_data()); + toku_free(key.get_data()); + toku_free(val.get_data()); TC(curs->del(DB_DELETE_ANY), 0); TCRET(curs->get(&key, &val, DB_CURRENT), DB_KEYEMPTY); TCRET(curs->del(0), DB_KEYEMPTY); diff --git a/cxx/tests/test_cursor_count.cpp b/cxx/tests/test_cursor_count.cpp index 22a5fef80f4..39bdf745b13 100644 --- a/cxx/tests/test_cursor_count.cpp +++ b/cxx/tests/test_cursor_count.cpp @@ -4,6 +4,7 @@ #include #include #include +#include int verbose; @@ -42,10 +43,10 @@ int my_cursor_count(Dbc *cursor, db_recno_t *count, Db *db) { if (!keyeq(&key, &nkey)) break; } r = 0; - if (nkey.get_data()) free(nkey.get_data()); - if (nval.get_data()) free(nval.get_data()); - if (key.get_data()) free(key.get_data()); - if (val.get_data()) free(val.get_data()); + if (nkey.get_data()) toku_free(nkey.get_data()); + if (nval.get_data()) toku_free(nval.get_data()); + if (key.get_data()) toku_free(key.get_data()); + if (val.get_data()) toku_free(val.get_data()); int rr = count_cursor->close(); assert(rr == 0); return r; } @@ -62,10 +63,10 @@ int my_next_nodup(Dbc *cursor, Dbt *key, Dbt *val) { if (r != 0) break; if (!keyeq(¤tkey, &nkey)) break; } - if (nkey.get_data()) free(nkey.get_data()); - if (nval.get_data()) free(nval.get_data()); - if (currentkey.get_data()) free(currentkey.get_data()); - if (currentval.get_data()) free(currentval.get_data()); + if (nkey.get_data()) toku_free(nkey.get_data()); + if (nval.get_data()) toku_free(nval.get_data()); + if (currentkey.get_data()) toku_free(currentkey.get_data()); + if (currentval.get_data()) toku_free(currentval.get_data()); if (r == 0) r = cursor->get(key, val, DB_CURRENT); return r; } @@ -82,10 +83,10 @@ int my_prev_nodup(Dbc *cursor, Dbt *key, Dbt *val) { if (r != 0) break; if (!keyeq(¤tkey, &nkey)) break; } - if (nkey.get_data()) free(nkey.get_data()); - if (nval.get_data()) free(nval.get_data()); - if (currentkey.get_data()) free(currentkey.get_data()); - if (currentval.get_data()) free(currentval.get_data()); + if (nkey.get_data()) toku_free(nkey.get_data()); + if (nval.get_data()) toku_free(nval.get_data()); + if (currentkey.get_data()) toku_free(currentkey.get_data()); + if (currentval.get_data()) toku_free(currentval.get_data()); if (r == 0) r = cursor->get(key, val, DB_CURRENT); return r; } @@ -99,10 +100,10 @@ int my_next_dup(Dbc *cursor, Dbt *key, Dbt *val) { Dbt nval; nval.set_flags(DB_DBT_REALLOC); r = cursor->get(&nkey, &nval, DB_NEXT); if (r == 0 && !keyeq(¤tkey, &nkey)) r = DB_NOTFOUND; - if (nkey.get_data()) free(nkey.get_data()); - if (nval.get_data()) free(nval.get_data()); - if (currentkey.get_data()) free(currentkey.get_data()); - if (currentval.get_data()) free(currentval.get_data()); + if (nkey.get_data()) toku_free(nkey.get_data()); + if (nval.get_data()) toku_free(nval.get_data()); + if (currentkey.get_data()) toku_free(currentkey.get_data()); + if (currentval.get_data()) toku_free(currentval.get_data()); if (r == 0) r = cursor->get(key, val, DB_CURRENT); return r; } @@ -116,10 +117,10 @@ int my_prev_dup(Dbc *cursor, Dbt *key, Dbt *val) { Dbt nval; nval.set_flags(DB_DBT_REALLOC); r = cursor->get(&nkey, &nval, DB_PREV); if (r == 0 && !keyeq(¤tkey, &nkey)) r = DB_NOTFOUND; - if (nkey.get_data()) free(nkey.get_data()); - if (nval.get_data()) free(nval.get_data()); - if (currentkey.get_data()) free(currentkey.get_data()); - if (currentval.get_data()) free(currentval.get_data()); + if (nkey.get_data()) toku_free(nkey.get_data()); + if (nval.get_data()) toku_free(nval.get_data()); + if (currentkey.get_data()) toku_free(currentkey.get_data()); + if (currentval.get_data()) toku_free(currentval.get_data()); if (r == 0) r = cursor->get(key, val, DB_CURRENT); return r; } @@ -153,8 +154,8 @@ void test_cursor_count_flags(Db *db) { Dbt key; key.set_flags(DB_DBT_MALLOC); Dbt val; val.set_flags(DB_DBT_MALLOC); r = cursor->get(&key, &val, DB_FIRST); assert(r == 0); - if (key.get_data()) free(key.get_data()); - if (val.get_data()) free(val.get_data()); + if (key.get_data()) toku_free(key.get_data()); + if (val.get_data()) toku_free(val.get_data()); db_recno_t n; r = cursor->count(&n, 1); assert(r == EINVAL); r = cursor->count(&n, 0); assert(r == 0); @@ -191,8 +192,8 @@ void walk(Db *db, int n) { if (k == n/2) assert((int)count == n); else assert(count == 1); } assert(i == 2*n-1); - free(key.get_data()); - free(val.get_data()); + toku_free(key.get_data()); + toku_free(val.get_data()); r = cursor->close(); assert(r == 0); } @@ -228,8 +229,8 @@ void test_zero_count(Db *db, int n) { if (r != 0) break; } assert(i == n); - if (key.get_data()) free(key.get_data()); - if (val.get_data()) free(val.get_data()); + if (key.get_data()) toku_free(key.get_data()); + if (val.get_data()) toku_free(val.get_data()); r = cursor->close(); assert(r == 0); } @@ -253,8 +254,8 @@ void test_next_nodup(Db *db, int n) { r = cursor->get(&key, &val, DB_NEXT_NODUP); } assert(i == n); - if (key.get_data()) free(key.get_data()); - if (val.get_data()) free(val.get_data()); + if (key.get_data()) toku_free(key.get_data()); + if (val.get_data()) toku_free(val.get_data()); r = cursor->close(); assert(r == 0); } @@ -278,8 +279,8 @@ void test_prev_nodup(Db *db, int n) { r = cursor->get(&key, &val, DB_PREV_NODUP); } assert(i == -1); - if (key.get_data()) free(key.get_data()); - if (val.get_data()) free(val.get_data()); + if (key.get_data()) toku_free(key.get_data()); + if (val.get_data()) toku_free(val.get_data()); r = cursor->close(); assert(r == 0); } @@ -319,8 +320,8 @@ void test_next_dup(Db *db, int n) { } assert(i == 0); #endif - if (key.get_data()) free(key.get_data()); - if (val.get_data()) free(val.get_data()); + if (key.get_data()) toku_free(key.get_data()); + if (val.get_data()) toku_free(val.get_data()); r = cursor->close(); assert(r == 0); } diff --git a/cxx/tests/test_db_assoc3.cpp b/cxx/tests/test_db_assoc3.cpp index 5ee6d04407c..b7f5217ab3b 100644 --- a/cxx/tests/test_db_assoc3.cpp +++ b/cxx/tests/test_db_assoc3.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include "../../src/tests/test.h" @@ -41,8 +42,8 @@ struct primary_data { }; void free_pd (struct primary_data *pd) { - free(pd->name.name); - free(pd); + toku_free(pd->name.name); + toku_free(pd); } void write_uchar_to_dbt (Dbt *dbt, const unsigned char c) { @@ -109,7 +110,7 @@ void read_name_from_dbt (const Dbt *dbt, int *off, struct name_key *nk) { read_uchar_from_dbt(dbt, off, &buf[i]); if (buf[i]==0) break; } - nk->name=(unsigned char*)(strdup((char*)buf)); + nk->name=(unsigned char*)(toku_strdup((char*)buf)); } void read_pd_from_dbt (const Dbt *dbt, int *off, struct primary_data *pd) { @@ -123,7 +124,7 @@ int name_offset_in_pd_dbt (void) { } int name_callback (Db *secondary, const Dbt *key, const Dbt *data, Dbt *result) { - struct primary_data *pd = (struct primary_data *) malloc(sizeof(*pd)); + struct primary_data *pd = (struct primary_data *) toku_malloc(sizeof(*pd)); int off=0; read_pd_from_dbt(data, &off, pd); static int buf[1000]; @@ -158,9 +159,9 @@ static void dbg_name_insert (unsigned char *name) { name = (unsigned char*)strdup((char*)name); n_names++; if (names==0) { - names=malloc(sizeof(*names)); + names=toku_malloc(sizeof(*names)); } else { - names = realloc(names, n_names*sizeof(*names)); + names = toku_realloc(names, n_names*sizeof(*names)); } names[n_names-1]=name; } @@ -228,8 +229,8 @@ void close_databases (void) { if (name_cursor) { r = name_cursor->close(); CKERR(r); } - if (nc_key.get_data()) free(nc_key.get_data()); - if (nc_data.get_data()) free(nc_data.get_data()); + if (nc_key.get_data()) toku_free(nc_key.get_data()); + if (nc_data.get_data()) toku_free(nc_data.get_data()); r = namedb->close(0); CKERR(r); delete namedb; r = dbp->close(0); CKERR(r); delete dbp; r = expiredb->close(0); CKERR(r); delete expiredb; @@ -388,7 +389,7 @@ void delete_oldest_expired (void) { count_all_items--; } savepkey = pkey; - savepkey.set_data(malloc(pkey.get_size())); + savepkey.set_data(toku_malloc(pkey.get_size())); memcpy(savepkey.get_data(), pkey.get_data(), pkey.get_size()); switch (r3) { case 0: @@ -409,7 +410,7 @@ void delete_oldest_expired (void) { assert(r==DB_KEYEMPTY); r = dbp->get(null_txn, &savepkey, &data, 0); assert(r==DB_NOTFOUND); - free(savepkey.get_data()); + toku_free(savepkey.get_data()); } // Use a cursor to step through the names. @@ -430,7 +431,7 @@ void step_name (void) { assert(cursor_count_n_items==calc_n_items); r = name_cursor->get(&nc_key, &nc_data, DB_FIRST); if (r==DB_NOTFOUND) { - nc_key.set_data(realloc(nc_key.get_data(), 1)); + nc_key.set_data(toku_realloc(nc_key.get_data(), 1)); ((char*)nc_key.get_data())[0]=0; cursor_count_n_items=0; } else { @@ -490,10 +491,10 @@ int test_main (int argc, const char *argv[]) { } nc_key.set_flags(DB_DBT_REALLOC); - nc_key.set_data(malloc(1)); // Iniitalize it. + nc_key.set_data(toku_malloc(1)); // Iniitalize it. ((char*)nc_key.get_data())[0]=0; nc_data.set_flags(DB_DBT_REALLOC); - nc_data.set_data(malloc(1)); // Iniitalize it. + nc_data.set_data(toku_malloc(1)); // Iniitalize it. mode = MODE_DEFAULT; diff --git a/cxx/tests/test_reverse_compare_fun.cpp b/cxx/tests/test_reverse_compare_fun.cpp index 70fb5fe8213..a2a5b6b595e 100644 --- a/cxx/tests/test_reverse_compare_fun.cpp +++ b/cxx/tests/test_reverse_compare_fun.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #define DIR __FILE__ ".dir" int verbose; @@ -48,8 +49,8 @@ void expect(Dbc *cursor, int k, int v) { assert(kk == k); assert(vv == v); - free(key.get_data()); - free(val.get_data()); + toku_free(key.get_data()); + toku_free(val.get_data()); } void test_reverse_compare(int n, int dup_flags) { diff --git a/include/memory.h b/include/memory.h index c08f6138f1b..0c76b021624 100644 --- a/include/memory.h +++ b/include/memory.h @@ -1,6 +1,10 @@ #ifndef MEMORY_H #define MEMORY_H +#if defined __cplusplus +extern "C" { +#endif + #ident "Copyright (c) 2007 Tokutek Inc. All rights reserved." #include @@ -99,4 +103,8 @@ extern int toku_realloc_counter; extern int toku_calloc_counter; extern int toku_free_counter; +#if defined __cplusplus +}; +#endif + #endif