diff --git a/src/proto_helper.c b/src/proto_helper.c index 790d03b..536533a 100644 --- a/src/proto_helper.c +++ b/src/proto_helper.c @@ -56,7 +56,6 @@ esc_recorded_event_t* esc_recorded_event_unpack(EventStore__Client__Messages__Ev ev->created_epoch = msg->has_created_epoch ? msg->created_epoch : 0; ev->data = buffer_copyfrom(msg->data.data, msg->data.len); ev->metadata = buffer_copyfrom(msg->metadata.data, msg->metadata.len); - //event_store__client__messages__event_record__free_unpacked(msg, &protobuf_allocator); return ev; } @@ -65,7 +64,6 @@ esc_resolved_event_t* esc_resolved_event_unpack(EventStore__Client__Messages__Re ev->original_position = esc_position_create(msg->prepare_position, msg->commit_position); ev->event = esc_recorded_event_unpack(msg->event); ev->link = esc_recorded_event_unpack(msg->link); - //event_store__client__messages__resolved_event__free_unpacked(msg, &protobuf_allocator); return ev; } @@ -133,10 +131,10 @@ buffer_t* esc_read_all_forward_pack(esc_position_t* last_checkpoint, int32_t cou send_msg.max_count = count; send_msg.require_master = 0; send_msg.resolve_link_tos = resolve_link_tos; - size_t s = event_store__client__messages__read_all_events__get_packed_size(&send_msg); - uint8_t buffer[s]; + size_t buf_size = event_store__client__messages__read_all_events__get_packed_size(&send_msg); + uint8_t buffer[buf_size]; event_store__client__messages__read_all_events__pack(&send_msg, buffer); - return buffer_copyfrom(buffer, s); + return buffer_copyfrom(buffer, buf_size); } const char* get_string_for_write_result(int write_result) { @@ -179,15 +177,16 @@ inspection_result_t* esc_write_result_unpack(buffer_t* buffer, esc_write_result_ } buffer_t* esc_append_to_stream_pack(const char* stream, int64_t expected_version, array_t* events) { + size_t events_size = array_size(events); EventStore__Client__Messages__WriteEvents send_msg; event_store__client__messages__write_events__init(&send_msg); send_msg.event_stream_id = (char*)stream; send_msg.expected_version = expected_version; send_msg.require_master = 0; - send_msg.n_events = events->size; - send_msg.events = malloc(events->size * sizeof(EventStore__Client__Messages__NewEvent*)); - for (size_t i = 0; i < events->size; i++) { - esc_event_data_t* event_data = events->data[i]; + send_msg.n_events = events_size; + send_msg.events = malloc(events_size * sizeof(EventStore__Client__Messages__NewEvent*)); + for (size_t i = 0; i < events_size; i++) { + esc_event_data_t* event_data = array_item(events, i); EventStore__Client__Messages__NewEvent* new_event = malloc(sizeof(EventStore__Client__Messages__NewEvent)); event_store__client__messages__new_event__init(new_event); new_event->event_id.data = (uint8_t *)event_data->event_id; @@ -202,12 +201,12 @@ buffer_t* esc_append_to_stream_pack(const char* stream, int64_t expected_version new_event->has_metadata = event_data->metadata ? 1 : 0; send_msg.events[i] = new_event; } - size_t size = event_store__client__messages__write_events__get_packed_size(&send_msg); - uint8_t buffer[size]; + size_t buf_size = event_store__client__messages__write_events__get_packed_size(&send_msg); + uint8_t buffer[buf_size]; event_store__client__messages__write_events__pack(&send_msg, buffer); - for (size_t i = 0; i < events->size; i++) { + for (size_t i = 0; i < events_size; i++) { free(send_msg.events[i]); } free(send_msg.events); - return buffer_copyfrom(buffer, size); + return buffer_copyfrom(buffer, buf_size); } diff --git a/src/utils/array.c b/src/utils/array.c index 006ab89..d84036b 100644 --- a/src/utils/array.c +++ b/src/utils/array.c @@ -6,6 +6,11 @@ #include "array.h" #include "debug.h" +struct st_array { + size_t size; + void** data; +}; + array_t* array_create(size_t n, ...) { va_list vl; va_start(vl, n); @@ -31,3 +36,11 @@ void array_destroy(array_t* array, array_deallocator destroyer) { free(array->data); free(array); } + +inline size_t array_size(array_t* array) { + return array->size; +} + +inline void* array_item(array_t* array, size_t index) { + return array->data[index]; +} diff --git a/src/utils/array.h b/src/utils/array.h index 4a65462..1313476 100644 --- a/src/utils/array.h +++ b/src/utils/array.h @@ -7,14 +7,14 @@ #include -typedef struct st_array { - size_t size; - void** data; -} array_t; +typedef struct st_array array_t; typedef void (*array_deallocator)(void*); array_t* array_create(size_t n, ...); void array_destroy(array_t* array, array_deallocator destroyer); +size_t array_size(array_t* array); +void* array_item(array_t* array, size_t index); + #endif //ESC_ARRAY_H