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
|
#pragma once
// Common datastructures (arrays, tables, closures)
#include <gmp.h>
#include <stdint.h>
#include <stdbool.h>
#include <pthread.h>
#define ARRAY_LENGTH_BITS 42
#define ARRAY_FREE_BITS 6
#define ARRAY_REFCOUNT_BITS 3
#define ARRAY_STRIDE_BITS 12
#define MAX_FOR_N_BITS(N) ((1<<(N))-1)
#define ARRAY_MAX_STRIDE MAX_FOR_N_BITS(ARRAY_STRIDE_BITS-1)
#define ARRAY_MIN_STRIDE (~MAX_FOR_N_BITS(ARRAY_STRIDE_BITS-1))
#define ARRAY_MAX_DATA_REFCOUNT MAX_FOR_N_BITS(ARRAY_REFCOUNT_BITS)
#define ARRAY_MAX_FREE_ENTRIES MAX_FOR_N_BITS(ARRAY_FREE_BITS)
typedef union {
int64_t small;
mpz_t *big;
} Int_t;
typedef struct {
void *data;
// All of the following fields add up to 64 bits, which means that array
// structs can be passed in two 64-bit registers. C will handle doing the
// bit arithmetic to extract the necessary values, which is cheaper than
// spilling onto the stack and needing to retrieve data from the stack.
int64_t length:ARRAY_LENGTH_BITS;
uint8_t free:ARRAY_FREE_BITS;
bool atomic:1;
uint8_t data_refcount:ARRAY_REFCOUNT_BITS;
int16_t stride:ARRAY_STRIDE_BITS;
} array_t;
typedef struct {
uint32_t occupied:1, index:31;
uint32_t next_bucket;
} bucket_t;
#define TABLE_MAX_BUCKETS 0x7fffffff
#define TABLE_MAX_DATA_REFCOUNT 3
typedef struct {
uint32_t count:31, last_free:31;
uint8_t data_refcount:2;
bucket_t buckets[0];
} bucket_info_t;
typedef struct table_s {
array_t entries;
bucket_info_t *bucket_info;
struct table_s *fallback;
} table_t;
typedef struct {
void *fn, *userdata;
} closure_t;
typedef struct Range_s {
Int_t first, last, step;
} Range_t;
typedef struct {
array_t items;
pthread_mutex_t mutex;
pthread_cond_t cond;
int64_t max_size;
} channel_t;
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0
|