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

static void prepare_read(uint64_t* cur_offset,
                          rd_action_read_t action,
                          void** ptr,
Matthieu Dorier's avatar
Matthieu Dorier committed
14
                          size_t* len);
Matthieu Dorier's avatar
Matthieu Dorier committed
15 16 17

void prepare_read_op(margo_instance_id mid, mobject_store_read_op_t read_op) 
{
Matthieu Dorier's avatar
Matthieu Dorier committed
18
	if(read_op->ready == 1) return;
Matthieu Dorier's avatar
Matthieu Dorier committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
	if(read_op->num_actions == 0) return;	

	rd_action_base_t action;

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

	DL_FOREACH(read_op->actions, action) {

		switch(action->type) {
		case READ_OPCODE_READ:
			prepare_read(&current_offset, 
				(rd_action_read_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_WRITE_ONLY, 
							&(read_op->bulk_handle));
		// TODO handle error

	}

Matthieu Dorier's avatar
Matthieu Dorier committed
48
	read_op->ready = 1;
Matthieu Dorier's avatar
Matthieu Dorier committed
49 50 51 52 53

	free(pointers);
	free(lengths);
}

Matthieu Dorier's avatar
Matthieu Dorier committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
////////////////////////////////////////////////////////////////////////////////
//                          STATIC FUNCTIONS BELOW                            //
////////////////////////////////////////////////////////////////////////////////

static void prepare_read(uint64_t* cur_offset,
                          rd_action_read_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;
}