Permalink
Browse files

lib: fix compiler errors

* makefile: compile test/api with CXX
  • Loading branch information...
indutny committed Jan 20, 2012
1 parent c168ff3 commit 055a9ba64be6c681fdb306ffea0b163ea8a55364
Showing with 54 additions and 52 deletions.
  1. +3 −3 Makefile
  2. +1 −1 deps/snappy/snappy-c.h
  3. +4 −4 include/private/pages.h
  4. +1 −6 include/private/utils.h
  5. +11 −12 src/bplus.c
  6. +21 −18 src/pages.c
  7. +12 −7 src/writer.c
  8. +1 −1 test/{api.c → api.cc}
View
@@ -8,8 +8,8 @@ all: bplus.a
test: test/api
test/api
-test/api: bplus.a test/api.c
- g++ $(CFLAGS) $(CPPFLAGS) $(LINKFLAGS) test/api.c -o test/api \
+test/api: bplus.a test/api.cc
+ $(CXX) $(CFLAGS) $(CPPFLAGS) $(LINKFLAGS) test/api.cc -o test/api \
bplus.a
OBJS =
@@ -32,7 +32,7 @@ bplus.a: $(OBJS)
$(AR) rcs bplus.a $(OBJS)
src/%.o: src/%.c $(DEPS)
- $(CC) $(CFLAGS) $(CPPFLAGS) $(DEFINES) -Iinclude/private -c $< -o $@
+ $(CC) $(CFLAGS) $(CSTDFLAG) $(CPPFLAGS) -Iinclude/private -c $< -o $@
deps/snappy/%.o: deps/snappy/%.cc
$(CC) $(CFLAGS) $(CPPFLAGS) $(DEFINES) -Iinclude/private -c $< -o $@
View
@@ -46,7 +46,7 @@ extern "C" {
typedef enum {
SNAPPY_OK = 0,
SNAPPY_INVALID_INPUT = 1,
- SNAPPY_BUFFER_TOO_SMALL = 2,
+ SNAPPY_BUFFER_TOO_SMALL = 2
} snappy_status;
/*
View
@@ -25,10 +25,10 @@ int bp__page_destroy(bp_tree_t* t, bp__page_t* page);
int bp__page_load(bp_tree_t* t, bp__page_t* page);
int bp__page_save(bp_tree_t* t, bp__page_t* page);
-inline int bp__page_search(bp_tree_t* t,
- bp__page_t* page,
- const bp__kv_t* kv,
- bp__page_search_res_t* result);
+int bp__page_search(bp_tree_t* t,
+ bp__page_t* page,
+ const bp__kv_t* kv,
+ bp__page_search_res_t* result);
int bp__page_get(bp_tree_t* t,
bp__page_t* page,
const bp__kv_t* kv,
View
@@ -7,7 +7,7 @@ extern "C" {
/* Thomas Wang, Integer Hash Functions. */
/* http://www.concentric.net/~Ttwang/tech/inthash.htm */
-inline uint32_t bp__compute_hash(uint32_t key) {
+uint32_t bp__compute_hash(uint32_t key) {
uint32_t hash = key;
hash = ~hash + (hash << 15); /* hash = (hash << 15) - hash - 1; */
hash = hash ^ (hash >> 12);
@@ -18,11 +18,6 @@ inline uint32_t bp__compute_hash(uint32_t key) {
return hash;
}
-
-inline uint32_t bp__read_uint32be(uint8_t* data) {
- return (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3];
-}
-
#ifdef __cplusplus
} // extern "C"
#endif
View
@@ -46,9 +46,9 @@ int bp_get(bp_tree_t* tree, const bp_key_t* key, bp_value_t* value) {
int bp_set(bp_tree_t* tree, const bp_key_t* key, const bp_value_t* value) {
+ int ret;
bp__kv_t kv;
- int ret;
ret = bp__writer_write((bp__writer_t*) tree, value->length, value->value,
&kv.offset, &kv.config);
if (ret) return ret;
@@ -73,13 +73,13 @@ int bp_remove(bp_tree_t* tree, const bp_key_t* key) {
int bp_gets(bp_tree_t* tree, const char* key, char** value) {
+ int ret;
bp_key_t bkey;
+ bp_value_t bvalue;
+
bkey.value = (char*) key;
bkey.length = strlen(key);
- bp_value_t bvalue;
-
- int ret;
ret = bp_get(tree, &bkey, &bvalue);
if (ret) return ret;
@@ -91,10 +91,11 @@ int bp_gets(bp_tree_t* tree, const char* key, char** value) {
int bp_sets(bp_tree_t* tree, const char* key, const char* value) {
bp_key_t bkey;
+ bp_value_t bvalue;
+
bkey.value = (char*) key;
bkey.length = strlen(key);
- bp_value_t bvalue;
bvalue.value = (char*) value;
bvalue.length = strlen(value);
@@ -131,17 +132,18 @@ int bp__tree_read_head(bp__writer_t* w, void* data) {
if (t->head_page == NULL) {
bp__page_create(t, 1, t->head.offset, t->head.config, &t->head_page);
}
- int ret = bp__page_load(t, t->head_page);
-
- return ret;
+ return bp__page_load(t, t->head_page);
}
int bp__tree_write_head(bp__writer_t* w, void* data) {
+ int ret;
bp_tree_t* t = (bp_tree_t*) w;
bp__tree_head_t* head = data;
+ bp__tree_head_t nhead;
+ uint32_t offset;
+ uint32_t csize;
- int ret;
if (t->head_page == NULL) {
/* TODO: page size should be configurable */
head->page_size = 64;
@@ -158,13 +160,10 @@ int bp__tree_write_head(bp__writer_t* w, void* data) {
head->hash = bp__compute_hash(head->offset);
/* Create temporary head with fields in network byte order */
- bp__tree_head_t nhead;
nhead.offset = htonl(head->offset);
nhead.config = htonl(head->config);
nhead.page_size = htonl(head->page_size);
nhead.hash = htonl(head->hash);
- uint32_t offset;
- uint32_t csize;
return bp__writer_write(w, sizeof(nhead), &nhead, &offset, &csize);
}
View
@@ -41,7 +41,7 @@ int bp__page_create(bp_tree_t* t,
int bp__page_destroy(bp_tree_t* t, bp__page_t* page) {
/* Free all keys */
- int i = 0;
+ uint32_t i = 0;
for (i = 0; i < page->length; i++) {
if (page->keys[i].value != NULL &&
page->keys[i].allocated) {
@@ -60,21 +60,23 @@ int bp__page_destroy(bp_tree_t* t, bp__page_t* page) {
int bp__page_load(bp_tree_t* t, bp__page_t* page) {
+ int ret;
+ uint32_t size, i, o;
bp__writer_t* w = (bp__writer_t*) t;
- int ret;
+ char* buff;
/* Read page size and leaf flag */
- uint32_t size = page->config & 0x7fffffff;
+ size = page->config & 0x7fffffff;
page->is_leaf = page->config >> 31;
/* Read page data */
- char* buff;
ret = bp__writer_read(w, page->offset, &size, (void**) &buff);
if (ret) return ret;
/* Parse data */
- uint32_t i = 0, o = 0;
+ i = 0;
+ o = 0;
while (o < size) {
page->keys[i].length = ntohl(*(uint32_t*) (buff + o));
page->keys[i].offset = ntohl(*(uint32_t*) (buff + o + 4));
@@ -98,13 +100,15 @@ int bp__page_load(bp_tree_t* t, bp__page_t* page) {
int bp__page_save(bp_tree_t* t, bp__page_t* page) {
+ int ret;
bp__writer_t* w = (bp__writer_t*) t;
+ uint32_t i, o;
/* Allocate space for serialization (header + keys); */
char* buff = malloc(page->byte_size);
if (buff == NULL) return BP_EALLOC;
- uint32_t i, o = 0;
+ o = 0;
for (i = 0; i < page->length; i++) {
assert(o + BP__KV_SIZE(page->keys[i]) <= page->byte_size);
@@ -118,7 +122,6 @@ int bp__page_save(bp_tree_t* t, bp__page_t* page) {
}
assert(o == page->byte_size);
- int ret = 0;
ret = bp__writer_write(w,
page->byte_size,
buff,
@@ -133,12 +136,14 @@ int bp__page_save(bp_tree_t* t, bp__page_t* page) {
}
-inline int bp__page_search(bp_tree_t* t,
- bp__page_t* page,
- const bp__kv_t* kv,
- bp__page_search_res_t* result) {
+int bp__page_search(bp_tree_t* t,
+ bp__page_t* page,
+ const bp__kv_t* kv,
+ bp__page_search_res_t* result) {
+ int ret;
uint32_t i = page->is_leaf ? 0 : 1;
int cmp = -1;
+ bp__page_t* child;
while (i < page->length) {
/* left key is always lower in non-leaf nodes */
@@ -156,11 +161,10 @@ inline int bp__page_search(bp_tree_t* t,
return BP_OK;
} else {
+
assert(i > 0);
i--;
- bp__page_t* child;
- int ret;
ret = bp__page_create(t,
0,
page->keys[i].offset,
@@ -323,18 +327,18 @@ int bp__page_split(bp_tree_t* t,
bp__page_t* parent,
const uint32_t index,
bp__page_t* child) {
+ int ret;
+ uint32_t i, middle;
bp__page_t* left;
bp__page_t* right;
+ bp__kv_t middle_key;
bp__page_create(t, child->is_leaf, 0, 0, &left);
bp__page_create(t, child->is_leaf, 0, 0, &right);
- uint32_t middle = t->head.page_size >> 1;
- bp__kv_t middle_key;
+ middle = t->head.page_size >> 1;
bp__kv_copy(&child->keys[middle], &middle_key, 1);
- uint32_t i;
-
/* non-leaf nodes has byte_size > 0 nullify it first */
left->byte_size = 0;
for (i = 0; i < middle; i++) {
@@ -351,7 +355,6 @@ int bp__page_split(bp_tree_t* t,
right->length = middle;
/* save left and right parts to get offsets */
- int ret;
ret = bp__page_save(t, left);
if (ret) return ret;
View
@@ -20,13 +20,17 @@ int bp__writer_create(bp__writer_t* w, const char* filename) {
int bp__writer_destroy(bp__writer_t* w) {
if (fclose(w->fd)) return BP_EFILE;
+ return BP_OK;
}
int bp__writer_read(bp__writer_t* w,
const uint32_t offset,
uint32_t* size,
void** data) {
+ size_t read;
+ char* cdata;
+
if (w->filesize < offset + *size) return BP_EFILEREAD_OOB;
/* flush any pending data before reading */
@@ -40,9 +44,7 @@ int bp__writer_read(bp__writer_t* w,
if (fseeko(w->fd, offset, SEEK_SET)) return BP_EFILE;
- size_t read;
-
- char* cdata = malloc(*size);
+ cdata = malloc(*size);
if (cdata == NULL) return BP_EALLOC;
read = fread(cdata, 1, *size, w->fd);
@@ -112,12 +114,14 @@ int bp__writer_write(bp__writer_t* w,
written = fwrite(data, 1, size, w->fd);
*csize = size;
} else {
+ int ret;
size_t max_csize = snappy_max_compressed_length(size);
+ size_t result_size;
char* compressed = malloc(max_csize);
if (compressed == NULL) return BP_EALLOC;
- size_t result_size = max_csize;
- int ret = snappy_compress(data, size, compressed, &result_size);
+ result_size = max_csize;
+ ret = snappy_compress(data, size, compressed, &result_size);
if (ret != SNAPPY_OK) {
free(compressed);
return BP_ESNAPPYC;
@@ -144,13 +148,14 @@ int bp__writer_find(bp__writer_t* w,
bp__writer_cb seek,
bp__writer_cb miss) {
int ret = 0;
+ uint32_t offset, size_tmp;
/* Write padding first */
ret = bp__writer_write(w, 0, NULL, NULL, NULL);
if (ret) return ret;
- uint32_t offset = w->filesize;
- uint32_t size_tmp = size;
+ offset = w->filesize;
+ size_tmp = size;
/* Start seeking from bottom of file */
while (offset >= size) {
@@ -26,7 +26,7 @@ int main(void) {
bp_set_compare_cb(&tree, compare_cb);
- const int n = 1000;
+ const int n = 10000;
int i = 0;
for (;i < n; i++) {
char key[1000];

0 comments on commit 055a9ba

Please sign in to comment.