err_t qbuffer_copyin(qbuffer_t* buf, qbuffer_iter_t start, qbuffer_iter_t end, const void* ptr, size_t ret_len) { int64_t num_bytes = qbuffer_iter_num_bytes(start, end); ssize_t num_parts = qbuffer_iter_num_parts(start, end); struct iovec* iov = NULL; size_t iovcnt; size_t i,j; MAYBE_STACK_SPACE(struct iovec, iov_onstack); err_t err; if( num_bytes < 0 || num_parts < 0 || start.offset < buf->offset_start || end.offset > buf->offset_end ) return EINVAL; MAYBE_STACK_ALLOC(struct iovec, num_parts, iov, iov_onstack); if( ! iov ) return ENOMEM; err = qbuffer_to_iov(buf, start, end, num_parts, iov, NULL, &iovcnt); if( err ) goto error; j = 0; for( i = 0; i < iovcnt; i++ ) { if( j + iov[i].iov_len > ret_len ) goto error_nospace; memcpy(iov[i].iov_base, PTR_ADDBYTES(ptr, j), iov[i].iov_len); j += iov[i].iov_len; } MAYBE_STACK_FREE(iov, iov_onstack); return 0; error_nospace: err = EMSGSIZE; error: MAYBE_STACK_FREE(iov, iov_onstack); return err; }
qioerr qbuffer_memset(qbuffer_t* buf, qbuffer_iter_t start, qbuffer_iter_t end, unsigned char byte) { int64_t num_bytes = qbuffer_iter_num_bytes(start, end); ssize_t num_parts = qbuffer_iter_num_parts(start, end); struct iovec* iov = NULL; size_t iovcnt; size_t i; MAYBE_STACK_SPACE(struct iovec, iov_onstack); qioerr err; if( num_bytes < 0 || num_parts < 0 || start.offset < buf->offset_start || end.offset > buf->offset_end ) QIO_RETURN_CONSTANT_ERROR(EINVAL, "range outside of buffer"); MAYBE_STACK_ALLOC(struct iovec, num_parts, iov, iov_onstack); if( ! iov ) return QIO_ENOMEM; err = qbuffer_to_iov(buf, start, end, num_parts, iov, NULL, &iovcnt); if( err ) goto error; for( i = 0; i < iovcnt; i++ ) { memset(iov[i].iov_base, byte, iov[i].iov_len); } MAYBE_STACK_FREE(iov, iov_onstack); return 0; error: MAYBE_STACK_FREE(iov, iov_onstack); return err; }
qioerr qbuffer_flatten(qbuffer_t* buf, qbuffer_iter_t start, qbuffer_iter_t end, qbytes_t** bytes_out) { int64_t num_bytes = qbuffer_iter_num_bytes(start, end); ssize_t num_parts = qbuffer_iter_num_parts(start, end); struct iovec* iov = NULL; size_t iovcnt; size_t i,j; qbytes_t* ret; MAYBE_STACK_SPACE(struct iovec, iov_onstack); qioerr err; if( num_bytes < 0 || num_parts < 0 || start.offset < buf->offset_start || end.offset > buf->offset_end ) { *bytes_out = 0; QIO_RETURN_CONSTANT_ERROR(EINVAL, "range outside of buffer"); } err = qbytes_create_calloc(&ret, num_bytes); if( err ) { *bytes_out = 0; return err; } MAYBE_STACK_ALLOC(struct iovec, num_parts, iov, iov_onstack); if( ! iov ) { // The buffer was successfully allocated, so we have to release it here. qbytes_release(ret); *bytes_out = 0; return QIO_ENOMEM; } err = qbuffer_to_iov(buf, start, end, num_parts, iov, NULL, &iovcnt); if( err ) { MAYBE_STACK_FREE(iov, iov_onstack); // The buffer was successfully allocated, so we have to release it here. qbytes_release(ret); *bytes_out = 0; return err; } j = 0; for( i = 0; i < iovcnt; i++ ) { qio_memcpy(PTR_ADDBYTES(ret->data, j), iov[i].iov_base, iov[i].iov_len); j += iov[i].iov_len; } MAYBE_STACK_FREE(iov, iov_onstack); *bytes_out = ret; return 0; }
err_t bulk_put_buffer(int64_t dst_locale, void* dst_addr, int64_t dst_len, qbuffer_t* buf, qbuffer_iter_t start, qbuffer_iter_t end) { int64_t num_bytes = qbuffer_iter_num_bytes(start, end); ssize_t num_parts = qbuffer_iter_num_parts(start, end); struct iovec* iov = NULL; size_t iovcnt; size_t i,j; int iov_onstack; err_t err; if( num_bytes < 0 || num_parts < 0 || start.offset < buf->offset_start || end.offset > buf->offset_end ) return EINVAL; MAYBE_STACK_ALLOC(num_parts*sizeof(struct iovec), iov, iov_onstack); if( ! iov ) return ENOMEM; err = qbuffer_to_iov(buf, start, end, num_parts, iov, NULL, &iovcnt); if( err ) goto error; j = 0; for( i = 0; i < iovcnt; i++ ) { if( j + iov[i].iov_len > dst_len ) goto error_nospace; //memcpy(PTR_ADDBYTES(ptr, j), iov[i].iov_base, iov[i].iov_len); // TODO -- note -- technically, this should be gasnet_put_bulk, // since we don't want to require src/dst to have a particular alignment. chpl_gen_comm_put( iov[i].iov_base, dst_locale, PTR_ADDBYTES(dst_addr, j), sizeof(uint8_t), CHPL_TYPE_uint8_t, iov[i].iov_len, -1, "<internal>" ); j += iov[i].iov_len; } MAYBE_STACK_FREE(iov, iov_onstack); return 0; error_nospace: err = EMSGSIZE; error: MAYBE_STACK_FREE(iov, iov_onstack); return err; }
qioerr qbuffer_copyin_buffer(qbuffer_t* dst, qbuffer_iter_t dst_start, qbuffer_iter_t dst_end, qbuffer_t* src, qbuffer_iter_t src_start, qbuffer_iter_t src_end) { int64_t dst_num_bytes = qbuffer_iter_num_bytes(dst_start, dst_end); ssize_t dst_num_parts = qbuffer_iter_num_parts(dst_start, dst_end); int64_t src_num_bytes = qbuffer_iter_num_bytes(src_start, src_end); ssize_t src_num_parts = qbuffer_iter_num_parts(src_start, src_end); struct iovec* iov = NULL; size_t iovcnt; size_t i; MAYBE_STACK_SPACE(struct iovec, iov_onstack); qioerr err; qbuffer_iter_t dst_cur, dst_cur_end; if( dst == src ) QIO_RETURN_CONSTANT_ERROR(EINVAL, "cannot copy a buffer to itself"); if( dst_num_bytes < 0 || dst_num_parts < 0 || dst_start.offset < dst->offset_start || dst_end.offset > dst->offset_end ) QIO_RETURN_CONSTANT_ERROR(EINVAL, "dst range outside of buffer"); if( src_num_bytes < 0 || src_num_parts < 0 || src_start.offset < src->offset_start || src_end.offset > src->offset_end ) QIO_RETURN_CONSTANT_ERROR(EINVAL, "src range outside of buffer"); MAYBE_STACK_ALLOC(struct iovec, src_num_parts, iov, iov_onstack); if( ! iov ) return QIO_ENOMEM; err = qbuffer_to_iov(src, src_start, src_end, src_num_parts, iov, NULL, &iovcnt); if( err ) goto error; dst_cur = dst_start; for( i = 0; i < iovcnt; i++ ) { dst_cur_end = dst_cur; qbuffer_iter_advance(dst, &dst_cur_end, iov[i].iov_len); err = qbuffer_copyin(dst, dst_cur, dst_cur_end, iov[i].iov_base, iov[i].iov_len); if( err ) goto error; dst_cur = dst_cur_end; } MAYBE_STACK_FREE(iov, iov_onstack); return 0; error: MAYBE_STACK_FREE(iov, iov_onstack); return err; }
err_t qbuffer_flatten(qbuffer_t* buf, qbuffer_iter_t start, qbuffer_iter_t end, qbytes_t** bytes_out) { int64_t num_bytes = qbuffer_iter_num_bytes(start, end); ssize_t num_parts = qbuffer_iter_num_parts(start, end); struct iovec* iov = NULL; size_t iovcnt; size_t i,j; qbytes_t* ret; MAYBE_STACK_SPACE(struct iovec, iov_onstack); err_t err; if( num_bytes < 0 || num_parts < 0 || start.offset < buf->offset_start || end.offset > buf->offset_end ) return EINVAL; err = qbytes_create_calloc(&ret, num_bytes); if( err ) return err; MAYBE_STACK_ALLOC(struct iovec, num_parts, iov, iov_onstack); if( ! iov ) { qbytes_release(ret); return ENOMEM; } err = qbuffer_to_iov(buf, start, end, num_parts, iov, NULL, &iovcnt); if( err ) { MAYBE_STACK_FREE(iov, iov_onstack); qbytes_release(ret); return err; } j = 0; for( i = 0; i < iovcnt; i++ ) { memcpy(PTR_ADDBYTES(ret->data, j), iov[i].iov_base, iov[i].iov_len); j += iov[i].iov_len; } MAYBE_STACK_FREE(iov, iov_onstack); *bytes_out = ret; return 0; }
err_t qbuffer_copyin_buffer(qbuffer_t* dst, qbuffer_iter_t dst_start, qbuffer_iter_t dst_end, qbuffer_t* src, qbuffer_iter_t src_start, qbuffer_iter_t src_end) { int64_t dst_num_bytes = qbuffer_iter_num_bytes(dst_start, dst_end); ssize_t dst_num_parts = qbuffer_iter_num_parts(dst_start, dst_end); int64_t src_num_bytes = qbuffer_iter_num_bytes(src_start, src_end); ssize_t src_num_parts = qbuffer_iter_num_parts(src_start, src_end); struct iovec* iov = NULL; size_t iovcnt; size_t i; MAYBE_STACK_SPACE(struct iovec, iov_onstack); err_t err; qbuffer_iter_t dst_cur, dst_cur_end; if( dst == src ) return EINVAL; if( dst_num_bytes < 0 || dst_num_parts < 0 || dst_start.offset < dst->offset_start || dst_end.offset > dst->offset_end ) return EINVAL; if( src_num_bytes < 0 || src_num_parts < 0 || src_start.offset < src->offset_start || src_end.offset > src->offset_end ) return EINVAL; MAYBE_STACK_ALLOC(struct iovec, src_num_parts, iov, iov_onstack); if( ! iov ) return ENOMEM; err = qbuffer_to_iov(src, src_start, src_end, src_num_parts, iov, NULL, &iovcnt); if( err ) goto error; dst_cur = dst_start; for( i = 0; i < iovcnt; i++ ) { dst_cur_end = dst_cur; qbuffer_iter_advance(dst, &dst_cur_end, iov[i].iov_len); err = qbuffer_copyin(dst, dst_cur, dst_cur_end, iov[i].iov_base, iov[i].iov_len); if( err ) goto error; dst_cur = dst_cur_end; } MAYBE_STACK_FREE(iov, iov_onstack); return 0; error: MAYBE_STACK_FREE(iov, iov_onstack); return err; }
qioerr qbuffer_copyin(qbuffer_t* buf, qbuffer_iter_t start, qbuffer_iter_t end, const void* ptr, size_t ret_len) { int64_t num_bytes = qbuffer_iter_num_bytes(start, end); ssize_t num_parts = qbuffer_iter_num_parts(start, end); struct iovec* iov = NULL; size_t iovcnt; size_t i,j; MAYBE_STACK_SPACE(struct iovec, iov_onstack); qioerr err; if( num_bytes < 0 || num_parts < 0 || start.offset < buf->offset_start || end.offset > buf->offset_end ) { QIO_RETURN_CONSTANT_ERROR(EINVAL, "range outside of buffer"); } MAYBE_STACK_ALLOC(struct iovec, num_parts, iov, iov_onstack); if( ! iov ) return QIO_ENOMEM; err = qbuffer_to_iov(buf, start, end, num_parts, iov, NULL, &iovcnt); if( err ) goto error; j = 0; for( i = 0; i < iovcnt; i++ ) { if( j + iov[i].iov_len > ret_len ) goto error_nospace; qio_memcpy(iov[i].iov_base, PTR_ADDBYTES(ptr, j), iov[i].iov_len); j += iov[i].iov_len; } MAYBE_STACK_FREE(iov, iov_onstack); return 0; error_nospace: QIO_GET_CONSTANT_ERROR(err, EMSGSIZE, "no space in buffer"); error: MAYBE_STACK_FREE(iov, iov_onstack); return err; }