iodine 0.4.19 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of iodine might be problematic. Click here for more details.

Files changed (146) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -2
  3. data/CHANGELOG.md +22 -0
  4. data/LIMITS.md +19 -9
  5. data/README.md +92 -77
  6. data/SPEC-PubSub-Draft.md +113 -0
  7. data/SPEC-Websocket-Draft.md +127 -143
  8. data/bin/http-hello +0 -1
  9. data/bin/raw-rbhttp +1 -1
  10. data/bin/raw_broadcast +8 -10
  11. data/bin/updated api +2 -2
  12. data/bin/ws-broadcast +2 -4
  13. data/bin/ws-echo +2 -2
  14. data/examples/config.ru +13 -13
  15. data/examples/echo.ru +5 -6
  16. data/examples/hello.ru +2 -3
  17. data/examples/info.md +316 -0
  18. data/examples/pubsub_engine.ru +81 -0
  19. data/examples/redis.ru +9 -9
  20. data/examples/shootout.ru +45 -11
  21. data/ext/iodine/defer.c +194 -297
  22. data/ext/iodine/defer.h +61 -53
  23. data/ext/iodine/evio.c +0 -260
  24. data/ext/iodine/evio.h +50 -22
  25. data/ext/iodine/evio_callbacks.c +26 -0
  26. data/ext/iodine/evio_epoll.c +251 -0
  27. data/ext/iodine/evio_kqueue.c +193 -0
  28. data/ext/iodine/extconf.rb +1 -1
  29. data/ext/iodine/facil.c +1420 -542
  30. data/ext/iodine/facil.h +151 -64
  31. data/ext/iodine/fio_ary.h +418 -0
  32. data/ext/iodine/{base64.c → fio_base64.c} +33 -24
  33. data/ext/iodine/{base64.h → fio_base64.h} +6 -7
  34. data/ext/iodine/{fio_cli_helper.c → fio_cli.c} +77 -58
  35. data/ext/iodine/{fio_cli_helper.h → fio_cli.h} +9 -4
  36. data/ext/iodine/fio_hashmap.h +759 -0
  37. data/ext/iodine/fio_json_parser.h +651 -0
  38. data/ext/iodine/fio_llist.h +257 -0
  39. data/ext/iodine/fio_mem.c +672 -0
  40. data/ext/iodine/fio_mem.h +140 -0
  41. data/ext/iodine/fio_random.c +248 -0
  42. data/ext/iodine/{random.h → fio_random.h} +11 -14
  43. data/ext/iodine/{sha1.c → fio_sha1.c} +28 -24
  44. data/ext/iodine/{sha1.h → fio_sha1.h} +38 -16
  45. data/ext/iodine/{sha2.c → fio_sha2.c} +66 -49
  46. data/ext/iodine/{sha2.h → fio_sha2.h} +57 -26
  47. data/ext/iodine/{fiobj_internal.c → fio_siphash.c} +9 -90
  48. data/ext/iodine/fio_siphash.h +18 -0
  49. data/ext/iodine/fio_tmpfile.h +38 -0
  50. data/ext/iodine/fiobj.h +24 -7
  51. data/ext/iodine/fiobj4sock.h +23 -0
  52. data/ext/iodine/fiobj_ary.c +143 -226
  53. data/ext/iodine/fiobj_ary.h +17 -16
  54. data/ext/iodine/fiobj_data.c +1160 -0
  55. data/ext/iodine/fiobj_data.h +164 -0
  56. data/ext/iodine/fiobj_hash.c +298 -406
  57. data/ext/iodine/fiobj_hash.h +101 -54
  58. data/ext/iodine/fiobj_json.c +478 -601
  59. data/ext/iodine/fiobj_json.h +34 -9
  60. data/ext/iodine/fiobj_numbers.c +383 -51
  61. data/ext/iodine/fiobj_numbers.h +87 -11
  62. data/ext/iodine/fiobj_str.c +423 -184
  63. data/ext/iodine/fiobj_str.h +81 -32
  64. data/ext/iodine/fiobject.c +273 -522
  65. data/ext/iodine/fiobject.h +477 -112
  66. data/ext/iodine/http.c +2243 -83
  67. data/ext/iodine/http.h +842 -121
  68. data/ext/iodine/http1.c +810 -385
  69. data/ext/iodine/http1.h +16 -39
  70. data/ext/iodine/http1_parser.c +146 -74
  71. data/ext/iodine/http1_parser.h +15 -4
  72. data/ext/iodine/http_internal.c +1258 -0
  73. data/ext/iodine/http_internal.h +226 -0
  74. data/ext/iodine/http_mime_parser.h +341 -0
  75. data/ext/iodine/iodine.c +86 -68
  76. data/ext/iodine/iodine.h +26 -11
  77. data/ext/iodine/iodine_helpers.c +8 -7
  78. data/ext/iodine/iodine_http.c +487 -324
  79. data/ext/iodine/iodine_json.c +304 -0
  80. data/ext/iodine/iodine_json.h +6 -0
  81. data/ext/iodine/iodine_protocol.c +107 -45
  82. data/ext/iodine/iodine_pubsub.c +526 -225
  83. data/ext/iodine/iodine_pubsub.h +10 -0
  84. data/ext/iodine/iodine_websockets.c +268 -510
  85. data/ext/iodine/iodine_websockets.h +2 -4
  86. data/ext/iodine/pubsub.c +726 -432
  87. data/ext/iodine/pubsub.h +85 -103
  88. data/ext/iodine/rb-call.c +4 -4
  89. data/ext/iodine/rb-defer.c +46 -22
  90. data/ext/iodine/rb-fiobj2rb.h +117 -0
  91. data/ext/iodine/rb-rack-io.c +73 -238
  92. data/ext/iodine/rb-rack-io.h +2 -2
  93. data/ext/iodine/rb-registry.c +35 -93
  94. data/ext/iodine/rb-registry.h +1 -0
  95. data/ext/iodine/redis_engine.c +742 -304
  96. data/ext/iodine/redis_engine.h +42 -39
  97. data/ext/iodine/resp_parser.h +311 -0
  98. data/ext/iodine/sock.c +627 -490
  99. data/ext/iodine/sock.h +345 -297
  100. data/ext/iodine/spnlock.inc +15 -4
  101. data/ext/iodine/websocket_parser.h +16 -20
  102. data/ext/iodine/websockets.c +188 -257
  103. data/ext/iodine/websockets.h +24 -133
  104. data/lib/iodine.rb +52 -7
  105. data/lib/iodine/cli.rb +6 -24
  106. data/lib/iodine/json.rb +40 -0
  107. data/lib/iodine/version.rb +1 -1
  108. data/lib/iodine/websocket.rb +5 -3
  109. data/lib/rack/handler/iodine.rb +58 -13
  110. metadata +38 -48
  111. data/bin/ws-shootout +0 -107
  112. data/examples/broadcast.ru +0 -56
  113. data/ext/iodine/bscrypt-common.h +0 -116
  114. data/ext/iodine/bscrypt.h +0 -49
  115. data/ext/iodine/fio2resp.c +0 -60
  116. data/ext/iodine/fio2resp.h +0 -51
  117. data/ext/iodine/fio_dict.c +0 -446
  118. data/ext/iodine/fio_dict.h +0 -99
  119. data/ext/iodine/fio_hash_table.h +0 -370
  120. data/ext/iodine/fio_list.h +0 -111
  121. data/ext/iodine/fiobj_internal.h +0 -280
  122. data/ext/iodine/fiobj_primitives.c +0 -131
  123. data/ext/iodine/fiobj_primitives.h +0 -55
  124. data/ext/iodine/fiobj_sym.c +0 -135
  125. data/ext/iodine/fiobj_sym.h +0 -60
  126. data/ext/iodine/hex.c +0 -124
  127. data/ext/iodine/hex.h +0 -70
  128. data/ext/iodine/http1_request.c +0 -81
  129. data/ext/iodine/http1_request.h +0 -58
  130. data/ext/iodine/http1_response.c +0 -417
  131. data/ext/iodine/http1_response.h +0 -95
  132. data/ext/iodine/http_request.c +0 -111
  133. data/ext/iodine/http_request.h +0 -102
  134. data/ext/iodine/http_response.c +0 -1703
  135. data/ext/iodine/http_response.h +0 -250
  136. data/ext/iodine/misc.c +0 -182
  137. data/ext/iodine/misc.h +0 -74
  138. data/ext/iodine/random.c +0 -208
  139. data/ext/iodine/redis_connection.c +0 -278
  140. data/ext/iodine/redis_connection.h +0 -86
  141. data/ext/iodine/resp.c +0 -842
  142. data/ext/iodine/resp.h +0 -261
  143. data/ext/iodine/siphash.c +0 -154
  144. data/ext/iodine/siphash.h +0 -22
  145. data/ext/iodine/xor-crypt.c +0 -193
  146. data/ext/iodine/xor-crypt.h +0 -107
@@ -0,0 +1,140 @@
1
+ /*
2
+ Copyright: Boaz Segev, 2018
3
+ License: MIT
4
+
5
+ Feel free to copy, use and enjoy according to the license provided.
6
+ */
7
+ #ifndef H_FIO_MEM_H
8
+
9
+ /**
10
+ * This is a custom memory allocator the utilizes memory pools to allow for
11
+ * concurrent memory allocations across threads.
12
+ *
13
+ * Allocated memory is always zeroed out and aligned on a 16 byte boundary.
14
+ *
15
+ * Reallocated memory is always aligned on a 16 byte boundary but it might be
16
+ * filled with junk data after the valid data (this is true also for
17
+ * `fio_realloc2`).
18
+ *
19
+ * The memory allocator assumes multiple concurrent allocation/deallocation,
20
+ * short life spans (memory is freed shortly, but not immediately, after it was
21
+ * allocated) and we small allocations (realloc almost always copies data).
22
+ *
23
+ * These assumptions allow the allocator to avoid lock contention by ignoring
24
+ * fragmentation within a memory "block" and waiting for the whole "block" to be
25
+ * freed before it's memory is recycled (no "free list").
26
+ *
27
+ * This allocator should NOT be used for objects with a long life-span, because
28
+ * even a single persistent object will prevent the re-use of the whole memory
29
+ * block (128Kb by default) from which it was allocated.
30
+ *
31
+ * A memory "block" can include any number of memory pages that are a multiple
32
+ * of 2 (up to 1Mb of memory). However, the default value, set by
33
+ * MEMORY_BLOCK_SIZE, is either 128Kb (set at th end of this header).
34
+ *
35
+ * Each block includes a header that uses reference counters and position
36
+ * markers.
37
+ *
38
+ * The position marker (`pos`) marks the next available byte (counted in
39
+ * multiples of 16).
40
+ *
41
+ * The reference counter (`ref`) counts how many pointers reference memory in
42
+ * the block (including the "arena" that "owns" the block).
43
+ *
44
+ * Except for the position marker (`pos`) that acts the same as `sbrk`, there's
45
+ * no way to know which "slices" are allocated and which "slices" are available.
46
+ *
47
+ * Small allocations are differentiated by their memory alignment. If a memory
48
+ * allocation is placed 8 bytes after whole block alignment, the memory was
49
+ * allocated directly using `mmap` (and it might be using a whole page more than
50
+ * request, just because it needed that extra header space!).
51
+ *
52
+ * The allocator uses `mmap` when requesting memory from the system and for
53
+ * allocations bigger than MEMORY_BLOCK_ALLOC_LIMIT (37.5% of the block).
54
+ *
55
+ * To replace the system's `malloc` function family compile with the
56
+ * `FIO_OVERRIDE_MALLOC` defined (`-DFIO_OVERRIDE_MALLOC`).
57
+ *
58
+ * When using tcmalloc or jemalloc, define `FIO_FORCE_MALLOC` to prevent
59
+ * `fio_mem` from compiling (`-DFIO_FORCE_MALLOC`). Function wrappers will be
60
+ * compiled just in case, so calls to `fio_malloc` will be routed to `malloc`.
61
+ *
62
+ */
63
+ #define H_FIO_MEM_H
64
+
65
+ #include <stdlib.h>
66
+
67
+ /** Allocates memory using a per-CPU core block memory pool. */
68
+ void *fio_malloc(size_t size);
69
+
70
+ /** Allocates memory using a per-CPU core block memory pool. memory is zeroed
71
+ * out. */
72
+ void *fio_calloc(size_t size, size_t count);
73
+
74
+ /** Frees memory that was allocated using this library. */
75
+ void fio_free(void *ptr);
76
+
77
+ /**
78
+ * Re-allocates memory. An attept to avoid copying the data is made only for
79
+ * memory allocations larger than 64Kb.
80
+ */
81
+ void *fio_realloc(void *ptr, size_t new_size);
82
+
83
+ /**
84
+ * Re-allocates memory. An attept to avoid copying the data is made only for
85
+ * memory allocations larger than 64Kb.
86
+ *
87
+ * This variation is slightly faster as it might copy less data.
88
+ */
89
+ void *fio_realloc2(void *ptr, size_t new_size, size_t copy_length);
90
+
91
+ /** Clears any memory locks, in case of a system call to `fork`. */
92
+ void fio_malloc_after_fork(void);
93
+
94
+ /** Tests the facil.io memory allocator. */
95
+ void fio_malloc_test(void);
96
+
97
+ /** If defined, `malloc` will be used instead of the fio_malloc functions */
98
+ #if FIO_FORCE_MALLOC
99
+ #define fio_malloc malloc
100
+ #define fio_calloc calloc
101
+ #define fio_free free
102
+ #define fio_realloc realloc
103
+ #define fio_realloc2(ptr, new_size, old_data_len) realloc((ptr), (new_size))
104
+ #define fio_malloc_test()
105
+ #define fio_malloc_after_fork
106
+
107
+ /* allows local override as well as global override */
108
+ #elif FIO_OVERRIDE_MALLOC
109
+ #define malloc fio_malloc
110
+ #define free fio_free
111
+ #define realloc fio_realloc
112
+ #define calloc fio_calloc
113
+
114
+ #endif
115
+
116
+ #ifndef FIO_MEM_MAX_BLOCKS_PER_CORE
117
+ /**
118
+ * The maximum number of available memory blocks that will be pooled before
119
+ * memory is returned to the system.
120
+ */
121
+ #define FIO_MEM_MAX_BLOCKS_PER_CORE 32 /* approx. 2Mb per CPU core */
122
+ #endif
123
+
124
+ /** Allocator default settings. */
125
+ #ifndef FIO_MEMORY_BLOCK_SIZE
126
+ #define FIO_MEMORY_BLOCK_SIZE ((uintptr_t)1 << 17) /* 17 == 128Kb */
127
+ #endif
128
+ #ifndef FIO_MEMORY_BLOCK_MASK
129
+ #define FIO_MEMORY_BLOCK_MASK (FIO_MEMORY_BLOCK_SIZE - 1) /* 0b111... */
130
+ #endif
131
+ #ifndef FIO_MEMORY_BLOCK_SLICES
132
+ #define FIO_MEMORY_BLOCK_SLICES (FIO_MEMORY_BLOCK_SIZE >> 4) /* 16B/slice */
133
+ #endif
134
+ #ifndef FIO_MEMORY_BLOCK_ALLOC_LIMIT
135
+ /* defaults to 37.5% of the block */
136
+ #define FIO_MEMORY_BLOCK_ALLOC_LIMIT \
137
+ ((FIO_MEMORY_BLOCK_SIZE >> 2) + (FIO_MEMORY_BLOCK_SIZE >> 3))
138
+ #endif
139
+
140
+ #endif /* H_FIO_MEM_H */
@@ -0,0 +1,248 @@
1
+ /*
2
+ Copyright: Boaz segev, 2016-2018
3
+ License: MIT except for any non-public-domain algorithms (none that I'm aware
4
+ of), which might be subject to their own licenses.
5
+
6
+ Feel free to copy, use and enjoy in accordance with to the license(s).
7
+ */
8
+ #ifndef _GNU_SOURCE
9
+ #define _GNU_SOURCE
10
+ #endif
11
+ #include "fio_random.h"
12
+
13
+ #include <errno.h>
14
+ #include <stdio.h>
15
+
16
+ #ifndef __has_include
17
+ #define __has_include(x) 0
18
+ #endif
19
+
20
+ /* include intrinsics if supported */
21
+ #if __has_include(<x86intrin.h>)
22
+ #include <x86intrin.h>
23
+ #define HAVE_X86Intrin
24
+ /*
25
+ see: https://software.intel.com/en-us/node/513411
26
+ and: https://software.intel.com/sites/landingpage/IntrinsicsGuide/
27
+ */
28
+ #endif /* __has_include(<x86intrin.h>) */
29
+
30
+ /* check for unix support */
31
+ #if defined(__unix__) || defined(__linux__) || defined(__APPLE__) || \
32
+ (__has_include(<unistd.h>) && __has_include(<pthread.h>))
33
+ #ifndef HAS_UNIX_FEATURES
34
+ #define HAS_UNIX_FEATURES
35
+ #endif
36
+ #endif
37
+
38
+ // clang-format off
39
+ #if !defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)
40
+ # if defined(__has_include)
41
+ # if __has_include(<endian.h>)
42
+ # include <endian.h>
43
+ # elif __has_include(<sys/endian.h>)
44
+ # include <sys/endian.h>
45
+ # endif
46
+ # endif
47
+ # if !defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__) && \
48
+ __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
49
+ # define __BIG_ENDIAN__
50
+ # endif
51
+ #endif
52
+ // clang-format on
53
+
54
+ #if defined(USE_ALT_RANDOM) || !defined(HAS_UNIX_FEATURES)
55
+ #include "fio_sha2.h"
56
+ #include <time.h>
57
+ #include <string.h>
58
+
59
+ static inline void fio_random_data(sha2_s *sha2) {
60
+ #ifdef RUSAGE_SELF
61
+ struct rusage rusage;
62
+ getrusage(RUSAGE_SELF, &rusage);
63
+ fio_sha2_write(sha2, &rusage, sizeof(rusage));
64
+ #elif defined CLOCKS_PER_SEC
65
+ size_t clk = (size_t)clock();
66
+ fio_sha2_write(&sha2, &clk, sizeof(clk));
67
+ time_t the_time;
68
+ time(&the_time);
69
+ fio_sha2_write(sha2, &the_time, sizeof(the_time));
70
+ fio_sha2_write(sha2, &fio_rand64, sizeof(void *));
71
+ fio_sha2_write(sha2, &fio_rand_bytes, sizeof(void *));
72
+ {
73
+ char junk_data[64];
74
+ fio_sha2_write(sha2, junk_data, 64);
75
+ }
76
+ #else
77
+ #error Random alternative failed to find access to the CPU clock state.
78
+ #endif
79
+ fio_sha2_write(sha2, sha2, sizeof(void *));
80
+ }
81
+
82
+ uint32_t fio_rand32(void) {
83
+ sha2_s sha2 = fio_sha2_init(SHA_512);
84
+ fio_random_data(&sha2);
85
+ fio_sha2_result(&sha2);
86
+ return *((uint32_t *)sha2.buffer);
87
+ }
88
+
89
+ uint64_t fio_rand64(void) {
90
+ sha2_s sha2 = fio_sha2_init(SHA_512);
91
+ fio_random_data(&sha2);
92
+ fio_sha2_result(&sha2);
93
+ return *((uint64_t *)sha2.buffer);
94
+ }
95
+
96
+ void fio_rand_bytes(void *target, size_t length) {
97
+ sha2_s sha2 = fio_sha2_init(SHA_512);
98
+ fio_random_data(&sha2);
99
+ fio_sha2_result(&sha2);
100
+
101
+ while (length >= 64) {
102
+ memcpy(target, sha2.digest.str, 64);
103
+ length -= 64;
104
+ target = (void *)((uintptr_t)target + 64);
105
+ fio_random_data(&sha2);
106
+ fio_sha2_result(&sha2);
107
+ }
108
+ if (length >= 32) {
109
+ memcpy(target, sha2.digest.str, 32);
110
+ length -= 32;
111
+ target = (void *)((uintptr_t)target + 32);
112
+ fio_random_data(&sha2);
113
+ fio_sha2_result(&sha2);
114
+ }
115
+ if (length >= 16) {
116
+ memcpy(target, sha2.digest.str, 16);
117
+ length -= 16;
118
+ target = (void *)((uintptr_t)target + 16);
119
+ fio_random_data(&sha2);
120
+ fio_sha2_result(&sha2);
121
+ }
122
+ if (length >= 8) {
123
+ memcpy(target, sha2.digest.str, 8);
124
+ length -= 8;
125
+ target = (void *)((uintptr_t)target + 8);
126
+ fio_random_data(&sha2);
127
+ fio_sha2_result(&sha2);
128
+ }
129
+ while (length) {
130
+ *((uint8_t *)target) = sha2.digest.str[length];
131
+ target = (void *)((uintptr_t)target + 1);
132
+ --length;
133
+ }
134
+ }
135
+
136
+ #else
137
+ /* ***************************************************************************
138
+ Unix Random Engine (use built in machine)
139
+ */
140
+ #include <errno.h>
141
+ #include <fcntl.h>
142
+ #include <pthread.h>
143
+ #include <unistd.h>
144
+
145
+ /* ***************************************************************************
146
+ Machine specific changes
147
+ */
148
+ // #ifdef __linux__
149
+ // #undef bswap16
150
+ // #undef bswap32
151
+ // #undef bswap64
152
+ // #include <machine/bswap.h>
153
+ // #endif
154
+ #ifdef HAVE_X86Intrin
155
+ // #undef bswap16
156
+ /*
157
+ #undef bswap32
158
+ #define bswap32(i) \
159
+ { __asm__("bswap %k0" : "+r"(i) :); }
160
+ */
161
+ #undef bswap64
162
+ #define bswap64(i) \
163
+ { __asm__("bswapq %0" : "+r"(i) :); }
164
+
165
+ // shadow sched_yield as _mm_pause for spinwait
166
+ #define sched_yield() _mm_pause()
167
+ #endif
168
+
169
+ /* ***************************************************************************
170
+ Random fd
171
+ ***************************************************************************** */
172
+
173
+ /* rand generator management */
174
+ static int fio_rand_fd_ = -1;
175
+ static void close_rand_fd(void) {
176
+ if (fio_rand_fd_ >= 0)
177
+ close(fio_rand_fd_);
178
+ fio_rand_fd_ = -1;
179
+ }
180
+ static void init_rand_fd(void) {
181
+ if (fio_rand_fd_ < 0) {
182
+ while ((fio_rand_fd_ = open("/dev/urandom", O_RDONLY)) == -1) {
183
+ if (errno == ENXIO) {
184
+ perror("FATAL ERROR: caanot initiate random generator");
185
+ exit(-1);
186
+ }
187
+ sched_yield();
188
+ }
189
+ }
190
+ atexit(close_rand_fd);
191
+ }
192
+
193
+ /* ***************************************************************************
194
+ Random API ... (why is this not a system call?)
195
+ ***************************************************************************** */
196
+
197
+ /* rand function template */
198
+ #define MAKE_RAND_FUNC(type, func_name) \
199
+ type func_name(void) { \
200
+ if (fio_rand_fd_ < 0) \
201
+ init_rand_fd(); \
202
+ type ret; \
203
+ while (read(fio_rand_fd_, &ret, sizeof(type)) < 0) \
204
+ sched_yield(); \
205
+ return ret; \
206
+ }
207
+ /* rand functions */
208
+ MAKE_RAND_FUNC(uint32_t, fio_rand32)
209
+ MAKE_RAND_FUNC(uint64_t, fio_rand64)
210
+ /* clear template */
211
+ #undef MAKE_RAND_FUNC
212
+
213
+ void fio_rand_bytes(void *target, size_t length) {
214
+ if (fio_rand_fd_ < 0)
215
+ init_rand_fd();
216
+ while (read(fio_rand_fd_, target, length) < 0)
217
+ sched_yield();
218
+ }
219
+ #endif /* Unix Random */
220
+
221
+ /*******************************************************************************
222
+ Random Testing
223
+ ***************************************************************************** */
224
+ #if DEBUG
225
+ void fio_random_test(void) {
226
+ uint64_t buffer[8];
227
+ clock_t start, end;
228
+ fio_rand64();
229
+ start = clock();
230
+ for (size_t i = 0; i < 100000; i++) {
231
+ buffer[i & 7] = fio_rand64();
232
+ }
233
+ end = clock();
234
+ fprintf(stderr,
235
+ "+ Random generator available\n+ created 100K X 64bits "
236
+ "Random %lu CPU clock count\n",
237
+ end - start);
238
+ start = clock();
239
+ for (size_t i = 0; i < 100000; i++) {
240
+ fio_rand_bytes(buffer, 64);
241
+ }
242
+ end = clock();
243
+ fprintf(stderr,
244
+ "+ created 100K X 512bits "
245
+ "Random %lu CPU clock count\n",
246
+ end - start);
247
+ }
248
+ #endif
@@ -5,12 +5,15 @@ of), which might be subject to their own licenses.
5
5
 
6
6
  Feel free to copy, use and enjoy in accordance with to the license(s).
7
7
  */
8
- #ifndef bscrypt_RANDOM_H
9
- #define bscrypt_RANDOM_H
10
- #include "bscrypt-common.h"
8
+ #ifndef fio_RANDOM_H
9
+ #define fio_RANDOM_H
11
10
  /* *****************************************************************************
12
11
  C++ extern
13
12
  */
13
+
14
+ #include <stdint.h>
15
+ #include <stdlib.h>
16
+
14
17
  #if defined(__cplusplus)
15
18
  extern "C" {
16
19
  #endif
@@ -20,22 +23,16 @@ Random stuff... (why is this not a system call?)
20
23
  */
21
24
 
22
25
  /** returns 32 random bits. */
23
- uint32_t bscrypt_rand32(void);
26
+ uint32_t fio_rand32(void);
24
27
 
25
28
  /** returns 64 random bits. */
26
- uint64_t bscrypt_rand64(void);
27
-
28
- /** returns 128 random bits. */
29
- bits128_u bscrypt_rand128(void);
30
-
31
- /** returns 256 random bits. */
32
- bits256_u bscrypt_rand256(void);
29
+ uint64_t fio_rand64(void);
33
30
 
34
31
  /** returns a variable length string of random bytes. */
35
- void bscrypt_rand_bytes(void *target, size_t length);
32
+ void fio_rand_bytes(void *target, size_t length);
36
33
 
37
- #if defined(DEBUG) && DEBUG == 1
38
- void bscrypt_test_random(void);
34
+ #if DEBUG
35
+ void fio_random_test(void);
39
36
  #endif
40
37
 
41
38
  /* *****************************************************************************
@@ -8,7 +8,10 @@ Feel free to copy, use and enjoy in accordance with to the license(s).
8
8
  #ifndef _GNU_SOURCE
9
9
  #define _GNU_SOURCE
10
10
  #endif
11
- #include "sha1.h"
11
+ #include "fio_sha1.h"
12
+
13
+ #include <string.h>
14
+
12
15
  /*****************************************************************************
13
16
  Useful Macros - Not all of them are used here, but it's a copy-paste convenience
14
17
  */
@@ -198,7 +201,7 @@ SHA-1 hashing
198
201
  Initialize or reset the `sha1` object. This must be performed before hashing
199
202
  data using sha1.
200
203
  */
201
- sha1_s bscrypt_sha1_init(void) {
204
+ sha1_s fio_sha1_init(void) {
202
205
  return (sha1_s){.digest.i[0] = 0x67452301,
203
206
  .digest.i[1] = 0xefcdab89,
204
207
  .digest.i[2] = 0x98badcfe,
@@ -209,7 +212,7 @@ sha1_s bscrypt_sha1_init(void) {
209
212
  /**
210
213
  Writes data to the sha1 buffer.
211
214
  */
212
- void bscrypt_sha1_write(sha1_s *s, const void *data, size_t len) {
215
+ void fio_sha1_write(sha1_s *s, const void *data, size_t len) {
213
216
  size_t in_buffer = s->length & 63;
214
217
  size_t partial = 64 - in_buffer;
215
218
  s->length += len;
@@ -234,7 +237,7 @@ void bscrypt_sha1_write(sha1_s *s, const void *data, size_t len) {
234
237
  return;
235
238
  }
236
239
 
237
- char *bscrypt_sha1_result(sha1_s *s) {
240
+ char *fio_sha1_result(sha1_s *s) {
238
241
  size_t in_buffer = s->length & 63;
239
242
  if (in_buffer > 55) {
240
243
  memcpy(s->buffer + in_buffer, sha1_padding, 64 - in_buffer);
@@ -273,6 +276,7 @@ char *bscrypt_sha1_result(sha1_s *s) {
273
276
  SHA-1 testing
274
277
  */
275
278
  #if defined(DEBUG) && DEBUG == 1
279
+ #include <stdio.h>
276
280
  #include <time.h>
277
281
 
278
282
  // clang-format off
@@ -281,10 +285,10 @@ SHA-1 testing
281
285
  #endif
282
286
  // clang-format on
283
287
 
284
- void bscrypt_test_sha1(void) {
288
+ void fio_sha1_test(void) {
285
289
  struct {
286
290
  char *str;
287
- char hash[21];
291
+ uint8_t hash[21];
288
292
  } sets[] = {
289
293
  {"The quick brown fox jumps over the lazy dog",
290
294
  {0x2f, 0xd4, 0xe1, 0xc6, 0x7a, 0x2d, 0x28, 0xfc, 0xed, 0x84, 0x9e,
@@ -300,20 +304,19 @@ void bscrypt_test_sha1(void) {
300
304
  int i = 0;
301
305
  sha1_s sha1;
302
306
  fprintf(stderr, "===================================\n");
303
- fprintf(stderr, "bscrypt SHA-1 struct size: %lu\n", sizeof(sha1_s));
304
- fprintf(stderr, "+ bscrypt");
307
+ fprintf(stderr, "fio SHA-1 struct size: %zu\n", sizeof(sha1_s));
308
+ fprintf(stderr, "+ fio");
305
309
  while (sets[i].str) {
306
- sha1 = bscrypt_sha1_init();
307
- bscrypt_sha1_write(&sha1, sets[i].str, strlen(sets[i].str));
308
- if (strcmp(bscrypt_sha1_result(&sha1), sets[i].hash)) {
309
- fprintf(stderr,
310
- ":\n--- bscrypt SHA-1 Test FAILED!\nstring: %s\nexpected: ",
310
+ sha1 = fio_sha1_init();
311
+ fio_sha1_write(&sha1, sets[i].str, strlen(sets[i].str));
312
+ if (strcmp(fio_sha1_result(&sha1), (char *)sets[i].hash)) {
313
+ fprintf(stderr, ":\n--- fio SHA-1 Test FAILED!\nstring: %s\nexpected: ",
311
314
  sets[i].str);
312
- char *p = sets[i].hash;
315
+ char *p = (char *)sets[i].hash;
313
316
  while (*p)
314
317
  fprintf(stderr, "%02x", *(p++) & 0xFF);
315
318
  fprintf(stderr, "\ngot: ");
316
- p = bscrypt_sha1_result(&sha1);
319
+ p = fio_sha1_result(&sha1);
317
320
  while (*p)
318
321
  fprintf(stderr, "%02x", *(p++) & 0xFF);
319
322
  fprintf(stderr, "\n");
@@ -325,27 +328,28 @@ void bscrypt_test_sha1(void) {
325
328
 
326
329
  #ifdef HAVE_OPENSSL
327
330
  fprintf(stderr, "===================================\n");
328
- fprintf(stderr, "bscrypt SHA-1 struct size: %lu\n", sizeof(sha1_s));
329
- fprintf(stderr, "OpenSSL SHA-1 struct size: %lu\n", sizeof(SHA_CTX));
331
+ fprintf(stderr, "fio SHA-1 struct size: %lu\n",
332
+ (unsigned long)sizeof(sha1_s));
333
+ fprintf(stderr, "OpenSSL SHA-1 struct size: %lu\n",
334
+ (unsigned long)sizeof(SHA_CTX));
330
335
  fprintf(stderr, "===================================\n");
331
336
 
332
337
  unsigned char hash[SHA512_DIGEST_LENGTH + 1];
333
338
  hash[SHA512_DIGEST_LENGTH] = 0;
334
339
  clock_t start;
335
340
  start = clock();
336
- for (size_t i = 0; i < 100000; i++) {
337
- sha1 = bscrypt_sha1_init();
338
- bscrypt_sha1_write(&sha1, "The quick brown fox jumps over the lazy dog ",
339
- 43);
340
- bscrypt_sha1_result(&sha1);
341
+ for (i = 0; i < 100000; i++) {
342
+ sha1 = fio_sha1_init();
343
+ fio_sha1_write(&sha1, "The quick brown fox jumps over the lazy dog ", 43);
344
+ fio_sha1_result(&sha1);
341
345
  }
342
- fprintf(stderr, "bscrypt 100K SHA-1: %lf\n",
346
+ fprintf(stderr, "fio 100K SHA-1: %lf\n",
343
347
  (double)(clock() - start) / CLOCKS_PER_SEC);
344
348
 
345
349
  hash[SHA_DIGEST_LENGTH] = 0;
346
350
  SHA_CTX o_sh1;
347
351
  start = clock();
348
- for (size_t i = 0; i < 100000; i++) {
352
+ for (i = 0; i < 100000; i++) {
349
353
  SHA1_Init(&o_sh1);
350
354
  SHA1_Update(&o_sh1, "The quick brown fox jumps over the lazy dog", 43);
351
355
  SHA1_Final(hash, &o_sh1);