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
/*
* bstack — persistent, fsync-durable binary stack backed by a single file.
*
* File format (16-byte header followed by payload):
* [0..8) magic: "BSTK" + major(1) + minor(1) + patch(2) + reserved(1)
* [8..16) committed payload length, little-endian uint64
* [16..) payload bytes
*
* All logical offsets are 0-based from the start of the payload region.
*
* Error handling
* --------------
* bstack_open returns NULL on failure; errno is set by the failing syscall,
* or to EINVAL for bad/short headers, or to EWOULDBLOCK when
* another process holds the exclusive lock.
* All other functions return 0 on success, -1 on failure with errno set.
*
* Thread safety
* -------------
* On Unix a pthread_rwlock protects each handle; on Windows an SRWLOCK is
* used. bstack_push / bstack_extend / bstack_pop / bstack_discard /
* bstack_set / bstack_zero hold a write lock; bstack_peek / bstack_get /
* bstack_len hold a read lock and may run concurrently with each other on
* both platforms.
*
* Multi-process safety
* --------------------
* bstack_open acquires an exclusive advisory lock on the file:
* Unix — flock(LOCK_EX|LOCK_NB)
* Windows — LockFileEx(LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY)
* The lock is released when bstack_close is called (fd / HANDLE is closed).
*
* Feature flags
* -------------
* Compile with -DBSTACK_FEATURE_SET to enable bstack_set and bstack_zero.
*/
typedef struct bstack bstack_t;
extern "C" BSTACK_FEATURE_SET
/*
* Overwrite len bytes in place starting at logical offset.
* The file size is never changed. An empty slice is a valid no-op.
* Returns EINVAL if offset + len would exceed the payload size or overflow
* uint64_t.
*
* Only available when compiled with -DBSTACK_FEATURE_SET.
*/
int ;
/*
* Overwrite n bytes with zeros in place starting at logical offset.
* The file size is never changed. n = 0 is a valid no-op.
* Returns EINVAL if offset + n would exceed the payload size or overflow
* uint64_t.
*
* Only available when compiled with -DBSTACK_FEATURE_SET.
*/
int ;
/* BSTACK_FEATURE_SET */
}
/* BSTACK_H */