Subastra
Loading...
Searching...
No Matches
misc.h
Go to the documentation of this file.
1#ifndef __H__MISC__
2#define __H__MISC__
3
4#include "defs.h"
5#include "memory.h"
6
9static void *read_binary_file(allocator_t allocator, const char *filename,
10 sz *size) {
11 ASSERT__(filename);
12 ASSERT__(size);
13
14 // TODO: do error handling
15 FILE *file = fopen(filename, "rb");
16 fseek(file, 0, SEEK_END);
17
18 *size = ftell(file);
19 rewind(file);
20
21 byte *buffer = allocator_alloc_ty(byte, allocator, *size);
22 fread(buffer, sizeof(byte), *size, file);
23 fclose(file);
24
25 return buffer;
26}
27
28static char *read_text_file(allocator_t allocator, const char *filename) {
29 ASSERT__(filename);
30
31 // TODO: do error handling
32 FILE *file = fopen(filename, "r");
33 fseek(file, 0, SEEK_END);
34
35 sz size = ftell(file);
36 rewind(file);
37
38 char *buffer = allocator_alloc_ty(char, allocator, size + 1);
39 fread(buffer, sizeof(char), size, file);
40 fclose(file);
41 buffer[size] = '\0';
42
43 return buffer;
44}
45
46static u64 hash_combine_u64(u64 seed, u64 value) {
47 return seed ^ (value + 0x9e3779b97f4a7c15 + (seed << 6) + (seed >> 2));
48}
49
50// Commutative combinational hash
52 // Combine the hashes
53 u64 o = hash_combine_u64(a, b) ^ hash_combine_u64(b, a);
54 // Mix up the bits
55 o ^= 0x9e3779b97f4a7c15 + (a << 6) + (a >> 2);
56 // Implemented using only commutative operations, so should be
57 // commutative
58 return o;
59}
60
61#endif
uint64_t u64
Definition defs.h:46
#define ASSERT__(expr)
Definition defs.h:21
size_t sz
Definition defs.h:51
static u64 hash_combine_com_u64(u64 a, u64 b)
Definition misc.h:51
static u64 hash_combine_u64(u64 seed, u64 value)
Definition misc.h:46
static void * read_binary_file(allocator_t allocator, const char *filename, sz *size)
Read a binary file file of into memory.
Definition misc.h:9
static char * read_text_file(allocator_t allocator, const char *filename)
Definition misc.h:28
A generic allocator type passed by value. Contains a fallback allocator and a set of function pointer...
Definition memory.h:30