# HG changeset patch # User Olaf Wintermann # Date 1361795107 -3600 # Node ID 55bf819cbc8872246d3c98b61440aa8eda33d749 # Parent 0ef0df7aa2c2a31cf7d4f5e42743b8591a0b2f86 added generic ncopy diff -r 0ef0df7aa2c2 -r 55bf819cbc88 ucx/buffer.c --- a/ucx/buffer.c Mon Feb 25 12:18:31 2013 +0100 +++ b/ucx/buffer.c Mon Feb 25 13:25:07 2013 +0100 @@ -202,3 +202,31 @@ free(buf); return ncp; } + +size_t ucx_buffer_generic_ncopy(void *s1, void *s2, + read_func readfnc, write_func writefnc, size_t bufsize, size_t n) { + if(n == 0) { + return 0; + } + + size_t ncp = 0; + char *buf = (char*)malloc(bufsize); + if(buf == NULL) { + return 0; + } + + size_t r; + size_t rn = bufsize > n ? n : bufsize; + while((r = readfnc(buf, 1, rn, s1)) != 0) { + r = writefnc(buf, 1, r, s2); + ncp += r; + n -= r; + rn = bufsize > n ? n : bufsize; + if(r == 0 || n == 0) { + break; + } + } + + free(buf); + return ncp; +} diff -r 0ef0df7aa2c2 -r 55bf819cbc88 ucx/buffer.h --- a/ucx/buffer.h Mon Feb 25 12:18:31 2013 +0100 +++ b/ucx/buffer.h Mon Feb 25 13:25:07 2013 +0100 @@ -85,13 +85,19 @@ size_t ucx_buffer_generic_copy(void *s1, void *s2, read_func r, write_func w, size_t bufsize); +size_t ucx_buffer_generic_ncopy(void *s1, void *s2, read_func r, write_func w, + size_t bufsize, size_t n); -#define UCX_DEFAULT_BUFFER_SIZE 0x4000000 +#define UCX_DEFAULT_BUFFER_SIZE 0x1000 #define ucx_buffer_copy(s1,s2,r,w) \ ucx_buffer_generic_copy(s1, s2, (read_func)r, (write_func)w, \ UCX_DEFAULT_BUFFER_SIZE) +#define ucx_buffer_ncopy(s1,s2,r,w, n) \ + ucx_buffer_generic_ncopy(s1, s2, (read_func)r, (write_func)w, \ + UCX_DEFAULT_BUFFER_SIZE, n) + #ifdef __cplusplus } #endif