Line data Source code
1 : /*
2 : * Copyright (C) 2013 Andrea Mazzoleni
3 : *
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.
8 : *
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.
13 : */
14 :
15 : #include "internal.h"
16 : #include "memory.h"
17 :
18 290 : void *raid_malloc_align(size_t size, void **freeptr)
19 : {
20 : unsigned char *ptr;
21 : uintptr_t offset;
22 :
23 290 : ptr = malloc(size + RAID_MALLOC_ALIGN);
24 290 : if (!ptr) {
25 : /* LCOV_EXCL_START */
26 : return 0;
27 : /* LCOV_EXCL_STOP */
28 : }
29 :
30 290 : *freeptr = ptr;
31 :
32 290 : offset = ((uintptr_t)ptr) % RAID_MALLOC_ALIGN;
33 :
34 290 : if (offset != 0)
35 284 : ptr += RAID_MALLOC_ALIGN - offset;
36 :
37 290 : return ptr;
38 : }
39 :
40 207 : void **raid_malloc_vector(int nd, int n, size_t size, void **freeptr)
41 : {
42 : void **v;
43 : unsigned char *va;
44 : int i;
45 :
46 207 : BUG_ON(n <= 0 || nd < 0);
47 :
48 207 : v = malloc(n * sizeof(void *));
49 207 : if (!v) {
50 : /* LCOV_EXCL_START */
51 : return 0;
52 : /* LCOV_EXCL_STOP */
53 : }
54 :
55 207 : va = raid_malloc_align(n * (size + RAID_MALLOC_DISPLACEMENT), freeptr);
56 207 : if (!va) {
57 : /* LCOV_EXCL_START */
58 : free(v);
59 : return 0;
60 : /* LCOV_EXCL_STOP */
61 : }
62 :
63 3676 : for (i = 0; i < n; ++i) {
64 3469 : v[i] = va;
65 3469 : va += size + RAID_MALLOC_DISPLACEMENT;
66 : }
67 :
68 : /* reverse order of the data blocks */
69 : /* because they are usually accessed from the last one */
70 866 : for (i = 0; i < nd / 2; ++i) {
71 659 : void *ptr = v[i];
72 :
73 659 : v[i] = v[nd - 1 - i];
74 659 : v[nd - 1 - i] = ptr;
75 : }
76 :
77 207 : return v;
78 : }
79 :
80 5 : void raid_mrand_vector(unsigned seed, int n, size_t size, void **vv)
81 : {
82 5 : unsigned char **v = (unsigned char **)vv;
83 : int i;
84 : size_t j;
85 :
86 124 : for (i = 0; i < n; ++i)
87 30583 : for (j = 0; j < size; ++j) {
88 : /* basic C99/C11 linear congruential generator */
89 30464 : seed = seed * 1103515245U + 12345U;
90 :
91 30464 : v[i][j] = seed >> 16;
92 : }
93 5 : }
94 :
95 2583 : int raid_mtest_vector(int n, size_t size, void **vv)
96 : {
97 2583 : unsigned char **v = (unsigned char **)vv;
98 : int i;
99 : size_t j;
100 : unsigned k;
101 : unsigned char d;
102 : unsigned char p;
103 :
104 : /* fill with 0 */
105 2583 : d = 0;
106 5257 : for (i = 0; i < n; ++i)
107 136288434 : for (j = 0; j < size; ++j)
108 136285760 : v[i][j] = d;
109 :
110 : /* test with all the byte patterns */
111 661234 : for (k = 1; k < 256; ++k) {
112 658651 : p = d;
113 658651 : d = k;
114 :
115 : /* forward fill */
116 1340760 : for (i = 0; i < n; ++i) {
117 24240075844 : for (j = 0; j < size; ++j) {
118 24239393735 : if (v[i][j] != p) {
119 : /* LCOV_EXCL_START */
120 : return -1;
121 : /* LCOV_EXCL_STOP */
122 : }
123 24239393729 : v[i][j] = d;
124 : }
125 : }
126 :
127 658645 : p = d;
128 658645 : d = ~p;
129 : /* backward fill with complement */
130 1340755 : for (i = 0; i < n; ++i) {
131 23878874802 : for (j = size; j > 0; --j) {
132 23878192692 : if (v[i][j - 1] != p) {
133 : /* LCOV_EXCL_START */
134 : return -1;
135 : /* LCOV_EXCL_STOP */
136 : }
137 23878192698 : v[i][j - 1] = d;
138 : }
139 : }
140 : }
141 :
142 2583 : return 0;
143 : }
144 :
|