prepare-write-op.c 3.92 KB
Newer Older
1 2 3 4 5
/*
 * (C) 2017 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */
Matthieu Dorier's avatar
Matthieu Dorier committed
6 7 8 9
#include "src/io-chain/prepare-write-op.h"
#include "src/io-chain/write-op-impl.h"
#include "src/util/utlist.h"
#include "src/util/log.h"
10

Matthieu Dorier's avatar
Matthieu Dorier committed
11
static void convert_write(uint64_t* cur_offset,
12 13
                          wr_action_write_t action,
                          void** ptr,
Matthieu Dorier's avatar
Matthieu Dorier committed
14
                          size_t* len);
15

Matthieu Dorier's avatar
Matthieu Dorier committed
16
static void convert_write_full(uint64_t* cur_offset,
17 18
                               wr_action_write_full_t action,
                               void** ptr,
Matthieu Dorier's avatar
Matthieu Dorier committed
19
                               size_t* len);
20

Matthieu Dorier's avatar
Matthieu Dorier committed
21
static void convert_write_same(uint64_t* cur_offset,
22 23
                               wr_action_write_same_t action,
                               void** ptr,
Matthieu Dorier's avatar
Matthieu Dorier committed
24
                               size_t* len);
25

Matthieu Dorier's avatar
Matthieu Dorier committed
26
static void convert_append(uint64_t* cur_offset,
27 28
                           wr_action_append_t action,
                           void** ptr,
Matthieu Dorier's avatar
Matthieu Dorier committed
29
                           size_t* len);
30 31 32

void prepare_write_op(margo_instance_id mid, mobject_store_write_op_t write_op) 
{
Matthieu Dorier's avatar
Matthieu Dorier committed
33
	if(write_op->ready == 1) return;
34 35 36 37
	if(write_op->num_actions == 0) {
		write_op->ready = 1;
		return;
	}	
38 39 40 41 42 43 44 45 46 47 48 49

	wr_action_base_t action;

	void** pointers = (void**)calloc(write_op->num_actions, sizeof(void*));
	size_t* lengths = (size_t*)calloc(write_op->num_actions, sizeof(size_t));
	uint64_t current_offset = 0;
	size_t i = 0;

	DL_FOREACH(write_op->actions, action) {

		switch(action->type) {
		case WRITE_OPCODE_WRITE:
Matthieu Dorier's avatar
Matthieu Dorier committed
50
			convert_write(&current_offset, 
51 52 53 54
				(wr_action_write_t)action, pointers+i, lengths+i);
			i += 1;
			break;
		case WRITE_OPCODE_WRITE_FULL:
Matthieu Dorier's avatar
Matthieu Dorier committed
55
			convert_write_full(&current_offset,
56 57 58 59
				(wr_action_write_full_t)action, pointers+i, lengths+i);
			i += 1;
			break;
		case WRITE_OPCODE_WRITE_SAME:
Matthieu Dorier's avatar
Matthieu Dorier committed
60
			convert_write_same(&current_offset, 
61 62 63 64
				(wr_action_write_same_t)action, pointers+i, lengths+i);
			i += 1;
			break;
		case WRITE_OPCODE_APPEND:
Matthieu Dorier's avatar
Matthieu Dorier committed
65
			convert_append(&current_offset, 
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
				(wr_action_append_t)action, pointers+i, lengths+i);
			i += 1;
			break;
		}	
	}

	uint32_t count = i;
	if(count != 0) {
		hg_return_t ret = margo_bulk_create(mid, count,
    						pointers, lengths, HG_BULK_READ_ONLY, 
							&(write_op->bulk_handle));
		// TODO handle error

	}

Matthieu Dorier's avatar
Matthieu Dorier committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	write_op->ready = 1;
}

////////////////////////////////////////////////////////////////////////////////
//                          STATIC FUNCTIONS BELOW                            //
////////////////////////////////////////////////////////////////////////////////

static void convert_write(uint64_t* cur_offset,
                          wr_action_write_t action,
                          void** ptr,
                          size_t* len)
{
	uint64_t pos = *cur_offset;
	*cur_offset += action->len;
	*ptr         = (void*)action->buffer.as_pointer;
	*len         = action->len;
	action->buffer.as_offset = pos;
}

static void convert_write_full(uint64_t* cur_offset,
                               wr_action_write_full_t action,
                               void** ptr,
                               size_t* len)
{
	uint64_t pos = *cur_offset;
	*cur_offset += action->len;
	*ptr         = (void*)action->buffer.as_pointer;
	*len         = action->len;
	action->buffer.as_offset = pos;
}

static void convert_write_same(uint64_t* cur_offset,
                               wr_action_write_same_t action,
                               void** ptr,
                               size_t* len)
{
	uint64_t pos = *cur_offset;
	*cur_offset += action->data_len;
	*ptr         = (void*)action->buffer.as_pointer;
	*len         = action->data_len;
	action->buffer.as_offset = pos;
122 123
}

Matthieu Dorier's avatar
Matthieu Dorier committed
124 125 126 127 128 129 130 131 132 133 134
static void convert_append(uint64_t* cur_offset,
                           wr_action_append_t action,
                           void** ptr,
                           size_t* len)
{
	uint64_t pos = *cur_offset;
	*cur_offset += action->len;
	*ptr         = (void*)action->buffer.as_pointer;
	*len         = action->len;
	action->buffer.as_offset = pos;
}