added generic ncopy

Mon, 25 Feb 2013 13:25:07 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 25 Feb 2013 13:25:07 +0100
changeset 86
55bf819cbc88
parent 85
0ef0df7aa2c2
child 87
bd444539cced

added generic ncopy

ucx/buffer.c file | annotate | diff | comparison | revisions
ucx/buffer.h file | annotate | diff | comparison | revisions
--- 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;
+}
--- 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

mercurial