summaryrefslogtreecommitdiff
path: root/include/tinyff/common.h
blob: 678bda9b3ae5c1f61a13159d75e22664371e6e95 (plain)
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
67
68
69
70
71
72
73
// Common header for tinyff library
// Not recommended to be included directly
#ifndef TINYFF_COMMON_H
#define TINYFF_COMMON_H

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <tinyff/stream.h>

// Flags
typedef bool ff_flag;

#define FF_ENABLE   true
#define FF_DISABLE  false

// Functions

static inline uint32_t get_big_endian(const uint8_t *buffer) {
    return (uint32_t)(buffer[0] << 24) |
           (uint32_t)(buffer[1] << 16) |
           (uint32_t)(buffer[2] << 8)  |
           (uint32_t)(buffer[3]);
}

static inline uint32_t get_little_endian(const uint8_t *buffer)
{
    return (uint32_t)(buffer[0])       |
           (uint32_t)(buffer[1] << 8)  |
           (uint32_t)(buffer[2] << 16) |
           (uint32_t)(buffer[3] << 24);
}


// Small stdlib helper functions
static inline size_t ff_strlen(const char *str)
{
    size_t len = 0;
    while (str[len] != '\0') {
        len++;
    }
    return len;
}


// Context bases
// Default

typedef struct {
    void* (*ff_alloc)(size_t size);
    void  (*ff_free)(void* ptr);
    void* (*ff_calloc)(size_t count, size_t size);
} ff_allocator;

typedef struct {
    // Debug settings
    ff_stream* ff_debug_stream;
    ff_flag ff_debug_enabled;
    
    // Allocation
    ff_allocator allocator;
} ff_ctx;

ff_ctx* ff_init(ff_allocator* allocator);
void ff_cleanup(ff_ctx* ctx);


// Small math functions
// TODO: Implement a full math library for tinyff

#define FF_ABS(a) (((a) < 0) ? -(a) : (a))

#endif