bool setup_test(size_t object_size) { object_t* object = benchmark_object_create(object_size); binn root; bool ok; if (object->type == type_map) { binn_create_object(&root); ok = write_object(&root, object); } else { binn_create_list(&root); ok = write_list(&root, object); } object_destroy(object); if (!ok) { fprintf(stderr, "binn error writing data!\n"); binn_free(&root); return false; } char filename[64]; benchmark_filename(filename, sizeof(filename), object_size, BENCHMARK_FORMAT_BINN, NULL); FILE* file = fopen(filename, "wb"); fwrite(binn_ptr(&root), binn_size(&root), 1, file); fclose(file); // like libbson, we call binn_free() regardless of whether // we placed it on the stack or the heap; it stores a flag // to say whether it should be freed. this apparently doesn't // cause warnings since GCC chooses not to inline binn_free(). binn_free(&root); return true; }
static bool write_object(binn* parent, object_t* object) { for (size_t i = 0; i < object->l; ++i) { char* key = CONST_CAST(object->children[i * 2].str); object_t* value = object->children + i * 2 + 1; switch (value->type) { case type_nil: if (!binn_object_set_null(parent, key)) return false; break; case type_bool: if (!binn_object_set_bool(parent, key, (BOOL)value->b)) return false; break; case type_double: if (!binn_object_set_double(parent, key, value->d)) return false; break; case type_int: if (!binn_object_set_int64(parent, key, value->i)) return false; break; case type_uint: if (!binn_object_set_uint64(parent, key, value->u)) return false; break; case type_str: if (!binn_object_set_str(parent, key, CONST_CAST(value->str))) return false; break; case type_array: { binn child; binn_create_list(&child); bool ok = write_list(&child, value); if (ok) ok = binn_object_set_object(parent, key, &child); binn_free(&child); if (!ok) return false; } break; case type_map: { binn child; binn_create_object(&child); bool ok = write_object(&child, value); if (ok) ok = binn_object_set_object(parent, key, &child); binn_free(&child); if (!ok) return false; } break; default: return false; } } return true; }
int dsm_send_msg(int nodefd, dsm_message_t *msg) { binn *obj; obj = binn_object(); binn_object_set_int32(obj, DSM_MSG_KEY_TYPE, msg->type); switch (msg->type) { case CONNECT: binn_object_set_int32(obj, DSM_MSG_KEY_BITNESS, msg->connect_args.bitness); binn_object_set_int32(obj, DSM_MSG_KEY_PAGESIZE, msg->connect_args.pagesize); break; case CONNECT_ACK: binn_object_set_int16(obj, DSM_MSG_KEY_BITNESS, msg->connect_ack_args.bitness_ok); binn_object_set_int16(obj, DSM_MSG_KEY_PAGESIZE, msg->connect_ack_args.pagesize_ok); binn_object_set_int32(obj, DSM_MSG_KEY_PAGECOUNT, msg->connect_ack_args.page_count); break; case LOCKPAGE: binn_object_set_int32(obj, DSM_MSG_KEY_PAGEID, msg->lockpage_args.page_id); binn_object_set_int16(obj, DSM_MSG_KEY_RIGHTS, msg->lockpage_args.access_rights); break; case INVALIDATE: binn_object_set_int32(obj, DSM_MSG_KEY_PAGEID, msg->invalidate_args.page_id); break; case INVALIDATE_ACK: binn_object_set_int32(obj, DSM_MSG_KEY_PAGEID, msg->invalidate_ack_args.page_id); break; case GIVEPAGE: binn_object_set_int32(obj, DSM_MSG_KEY_PAGEID, msg->givepage_args.page_id); binn_object_set_int16(obj, DSM_MSG_KEY_RIGHTS, msg->givepage_args.access_rights); binn_object_set_blob(obj, DSM_MSG_KEY_DATA, msg->givepage_args.data, dsm_g->mem->pagesize); break; case SYNC_BARRIER: binn_object_set_int16(obj, DSM_MSG_KEY_BARRIER, msg->sync_barrier_args.slave_to_wait); case BARRIER_ACK: break; case TERMINATE: break; default: log("Unknown message type to send !\n"); return -1; } if(dsm_send(nodefd, binn_ptr(obj), binn_size(obj)) < 0) { log("Could not send to node %d message type: %d\n", nodefd, msg->type); return -1; } binn_free(obj); return 0; }
int dsm_receive_msg(int nodefd, dsm_message_t *msg) { binn *obj; int blob_size; void* buffer; int buffersize; //char buffer[BUFFER_LEN]; //void* ptr = (void *) &buffer; if(dsm_g->mem == NULL) { buffersize = MIN_BUFFERSIZE; } else { buffersize = MIN_BUFFERSIZE + dsm_g->mem->pagesize; } /* We cant receive a page if the mem struct isn't yet initialized anyway */ buffer = malloc(buffersize); if (buffer == NULL) { error("Could not allocate memory (malloc)\n"); } if(dsm_receive(nodefd, buffer) < 0) { debug("dsm_receive 0 byte, node disconnected\n"); return -1; } obj = binn_open(buffer); msg->type = binn_object_int32(obj, DSM_MSG_KEY_TYPE); switch (msg->type) { case CONNECT: msg->connect_args.bitness = binn_object_int32(obj, DSM_MSG_KEY_BITNESS); msg->connect_args.pagesize = binn_object_int32(obj, DSM_MSG_KEY_PAGESIZE); break; case CONNECT_ACK: msg->connect_ack_args.bitness_ok = binn_object_int16(obj, DSM_MSG_KEY_BITNESS); msg->connect_ack_args.pagesize_ok = binn_object_int16(obj, DSM_MSG_KEY_PAGESIZE); msg->connect_ack_args.page_count = binn_object_int32(obj, DSM_MSG_KEY_PAGECOUNT); break; case LOCKPAGE: msg->lockpage_args.page_id = binn_object_int32(obj, DSM_MSG_KEY_PAGEID); msg->lockpage_args.access_rights = binn_object_int16(obj, DSM_MSG_KEY_RIGHTS); break; case INVALIDATE: msg->invalidate_args.page_id = binn_object_int32(obj, DSM_MSG_KEY_PAGEID); break; case INVALIDATE_ACK: msg->invalidate_ack_args.page_id = binn_object_int32(obj, DSM_MSG_KEY_PAGEID); break; case GIVEPAGE: msg->givepage_args.page_id = binn_object_int32(obj, DSM_MSG_KEY_PAGEID); msg->givepage_args.access_rights = binn_object_int16(obj, DSM_MSG_KEY_RIGHTS); msg->givepage_args.data = binn_object_blob(obj, DSM_MSG_KEY_DATA, &blob_size); break; case SYNC_BARRIER: msg->sync_barrier_args.slave_to_wait = binn_object_int16(obj, DSM_MSG_KEY_BARRIER); break; case BARRIER_ACK: break; case TERMINATE: break; default: log("Unknown message type received !\n"); return -1; } binn_free(obj); free(buffer); return 0; }