sample_offsets.c 3.95 KB
Newer Older
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
1 2
/* Make a db.h that will be link-time compatible with Sleepycat's Berkeley DB. */

Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
3
#include <db.h>
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
4 5
#include <stdio.h>
#include <stdlib.h>
6
#include <assert.h>
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
7 8 9 10 11 12 13 14


#define DECL_LIMIT 100
#define FIELD_LIMIT 100
struct fieldinfo {
    char decl[DECL_LIMIT];
    unsigned int off;
    unsigned int size;
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
15 16
} fields[FIELD_LIMIT];
int field_counter=0;
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
17 18 19 20 21 22 23 24 25 26


int compare_fields (const void *av, const void *bv) {
    const struct fieldinfo *a = av;
    const struct fieldinfo *b = bv;
    if (a->off < b->off) return -1;
    if (a->off > b->off) return 1;
    return 0;
}				      

Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
27 28 29 30 31
#define STRUCT_SETUP(typ, name, fstring) ({ snprintf(fields[field_counter].decl, DECL_LIMIT, fstring, #name); \
	    fields[field_counter].off = __builtin_offsetof(typ, name);       \
            { typ dummy;                                           \
		fields[field_counter].size = sizeof(dummy.name); } \
	    field_counter++; })
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
32

33 34 35 36 37 38 39 40 41
FILE *outf;
void open_file (void) {
    char fname[100];
    snprintf(fname, 100, "sample_offsets_%d.h", __WORDSIZE);
    outf = fopen(fname, "w");
    assert(outf);

}

Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
42
void sort_and_dump_fields (const char *structname, unsigned int sizeofstruct) {
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
43
    int i;
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
44 45 46
    qsort(fields, field_counter, sizeof(fields[0]), compare_fields);
    fprintf(outf, "struct fieldinfo %s_fields%d[] = {\n", structname, __WORDSIZE);
    for (i=0; i<field_counter; i++) {
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
47
	fprintf(outf, "  {\"%s\", %d, %d},\n", fields[i].decl, fields[i].off, fields[i].size);
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
48
    }
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
49
    fprintf(outf, "  {0, 0, %d} /* size of whole struct */\n", sizeofstruct);
50
    fprintf(outf, "};\n");
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
51
}
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

void sample_db_offsets (void) {
    /* Do these in alphabetical order. */
    field_counter=0;
    STRUCT_SETUP(DB,app_private,    "void *%s");
    STRUCT_SETUP(DB,close,          "int (*%s) (DB*, u_int32_t)");
    STRUCT_SETUP(DB,cursor,         "int (*%s) (DB *, DB_TXN *, DBC **, u_int32_t)");
    STRUCT_SETUP(DB,del,            "int (*%s) (DB *, DB_TXN *, DBT *, u_int32_t)");
    STRUCT_SETUP(DB,get,            "int (*%s) (DB *, DB_TXN *, DBT *, DBT *, u_int32_t)");
    STRUCT_SETUP(DB,key_range,      "int (*%s) (DB *, DB_TXN *, DBT *, DB_KEY_RANGE *, u_int32_t)");
    STRUCT_SETUP(DB,open,           "int (*%s) (DB *, DB_TXN *, const char *, const char *, DBTYPE, u_int32_t, int)");
    STRUCT_SETUP(DB,put,            "int (*%s) (DB *, DB_TXN *, DBT *, DBT *, u_int32_t)");
    STRUCT_SETUP(DB,remove,         "int (*%s) (DB *, const char *, const char *, u_int32_t)");
    STRUCT_SETUP(DB,rename,         "int (*%s) (DB *, const char *, const char *, const char *, u_int32_t)");
    STRUCT_SETUP(DB,set_bt_compare, "int (*%s) (DB *, int (*)(DB *, const DBT *, const DBT *))");
    STRUCT_SETUP(DB,set_flags,      "int (*%s) (DB *, u_int32_t)");
    STRUCT_SETUP(DB,stat,           "int (*%s) (DB *, void *, u_int32_t)");
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
69
    sort_and_dump_fields("db", sizeof(DB));
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
70
}
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
71

Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
72 73 74 75 76 77 78
void sample_dbt_offsets (void) {
    field_counter=0;
    STRUCT_SETUP(DBT,app_private, "void*%s");
    STRUCT_SETUP(DBT,data,        "void*%s");
    STRUCT_SETUP(DBT,flags,       "u_int32_t %s");
    STRUCT_SETUP(DBT,size,        "u_int32_t %s");
    STRUCT_SETUP(DBT,ulen,        "u_int32_t %s");
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
79
    sort_and_dump_fields("dbt", sizeof(DBT));
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
80
}
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
81 82 83 84 85 86 87 88

void sample_db_txn_offsets (void) {
    field_counter=0;
    STRUCT_SETUP(DB_TXN, commit,      "int (*%s) (DB_TXN*, u_int32_t)");
    STRUCT_SETUP(DB_TXN, id,          "u_int32_t (*%s) (DB_TXN *)");
    sort_and_dump_fields("db_txn", sizeof(DB_TXN));
}

Bradley C. Kuszmaul's avatar
dbc  
Bradley C. Kuszmaul committed
89 90 91 92 93 94 95 96 97 98
void sample_dbc_offsets (void) {
    field_counter=0;
    STRUCT_SETUP(DBC, c_get,   "int (*%s) (DBC *, DBT *, DBT *, u_int32_t)");
    STRUCT_SETUP(DBC, c_close, "int (*%s) (DBC *)");
    STRUCT_SETUP(DBC, c_del,   "int (*%s) (DBC *, u_int32_t)");
    sort_and_dump_fields("dbc", sizeof(DBC));
}



Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
99
int main (int argc __attribute__((__unused__)), char *argv[] __attribute__((__unused__))) {
100 101
    open_file();
    fprintf(outf, "/* BDB offsets on a %d-bit machine */\n", __WORDSIZE);
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
102 103
    sample_db_offsets();
    sample_dbt_offsets();
Bradley C. Kuszmaul's avatar
txn  
Bradley C. Kuszmaul committed
104
    sample_db_txn_offsets();
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
105
    sample_dbc_offsets();
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
106 107
    return 0;
}