mariadb/buildheader/make_db_h.c
Bradley C. Kuszmaul 2c41f5609a dbc
git-svn-id: file:///svn/tokudb@256 c7de825b-a66e-492c-adef-691d508d4ae1
2007-09-05 19:11:52 +00:00

100 lines
3.4 KiB
C

/* Make a db.h that will be link-time compatible with Sleepycat's Berkeley DB. */
#include <db.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
void print_dbtype(void) {
/* DBTYPE is mentioned by db_open.html */
printf("typedef enum {\n");
printf(" DB_BTREE=%d\n", DB_BTREE);
printf("} DBTYPE;\n");
}
//#define DECL_LIMIT 100
struct fieldinfo {
char *decl;
unsigned int off;
unsigned int size;
};
#include "sample_offsets_32.h"
#include "sample_offsets_64.h"
void print_struct (const char *structname, int need_internal, struct fieldinfo *fields32, struct fieldinfo *fields64, unsigned int N) {
unsigned int i;
unsigned int current_32 = 0;
unsigned int current_64 = 0;
int dummy_counter=0;
int did_toku_internal=0;
// int total32 = fields32[N-1].size;
// int total64 = fields32[N-1].size;
printf("struct __toku_%s {\n", structname);
for (i=0; i<N-1; i++) {
unsigned int this_32 = fields32[i].off;
unsigned int this_64 = fields64[i].off;
assert(strcmp(fields32[i].decl, fields64[i].decl)==0);
//fprintf(stderr, "this32=%d current32=%d this64=%d current64=%d\n", this_32, current_32, this_64, current_64);
if (this_32 > current_32 || this_64 > current_64) {
unsigned int diff32 = this_32-current_32;
unsigned int diff64 = this_64-current_64;
assert(this_32 > current_32 && this_64 > current_64);
if (diff32!=diff64) {
unsigned int diff = diff64-diff32;
unsigned int n_dummys = diff/4;
if (need_internal && !did_toku_internal) {
printf(" struct __toku_%s_internal *i;\n", structname);
n_dummys--;
did_toku_internal=1;
}
if (n_dummys>0) printf(" void* dummy%d[%d];\n", dummy_counter++, n_dummys);
diff64-=diff*2;
diff32-=diff;
}
assert(diff32==diff64);
if (diff32>0) {
printf(" char dummy%d[%d];\n", dummy_counter++, diff32);
}
current_32 = this_32;
current_64 = this_64;
}
if (this_32<current_32 || this_64<current_64) {
printf("Whoops\n");
}
printf(" %s; /* 32-bit offset=%d size=%d, 64=bit offset=%d size=%d */\n", fields32[i].decl, fields32[i].off, fields32[i].size, fields64[i].off, fields64[i].size);
current_32 += fields32[i].size;
current_64 += fields64[i].size;
}
assert(did_toku_internal || !need_internal);
printf("};\n");
}
int main (int argc __attribute__((__unused__)), char *argv[] __attribute__((__unused__))) {
printf("#ifndef _DB_H\n");
printf("#define _DB_H\n");
printf("/* This code generated by make_db_h. Copyright (c) 2007 Tokutek */\n");
printf("#if defined(__cplusplus)\nextern \"C\" {\n#endif\n");
printf("typedef struct __toku_db DB;\n");
print_dbtype();
assert(sizeof(db_fields32)==sizeof(db_fields64));
print_struct("db", 1, db_fields32, db_fields64, sizeof(db_fields32)/sizeof(db_fields32[0]));
assert(sizeof(dbt_fields32)==sizeof(dbt_fields64));
print_struct("dbt", 0, dbt_fields32, dbt_fields64, sizeof(dbt_fields32)/sizeof(dbt_fields32[0]));
assert(sizeof(db_txn_fields32)==sizeof(db_txn_fields64));
print_struct("db_txn", 1, db_txn_fields32, db_txn_fields64, sizeof(db_txn_fields32)/sizeof(db_txn_fields32[0]));
assert(sizeof(dbc_fields32)==sizeof(dbc_fields64));
print_struct("dbc", 1, dbc_fields32, dbc_fields64, sizeof(dbc_fields32)/sizeof(dbc_fields32[0]));
printf("#if defined(__cplusplus)\n}\n#endif\n");
printf("#endif\n");
return 0;
}