new function mem_get, bugfix in ptr_type
authorMatthias Kramm <kramm@quiss.org>
Tue, 3 Feb 2009 18:52:35 +0000 (19:52 +0100)
committerMatthias Kramm <kramm@quiss.org>
Tue, 3 Feb 2009 18:52:35 +0000 (19:52 +0100)
lib/q.c
lib/q.h

diff --git a/lib/q.c b/lib/q.c
index 512e3d0..def25ba 100644 (file)
--- a/lib/q.c
+++ b/lib/q.c
@@ -68,7 +68,9 @@ static int mem_put_(mem_t*m,const void*data, int length, int null)
     int n = m->pos;
     m->pos += length + (null?1:0);
     if(m->pos > m->len) { 
-        m->len = (m->pos+63)&~63;
+        int v1 = (m->pos+63)&~63;
+        int v2 = m->len + m->len / 2;
+        m->len = v1>v2?v1:v2;
        m->buffer = m->buffer?(char*)rfx_realloc(m->buffer,m->len):(char*)rfx_alloc(m->len);
     }
     assert(n+length <= m->len);
@@ -85,6 +87,15 @@ int mem_putstring(mem_t*m,string_t str)
 {
     return mem_put_(m, str.str, str.len, 1);
 }
+int mem_get(mem_t*m, void*data, int length)
+{
+    if(m->read_pos + length > m->pos) {
+        length = m->pos - m->read_pos;
+    }
+    memcpy(data, m->buffer+m->read_pos, length);
+    m->read_pos += length;
+    return length;
+}
 
 // ------------------------------- ringbuffer_t -------------------------------
 
@@ -593,7 +604,7 @@ char ptr_equals(const void*o1, const void*o2)
 }
 unsigned int ptr_hash(const void*o) 
 {
-    return string_hash3(o, sizeof(o));
+    return string_hash3(&o, sizeof(o));
 }
 void* ptr_dup(const void*o) 
 {
diff --git a/lib/q.h b/lib/q.h
index b90b57a..cc965b0 100644 (file)
--- a/lib/q.h
+++ b/lib/q.h
@@ -35,6 +35,7 @@ typedef struct _mem_t {
     char*buffer;
     int len;
     int pos;
+    int read_pos;
 } mem_t;
 
 /* fifo buffered growing mem region */
@@ -127,6 +128,7 @@ unsigned int crc32_add_string(unsigned int crc32, const char*s);
 void mem_init(mem_t*mem);
 int mem_put(mem_t*m, void*data, int length);
 int mem_putstring(mem_t*m, string_t str);
+int mem_get(mem_t*m, void*data, int length);
 void mem_clear(mem_t*mem);
 void mem_destroy(mem_t*mem);