Skip to content
Permalink
Browse files

lib: Convert stream_new to use one malloc

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>
  • Loading branch information...
donaldsharp committed Aug 22, 2018
1 parent db3c830 commit de75223eafe083d4d6c82155a6bf8b328f806634
Showing with 26 additions and 27 deletions.
  1. +20 −22 lib/stream.c
  2. +6 −5 lib/stream.h
@@ -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)
@@ -98,14 +98,15 @@
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. */

0 comments on commit de75223

Please sign in to comment.
You can’t perform that action at this time.