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;
51 always_inline u8 * format_u32_binary (u8 * s, va_list * va)
53 u32 val = va_arg (*va, u32);
56 for (i = BITS (val) - 1; i >= 0; i--)
67 #define VERBOSE1(fmt, args...) \
70 fformat (stdout, fmt, ## args); \
73 #define VERBOSE2(fmt, args...) \
76 fformat (stdout, fmt, ## args); \
79 #define VERBOSE3(fmt, args...) \
82 fformat (stdout, fmt, ## args); \
85 #define clib_mem_free_safe(p) \
94 /* XXX - I get undefined symbol trying to call random_u32() <vppinfra/random.h> */
95 /* Simple random number generator with period 2^31 - 1. */
96 static u32 my_random_u32 (u32 * seed_return)
98 /* Unlikely mask value to XOR into seed.
99 Otherwise small seed values would give
100 non-random seeming smallish numbers. */
101 const u32 mask = 0x12345678;
102 u32 seed, a, b, result;
109 seed = 16807 * b - 2836 * a;
112 seed += ((u32) 1 << 31) - 1;
116 *seed_return = seed ^ mask;
121 static u32 bounded_random_u32 (u32 * seed, uword lo, uword hi)
128 return ((my_random_u32 (seed) % (hi - lo + ((hi != ~0) ? (1) : (0)))) + lo);
131 #define fill_with_random_data(ptr, bytes, seed) \
133 u8 * _v(p) = (u8 *) (ptr); \
134 uword _v(b) = (bytes); \
137 for (_v(i) = 0; _v(i) < _v(b); _v(i)++) \
138 _v(p)[_v(i)] = (u8) bounded_random_u32 (&(seed), 0, 255); \
142 #define compute_mem_hash(hash, ptr, bytes) \
144 u8 * _v(p) = (u8 *) (ptr); \
145 uword _v(b) = (uword) (bytes); \
147 uword _v(h) = (u8) (hash); \
149 if (_v(p) && _v(b) > 0) \
151 for (_v(i) = 0; _v(i) < _v(b); _v(i)++) \
152 _v(h) ^= _v(p)[_v(i)]; \
158 #define log2_align_down(value, align) \
160 uword _v = (uword) (value); \
161 uword _a = (uword) (align); \
162 uword _m = (1 << _a) - 1; \
167 #define log2_align_up(value, align) \
169 uword _v = (uword) (value); \
170 uword _a = (uword) (align); \
171 uword _m = (1 << _a) - 1; \
173 _v = (_v + _m) & ~_m; \
176 #define log2_align_ptr_down(ptr, align) \
177 uword_to_pointer (log2_align_down (pointer_to_uword (ptr), align), void *)
179 #define log2_align_ptr_up(ptr, align) \
180 uword_to_pointer (log2_align_up (pointer_to_uword (ptr), align), void *)
182 #define MAX_LOG2_ALIGN 6
183 #define MAX_UNALIGN_OFFSET ((1 << MAX_LOG2_ALIGN) - 1)
185 /* Allocates pointer to memory whose address is:
186 addr = <log2_align>-aligned address */
187 always_inline void * alloc_aligned (uword size, uword log2_align, void ** ptr_to_free)
194 p = (void *) clib_mem_alloc (size + (1 << log2_align) - 1);
199 return (p) ? log2_align_ptr_up (p, log2_align) : (NULL);
202 /* Allocates pointer to memory whose address is:
203 addr = MAX_LOG2_ALIGN-aligned address + <offset> */
204 always_inline void * alloc_unaligned (uword size, uword offset, void ** ptr_to_free)
211 ASSERT (offset <= MAX_UNALIGN_OFFSET);
213 p = alloc_aligned (size + (1 << MAX_LOG2_ALIGN), MAX_LOG2_ALIGN, ptr_to_free);
218 return (void *) ((u8 *) p + (offset % MAX_UNALIGN_OFFSET));
221 #define memory_snap() \
223 clib_mem_usage_t _usage = { 0 }; \
224 clib_mem_usage (&_usage); \
225 fformat (stdout, "%U\n", format_clib_mem_usage, _usage, 0); \
229 #endif /* included_test_vec_h */