mariadb/buildheader/make_db_h.c
Bradley C. Kuszmaul b2193274eb key_range lsn txn_active
git-svn-id: file:///svn/tokudb@264 c7de825b-a66e-492c-adef-691d508d4ae1
2007-09-05 20:16:26 +00:00

127 lines
4.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;
//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* __toku_dummy%d[%d];\n", dummy_counter++, n_dummys);
diff64-=diff*2;
diff32-=diff;
}
assert(diff32==diff64);
if (diff32>0) {
printf(" char __toku_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 this_32=%d this_64=%d\n", this_32, this_64);
}
if (i+1<N) {
assert(strcmp(fields32[i].decl, fields64[i].decl)==0);
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);
} else {
assert(fields32[i].decl==0);
assert(fields64[i].decl==0);
}
current_32 += fields32[i].size;
current_64 += fields64[i].size;
}
{
unsigned int this_32 = fields32[N-1].off;
unsigned int this_64 = fields64[N-1].off;
unsigned int diff32 = this_32-current_32;
unsigned int diff64 = this_64-current_64;
if (diff32>0 && diff32<diff64) {
unsigned int diff = diff64-diff32;
printf(" void* __toku_dummy%d[%d]; /* Padding at the end */ \n", dummy_counter++, diff/4);
diff64-=diff*2;
diff32-=diff;
}
if (diff32>0) {
printf(" char __toku_dummy%d[%d]; /* Padding at the end */ \n", dummy_counter++, diff32);
diff64-=diff32;
diff32=0;
}
if (diff64>0) printf(" /* %d more bytes of alignment in the 64-bit case. */\n", diff64);
assert(diff64<8); /* there could be a few left from alignment. */
}
printf("};\n");
assert(did_toku_internal || !need_internal);
}
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]));
assert(sizeof(db_env_fields32)==sizeof(db_env_fields64));
print_struct("db_env", 1, db_env_fields32, db_env_fields64, sizeof(db_env_fields32)/sizeof(db_env_fields32[0]));
printf("#if defined(__cplusplus)\n}\n#endif\n");
printf("#endif\n");
return 0;
}