make_db_h.c 2.5 KB
Newer Older
1 2 3 4 5
/* Make a db.h that will be link-time compatible with Sleepycat's Berkeley DB. */

#include <db.h>
#include <stdio.h>
#include <stdlib.h>
6 7
#include <assert.h>
#include <string.h>
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22


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[DECL_LIMIT];
    unsigned int off;
    unsigned int size;
23
};
24

25 26
#include "sample_offsets_32.h"
#include "sample_offsets_64.h"
27 28

void print_db_struct (void) {
29 30 31
    unsigned int i;
    unsigned int current_32 = 0;
    unsigned int current_64 = 0;
32
    int dummy_counter=0;
33
//    int did_toku_internal=0;
34
    printf("struct __toku_db {\n");
35 36 37 38 39 40
    assert(sizeof(fields32)==sizeof(fields64));
    for (i=0; i<sizeof(fields32)/sizeof(fields32[0]); i++) {
	unsigned int this_32 = fields32[i].off;
	unsigned int this_64 = fields64[i].off;
	assert(strcmp(fields32[i].decl, fields64[i].decl)==0);
#if 0
41 42 43 44 45
	if (!did_toku_internal && this_offset+sizeof(void*)>current_offset) {
	    printf("  struct __tokudb_internal *i;\n");
	    current_offset+=sizeof(void*);
	    did_toku_internal=1;
	}
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#endif
	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;
		printf("  void* dummy%d[%d];\n", dummy_counter++, diff/4);
		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;
64
	}
65
	if (this_32<current_32 || this_64<current_64) {
66 67
	    printf("Whoops\n");
	}
68 69 70
	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;
71
    }
72
    printf("};\n");
73
}
74

75 76 77 78 79 80 81 82 83 84 85 86
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("#if defined(__cplusplus)\n}\n#endif\n");
    printf("typedef struct __toku_db DB;\n");
    print_dbtype();
    print_db_struct();
    printf("#endif\n");
    return 0;
}