blob: 28e8e2a081de32ed03e53263a0eec64a7803be19 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus
17
18 Permission is hereby granted, free of charge, to any person obtaining
19 a copy of this software and associated documentation files (the
20 "Software"), to deal in the Software without restriction, including
21 without limitation the rights to use, copy, modify, merge, publish,
22 distribute, sublicense, and/or sell copies of the Software, and to
23 permit persons to whom the Software is furnished to do so, subject to
24 the following conditions:
25
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
28
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36*/
37
38#ifndef included_test_vec_h
39#define included_test_vec_h
40
41
42#include <vppinfra/clib.h>
43#include <vppinfra/mem.h>
44#include <vppinfra/format.h>
45#include <vppinfra/error.h>
46
47
48extern uword g_verbose;
49extern u32 g_seed;
50
Dave Barachc3799992016-08-15 11:12:27 -040051always_inline u8 *
52format_u32_binary (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -070053{
Dave Barachc3799992016-08-15 11:12:27 -040054 u32 val = va_arg (*va, u32);
55 word i = 0;
56
57 for (i = BITS (val) - 1; i >= 0; i--)
58 {
59 if (val & (1 << i))
60 s = format (s, "1");
61 else
62 s = format (s, "0");
63 }
64
65 return s;
Ed Warnickecb9cada2015-12-08 15:45:58 -070066}
67
68#define VERBOSE1(fmt, args...) \
69do { \
70 if (g_verbose >= 1) \
71 fformat (stdout, fmt, ## args); \
72} while (0)
73
74#define VERBOSE2(fmt, args...) \
75do { \
76 if (g_verbose >= 2) \
77 fformat (stdout, fmt, ## args); \
78} while (0)
79
80#define VERBOSE3(fmt, args...) \
81do { \
82 if (g_verbose >= 3) \
83 fformat (stdout, fmt, ## args); \
84} while (0)
85
86#define clib_mem_free_safe(p) \
87do { \
88 if (p) \
89 { \
90 clib_mem_free (p); \
91 (p) = NULL; \
92 } \
93} while (0)
94
95/* XXX - I get undefined symbol trying to call random_u32() <vppinfra/random.h> */
96/* Simple random number generator with period 2^31 - 1. */
Dave Barachc3799992016-08-15 11:12:27 -040097static u32
98my_random_u32 (u32 * seed_return)
Ed Warnickecb9cada2015-12-08 15:45:58 -070099{
100 /* Unlikely mask value to XOR into seed.
101 Otherwise small seed values would give
102 non-random seeming smallish numbers. */
103 const u32 mask = 0x12345678;
104 u32 seed, a, b, result;
105
106 seed = *seed_return;
107 seed ^= mask;
108
109 a = seed / 127773;
110 b = seed % 127773;
111 seed = 16807 * b - 2836 * a;
112
113 if ((i32) seed < 0)
114 seed += ((u32) 1 << 31) - 1;
115
116 result = seed;
117
118 *seed_return = seed ^ mask;
119
120 return result;
121}
122
Dave Barachc3799992016-08-15 11:12:27 -0400123static u32
124bounded_random_u32 (u32 * seed, uword lo, uword hi)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125{
126 if (lo == hi)
127 return lo;
128
129 ASSERT (lo < hi);
130
Dave Barachc3799992016-08-15 11:12:27 -0400131 return ((my_random_u32 (seed) % (hi - lo + ((hi != ~0) ? (1) : (0)))) + lo);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132}
133
134#define fill_with_random_data(ptr, bytes, seed) \
135do { \
136 u8 * _v(p) = (u8 *) (ptr); \
137 uword _v(b) = (bytes); \
138 uword _v(i); \
139 \
140 for (_v(i) = 0; _v(i) < _v(b); _v(i)++) \
141 _v(p)[_v(i)] = (u8) bounded_random_u32 (&(seed), 0, 255); \
142 \
143} while (0)
144
145#define compute_mem_hash(hash, ptr, bytes) \
146({ \
147 u8 * _v(p) = (u8 *) (ptr); \
148 uword _v(b) = (uword) (bytes); \
149 uword _v(i); \
150 uword _v(h) = (u8) (hash); \
151 \
152 if (_v(p) && _v(b) > 0) \
153 { \
154 for (_v(i) = 0; _v(i) < _v(b); _v(i)++) \
155 _v(h) ^= _v(p)[_v(i)]; \
156 } \
157 \
158 _v(h); \
159})
160
161#define log2_align_down(value, align) \
162({ \
163 uword _v = (uword) (value); \
164 uword _a = (uword) (align); \
165 uword _m = (1 << _a) - 1; \
166 \
167 _v = _v & ~_m; \
168})
169
170#define log2_align_up(value, align) \
171({ \
172 uword _v = (uword) (value); \
173 uword _a = (uword) (align); \
174 uword _m = (1 << _a) - 1; \
175 \
176 _v = (_v + _m) & ~_m; \
177})
178
179#define log2_align_ptr_down(ptr, align) \
180uword_to_pointer (log2_align_down (pointer_to_uword (ptr), align), void *)
181
182#define log2_align_ptr_up(ptr, align) \
183uword_to_pointer (log2_align_up (pointer_to_uword (ptr), align), void *)
184
185#define MAX_LOG2_ALIGN 6
186#define MAX_UNALIGN_OFFSET ((1 << MAX_LOG2_ALIGN) - 1)
187
188/* Allocates pointer to memory whose address is:
189 addr = <log2_align>-aligned address */
Dave Barachc3799992016-08-15 11:12:27 -0400190always_inline void *
191alloc_aligned (uword size, uword log2_align, void **ptr_to_free)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192{
Dave Barachc3799992016-08-15 11:12:27 -0400193 void *p;
194
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195 if (size <= 0)
196 return NULL;
Dave Barachc3799992016-08-15 11:12:27 -0400197
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198 p = (void *) clib_mem_alloc (size + (1 << log2_align) - 1);
Dave Barachc3799992016-08-15 11:12:27 -0400199
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200 if (ptr_to_free)
201 *ptr_to_free = p;
202
203 return (p) ? log2_align_ptr_up (p, log2_align) : (NULL);
204}
205
206/* Allocates pointer to memory whose address is:
207 addr = MAX_LOG2_ALIGN-aligned address + <offset> */
Dave Barachc3799992016-08-15 11:12:27 -0400208always_inline void *
209alloc_unaligned (uword size, uword offset, void **ptr_to_free)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210{
Dave Barachc3799992016-08-15 11:12:27 -0400211 void *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212
213 if (size <= 0)
214 return NULL;
215
216 ASSERT (offset <= MAX_UNALIGN_OFFSET);
217
Dave Barachc3799992016-08-15 11:12:27 -0400218 p =
219 alloc_aligned (size + (1 << MAX_LOG2_ALIGN), MAX_LOG2_ALIGN, ptr_to_free);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220
Dave Barachc3799992016-08-15 11:12:27 -0400221 if (!p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222 return NULL;
223
224 return (void *) ((u8 *) p + (offset % MAX_UNALIGN_OFFSET));
225}
226
227#define memory_snap() \
228do { \
229 clib_mem_usage_t _usage = { 0 }; \
230 clib_mem_usage (&_usage); \
231 fformat (stdout, "%U\n", format_clib_mem_usage, _usage, 0); \
232} while (0)
233
234
235#endif /* included_test_vec_h */
Dave Barachc3799992016-08-15 11:12:27 -0400236
237/*
238 * fd.io coding-style-patch-verification: ON
239 *
240 * Local Variables:
241 * eval: (c-set-style "gnu")
242 * End:
243 */