pbwire 0.1.17 → 0.1.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 81adb9cce0acdf3f9df2e113d89603772ced5b3405fc7ff3b6838dbe70013717
4
- data.tar.gz: 741ef3a24b09352600fbe128bfe5f235aa5d85f37aa4fcab12d44974c562af1d
3
+ metadata.gz: 8781f7d495650c1f5ce775c999f09e5a630254ce1f5b59ef0c0a51d221764124
4
+ data.tar.gz: 371d44ed08a68b18d2bf4f42486a04b5433f9fb566ed44119261fe5e4e1e1af4
5
5
  SHA512:
6
- metadata.gz: 0f80f2bdb700896d8820e4830879b3130ef285917d24a69fc81262e577814f3d6ecfe3904c40e23471338762577a05e2600885547f7b7d384e9cf7332cc2ff4a
7
- data.tar.gz: e139839819804d3cc2dd7bc0998a949704074a936938c8a186a91acbfb18e683f529f55af811e4627ab957ff063f7e285a52177f26515e2888ecd57346f34a1f
6
+ metadata.gz: 68811e3f5b8d69260f00999276eb1e56cd67863a20e10cce6c3232b56791ef7cce04a075ac1ab11e0a0cbc10ad970397ac8a2c36e873fd63fa3ba60bb17f4deb
7
+ data.tar.gz: fa1022f7047cb609c6b2744abc1a58ec192f2fb03580cd2290ecbf9df05bce853d8eb5aa3fcc04e935b83df39db44a1eba8fa41f054ab31a19b343fd79abcd86
@@ -0,0 +1,322 @@
1
+ /* Bump allocator backing pbw_ctx: allocation is a pointer add, releasing is a
2
+ * pointer reset. Chunks double in size so a steady decode workload stops
3
+ * calling malloc entirely after the first few messages. */
4
+
5
+ #include "internal.h"
6
+
7
+ #define PBW_ARENA_MIN_CHUNK 4096u
8
+
9
+ static size_t
10
+ align_up(size_t size)
11
+ {
12
+ /* 8 bytes keeps every struct in this library naturally aligned. */
13
+ return (size + 7u) & ~(size_t)7u;
14
+ }
15
+
16
+ static int
17
+ chunk_add(pbw_arena *arena, size_t need)
18
+ {
19
+ size_t cap;
20
+ pbw_arena_chunk *chunk;
21
+
22
+ if (arena == NULL || arena->no_grow) {
23
+ return 0;
24
+ }
25
+ cap = arena->chunk_size;
26
+ if (cap < need) {
27
+ cap = need;
28
+ }
29
+ if (cap > SIZE_MAX - PBW_CHUNK_HDR) {
30
+ return 0;
31
+ }
32
+ chunk = (pbw_arena_chunk *)malloc(PBW_CHUNK_HDR + cap);
33
+ if (chunk == NULL) {
34
+ return 0;
35
+ }
36
+ chunk->next = arena->head;
37
+ chunk->used = 0;
38
+ chunk->cap = cap;
39
+ arena->head = chunk;
40
+ /* Grow the next chunk so a workload that needs more keeps its balance: the
41
+ * chunk count stays small while per-call allocation stays O(1). */
42
+ arena->chunk_size = cap < (size_t)1 << 20 ? cap * 2 : cap;
43
+ return 1;
44
+ }
45
+
46
+ void *
47
+ pbw_arena_alloc_slow(pbw_arena *arena, size_t size)
48
+ {
49
+ pbw_arena_chunk *chunk;
50
+ void *ptr;
51
+
52
+ size_t large_at;
53
+
54
+ if (arena == NULL) {
55
+ return NULL;
56
+ }
57
+ large_at = arena->retain_cap > PBW_ARENA_LARGE_THRESHOLD
58
+ ? arena->retain_cap
59
+ : PBW_ARENA_LARGE_THRESHOLD;
60
+
61
+ if (size > SIZE_MAX - 7u) {
62
+ return NULL;
63
+ }
64
+ size = align_up(size == 0 ? 1 : size);
65
+ if (arena->no_grow) {
66
+ /* Caller memory: the head chunk is the whole capacity, and the fast
67
+ * path already handled every allocation that fits, so reaching here is
68
+ * the caller's NOMEM. */
69
+ return NULL;
70
+ }
71
+ if (size > large_at) {
72
+ /* Oversized single allocation: one malloc on the large list, freed on
73
+ * reset/free instead of pinning the reusable head chunk. */
74
+ pbw_arena_chunk *big;
75
+
76
+ /* new_packed guards the same addition; sizes here can approach
77
+ * SIZE_MAX - 7 through the public bytes constructors. */
78
+ if (size > SIZE_MAX - PBW_CHUNK_HDR) {
79
+ return NULL;
80
+ }
81
+ big = (pbw_arena_chunk *)malloc(PBW_CHUNK_HDR + size);
82
+
83
+ if (big == NULL) {
84
+ return NULL;
85
+ }
86
+ big->next = arena->large;
87
+ big->used = size;
88
+ big->cap = size;
89
+ arena->large = big;
90
+ return (char *)big + PBW_CHUNK_HDR;
91
+ }
92
+ chunk = arena->head;
93
+ if (chunk == NULL || !pbw_cap_holds(chunk->cap, chunk->used) ||
94
+ chunk->cap - chunk->used < size) {
95
+ if (!chunk_add(arena, size)) {
96
+ return NULL;
97
+ }
98
+ chunk = arena->head;
99
+ }
100
+ ptr = (char *)chunk + PBW_CHUNK_HDR + chunk->used;
101
+ chunk->used += size;
102
+ return ptr;
103
+ }
104
+
105
+ void
106
+ pbw_arena_reset(pbw_arena *arena)
107
+ {
108
+ /* Chunks are pushed at the head and grow, so the head is the largest one:
109
+ * keep it and drop the rest. A workload that needs the same amount of space
110
+ * every time then stops calling malloc entirely. */
111
+ pbw_arena_chunk *head;
112
+ pbw_arena_chunk *chunk;
113
+
114
+ if (arena == NULL) {
115
+ return;
116
+ }
117
+ head = arena->head;
118
+
119
+ if (arena->no_grow) {
120
+ /* Caller memory: nothing can be given back, so a reset is a pointer
121
+ * move and the block stays exactly where the caller put it. */
122
+ arena->readonly = 0;
123
+ arena->has_heap = 0;
124
+ if (head != NULL) {
125
+ head->used = 0;
126
+ }
127
+ return;
128
+ }
129
+ if (arena->block != NULL) {
130
+ /* A packed arena belongs to one decoded tree and is never reset; it
131
+ * is released as a whole by pbw_message_free(). */
132
+ arena->readonly = 0;
133
+ return;
134
+ }
135
+ /* A reset context is a blank slate: trees built into it afterwards are
136
+ * mutable until a decode seals it read-only again. */
137
+ arena->readonly = 0;
138
+ arena->has_heap = 0;
139
+ /* Large single allocations never become resident. Free them even when no
140
+ * head chunk exists: a context that only ever held oversized payloads
141
+ * (pbw_value_new_bytes_ctx above the threshold, no small allocation)
142
+ * would otherwise pin every one of them until pbw_ctx_free(). */
143
+ for (chunk = arena->large; chunk != NULL;) {
144
+ pbw_arena_chunk *next = chunk->next;
145
+
146
+ free(chunk);
147
+ chunk = next;
148
+ }
149
+ arena->large = NULL;
150
+ if (head == NULL) {
151
+ arena->chunk_size = PBW_ARENA_MIN_CHUNK;
152
+ return;
153
+ }
154
+ /* An outlier head past the budget is dropped: steady workloads under
155
+ * budget stay resident, anything above pays one malloc per reset instead
156
+ * of pinning the peak forever. */
157
+ while (head != NULL && head->cap > arena->retain_cap) {
158
+ pbw_arena_chunk *next = head->next;
159
+
160
+ free(head);
161
+ head = next;
162
+ }
163
+ arena->head = head;
164
+ if (head == NULL) {
165
+ arena->chunk_size = PBW_ARENA_MIN_CHUNK;
166
+ return;
167
+ }
168
+ /* Keep the head prefix that holds what was used: a tree bigger than one
169
+ * chunk then reuses its chunks instead of re-mallocing them every
170
+ * decode. Kept bytes stop at the retain budget (plus one chunk, which
171
+ * bounds the overshoot); the tail is freed. The head chunk itself is
172
+ * always kept, even on an idle reset (used == 0): keeping a prefix
173
+ * means at least one chunk stays reachable, never none. */
174
+ {
175
+ size_t used = 0;
176
+ size_t kept;
177
+ pbw_arena_chunk *last;
178
+ pbw_arena_chunk *rest;
179
+
180
+ for (chunk = head; chunk != NULL; chunk = chunk->next) {
181
+ used += chunk->used;
182
+ }
183
+ last = head;
184
+ kept = head->cap;
185
+ rest = head->next;
186
+ while (rest != NULL && kept < used && kept <= arena->retain_cap) {
187
+ kept += rest->cap;
188
+ last = rest;
189
+ rest = rest->next;
190
+ }
191
+ while (rest != NULL) {
192
+ pbw_arena_chunk *next = rest->next;
193
+
194
+ free(rest);
195
+ rest = next;
196
+ }
197
+ last->next = NULL;
198
+ for (chunk = head; chunk != NULL; chunk = chunk->next) {
199
+ chunk->used = 0;
200
+ }
201
+ }
202
+ }
203
+
204
+ pbw_arena *
205
+ pbw_arena_new_packed(size_t first_chunk)
206
+ {
207
+ size_t total;
208
+ uint8_t *block;
209
+ pbw_arena *arena;
210
+ pbw_arena_chunk *chunk;
211
+
212
+ if (first_chunk > SIZE_MAX - PBW_ARENA_HDR - PBW_CHUNK_HDR) {
213
+ return NULL;
214
+ }
215
+ total = PBW_ARENA_HDR + PBW_CHUNK_HDR + first_chunk;
216
+ block = (uint8_t *)malloc(total);
217
+ if (block == NULL) {
218
+ return NULL;
219
+ }
220
+ arena = (pbw_arena *)(void *)block;
221
+ chunk = (pbw_arena_chunk *)(void *)(block + PBW_ARENA_HDR);
222
+ chunk->next = NULL;
223
+ chunk->used = 0;
224
+ chunk->cap = first_chunk;
225
+ arena->head = chunk;
226
+ arena->large = NULL;
227
+ arena->retain_cap = PBW_ARENA_LARGE_THRESHOLD;
228
+ arena->chunk_size = first_chunk < PBW_ARENA_MIN_CHUNK ? PBW_ARENA_MIN_CHUNK
229
+ : first_chunk;
230
+ arena->readonly = 0;
231
+ arena->block = block;
232
+ arena->has_heap = 0;
233
+ arena->no_grow = 0;
234
+ return arena;
235
+ }
236
+
237
+ int
238
+ pbw_arena_init_mem(pbw_arena *arena, void *mem, size_t len)
239
+ {
240
+ pbw_arena_chunk *chunk;
241
+
242
+ if (arena == NULL || mem == NULL || len < PBW_CHUNK_HDR + 8u) {
243
+ return 0;
244
+ }
245
+ memset(arena, 0, sizeof(*arena));
246
+ chunk = (pbw_arena_chunk *)mem;
247
+ chunk->next = NULL;
248
+ chunk->used = 0;
249
+ chunk->cap = len - PBW_CHUNK_HDR;
250
+ arena->head = chunk;
251
+ arena->chunk_size = chunk->cap;
252
+ arena->block = mem;
253
+ arena->no_grow = 1;
254
+ return 1;
255
+ }
256
+
257
+ void
258
+ pbw_arena_free(pbw_arena *arena)
259
+ {
260
+ pbw_arena_chunk *chunk;
261
+ pbw_arena_chunk *big;
262
+
263
+ if (arena == NULL) {
264
+ return;
265
+ }
266
+ if (arena->no_grow) {
267
+ /* Caller memory: the arena never allocated, so it owns nothing to
268
+ * free and the caller's block stays valid after this call. */
269
+ return;
270
+ }
271
+ chunk = arena->head;
272
+ big = arena->large;
273
+
274
+ while (big != NULL) {
275
+ pbw_arena_chunk *next = big->next;
276
+
277
+ free(big);
278
+ big = next;
279
+ }
280
+ arena->large = NULL;
281
+ if (arena->block != NULL) {
282
+ /* The arena struct and its first chunk live in the block itself. */
283
+ uint8_t *base = (uint8_t *)arena->block;
284
+ pbw_arena_chunk *first =
285
+ (pbw_arena_chunk *)(void *)(base + PBW_ARENA_HDR);
286
+
287
+ for (chunk = arena->head; chunk != NULL && chunk != first;) {
288
+ pbw_arena_chunk *next = chunk->next;
289
+
290
+ free(chunk);
291
+ chunk = next;
292
+ }
293
+ free(base);
294
+ return;
295
+ }
296
+ while (chunk != NULL) {
297
+ pbw_arena_chunk *next = chunk->next;
298
+ free(chunk);
299
+ chunk = next;
300
+ }
301
+ arena->head = NULL;
302
+ arena->chunk_size = PBW_ARENA_MIN_CHUNK;
303
+ }
304
+
305
+ size_t
306
+ pbw_arena_used(const pbw_arena *arena)
307
+ {
308
+ const pbw_arena_chunk *chunk;
309
+ size_t used = 0;
310
+
311
+ if (arena == NULL) {
312
+ return 0;
313
+ }
314
+
315
+ for (chunk = arena->head; chunk != NULL; chunk = chunk->next) {
316
+ used += chunk->used;
317
+ }
318
+ for (chunk = arena->large; chunk != NULL; chunk = chunk->next) {
319
+ used += chunk->used;
320
+ }
321
+ return used;
322
+ }
@@ -0,0 +1,334 @@
1
+ /* Reusable per-caller workspace: one bump allocator for decoded messages and one
2
+ * growable buffer for encoded output. This is the explicit, thread-safe-by-
3
+ * construction version of pbwire's per-scheduler thread data. */
4
+
5
+ #include "internal.h"
6
+
7
+ struct pbw_ctx {
8
+ pbw_arena arena;
9
+ pbw_buf out;
10
+ /* Opaque binding slot (Erlang NIF thread data, Go state, ...). Set once
11
+ * by the binding; the core never reads it, and reset/pool keep it. */
12
+ void *user;
13
+ };
14
+
15
+ /* Bounded freelist of contexts. Unsynchronized by design (the core stays
16
+ * C99 with no dependencies): one pool per worker thread, never shared
17
+ * concurrently. Preallocation is lazy: the first acquire allocates, steady
18
+ * use then stops calling malloc. */
19
+ struct pbw_pool {
20
+ pbw_ctx **idle;
21
+ size_t count;
22
+ size_t cap; /* max_idle: how many a release keeps */
23
+ size_t idle_cap; /* allocated slots in idle[] */
24
+ size_t retain_cap; /* budget stamped on contexts this pool creates */
25
+ };
26
+
27
+ int
28
+ pbw_abi_version(void)
29
+ {
30
+ return PBW_ABI_VERSION;
31
+ }
32
+
33
+ pbw_ctx *
34
+ pbw_ctx_new(void)
35
+ {
36
+ pbw_ctx *ctx = (pbw_ctx *)calloc(1, sizeof(*ctx));
37
+
38
+ if (ctx == NULL) {
39
+ return NULL;
40
+ }
41
+ ctx->arena.chunk_size = 4096;
42
+ /* Writable: an empty context holds nothing to protect. Decoding seals
43
+ * it read-only; resetting returns it to this state. */
44
+ ctx->arena.readonly = 0;
45
+ ctx->arena.retain_cap = PBW_ARENA_LARGE_THRESHOLD;
46
+ pbw_buf_init(&ctx->out);
47
+ return ctx;
48
+ }
49
+
50
+ pbw_ctx *
51
+ pbw_ctx_new_mem(void *mem, size_t len)
52
+ {
53
+ pbw_ctx *ctx;
54
+ size_t hdr;
55
+
56
+ if (mem == NULL || ((uintptr_t)mem & 7u) != 0u) {
57
+ /* 8-byte alignment keeps every value slot naturally aligned. */
58
+ return NULL;
59
+ }
60
+ hdr = (sizeof(*ctx) + 7u) & ~(size_t)7u;
61
+ if (len < hdr + PBW_CHUNK_HDR + 8u) {
62
+ return NULL;
63
+ }
64
+ ctx = (pbw_ctx *)mem;
65
+ memset(ctx, 0, sizeof(*ctx));
66
+ pbw_buf_init(&ctx->out);
67
+ /* No heap here: pbw_encode_ctx() reports NOMEM instead of allocating; the
68
+ * encoder for this kind of context is pbw_encode_to(caller memory). */
69
+ ctx->out.fixed = 1;
70
+ if (!pbw_arena_init_mem(&ctx->arena, (uint8_t *)mem + hdr, len - hdr)) {
71
+ return NULL;
72
+ }
73
+ return ctx;
74
+ }
75
+
76
+ void
77
+ pbw_ctx_free(pbw_ctx *ctx)
78
+ {
79
+ if (ctx == NULL) {
80
+ return;
81
+ }
82
+ if (ctx->arena.no_grow) {
83
+ /* Caller memory (pbw_ctx_new_mem): the handle and the arena belong to
84
+ * the caller, so closing releases nothing and the block stays valid. */
85
+ return;
86
+ }
87
+ pbw_arena_free(&ctx->arena);
88
+ pbw_buf_free(&ctx->out);
89
+ free(ctx);
90
+ }
91
+
92
+ void
93
+ pbw_ctx_set_user(pbw_ctx *ctx, void *user)
94
+ {
95
+ if (ctx != NULL) {
96
+ ctx->user = user;
97
+ }
98
+ }
99
+
100
+ void *
101
+ pbw_ctx_user(const pbw_ctx *ctx)
102
+ {
103
+ return ctx != NULL ? ctx->user : NULL;
104
+ }
105
+
106
+ void
107
+ pbw_ctx_set_retain_cap(pbw_ctx *ctx, size_t cap)
108
+ {
109
+ if (ctx == NULL) {
110
+ return;
111
+ }
112
+ ctx->arena.retain_cap = cap;
113
+ }
114
+
115
+ void
116
+ pbw_ctx_reset(pbw_ctx *ctx)
117
+ {
118
+ if (ctx == NULL) {
119
+ return;
120
+ }
121
+ pbw_arena_reset(&ctx->arena);
122
+ if (ctx->out.cap > ctx->arena.retain_cap) {
123
+ /* Same rule as the arena head: an oversized encode buffer is freed
124
+ * instead of staying resident. A steady workload under budget keeps
125
+ * reusing it with no malloc. */
126
+ pbw_buf_free(&ctx->out);
127
+ }
128
+ ctx->out.len = 0;
129
+ ctx->out.oom = 0;
130
+ }
131
+
132
+ size_t
133
+ pbw_ctx_used(const pbw_ctx *ctx)
134
+ {
135
+ return ctx == NULL ? 0 : pbw_arena_used(&ctx->arena);
136
+ }
137
+
138
+ pbw_pool *
139
+ pbw_pool_new(size_t max_idle, size_t retain_cap)
140
+ {
141
+ pbw_pool *pool = (pbw_pool *)calloc(1, sizeof(*pool));
142
+
143
+ if (pool == NULL) {
144
+ return NULL;
145
+ }
146
+ pool->retain_cap = retain_cap > 0 ? retain_cap
147
+ : PBW_ARENA_LARGE_THRESHOLD;
148
+ pool->cap = max_idle;
149
+ return pool;
150
+ }
151
+
152
+ void
153
+ pbw_pool_free(pbw_pool *pool)
154
+ {
155
+ size_t i;
156
+
157
+ if (pool == NULL) {
158
+ return;
159
+ }
160
+ for (i = 0; i < pool->count; i++) {
161
+ pbw_ctx_free(pool->idle[i]);
162
+ }
163
+ free(pool->idle);
164
+ free(pool);
165
+ }
166
+
167
+ pbw_ctx *
168
+ pbw_pool_acquire(pbw_pool *pool)
169
+ {
170
+ pbw_ctx *ctx;
171
+
172
+ if (pool == NULL) {
173
+ return NULL;
174
+ }
175
+ if (pool->count > 0) {
176
+ return pool->idle[--pool->count];
177
+ }
178
+ ctx = pbw_ctx_new();
179
+ if (ctx == NULL) {
180
+ return NULL;
181
+ }
182
+ pbw_ctx_set_retain_cap(ctx, pool->retain_cap);
183
+ return ctx;
184
+ }
185
+
186
+ void
187
+ pbw_pool_release(pbw_pool *pool, pbw_ctx *ctx)
188
+ {
189
+ pbw_ctx **slot;
190
+
191
+ if (pool == NULL || ctx == NULL) {
192
+ return;
193
+ }
194
+ if (ctx->arena.no_grow) {
195
+ /* Caller memory can never grow: pooling it would hand out a context
196
+ * that fails where a pooled one succeeds, so it stays with its
197
+ * caller. */
198
+ return;
199
+ }
200
+ /* A released context is always clean: the next acquire can use it
201
+ * without resetting. The retain cap bounds what the reset keeps, so
202
+ * an outlier never pins the pool. */
203
+ pbw_ctx_reset(ctx);
204
+ if (pool->count >= pool->cap) {
205
+ pbw_ctx_free(ctx);
206
+ return;
207
+ }
208
+ /* Grow the slot array by doubling: one realloc per doubling instead of
209
+ * one per release, which matters for a pool that fills up. */
210
+ if (pool->count == pool->idle_cap) {
211
+ size_t want;
212
+
213
+ if (pool->idle_cap > SIZE_MAX / 2 / sizeof(*slot)) {
214
+ pbw_ctx_free(ctx);
215
+ return;
216
+ }
217
+ want = pool->idle_cap != 0 ? pool->idle_cap * 2 : 8;
218
+ if (want > pool->cap) {
219
+ want = pool->cap;
220
+ }
221
+ if (want <= pool->idle_cap || want > SIZE_MAX / sizeof(*slot)) {
222
+ pbw_ctx_free(ctx);
223
+ return;
224
+ }
225
+ slot = (pbw_ctx **)realloc(pool->idle, want * sizeof(*slot));
226
+ if (slot == NULL) {
227
+ pbw_ctx_free(ctx);
228
+ return;
229
+ }
230
+ pool->idle = slot;
231
+ pool->idle_cap = want;
232
+ }
233
+ pool->idle[pool->count++] = ctx;
234
+ }
235
+
236
+ size_t
237
+ pbw_pool_idle(const pbw_pool *pool)
238
+ {
239
+ return pool == NULL ? 0 : pool->count;
240
+ }
241
+
242
+ pbw_status
243
+ pbw_decode_ctx(pbw_ctx *ctx, const pbw_descriptor *desc, const void *buf,
244
+ size_t len, pbw_message **out)
245
+ {
246
+ return pbw_decode_ctx_limits(ctx, desc, buf, len, NULL, out);
247
+ }
248
+
249
+ pbw_status
250
+ pbw_decode_ctx_limits(pbw_ctx *ctx, const pbw_descriptor *desc,
251
+ const void *buf, size_t len, const pbw_limits *limits,
252
+ pbw_message **out)
253
+ {
254
+ if (out != NULL) {
255
+ *out = NULL;
256
+ }
257
+ if (ctx == NULL || out == NULL || (buf == NULL && len > 0)) {
258
+ return PBW_ERR_INVALID;
259
+ }
260
+ /* Every failure below leaves NULL: callers can rely on *out alone. */
261
+ if (limits != NULL && limits->max_bytes > 0 && len > limits->max_bytes) {
262
+ return PBW_ERR_LIMIT;
263
+ }
264
+ /* A sealed arena holds the previous decode: reset it here so a
265
+ * decode_in/reset/decode loop costs one call, not two. Only the
266
+ * sealed (read-only) state resets implicitly; a dirty writable arena
267
+ * keeps its trees, as before. */
268
+ if (ctx->arena.readonly) {
269
+ pbw_arena_reset(&ctx->arena);
270
+ }
271
+ {
272
+ pbw_status status =
273
+ pbw_decode_arena_lim(&ctx->arena, desc, buf, len, pbw_effective_depth(limits), out);
274
+ if (status == PBW_OK) {
275
+ /* Seal the arena: decoded trees are read-only, later builds
276
+ * need a reset first. */
277
+ ctx->arena.readonly = 1;
278
+ } else {
279
+ /* A failed decode leaves partial trees behind: drop them, or a
280
+ * loop over hostile frames grows the arena without bound. */
281
+ pbw_arena_reset(&ctx->arena);
282
+ }
283
+ return status;
284
+ }
285
+ }
286
+
287
+ pbw_status
288
+ pbw_encode_ctx(pbw_ctx *ctx, const pbw_message *msg, const uint8_t **out,
289
+ size_t *len)
290
+ {
291
+ pbw_status status;
292
+
293
+ if (ctx == NULL || out == NULL || len == NULL) {
294
+ return PBW_ERR_INVALID;
295
+ }
296
+ *out = NULL;
297
+ *len = 0;
298
+ status = pbw_encode_buf(&ctx->out, msg);
299
+ if (status != PBW_OK) {
300
+ *out = NULL;
301
+ *len = 0;
302
+ return status;
303
+ }
304
+ *out = ctx->out.data != NULL ? ctx->out.data : (const uint8_t *)"";
305
+ *len = ctx->out.len;
306
+ return PBW_OK;
307
+ }
308
+
309
+ pbw_message *
310
+ pbw_message_new_ctx(pbw_ctx *ctx, const pbw_descriptor *desc)
311
+ {
312
+ if (ctx == NULL || ctx->arena.readonly) {
313
+ return NULL;
314
+ }
315
+ return pbw_message_new_a(&ctx->arena, desc);
316
+ }
317
+
318
+ pbw_value *
319
+ pbw_value_new_uint_ctx(pbw_ctx *ctx, uint64_t bits)
320
+ {
321
+ if (ctx == NULL || ctx->arena.readonly) {
322
+ return NULL;
323
+ }
324
+ return pbw_value_new_uint_a(&ctx->arena, bits);
325
+ }
326
+
327
+ pbw_value *
328
+ pbw_value_new_bytes_ctx(pbw_ctx *ctx, const void *data, size_t len)
329
+ {
330
+ if (ctx == NULL || ctx->arena.readonly) {
331
+ return NULL;
332
+ }
333
+ return pbw_value_new_bytes_a(&ctx->arena, data, len);
334
+ }