summaryrefslogtreecommitdiff
path: root/src/packet_storage.c
blob: 1094afdbc40d7784f9979fd06153f8e6bcf86318 (plain) (blame)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <pthread.h>
#include <stdint.h>
#include <stdlib.h>
// dependency: zlib
#include <zlib.h>
//
#include "common.c"

const uint64_t max_storage = 1 << 16;

#define hash_fmt                                                                                   \
    "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"                             \
    "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"

#define hash_ref(_hash)                                                                            \
    _hash[ 0 ], _hash[ 1 ], _hash[ 2 ], _hash[ 3 ], _hash[ 4 ], _hash[ 5 ], _hash[ 6 ],            \
        _hash[ 7 ], _hash[ 8 ], _hash[ 9 ], _hash[ 10 ], _hash[ 11 ], _hash[ 12 ], _hash[ 13 ],    \
        _hash[ 14 ], _hash[ 15 ], _hash[ 16 ], _hash[ 17 ], _hash[ 18 ], _hash[ 19 ], _hash[ 20 ], \
        _hash[ 21 ], _hash[ 22 ], _hash[ 23 ], _hash[ 24 ], _hash[ 25 ], _hash[ 26 ], _hash[ 27 ], \
        _hash[ 28 ], _hash[ 29 ], _hash[ 30 ], _hash[ 31 ]

uint64_t current_packet_ptr = 0;
uint64_t packet_count       = 0;
struct data_packet_entry {
    struct data_packet packet;
    uint64_t           next;
    uint64_t           prev;
    bool               is_full;
}* packet_storage;

// TODO: yes, i know it's weird, but this has to be zero
#define minus_one ((uint64_t) 0)

struct data_packet* load_packet(FILE* file) {
    if (!file) { return NULL; }

    fseek(file, 0, SEEK_END);
    size_t file_size = ftell(file);
    fseek(file, 0, SEEK_SET);

    if (file_size == 0) {
        fclose(file);
        return NULL;
    }

    byte comp_buf[ file_size ];

    size_t r = fread(comp_buf, 1, file_size, file);
    fclose(file);
    if (r != file_size) { return NULL; }

    byte buffer[ PACKET_SIZE ];

    uLongf dest_len = (uLongf) PACKET_SIZE;
    int    unres    = uncompress(buffer, &dest_len, comp_buf, (uLongf) file_size);

    if (unres != Z_OK || dest_len != (uLongf) PACKET_SIZE) { return NULL; }

    bool                success;
    struct data_packet* packet = malloc(sizeof(struct data_packet));
    if (!packet) { return NULL; }
    *packet = s2data_packet(PACKET_SIZE, buffer, &success);

    if (!success) {
        free(packet);
        return NULL;
    }

    return packet;
}

uint64_t set_data_packet(struct data_packet* new_packet);

struct data_packet_entry* get_data_packet(const hash _hash) {
    uint64_t hash_val;
    memcpy(&hash_val, _hash, sizeof(uint64_t));
    uint64_t cur_index = hash_val % max_storage;

    pthread_mutex_lock(&packet_storage_mutex);

    struct data_packet_entry* current_entry = &packet_storage[ cur_index ];
    while (cur_index != minus_one) {
        if (current_entry->is_full
            && memcmp(current_entry->packet.hash, _hash, sizeof(hash)) == 0) {
            pthread_mutex_unlock(&packet_storage_mutex);
            return current_entry;
        }

        cur_index = current_entry->next;
        if (cur_index != minus_one) { current_entry = &packet_storage[ cur_index ]; }
    }

    pthread_mutex_unlock(&packet_storage_mutex);
    char filename[ 128 ];
    sprintf(filename, "packets/" hash_fmt ".bin", hash_ref(_hash));
    FILE* file = fopen(filename, "rb");

    struct data_packet* packet = load_packet(file);

    if (packet) {
        uint64_t add_idx = set_data_packet(packet);
        if (add_idx != minus_one) {
            free(packet);
            return &packet_storage[ add_idx ];
        } else {
            static __thread struct data_packet_entry temp_entry;
            temp_entry.packet = *packet;
            free(packet);
            return &temp_entry;
        }
    }

    return NULL;
}

uint64_t set_data_packet(struct data_packet* new_packet) {
    pthread_mutex_lock(&packet_storage_mutex);

    uint64_t hash_val;
    memcpy(&hash_val, new_packet->hash, sizeof(uint64_t));
    uint64_t start_index = hash_val % max_storage;
    uint64_t cur_index   = start_index;

    struct data_packet_entry* current_entry = &packet_storage[ cur_index ];

    while (true) {
        if (current_entry->is_full
            && memcmp(current_entry->packet.hash, new_packet->hash, sizeof(hash)) == 0) {
            memcpy(&current_entry->packet, new_packet, sizeof(struct data_packet));
            pthread_mutex_unlock(&packet_storage_mutex);
            return cur_index;
        }

        if (current_entry->next == minus_one) break;
        cur_index     = current_entry->next;
        current_entry = &packet_storage[ cur_index ];
    }

    if (!current_entry->is_full) {
        memcpy(&current_entry->packet, new_packet, sizeof(struct data_packet));
        current_entry->is_full = true;
        current_entry->next    = minus_one;
        current_entry->prev    = minus_one;
        packet_count++;
        pthread_mutex_unlock(&packet_storage_mutex);
        return cur_index;
    }

    uint64_t probes             = 0;
    uint64_t linear_probe_index = (cur_index + 1) % max_storage;

    while (probes < max_storage) {
        struct data_packet_entry* empty_slot = &packet_storage[ linear_probe_index ];

        if (!empty_slot->is_full) {
            memcpy(&empty_slot->packet, new_packet, sizeof(struct data_packet));
            empty_slot->is_full = true;
            empty_slot->next    = minus_one;
            empty_slot->prev    = cur_index;
            current_entry->next = linear_probe_index;
            packet_count++;
            pthread_mutex_unlock(&packet_storage_mutex);
            return linear_probe_index;
        }

        linear_probe_index = (linear_probe_index + 1) % max_storage;
        probes++;
    }

    // table full
    pthread_mutex_unlock(&packet_storage_mutex);
    return minus_one;
}

#define overload __attribute__((overloadable))

bool _delete_at_index(uint64_t index);

overload void delete_data_packet(hash _hash) {
    pthread_mutex_lock(&packet_storage_mutex);

    uint64_t hash_val;
    memcpy(&hash_val, _hash, sizeof(uint64_t));
    uint64_t start_index = hash_val % max_storage;
    uint64_t cur_index   = start_index;

    while (cur_index != minus_one) {
        struct data_packet_entry* current_entry = &packet_storage[ cur_index ];
        if (current_entry->is_full
            && memcmp(current_entry->packet.hash, _hash, sizeof(hash)) == 0) {
            _delete_at_index(cur_index);
            pthread_mutex_unlock(&packet_storage_mutex);
            return;
        }

        if (current_entry->next == minus_one) {
            pthread_mutex_unlock(&packet_storage_mutex);
            return;
        }
        cur_index = current_entry->next;
    }

    pthread_mutex_unlock(&packet_storage_mutex);
}

overload void delete_data_packet(uint64_t index) {
    pthread_mutex_lock(&packet_storage_mutex);
    _delete_at_index(index);
    pthread_mutex_unlock(&packet_storage_mutex);
}

// caller must already have a lock!
bool _delete_at_index(uint64_t index) {
    if (index >= max_storage) return false;
    struct data_packet_entry* current_entry = &packet_storage[ index ];
    if (!current_entry->is_full) return false;

    if (current_entry->prev != minus_one) {
        struct data_packet_entry* prev_entry = &packet_storage[ current_entry->prev ];
        prev_entry->next                     = current_entry->next;
    }

    if (current_entry->next != minus_one) {
        struct data_packet_entry* next_entry = &packet_storage[ current_entry->next ];
        next_entry->prev                     = current_entry->prev;
    }

    current_entry->is_full = false;
    current_entry->next    = minus_one;
    current_entry->prev    = minus_one;

    return true;
}