2 * Copyright (C) 2013 Andrea Mazzoleni
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
18 void *raid_malloc_align(size_t size, size_t align_size, void **freeptr)
23 ptr = malloc(size + align_size);
32 offset = ((uintptr_t)ptr) % align_size;
35 ptr += align_size - offset;
40 void *raid_malloc(size_t size, void **freeptr)
42 return raid_malloc_align(size, RAID_MALLOC_ALIGN, freeptr);
45 void **raid_malloc_vector_align(int nd, int n, size_t size, size_t align_size, size_t displacement_size, void **freeptr)
51 BUG_ON(n <= 0 || nd < 0);
53 v = malloc(n * sizeof(void *));
60 va = raid_malloc_align(n * (size + displacement_size), align_size, freeptr);
68 for (i = 0; i < n; ++i) {
70 va += size + displacement_size;
73 /* reverse order of the data blocks */
74 /* because they are usually accessed from the last one */
75 for (i = 0; i < nd / 2; ++i) {
85 void **raid_malloc_vector(int nd, int n, size_t size, void **freeptr)
87 return raid_malloc_vector_align(nd, n, size, RAID_MALLOC_ALIGN, RAID_MALLOC_DISPLACEMENT, freeptr);
90 void raid_mrand_vector(unsigned seed, int n, size_t size, void **vv)
92 unsigned char **v = (unsigned char **)vv;
96 for (i = 0; i < n; ++i)
97 for (j = 0; j < size; ++j) {
98 /* basic C99/C11 linear congruential generator */
99 seed = seed * 1103515245U + 12345U;
101 v[i][j] = seed >> 16;
105 int raid_mtest_vector(int n, size_t size, void **vv)
107 unsigned char **v = (unsigned char **)vv;
116 for (i = 0; i < n; ++i)
117 for (j = 0; j < size; ++j)
120 /* test with all the byte patterns */
121 for (k = 1; k < 256; ++k) {
126 for (i = 0; i < n; ++i) {
127 for (j = 0; j < size; ++j) {
129 /* LCOV_EXCL_START */
139 /* backward fill with complement */
140 for (i = 0; i < n; ++i) {
141 for (j = size; j > 0; --j) {
142 if (v[i][j - 1] != p) {
143 /* LCOV_EXCL_START */