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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus
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:
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
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.
38 #ifndef included_test_vec_h
39 #define included_test_vec_h
42 #include <vppinfra/clib.h>
43 #include <vppinfra/mem.h>
44 #include <vppinfra/format.h>
45 #include <vppinfra/error.h>
48 extern uword g_verbose;
52 format_u32_binary (u8 * s, va_list * va)
54 u32 val = va_arg (*va, u32);
57 for (i = BITS (val) - 1; i >= 0; i--)
68 #define VERBOSE1(fmt, args...) \
71 fformat (stdout, fmt, ## args); \
74 #define VERBOSE2(fmt, args...) \
77 fformat (stdout, fmt, ## args); \
80 #define VERBOSE3(fmt, args...) \
83 fformat (stdout, fmt, ## args); \
86 #define clib_mem_free_safe(p) \
95 /* XXX - I get undefined symbol trying to call random_u32() <vppinfra/random.h> */
96 /* Simple random number generator with period 2^31 - 1. */
98 my_random_u32 (u32 * seed_return)
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;
111 seed = 16807 * b - 2836 * a;
114 seed += ((u32) 1 << 31) - 1;
118 *seed_return = seed ^ mask;
124 bounded_random_u32 (u32 * seed, uword lo, uword hi)
131 return ((my_random_u32 (seed) % (hi - lo + ((hi != ~0) ? (1) : (0)))) + lo);
134 #define fill_with_random_data(ptr, bytes, seed) \
136 u8 * _v(p) = (u8 *) (ptr); \
137 uword _v(b) = (bytes); \
140 for (_v(i) = 0; _v(i) < _v(b); _v(i)++) \
141 _v(p)[_v(i)] = (u8) bounded_random_u32 (&(seed), 0, 255); \
145 #define compute_mem_hash(hash, ptr, bytes) \
147 u8 * _v(p) = (u8 *) (ptr); \
148 uword _v(b) = (uword) (bytes); \
150 uword _v(h) = (u8) (hash); \
152 if (_v(p) && _v(b) > 0) \
154 for (_v(i) = 0; _v(i) < _v(b); _v(i)++) \
155 _v(h) ^= _v(p)[_v(i)]; \
161 #define log2_align_down(value, align) \
163 uword _v = (uword) (value); \
164 uword _a = (uword) (align); \
165 uword _m = (1 << _a) - 1; \
170 #define log2_align_up(value, align) \
172 uword _v = (uword) (value); \
173 uword _a = (uword) (align); \
174 uword _m = (1 << _a) - 1; \
176 _v = (_v + _m) & ~_m; \
179 #define log2_align_ptr_down(ptr, align) \
180 uword_to_pointer (log2_align_down (pointer_to_uword (ptr), align), void *)
182 #define log2_align_ptr_up(ptr, align) \
183 uword_to_pointer (log2_align_up (pointer_to_uword (ptr), align), void *)
185 #define MAX_LOG2_ALIGN 6
186 #define MAX_UNALIGN_OFFSET ((1 << MAX_LOG2_ALIGN) - 1)
188 /* Allocates pointer to memory whose address is:
189 addr = <log2_align>-aligned address */
191 alloc_aligned (uword size, uword log2_align, void **ptr_to_free)
198 p = (void *) clib_mem_alloc (size + (1 << log2_align) - 1);
203 return (p) ? log2_align_ptr_up (p, log2_align) : (NULL);
206 /* Allocates pointer to memory whose address is:
207 addr = MAX_LOG2_ALIGN-aligned address + <offset> */
209 alloc_unaligned (uword size, uword offset, void **ptr_to_free)
216 ASSERT (offset <= MAX_UNALIGN_OFFSET);
219 alloc_aligned (size + (1 << MAX_LOG2_ALIGN), MAX_LOG2_ALIGN, ptr_to_free);
224 return (void *) ((u8 *) p + (offset % MAX_UNALIGN_OFFSET));
227 #define memory_snap() \
229 clib_mem_usage_t _usage = { 0 }; \
230 clib_mem_usage (&_usage); \
231 fformat (stdout, "%U\n", format_clib_mem_usage, _usage, 0); \
235 #endif /* included_test_vec_h */
238 * fd.io coding-style-patch-verification: ON
241 * eval: (c-set-style "gnu")