-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.h
66 lines (57 loc) · 1.42 KB
/
types.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef TYPES_H
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
struct data
{
u64 length;
u8 bytes[1]; // NOTE: this is supposed to be longer than 1 byte!
};
inline
i64 ceilingDivide(i64 numerator, i64 denominator)
{
return ((numerator - 1)/denominator + 1);
}
data* allocateData(u64 length, bool clear = false)
{
u64 longAlignedLength = ceilingDivide(length, 8) * 8;
data* result;
if(clear)
{
result = (data*)calloc(1, sizeof(u64) + longAlignedLength);
}
else
{
result = (data*)malloc(sizeof(u64) + longAlignedLength);
}
result->length = length;
return result;
}
data* reallocateData(data* oldData, u64 newLength, bool clear = false)
{
u64 chunkCount = ceilingDivide(newLength, 8);
u64 newLongAlignedLength = chunkCount * 8;
u64 oldLength = oldData->length;
data* result = (data*)realloc(oldData, sizeof(u64) + newLongAlignedLength);
result->length = newLength;
if(clear && (newLength > oldLength))
{
u64* byteChunks = (u64*) &result->bytes;
for(u64 chunkIndex = ceilingDivide(oldLength, 8);
chunkIndex < chunkCount;
++chunkIndex)
{
*(byteChunks + chunkIndex) = 0;
}
}
return result;
}
#define TYPES_H
#endif