ucx/buffer.h

changeset 5
88625853ae74
parent 1
1bcaac272cdf
child 17
11dffb40cd91
--- a/ucx/buffer.h	Sat Dec 01 20:34:55 2012 +0100
+++ b/ucx/buffer.h	Mon Aug 12 14:40:19 2013 +0200
@@ -1,5 +1,33 @@
-#ifndef BUFFER_H
-#define	BUFFER_H
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2013 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef UCX_BUFFER_H
+#define	UCX_BUFFER_H
 
 #include "ucx.h"
 #include <sys/types.h>
@@ -9,7 +37,9 @@
 extern "C" {
 #endif
 
+/* no autoextend or autofree behaviour */
 #define UCX_BUFFER_DEFAULT      0x00
+/* the buffer shall free the occupied memory space */
 #define UCX_BUFFER_AUTOFREE     0x01
 /* the buffer may automatically double its size on write operations */
 #define UCX_BUFFER_AUTOEXTEND   0x02
@@ -44,13 +74,16 @@
  * SEEK_CUR marks the current position
  * SEEK_END marks the first 0-byte in the buffer
  *
- * ucx_memseek returns 0 on success and -1 if the new position is beyond the
+ * ucx_buffer_seek returns 0 on success and -1 if the new position is beyond the
  * bounds of the allocated buffer. In that case the position of the buffer
  * remains unchanged.
  *
  */
 int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence);
 
+#define ucx_buffer_clear(buffer)    memset(buffer->space, 0, buffer->size); \
+                                    buffer->size = 0; buffer->pos = 0;
+
 /*
  * returns non-zero, if the current buffer position has exceeded the last
  * available byte of the underlying buffer
@@ -67,11 +100,6 @@
 size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems,
         UcxBuffer *buffer);
 
-/* when autoextend is enabled, ensure you get the latest pointer to the data */
-//define ucx_buffer_write(data, itemsize, nitems, buffer) \
-//    ucx_bufio(data, itemsize, nitems, buffer, 0)
-//define ucx_buffer_read(data, itemsize, nitems, buffer) \
-//        ucx_bufio(data, itemsize, nitems, buffer, 1)
 int ucx_buffer_putc(UcxBuffer *b, int c);
 int ucx_buffer_getc(UcxBuffer *b);
 
@@ -85,16 +113,22 @@
 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
 
-#endif	/* BUFFER_H */
+#endif	/* UCX_BUFFER_H */
 

mercurial