Line data Source code
1 : /*
2 : * Copyright (C) 2011 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 3 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 : * You should have received a copy of the GNU General Public License
15 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #ifndef __UTIL_H
19 : #define __UTIL_H
20 :
21 : /****************************************************************************/
22 : /* memory */
23 :
24 : /**
25 : * Safe aligned malloc.
26 : * If no memory is available, it aborts.
27 : */
28 : void* malloc_nofail_align(size_t size, void** freeptr);
29 :
30 : /**
31 : * Safe aligned vector allocation.
32 : * If no memory is available, it aborts.
33 : */
34 : void** malloc_nofail_vector_align(int nd, int n, size_t size, void** freeptr);
35 :
36 : /**
37 : * Safe allocation with memory test.
38 : */
39 : void* malloc_nofail_test(size_t size);
40 :
41 : /**
42 : * Test the memory vector for RAM problems.
43 : * If a problem is found, it crashes.
44 : */
45 : void mtest_vector(int n, size_t size, void** vv);
46 :
47 : /****************************************************************************/
48 : /* crc */
49 :
50 : /**
51 : * CRC initial value.
52 : * Using a not zero value allows to detect a leading run of zeros.
53 : */
54 : #define CRC_IV 0xffffffffU
55 :
56 : /**
57 : * CRC-32 (Castagnoli) table.
58 : */
59 : extern uint32_t CRC32C_0[256];
60 : extern uint32_t CRC32C_1[256];
61 : extern uint32_t CRC32C_2[256];
62 : extern uint32_t CRC32C_3[256];
63 :
64 : /**
65 : * If the CPU support the CRC instructions.
66 : */
67 : #if HAVE_SSE42
68 : extern int crc_x86;
69 : #endif
70 :
71 : /**
72 : * Compute CRC-32 (Castagnoli) for a single byte without the IV.
73 : */
74 23710549 : static inline uint32_t crc32c_plain_char(uint32_t crc, unsigned char c)
75 : {
76 : #if HAVE_SSE42
77 23710549 : if (tommy_likely(crc_x86)) {
78 23710549 : asm("crc32b %1, %0\n" : "+r" (crc) : "m" (c));
79 23710549 : return crc;
80 : }
81 : #endif
82 0 : return CRC32C_0[(crc ^ c) & 0xff] ^ (crc >> 8);
83 : }
84 :
85 : /**
86 : * Compute the CRC-32 (Castagnoli) without the IV.
87 : */
88 337 : static inline uint32_t crc32c_gen_plain(uint32_t crc, const unsigned char* ptr, unsigned size)
89 : {
90 20972361 : while (size >= 4) {
91 20971687 : crc ^= ptr[0] | (uint32_t)ptr[1] << 8 | (uint32_t)ptr[2] << 16 | (uint32_t)ptr[3] << 24;
92 20971687 : crc = CRC32C_3[crc & 0xff] ^ CRC32C_2[(crc >> 8) & 0xff] ^ CRC32C_1[(crc >> 16) & 0xff] ^ CRC32C_0[crc >> 24];
93 20971687 : ptr += 4;
94 20971687 : size -= 4;
95 : }
96 :
97 683 : while (size) {
98 9 : crc = CRC32C_0[(crc ^ *ptr) & 0xff] ^ (crc >> 8);
99 9 : ++ptr;
100 9 : --size;
101 : }
102 :
103 337 : return crc;
104 : }
105 :
106 : /**
107 : * Compute the CRC-32 (Castagnoli) without the IV.
108 : */
109 : #if HAVE_SSE42
110 131430214 : static inline uint32_t crc32c_x86_plain(uint32_t crc, const unsigned char* ptr, unsigned size)
111 : {
112 : #ifdef CONFIG_X86_64
113 : uint64_t crc64 = crc;
114 : while (size >= 8) {
115 : asm("crc32q %1, %0\n" : "+r" (crc64) : "m" (*(const uint64_t*)ptr));
116 : ptr += 8;
117 : size -= 8;
118 : }
119 : crc = crc64;
120 : #else
121 972142050 : while (size >= 4) {
122 709281622 : asm("crc32l %1, %0\n" : "+r" (crc) : "m" (*(const uint32_t*)ptr));
123 709281622 : ptr += 4;
124 709281622 : size -= 4;
125 : }
126 : #endif
127 382290808 : while (size) {
128 119430380 : asm("crc32b %1, %0\n" : "+r" (crc) : "m" (*ptr));
129 119430380 : ++ptr;
130 119430380 : --size;
131 : }
132 :
133 131430214 : return crc;
134 : }
135 : #endif
136 :
137 : /**
138 : * Compute CRC-32 (Castagnoli) without the IV.
139 : */
140 130044002 : static inline uint32_t crc32c_plain(uint32_t crc, const unsigned char* ptr, unsigned size)
141 : {
142 : #if HAVE_SSE42
143 130044002 : if (tommy_likely(crc_x86)) {
144 130044002 : return crc32c_x86_plain(crc, ptr, size);
145 : }
146 : #endif
147 0 : return crc32c_gen_plain(crc, ptr, size);
148 : }
149 :
150 : /**
151 : * Compute the CRC-32 (Castagnoli)
152 : */
153 : uint32_t (*crc32c)(uint32_t crc, const unsigned char* ptr, unsigned size);
154 :
155 : /**
156 : * Internal entry points for testing.
157 : */
158 : uint32_t crc32c_gen(uint32_t crc, const unsigned char* ptr, unsigned size);
159 : uint32_t crc32c_x86(uint32_t crc, const unsigned char* ptr, unsigned size);
160 :
161 : /**
162 : * Initialize the CRC-32 (Castagnoli) support.
163 : */
164 : void crc32c_init(void);
165 :
166 : /****************************************************************************/
167 : /* hash */
168 :
169 : /**
170 : * Size of the hash.
171 : */
172 :
173 : #define HASH_SIZE 16
174 :
175 : /**
176 : * Hash kinds.
177 : */
178 : #define HASH_UNDEFINED 0
179 : #define HASH_MURMUR3 1
180 : #define HASH_SPOOKY2 2
181 :
182 : /**
183 : * Compute the HASH of a memory block.
184 : * Seed is a 128 bit vector.
185 : */
186 : void memhash(unsigned kind, const unsigned char* seed, void* digest, const void* src, size_t size);
187 :
188 : /**
189 : * Return the hash name.
190 : */
191 : const char* hash_config_name(unsigned kind);
192 :
193 : /**
194 : * Count the number of different bits in the two buffers.
195 : */
196 : unsigned memdiff(const unsigned char* data1, const unsigned char* data2, size_t size);
197 :
198 : /****************************************************************************/
199 : /* lock */
200 :
201 : /**
202 : * Create and locks the lock file.
203 : * Return -1 on error, otherwise it's the file handle to pass to lock_unlock().
204 : */
205 : int lock_lock(const char* file);
206 :
207 : /**
208 : * Unlock the lock file.
209 : * Return -1 on error.
210 : */
211 : int lock_unlock(int f);
212 :
213 : #endif
214 :
|