]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: Convert stream_new to use one malloc 2886/head
authorDonald Sharp <sharpd@cumulusnetworks.com>
Wed, 22 Aug 2018 00:34:42 +0000 (20:34 -0400)
committerDonald Sharp <sharpd@cumulusnetworks.com>
Wed, 22 Aug 2018 11:58:54 +0000 (07:58 -0400)
Modify stream.c to have stream_new call one malloc call
instead of two.  Also change stream_resize_orig to
use stream_resize_inplace and to send an error
to the developer to switch over.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
lib/stream.c
lib/stream.h

index 50ee9a5d5612930d333a4d1e861061e72b524f7d..55e7f6435826118df2c8735f12a3832ea7912191 100644 (file)
@@ -28,9 +28,9 @@
 #include "network.h"
 #include "prefix.h"
 #include "log.h"
+#include "lib_errors.h"
 
 DEFINE_MTYPE_STATIC(LIB, STREAM, "Stream")
-DEFINE_MTYPE_STATIC(LIB, STREAM_DATA, "Stream data")
 DEFINE_MTYPE_STATIC(LIB, STREAM_FIFO, "Stream FIFO")
 
 /* Tests whether a position is valid */
@@ -100,9 +100,7 @@ struct stream *stream_new(size_t size)
 
        assert(size > 0);
 
-       s = XMALLOC(MTYPE_STREAM, sizeof(struct stream));
-
-       s->data = XMALLOC(MTYPE_STREAM_DATA, size);
+       s = XMALLOC(MTYPE_STREAM, sizeof(struct stream) + size);
 
        s->getp = s->endp = 0;
        s->next = NULL;
@@ -116,7 +114,6 @@ void stream_free(struct stream *s)
        if (!s)
                return;
 
-       XFREE(MTYPE_STREAM_DATA, s->data);
        XFREE(MTYPE_STREAM, s);
 }
 
@@ -168,30 +165,31 @@ struct stream *stream_dupcat(struct stream *s1, struct stream *s2,
 
 size_t stream_resize_inplace(struct stream **sptr, size_t newsize)
 {
-       return stream_resize_orig(*sptr, newsize);
-}
+       struct stream *orig = *sptr;
 
-size_t stream_resize_orig(struct stream *s, size_t newsize)
-{
-       uint8_t *newdata;
-       STREAM_VERIFY_SANE(s);
+       STREAM_VERIFY_SANE(orig);
 
-       newdata = XREALLOC(MTYPE_STREAM_DATA, s->data, newsize);
+       orig = XREALLOC(MTYPE_STREAM, orig, sizeof(struct stream) + newsize);
 
-       if (newdata == NULL)
-               return s->size;
+       orig->size = newsize;
 
-       s->data = newdata;
-       s->size = newsize;
+       if (orig->endp > orig->size)
+               orig->endp = orig->size;
+       if (orig->getp > orig->endp)
+               orig->getp = orig->endp;
 
-       if (s->endp > s->size)
-               s->endp = s->size;
-       if (s->getp > s->endp)
-               s->getp = s->endp;
+       STREAM_VERIFY_SANE(orig);
 
-       STREAM_VERIFY_SANE(s);
+       *sptr = orig;
+       return orig->size;
+}
 
-       return s->size;
+size_t __attribute__((deprecated))stream_resize_orig(struct stream *s,
+                                                    size_t newsize)
+{
+       assert("stream_resize: Switch code to use stream_resize_inplace" == NULL);
+
+       return stream_resize_inplace(&s, newsize);
 }
 
 size_t stream_get_getp(struct stream *s)
index e8cd76362ed7fa14a45e4e371e399214d0e8fd5f..ef9366e1ae0b1da43950dce715e8aefa995cca65 100644 (file)
 struct stream {
        struct stream *next;
 
-       /* Remainder is ***private*** to stream
+       /*
+        * Remainder is ***private*** to stream
         * direct access is frowned upon!
         * Use the appropriate functions/macros
         */
-       size_t getp;     /* next get position */
-       size_t endp;     /* last valid data position */
-       size_t size;     /* size of data segment */
-       unsigned char *data; /* data pointer */
+       size_t getp;           /* next get position */
+       size_t endp;           /* last valid data position */
+       size_t size;           /* size of data segment */
+       unsigned char data[0]; /* data pointer */
 };
 
 /* First in first out queue structure. */