mbuf: cmp funcs, make free() zero-fill
authorMarko Kreen <markokr@gmail.com>
Fri, 23 Oct 2009 09:11:03 +0000 (12:11 +0300)
committerMarko Kreen <markokr@gmail.com>
Tue, 8 Dec 2009 12:03:18 +0000 (14:03 +0200)
usual/mbuf.h

index aa0ae81ac2ab41b28cba72ca966ebed7a9a83e61..789d83c38eccc70834807a1c68e422cbdc061b17 100644 (file)
@@ -59,10 +59,10 @@ static inline void mbuf_init_dynamic(struct MBuf *buf)
 
 static inline void mbuf_free(struct MBuf *buf)
 {
-       if (!buf->fixed && buf->data) {
-               free(buf->data);
-               buf->data = NULL;
-               buf->read_pos = buf->write_pos = buf->alloc_len = 0;
+       if (buf->data) {
+               if (!buf->fixed)
+                       free(buf->data);
+               memset(buf, 0, sizeof(*buf));
        }
 }
 
@@ -109,6 +109,21 @@ static inline const void *mbuf_data(const struct MBuf *buf)
        return buf->data;
 }
 
+static inline bool mbuf_eq(const struct MBuf *buf1, const struct MBuf *buf2)
+{
+       if (buf1 == buf2) return true;
+       if (!buf1 || !buf2 || (mbuf_written(buf1) != mbuf_written(buf2)))
+               return false;
+       return memcmp(mbuf_data(buf1), mbuf_data(buf2), mbuf_written(buf1)) == 0;
+}
+
+static inline bool mbuf_eq_str(const struct MBuf *buf1, const char *s)
+{
+       struct MBuf tmp;
+       mbuf_init_fixed_reader(&tmp, s, strlen(s));
+       return mbuf_eq(buf1, &tmp);
+}
+
 /*
  * Read functions.
  */