AmendHub

Download

cyberslak

/

lightsout

/

dbuf.h

 

(View History)

cyberslak   Add LICENSE information Latest amendment: 22 on 2025-03-15

1 // SPDX-License-Identifier: MIT
2
3 #pragma once
4 #include "stdint.h"
5 #include "util.h"
6
7 struct dbuf
8 {
9 size_t capacity;
10 size_t len;
11 u8 *buf;
12 };
13
14 inline struct dbuf _db_new(size_t capacity)
15 {
16 struct dbuf db;
17 db.capacity = capacity;
18 db.len = 0;
19 db.buf = xmalloc(capacity);
20 return db;
21 }
22
23 // Unfortunately, pre-C99 we can't have an initializer list with xmalloc
24 #define DB_INIT(name, capacity) \
25 struct dbuf name = _db_new(capacity)
26
27 void db_init(struct dbuf* db, size_t capacity);
28 void db_extend(struct dbuf* db, u8* buf, size_t bufsize);
29 void db_printf(struct dbuf* db, const char* fmt, ...);
30 void db_destroy(struct dbuf* db);
31 void db_clear(struct dbuf* db);