demake 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/demake +163 -176
- data/lib/apps/example/Makefile +374 -0
- data/lib/{data → apps}/example/demake/applications +1 -1
- data/lib/apps/example/demake/brief_description +1 -0
- data/lib/{data → apps}/example/demake/license +0 -0
- data/lib/{data → apps}/example/demake/settings.rb +14 -14
- data/lib/apps/example/demake/suggestion +1 -0
- data/lib/{data → apps}/example/demake/test-target.rb +0 -0
- data/lib/{data → apps}/example/src/goodbye.c +0 -0
- data/lib/{data → apps}/example/src/hello.c +0 -0
- data/lib/{data → apps}/example/src/string/string.c +0 -0
- data/lib/{data → apps}/example/src/string/string.h +0 -0
- data/lib/apps/oreo/Makefile +260 -0
- data/lib/{data → apps}/oreo/demake/applications +0 -0
- data/lib/apps/oreo/demake/brief_description +1 -0
- data/lib/{data → apps}/oreo/demake/license +0 -0
- data/lib/{data → apps}/oreo/demake/settings.rb +13 -13
- data/lib/apps/oreo/demake/suggestion +1 -0
- data/lib/{data → apps}/oreo/demake/test-target.rb +0 -0
- data/lib/apps/oreo/src/defines.h +29 -0
- data/lib/{data → apps}/oreo/src/fast_read_file.h +18 -18
- data/lib/{data → apps}/oreo/src/oreo.c +4 -4
- data/lib/{data → apps}/oreo/src/typedefs.h +8 -8
- data/lib/data/libsrc/auto_bits.h +33 -0
- data/lib/data/libsrc/base.h +10 -0
- data/lib/data/libsrc/cpu_mark_check.h +267 -0
- data/lib/data/libsrc/defines.h +29 -0
- data/lib/data/libsrc/fast_read_file.h +259 -0
- data/lib/data/libsrc/fast_sha2.h +1840 -0
- data/lib/data/libsrc/parse_arguments.h +0 -0
- data/lib/data/libsrc/rb_library.c +140 -0
- data/lib/data/libsrc/typedefs.h +61 -0
- data/lib/template/build_target.rb +0 -0
- data/lib/template/clean_target.rb +0 -0
- data/lib/template/debug_executable_target.rb +0 -0
- data/lib/template/debug_library_target.rb +0 -0
- data/lib/template/debug_target.rb +0 -0
- data/lib/template/dependency_targets.rb +0 -0
- data/lib/template/executable_debug_target.rb +0 -0
- data/lib/template/executable_target.rb +0 -0
- data/lib/template/generic_dependency_targets.rb +0 -0
- data/lib/template/library_debug_target.rb +0 -0
- data/lib/template/library_target.rb +0 -0
- data/lib/template/license_target.rb +0 -0
- data/lib/template/link_library_target.rb +0 -0
- data/lib/template/strip_build.rb +0 -0
- metadata +35 -20
- data/lib/data/oreo/src/defines.h +0 -29
- /data/lib/{data → apps}/oreo/oreo_test.txt +0 -0
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
/*
|
|
2
|
+
|
|
3
|
+
cpu_mark_check.h -- *Full Library* Header File for Inline CPU Profiling
|
|
4
|
+
|
|
5
|
+
See the end of this file for an example.
|
|
6
|
+
|
|
7
|
+
Public Functions:
|
|
8
|
+
|
|
9
|
+
i32 cpu_mark_new(const c8 *description);
|
|
10
|
+
void cpu_mark_check(i32 index, i32 limit);
|
|
11
|
+
void cpu_mark_new_description(i32 index, const c8 *description);
|
|
12
|
+
void cpu_mark_reset(i32 index);
|
|
13
|
+
void cpu_mark_remove(i32 index);
|
|
14
|
+
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
#ifndef CPU_MARK_CHECK_H_Minaswan /* Prevents multiple inclusions */
|
|
18
|
+
#define CPU_MARK_CHECK_H_Minaswan
|
|
19
|
+
#define CPU_MARK_CHECK_VERSION "0.1.0"
|
|
20
|
+
#ifndef TYPEDEFS_H_Minaswan /* Header guard for typedefs.h */
|
|
21
|
+
#define TYPEDEFS_H_Minaswan
|
|
22
|
+
#include <stdint.h>
|
|
23
|
+
typedef uint8_t b8; /* Booleans */
|
|
24
|
+
typedef uint16_t b16;
|
|
25
|
+
typedef uint32_t b32;
|
|
26
|
+
typedef uint64_t b64;
|
|
27
|
+
|
|
28
|
+
typedef char c8; /* Characters */
|
|
29
|
+
typedef unsigned char uc8;
|
|
30
|
+
|
|
31
|
+
typedef uint8_t u8; /* Unsigned Numbers */
|
|
32
|
+
typedef uint16_t u16;
|
|
33
|
+
typedef uint32_t u32;
|
|
34
|
+
typedef uint64_t u64;
|
|
35
|
+
|
|
36
|
+
typedef int8_t i8; /* Signed Numbers */
|
|
37
|
+
typedef int16_t i16;
|
|
38
|
+
typedef int32_t i32;
|
|
39
|
+
typedef int64_t i64;
|
|
40
|
+
|
|
41
|
+
typedef float f32; /* Floating Point Numbers */
|
|
42
|
+
typedef double f64;
|
|
43
|
+
#endif /* TYPEDEFS_H_Minaswan */
|
|
44
|
+
|
|
45
|
+
/* Public Functions */
|
|
46
|
+
i32 cpu_mark_new(const c8 *description);
|
|
47
|
+
void cpu_mark_check(i32 index, i32 limit);
|
|
48
|
+
void cpu_mark_new_description(i32 index, const c8 *description);
|
|
49
|
+
void cpu_mark_reset(i32 index);
|
|
50
|
+
void cpu_mark_remove(i32 index);
|
|
51
|
+
|
|
52
|
+
#ifdef CPU_MARK_CHECK_IMPLEMENTATION
|
|
53
|
+
#include <stdio.h>
|
|
54
|
+
#include <stdlib.h>
|
|
55
|
+
#include <string.h>
|
|
56
|
+
#include <time.h>
|
|
57
|
+
#include <sys/time.h>
|
|
58
|
+
|
|
59
|
+
#ifndef FALSE
|
|
60
|
+
#define FALSE 0
|
|
61
|
+
#endif
|
|
62
|
+
|
|
63
|
+
#ifndef TRUE
|
|
64
|
+
#define TRUE 1
|
|
65
|
+
#endif
|
|
66
|
+
|
|
67
|
+
#ifndef NULL
|
|
68
|
+
#define NULL ((void *) 0)
|
|
69
|
+
#endif
|
|
70
|
+
|
|
71
|
+
#define CPU_MARK_DEFAULT_USEC 1000
|
|
72
|
+
|
|
73
|
+
struct cpu_mark_time {
|
|
74
|
+
i32 index;
|
|
75
|
+
u64 threshold;
|
|
76
|
+
const c8 *description;
|
|
77
|
+
struct timeval last;
|
|
78
|
+
struct cpu_mark_time *next;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
struct cpu_mark_time *cpu_mark_list = NULL;
|
|
82
|
+
|
|
83
|
+
i32 cpu_mark_next_index(void)
|
|
84
|
+
{
|
|
85
|
+
struct cpu_mark_time *cpu_mark = NULL;
|
|
86
|
+
i32 index = 0;
|
|
87
|
+
|
|
88
|
+
for(cpu_mark = cpu_mark_list; cpu_mark; cpu_mark = cpu_mark->next) {
|
|
89
|
+
if(cpu_mark->index >= index)
|
|
90
|
+
index = cpu_mark->index;
|
|
91
|
+
}
|
|
92
|
+
index++;
|
|
93
|
+
|
|
94
|
+
return(index);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
i32 cpu_mark_new(const c8 *description)
|
|
98
|
+
{
|
|
99
|
+
struct cpu_mark_time *cpu_mark = NULL;
|
|
100
|
+
|
|
101
|
+
cpu_mark = (struct cpu_mark_time *)calloc(1, sizeof(struct cpu_mark_time));
|
|
102
|
+
cpu_mark->index = cpu_mark_next_index();
|
|
103
|
+
if(description && *description)
|
|
104
|
+
cpu_mark->description = description;
|
|
105
|
+
else
|
|
106
|
+
cpu_mark->description = "No Description";
|
|
107
|
+
cpu_mark->next = cpu_mark_list;
|
|
108
|
+
cpu_mark_list = cpu_mark;
|
|
109
|
+
gettimeofday(&cpu_mark->last, (struct timezone *) 0);
|
|
110
|
+
|
|
111
|
+
return(cpu_mark->index);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
void cpu_mark_remove(i32 index)
|
|
115
|
+
{
|
|
116
|
+
struct cpu_mark_time *cpu_mark = NULL, *next_cpu_mark = NULL;
|
|
117
|
+
|
|
118
|
+
if(cpu_mark_list && cpu_mark_list->index == index) {
|
|
119
|
+
cpu_mark = cpu_mark_list;
|
|
120
|
+
cpu_mark_list = cpu_mark->next;
|
|
121
|
+
free(cpu_mark);
|
|
122
|
+
} else if(cpu_mark_list) {
|
|
123
|
+
for(cpu_mark = cpu_mark_list; cpu_mark; cpu_mark = cpu_mark->next) {
|
|
124
|
+
if(cpu_mark->next && cpu_mark->next->index == index) {
|
|
125
|
+
next_cpu_mark = cpu_mark->next;
|
|
126
|
+
cpu_mark->next = next_cpu_mark->next;
|
|
127
|
+
free(next_cpu_mark);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
void cpu_mark_reset(i32 index)
|
|
134
|
+
{
|
|
135
|
+
struct cpu_mark_time *cpu_mark = NULL;
|
|
136
|
+
|
|
137
|
+
for(cpu_mark = cpu_mark_list; cpu_mark; cpu_mark = cpu_mark->next) {
|
|
138
|
+
if(cpu_mark->index == index) {
|
|
139
|
+
gettimeofday(&cpu_mark->last, (struct timezone *) 0);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
struct cpu_mark_time *cpu_mark_find_by_index(i32 index)
|
|
145
|
+
{
|
|
146
|
+
struct cpu_mark_time *cpu_mark = NULL;
|
|
147
|
+
|
|
148
|
+
for(cpu_mark = cpu_mark_list; cpu_mark; cpu_mark = cpu_mark->next) {
|
|
149
|
+
if(cpu_mark->index == index)
|
|
150
|
+
return(cpu_mark);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return(NULL);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
void cpu_mark_new_description(i32 index, const c8 *description)
|
|
157
|
+
{
|
|
158
|
+
struct cpu_mark_time *cpu_mark = NULL;
|
|
159
|
+
|
|
160
|
+
cpu_mark = cpu_mark_find_by_index(index);
|
|
161
|
+
|
|
162
|
+
if(cpu_mark) {
|
|
163
|
+
if(description && *description)
|
|
164
|
+
cpu_mark->description = description;
|
|
165
|
+
else
|
|
166
|
+
cpu_mark->description = "No Description";
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
struct timeval cpu_mark_subtract_time(struct timeval *time1, struct timeval *time2)
|
|
171
|
+
{
|
|
172
|
+
struct timeval difference, no_time;
|
|
173
|
+
|
|
174
|
+
no_time.tv_sec = 0;
|
|
175
|
+
no_time.tv_usec = 0;
|
|
176
|
+
|
|
177
|
+
if(time1->tv_sec < time2->tv_sec)
|
|
178
|
+
return no_time;
|
|
179
|
+
else if(time1->tv_sec == time2->tv_sec) {
|
|
180
|
+
if(time1->tv_usec < time2->tv_usec)
|
|
181
|
+
return no_time;
|
|
182
|
+
else {
|
|
183
|
+
difference.tv_sec = 0;
|
|
184
|
+
difference.tv_usec = time1->tv_usec - time2->tv_usec;
|
|
185
|
+
return difference;
|
|
186
|
+
}
|
|
187
|
+
} else {
|
|
188
|
+
difference.tv_sec = time1->tv_sec - time2->tv_sec;
|
|
189
|
+
if(time1->tv_usec < time2->tv_usec) {
|
|
190
|
+
difference.tv_usec = time1->tv_usec + 1000000 - time2->tv_usec;
|
|
191
|
+
difference.tv_sec--;
|
|
192
|
+
} else
|
|
193
|
+
difference.tv_usec = time1->tv_usec - time2->tv_usec;
|
|
194
|
+
return difference;
|
|
195
|
+
}
|
|
196
|
+
return no_time;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
void cpu_mark_check(i32 index, i32 limit)
|
|
200
|
+
{
|
|
201
|
+
struct cpu_mark_time *cpu_mark = cpu_mark_find_by_index(index);
|
|
202
|
+
struct timeval current, spent;
|
|
203
|
+
c8 local_buf[1024];
|
|
204
|
+
c8 local_buf2[1024];
|
|
205
|
+
|
|
206
|
+
if(!cpu_mark)
|
|
207
|
+
return;
|
|
208
|
+
|
|
209
|
+
gettimeofday(¤t, (struct timezone *) 0);
|
|
210
|
+
spent = cpu_mark_subtract_time(¤t, &cpu_mark->last);
|
|
211
|
+
if(spent.tv_sec || spent.tv_usec > limit) {
|
|
212
|
+
sprintf(local_buf2, "CPU Threshold (%d): ", cpu_mark->index);
|
|
213
|
+
strcat(local_buf2, cpu_mark->description);
|
|
214
|
+
printf("%s\r\n", local_buf2);
|
|
215
|
+
sprintf(local_buf2, "CPU Threshold (%d): ", cpu_mark->index);
|
|
216
|
+
if(spent.tv_sec) {
|
|
217
|
+
sprintf(local_buf, " secs = %ld", spent.tv_sec);
|
|
218
|
+
strcat(local_buf2, local_buf);
|
|
219
|
+
}
|
|
220
|
+
if(spent.tv_usec) {
|
|
221
|
+
sprintf(local_buf, " usecs = %ld", spent.tv_usec);
|
|
222
|
+
strcat(local_buf2, local_buf);
|
|
223
|
+
}
|
|
224
|
+
printf("%s\r\n", local_buf2);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
#endif /* CPU_MARK_CHECK_IMPLEMENTATION */
|
|
228
|
+
#endif /* CPU_MARK_CHECK_H_Minaswan */
|
|
229
|
+
|
|
230
|
+
/*
|
|
231
|
+
Example:
|
|
232
|
+
|
|
233
|
+
#define CPU_MARK_CHECK_IMPLEMENTATION
|
|
234
|
+
#include "cpu_mark_check.h"
|
|
235
|
+
|
|
236
|
+
i32 mark1 = cpu_mark_new("Startup");
|
|
237
|
+
|
|
238
|
+
i32 i = 0, j = 0, k = 0;
|
|
239
|
+
for(i = 0; i < 100000; i++) // Perform "startup" work
|
|
240
|
+
j += i;
|
|
241
|
+
k += j;
|
|
242
|
+
printf("k = %d\n", k); // So compiler won't optimize out testing
|
|
243
|
+
cpu_mark_check(mark1, 0); // If it's been more than 0 msec print mark time
|
|
244
|
+
|
|
245
|
+
cpu_mark_reset(mark1); // Reset counter to start "main" work
|
|
246
|
+
// Could also just create new if we wanted "startup" to keep going
|
|
247
|
+
// i32 mark2 = cpu_mark_new("Main Working");
|
|
248
|
+
|
|
249
|
+
cpu_mark_new_description(mark1, "Main Working");
|
|
250
|
+
j = 0;
|
|
251
|
+
for(i = 0; i < 100000; i++) // Perform "main" work
|
|
252
|
+
j += i;
|
|
253
|
+
k += j;
|
|
254
|
+
printf("k = %d\n", k);
|
|
255
|
+
cpu_mark_check(mark1, 0); // If it's been more than 0 msec print mark time
|
|
256
|
+
|
|
257
|
+
cpu_mark_new_description(mark1, "Main Still Working");
|
|
258
|
+
j = 0;
|
|
259
|
+
for(i = 0; i < 100000; i++) // Perform more "main" work
|
|
260
|
+
j += i;
|
|
261
|
+
k += j;
|
|
262
|
+
printf("k = %d\n", k);
|
|
263
|
+
cpu_mark_check(mark1, 0); // If it's been more than 0 msec print mark time
|
|
264
|
+
|
|
265
|
+
cpu_mark_remove(mark1);
|
|
266
|
+
|
|
267
|
+
*/
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/*
|
|
2
|
+
defines.h -- Basic C preprocessor macro definitions
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
#ifndef DEFINES_H_Minaswan /* Header guard to prevent multiple inclusions */
|
|
6
|
+
#define DEFINES_H_Minaswan
|
|
7
|
+
#define DEFINES_VERSION "0.1.0"
|
|
8
|
+
#ifndef NULL
|
|
9
|
+
#define NULL ((void *) 0)
|
|
10
|
+
#endif
|
|
11
|
+
#ifndef FALSE
|
|
12
|
+
#define FALSE 0
|
|
13
|
+
#endif
|
|
14
|
+
#ifndef TRUE
|
|
15
|
+
#define TRUE 1
|
|
16
|
+
#endif
|
|
17
|
+
#ifndef NO
|
|
18
|
+
#define NO 0
|
|
19
|
+
#endif
|
|
20
|
+
#ifndef YES
|
|
21
|
+
#define YES 1
|
|
22
|
+
#endif
|
|
23
|
+
#ifndef OFF
|
|
24
|
+
#define OFF 0
|
|
25
|
+
#endif
|
|
26
|
+
#ifndef ON
|
|
27
|
+
#define ON 1
|
|
28
|
+
#endif
|
|
29
|
+
#endif /* DEFINES_H_Minaswan */
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/*
|
|
2
|
+
|
|
3
|
+
fast_read_file.h -- *Full Library* Header File
|
|
4
|
+
|
|
5
|
+
For reading the entire contents of a file into 64-bit aligned memory.
|
|
6
|
+
|
|
7
|
+
See the end of this file for examples.
|
|
8
|
+
|
|
9
|
+
Public Functions: ** DON'T FORGET TO FREE YOUR MEMORY AFTER USE **
|
|
10
|
+
|
|
11
|
+
c8 *fast_read_all_file_mapped(FILE *file, u64 *total_bytes); ** READ ONLY **
|
|
12
|
+
void free_all_mapped_read_memory(c8 *data, u64 *total_bytes);
|
|
13
|
+
|
|
14
|
+
c8 *fast_read_all_file_aligned(FILE *file, u64 *total_bytes); ** READ/WRITE **
|
|
15
|
+
void free_all_aligned_read_memory(c8 *data);
|
|
16
|
+
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
#ifndef FAST_READ_FILE_H_Minaswan /* Prevent multiple inclusions */
|
|
20
|
+
#define FAST_READ_FILE_H_Minaswan
|
|
21
|
+
#define FAST_READ_FILE_VERSION "0.1.0"
|
|
22
|
+
#ifndef TYPEDEFS_H_Minaswan /* Header guard for typedefs.h */
|
|
23
|
+
#define TYPEDEFS_H_Minaswan
|
|
24
|
+
#include <stdint.h>
|
|
25
|
+
typedef uint8_t b8; /* Booleans */
|
|
26
|
+
typedef uint16_t b16;
|
|
27
|
+
typedef uint32_t b32;
|
|
28
|
+
typedef uint64_t b64;
|
|
29
|
+
|
|
30
|
+
typedef char c8; /* Characters */
|
|
31
|
+
typedef unsigned char uc8;
|
|
32
|
+
|
|
33
|
+
typedef uint8_t u8; /* Unsigned Numbers */
|
|
34
|
+
typedef uint16_t u16;
|
|
35
|
+
typedef uint32_t u32;
|
|
36
|
+
typedef uint64_t u64;
|
|
37
|
+
|
|
38
|
+
typedef int8_t i8; /* Signed Numbers */
|
|
39
|
+
typedef int16_t i16;
|
|
40
|
+
typedef int32_t i32;
|
|
41
|
+
typedef int64_t i64;
|
|
42
|
+
|
|
43
|
+
typedef float f32; /* Floating Point Numbers */
|
|
44
|
+
typedef double f64;
|
|
45
|
+
#endif /* TYPEDEFS_H_Minaswan */
|
|
46
|
+
|
|
47
|
+
#include <stdio.h>
|
|
48
|
+
|
|
49
|
+
/* Public Functions */
|
|
50
|
+
void free_all_mapped_read_memory(c8 *data, u64 *total_bytes);
|
|
51
|
+
void free_all_aligned_read_memory(c8 *data);
|
|
52
|
+
c8 *fast_read_all_file_mapped(FILE *file, u64 *total_bytes);
|
|
53
|
+
c8 *fast_read_all_file_aligned(FILE *file, u64 *total_bytes);
|
|
54
|
+
|
|
55
|
+
#ifdef FAST_READ_FILE_IMPLEMENTATION /* Effectively read_all_file.c */
|
|
56
|
+
#include <stdlib.h>
|
|
57
|
+
#include <string.h>
|
|
58
|
+
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
|
|
59
|
+
#include <windows.h>
|
|
60
|
+
#else
|
|
61
|
+
#include <sys/mman.h>
|
|
62
|
+
#include <sys/stat.h>
|
|
63
|
+
#include <fcntl.h>
|
|
64
|
+
#include <unistd.h>
|
|
65
|
+
#endif
|
|
66
|
+
|
|
67
|
+
extern i32 fileno(FILE *stream);
|
|
68
|
+
|
|
69
|
+
#define FAST_READ_FILE_BUFFER_SIZE 4096
|
|
70
|
+
|
|
71
|
+
#ifndef FALSE
|
|
72
|
+
#define FALSE 0
|
|
73
|
+
#endif
|
|
74
|
+
|
|
75
|
+
#ifndef TRUE
|
|
76
|
+
#define TRUE 1
|
|
77
|
+
#endif
|
|
78
|
+
|
|
79
|
+
#ifndef NULL
|
|
80
|
+
#define NULL ((void *) 0)
|
|
81
|
+
#endif
|
|
82
|
+
|
|
83
|
+
#ifndef MIN
|
|
84
|
+
#define MIN(a, b) ((a) < (b) ? a : b)
|
|
85
|
+
#endif
|
|
86
|
+
|
|
87
|
+
void *fast_file_align64_malloc(size_t size)
|
|
88
|
+
{
|
|
89
|
+
void *result = NULL;
|
|
90
|
+
|
|
91
|
+
if(size == 0)
|
|
92
|
+
return(NULL);
|
|
93
|
+
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
|
|
94
|
+
result = _aligned_malloc(size, 64);
|
|
95
|
+
#else
|
|
96
|
+
#if(defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \
|
|
97
|
+
(defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
|
|
98
|
+
if(posix_memalign(&result, 64, size) != 0)
|
|
99
|
+
return(NULL);
|
|
100
|
+
#else
|
|
101
|
+
result = malloc(size);
|
|
102
|
+
#endif
|
|
103
|
+
#endif
|
|
104
|
+
return(result);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
void *fast_file_align64_calloc(size_t size)
|
|
108
|
+
{
|
|
109
|
+
void *result = fast_file_align64_malloc(size);
|
|
110
|
+
|
|
111
|
+
if(result)
|
|
112
|
+
memset(result, 0, size);
|
|
113
|
+
return(result);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
void *fast_file_align64_realloc(void *existing_memory, size_t old_size, size_t new_size)
|
|
117
|
+
{
|
|
118
|
+
void *new_memory = fast_file_align64_calloc(new_size);
|
|
119
|
+
|
|
120
|
+
if(new_memory && existing_memory) {
|
|
121
|
+
memcpy(new_memory, existing_memory, MIN(new_size, old_size));
|
|
122
|
+
free(existing_memory);
|
|
123
|
+
return(new_memory);
|
|
124
|
+
}
|
|
125
|
+
return(NULL);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
c8 *fast_read_all_file_aligned(FILE *file, u64 *total_bytes)
|
|
129
|
+
{
|
|
130
|
+
size_t bytes = 0, count = 0;
|
|
131
|
+
c8 buffer[FAST_READ_FILE_BUFFER_SIZE + 1];
|
|
132
|
+
c8 *data = NULL;
|
|
133
|
+
|
|
134
|
+
bytes = fread(buffer, sizeof(c8), FAST_READ_FILE_BUFFER_SIZE, file);
|
|
135
|
+
while(bytes) {
|
|
136
|
+
if(data) {
|
|
137
|
+
data = fast_file_align64_realloc(data, *total_bytes + 1, *total_bytes + bytes + 1);
|
|
138
|
+
count = 0;
|
|
139
|
+
while(count < bytes) {
|
|
140
|
+
*(data + *total_bytes + count) = buffer[count];
|
|
141
|
+
count++;
|
|
142
|
+
}
|
|
143
|
+
} else {
|
|
144
|
+
data = fast_file_align64_calloc(bytes + 1);
|
|
145
|
+
count = 0;
|
|
146
|
+
while(count < bytes) {
|
|
147
|
+
*(data + *total_bytes + count) = buffer[count];
|
|
148
|
+
count++;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
*total_bytes += (u32)bytes;
|
|
152
|
+
*(data + *total_bytes) = '\0';
|
|
153
|
+
bytes = fread(buffer, sizeof(c8), FAST_READ_FILE_BUFFER_SIZE, file);
|
|
154
|
+
}
|
|
155
|
+
return(data);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
c8 *fast_read_all_file_mapped(FILE *file, u64 *total_bytes)
|
|
159
|
+
{
|
|
160
|
+
c8 *data = NULL;
|
|
161
|
+
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
|
|
162
|
+
HANDLE windows_file_handle = {0};
|
|
163
|
+
HANDLE windows_map_handle = {0};
|
|
164
|
+
LARGE_INTEGER windows_file_size = {0};
|
|
165
|
+
#else
|
|
166
|
+
i32 file_descriptor = fileno(file);
|
|
167
|
+
struct stat st = {0};
|
|
168
|
+
#endif
|
|
169
|
+
|
|
170
|
+
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
|
|
171
|
+
windows_file_handle = (HANDLE)_get_osfhandle(fileno(file));
|
|
172
|
+
if(windows_file_handle == INVALID_HANDLE_VALUE)
|
|
173
|
+
return(NULL);
|
|
174
|
+
if(!GetFileSizeEx(windows_file_handle, &windows_file_size) ||
|
|
175
|
+
windows_file_size.QuadPart == 0) {
|
|
176
|
+
CloseHandle(windows_file_handle);
|
|
177
|
+
return(NULL);
|
|
178
|
+
}
|
|
179
|
+
*total_bytes = (u64)windows_file_size.QuadPart;
|
|
180
|
+
windows_map_handle = CreateFileMapping(windows_file_handle,
|
|
181
|
+
NULL, PAGE_READONLY, 0, 0, NULL);
|
|
182
|
+
data = MapViewOfFile(windows_map_handle, FILE_MAP_READ, 0, 0, 0);
|
|
183
|
+
CloseHandle(windows_file_handle);
|
|
184
|
+
CloseHandle(windows_map_handle);
|
|
185
|
+
#else
|
|
186
|
+
if(file_descriptor == -1)
|
|
187
|
+
return(NULL);
|
|
188
|
+
if(fstat(file_descriptor, &st) == -1 || st.st_size == 0) {
|
|
189
|
+
close(file_descriptor);
|
|
190
|
+
return(NULL);
|
|
191
|
+
}
|
|
192
|
+
*total_bytes = (u64)st.st_size;
|
|
193
|
+
data = mmap(NULL, *total_bytes, PROT_READ, MAP_PRIVATE,
|
|
194
|
+
file_descriptor, 0);
|
|
195
|
+
if(data == MAP_FAILED) {
|
|
196
|
+
close(file_descriptor);
|
|
197
|
+
return(NULL);
|
|
198
|
+
}
|
|
199
|
+
#ifdef POSIX_FADV_SEQUENTIAL
|
|
200
|
+
posix_fadvise(file_descriptor, 0, (i64)*total_bytes,
|
|
201
|
+
POSIX_FADV_SEQUENTIAL);
|
|
202
|
+
#endif
|
|
203
|
+
#ifdef MADV_SEQUENTIAL
|
|
204
|
+
madvise(data, *total_bytes, MADV_SEQUENTIAL);
|
|
205
|
+
#endif
|
|
206
|
+
close(file_descriptor);
|
|
207
|
+
#endif
|
|
208
|
+
return(data);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
void free_all_mapped_read_memory(c8 *data, u64 *total_bytes)
|
|
212
|
+
{
|
|
213
|
+
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
|
|
214
|
+
UnmapViewOfFile(data);
|
|
215
|
+
#else
|
|
216
|
+
munmap(data, *total_bytes);
|
|
217
|
+
#endif
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
void free_all_aligned_read_memory(c8 *data)
|
|
221
|
+
{
|
|
222
|
+
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
|
|
223
|
+
_aligned_free(data);
|
|
224
|
+
#else
|
|
225
|
+
free(data);
|
|
226
|
+
#endif
|
|
227
|
+
}
|
|
228
|
+
#endif /* FAST_READ_FILE_IMPLEMENTATION */
|
|
229
|
+
#endif /* FAST_READ_FILE_H_Minaswan */
|
|
230
|
+
|
|
231
|
+
/*
|
|
232
|
+
|
|
233
|
+
Examples: ** DON'T FORGET TO FREE YOUR MEMORY AFTER USE **
|
|
234
|
+
|
|
235
|
+
#define FAST_READ_FILE_IMPLEMENTATION
|
|
236
|
+
#include "fast_read_file.h"
|
|
237
|
+
|
|
238
|
+
// Read everything from file example:
|
|
239
|
+
|
|
240
|
+
FILE *file = fopen(argv[1], "rb");
|
|
241
|
+
u64 total_bytes = 0;
|
|
242
|
+
c8 *data = fast_read_all_file_mapped(file, &total_bytes);
|
|
243
|
+
use(string);
|
|
244
|
+
free_all_mapped_read_memory(string); // Note: Doesn't use malloc/free
|
|
245
|
+
|
|
246
|
+
// Read everything from stdin / non-kernal mapping example:
|
|
247
|
+
|
|
248
|
+
u64 total_bytes = 0;
|
|
249
|
+
c8 *data = fast_read_all_file_aligned(stdin, &total_bytes);
|
|
250
|
+
use(string);
|
|
251
|
+
free_all_aligned_read_memory(string); // Note: Might not use malloc/free
|
|
252
|
+
|
|
253
|
+
FILE *file = fopen(argv[1], "rb");
|
|
254
|
+
u64 total_bytes = 0;
|
|
255
|
+
c8 *data = fast_read_all_file_aligned(file, &total_bytes);
|
|
256
|
+
use(string);
|
|
257
|
+
free_all_aligned_read_memory(string); // Note: Might not use malloc/free
|
|
258
|
+
|
|
259
|
+
*/
|