-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscary.c
148 lines (131 loc) · 3.23 KB
/
scary.c
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
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "scary.h"
#define NONNULL(p) do { \
errno = 0; \
void *q = (p); \
if (q == NULL) { \
perror("scary"); \
abort(); \
} \
} while (0)
static inline void *xmalloc(size_t size)
{
void *p;
NONNULL(p = malloc(size));
return p;
}
static inline void *xrealloc(void *p0, size_t size)
{
void *p;
NONNULL(p = realloc(p0, size));
return p;
}
typedef struct {
size_t capacity, length, elem_size;
uint8_t space[];
} Scary;
enum {
SCARY_OFFSET = offsetof(Scary, space),
SCARY_INIT = 8,
SCARY_INC_RATIO = 2,
};
static inline void *opaque(Scary *a)
{
return a->space;
}
static inline Scary *get(const void *p)
{
const uint8_t *bp = p;
return (Scary *) (bp - SCARY_OFFSET);
}
void *scary_new(size_t elem_size)
{
size_t cap = elem_size * SCARY_INIT;
Scary *ary = xmalloc(sizeof(Scary) + cap);
ary->capacity = cap;
ary->length = 0;
ary->elem_size = elem_size;
return opaque(ary);
}
void scary_free(void *p)
{
if (p != NULL)
free(get(p));
}
static Scary *maybe_resize(void *p)
{
const void **pp = p;
Scary *ary = get(*pp);
if (ary->capacity <= ary->length * ary->elem_size) {
ary->capacity *= SCARY_INC_RATIO;
ary = xrealloc(ary, sizeof(Scary) + ary->capacity);
*pp = opaque(ary);
}
return ary;
}
size_t scary_length(const void *p)
{
return get(p)->length;
}
#define DEF_PUSH_VARIANT2(type, suffix) \
void scary_push_##suffix(type **p, type elem) \
{ \
Scary *ary = maybe_resize(p); \
type *sp = (type *) ary->space; \
sp[ary->length++] = elem; \
}
#define DEF_PUSH_VARIANT_T(type) DEF_PUSH_VARIANT2(type##_t, type)
#define DEF_PUSH_VARIANT1(type) DEF_PUSH_VARIANT2(type, type)
DEF_PUSH_VARIANT_T(int8)
DEF_PUSH_VARIANT_T(int16)
DEF_PUSH_VARIANT_T(int32)
DEF_PUSH_VARIANT_T(int64)
DEF_PUSH_VARIANT_T(uint8)
DEF_PUSH_VARIANT_T(uint16)
DEF_PUSH_VARIANT_T(uint32)
DEF_PUSH_VARIANT_T(uint64)
DEF_PUSH_VARIANT1(char)
static void scary_push_ptr(void *p, const void *elem)
{
Scary *ary = maybe_resize(p);
const void **sp = (const void **) ary->space;
sp[ary->length++] = elem;
}
#define DEF_PUSH_VARIANT_PTR(type, suffix) \
void scary_push_##suffix##p(type ***p, const type *elem) \
{ \
scary_push_ptr(p, elem); \
}
#define DEF_PUSH_VARIANT_T_PTR(type) DEF_PUSH_VARIANT_PTR(type##_t, type)
#define DEF_PUSH_VARIANT1_PTR(type) DEF_PUSH_VARIANT_PTR(type, type)
DEF_PUSH_VARIANT_T_PTR(int8)
DEF_PUSH_VARIANT_T_PTR(int16)
DEF_PUSH_VARIANT_T_PTR(int32)
DEF_PUSH_VARIANT_T_PTR(int64)
DEF_PUSH_VARIANT_T_PTR(uint8)
DEF_PUSH_VARIANT_T_PTR(uint16)
DEF_PUSH_VARIANT_T_PTR(uint32)
DEF_PUSH_VARIANT_T_PTR(uint64)
DEF_PUSH_VARIANT1_PTR(char)
DEF_PUSH_VARIANT1_PTR(void)
void scary_push_ccharp(const char ***p, const char *elem)
{
scary_push_ptr(p, elem);
}
void scary_pop(void *p)
{
get(p)->length--; // do not shrink for speed
}
void *scary_dup(void *p)
{
Scary *ary = get(p);
Scary *dup = xmalloc(sizeof(Scary) + ary->capacity);
*dup = *ary;
memcpy(dup->space, ary->space, ary->capacity);
return opaque(dup);
}