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 : * Tests 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 : #if HAVE_SSE42
65 : /**
66 : * If the CPU support the CRC instructions.
67 : */
68 : extern int crc_x86;
69 : #endif
70 :
71 : /**
72 : * Computes CRC-32 (Castagnoli) for a single byte without IV.
73 : */
74 134187612 : static inline uint32_t crc32c_plain(uint32_t crc, unsigned char c)
75 : {
76 : #if HAVE_SSE42
77 134187612 : if (tommy_likely(crc_x86)) {
78 134187612 : asm("crc32b %1, %0\n" : "+r" (crc) : "m" (c));
79 134187612 : return crc;
80 : } else
81 : #endif
82 : {
83 0 : return CRC32C_0[(crc ^ c) & 0xff] ^ (crc >> 8);
84 : }
85 : }
86 :
87 : /**
88 : * Computes the CRC-32 (Castagnoli)
89 : */
90 : uint32_t (*crc32c)(uint32_t crc, const unsigned char* ptr, unsigned size);
91 : uint32_t crc32c_gen(uint32_t crc, const unsigned char* ptr, unsigned size);
92 : uint32_t crc32c_x86(uint32_t crc, const unsigned char* ptr, unsigned size);
93 :
94 : /**
95 : * Initializes the CRC-32 (Castagnoli) support.
96 : */
97 : void crc32c_init(void);
98 :
99 : /****************************************************************************/
100 : /* hash */
101 :
102 : /**
103 : * Size of the hash.
104 : */
105 :
106 : #define HASH_SIZE 16
107 :
108 : /**
109 : * Hash kinds.
110 : */
111 : #define HASH_UNDEFINED 0
112 : #define HASH_MURMUR3 1
113 : #define HASH_SPOOKY2 2
114 :
115 : /**
116 : * Computes the HASH of a memory block.
117 : * Seed is a 128 bit vector.
118 : */
119 : void memhash(unsigned kind, const unsigned char* seed, void* digest, const void* src, unsigned size);
120 :
121 : /**
122 : * Return the hash name.
123 : */
124 : const char* hash_config_name(unsigned kind);
125 :
126 : /****************************************************************************/
127 : /* lock */
128 :
129 : /**
130 : * Creates and locks the lock file.
131 : * Returns -1 on error, otherwise it's the file handle to pass to lock_unlock().
132 : */
133 : int lock_lock(const char* file);
134 :
135 : /**
136 : * Unlocks the lock file.
137 : * Returns -1 on error.
138 : */
139 : int lock_unlock(int f);
140 :
141 : #endif
142 :
|