aboutsummaryrefslogtreecommitdiffstats
path: root/src/vppinfra/random_buffer.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/vppinfra/random_buffer.h')
-rw-r--r--src/vppinfra/random_buffer.h18
1 files changed, 11 insertions, 7 deletions
diff --git a/src/vppinfra/random_buffer.h b/src/vppinfra/random_buffer.h
index 320394d1862..12343c10535 100644
--- a/src/vppinfra/random_buffer.h
+++ b/src/vppinfra/random_buffer.h
@@ -42,9 +42,7 @@
#include <vppinfra/random_isaac.h>
#include <vppinfra/warnings.h>
-/* *INDENT-OFF* */
WARN_OFF(array-bounds)
-/* *INDENT-ON* */
typedef struct
{
@@ -54,6 +52,9 @@ typedef struct
/* Random buffer. */
uword *buffer;
+ /* An actual length to be applied before using the buffer. */
+ uword next_read_len;
+
/* Cache up to 1 word worth of bytes for random data
less than one word at a time. */
uword n_cached_bytes;
@@ -84,11 +85,16 @@ clib_random_buffer_get_data (clib_random_buffer_t * b, uword n_bytes)
{
uword n_words, i, l;
+ if (b->buffer)
+ vec_set_len (b->buffer, b->next_read_len);
+ else
+ ASSERT (b->next_read_len == 0);
+
l = b->n_cached_bytes;
if (n_bytes <= l)
{
b->n_cached_bytes = l - n_bytes;
- return &b->cached_bytes[l];
+ return &b->cached_bytes[l - n_bytes];
}
n_words = n_bytes / sizeof (uword);
@@ -100,21 +106,19 @@ clib_random_buffer_get_data (clib_random_buffer_t * b, uword n_bytes)
clib_random_buffer_fill (b, n_words);
i = vec_len (b->buffer) - n_words;
- _vec_len (b->buffer) = i;
+ b->next_read_len = i;
if (n_bytes < sizeof (uword))
{
b->cached_word = b->buffer[i];
b->n_cached_bytes = sizeof (uword) - n_bytes;
- return b->cached_bytes;
+ return &b->cached_bytes[sizeof (uword) - n_bytes];
}
else
return b->buffer + i;
}
-/* *INDENT-OFF* */
WARN_ON(array-bounds)
-/* *INDENT-ON* */
#endif /* included_clib_random_buffer_h */