Mercurial > hg > dmlib
annotate src/dmgrowbuf.h @ 1700:a2e65aa47554
Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
DMGrowBuf::is_const flag, that marks a buffer "unmodifiable", though
obviously that is not strictly enforced.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 05 Jun 2018 15:04:15 +0300 |
parents | f71cd6691e05 |
children | 7eb00206b36d |
rev | line source |
---|---|
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 * DMLib |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 * -- Growable buffer implementation |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
4 * Programmed and designed by Matti 'ccr' Hamalainen |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 * (C) Copyright 2018 Tecnic Software productions (TNSP) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 */ |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 #ifndef DMGROWBUF_H |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
8 #define DMGROWBUF_H |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 #include "dmlib.h" |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
11 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 #ifdef __cplusplus |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
13 extern "C" { |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
14 #endif |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
15 |
1687 | 16 |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
17 typedef struct |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
18 { |
1697
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
19 Uint8 |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
20 *data; // Actually allocated data pointer |
1533 | 21 |
1697
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
22 size_t |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
23 offs, // Current offset |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
24 min_offs, |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
25 max_offs, |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
26 size, // Actual allocated size |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
27 len, // Size requested |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
28 mingrow; // Minimum amount of bytes the allocation size grows by |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
29 |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
30 BOOL |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
31 allocated, // TRUE if this structure itself has been allocated and can be freed, FALSE if static |
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
32 backwards, // TRUE if the buffer grows backwards (e.g. "offs" moves backwards) |
1700
a2e65aa47554
Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents:
1699
diff
changeset
|
33 literal, // TRUE if dmGrowBufPut*() functions stores data "literally" in backwards mode |
a2e65aa47554
Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents:
1699
diff
changeset
|
34 is_const; // TRUE will cause any reallocs etc. modifications to fail |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
35 } DMGrowBuf; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
36 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 |
1694
e568535e1a96
Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents:
1693
diff
changeset
|
38 int dmGrowBufInit(DMGrowBuf *buf); |
1458
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
39 int dmGrowBufAlloc(DMGrowBuf *buf, const size_t initial, const size_t mingrow); |
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
40 int dmGrowBufNew(DMGrowBuf **pbuf, const size_t initial, const size_t mingrow); |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
41 void dmGrowBufFree(DMGrowBuf *buf); |
1687 | 42 |
1700
a2e65aa47554
Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents:
1699
diff
changeset
|
43 DMGrowBuf * dmGrowBufConstCopy(DMGrowBuf *dst, const DMGrowBuf *src); |
a2e65aa47554
Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents:
1699
diff
changeset
|
44 DMGrowBuf * dmGrowBufCreateFrom(DMGrowBuf *buf, Uint8 *data, size_t len); |
a2e65aa47554
Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents:
1699
diff
changeset
|
45 |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 BOOL dmGrowBufGrow(DMGrowBuf *buf, const size_t amount); |
1455
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
47 BOOL dmGrowBufCheckGrow(DMGrowBuf *buf, const size_t nsize); |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
48 |
1697
1036b0dcccb5
Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents:
1694
diff
changeset
|
49 BOOL dmGrowBufPut(DMGrowBuf *buf, const Uint8 *data, const size_t len); |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 BOOL dmGrowBufPutU8(DMGrowBuf *buf, const Uint8 value); |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 BOOL dmGrowBufPutU16BE(DMGrowBuf *buf, const Uint16 val); |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 BOOL dmGrowBufPutU16LE(DMGrowBuf *buf, const Uint16 val); |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
53 BOOL dmGrowBufPutU32BE(DMGrowBuf *buf, const Uint32 val); |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
54 BOOL dmGrowBufPutU32LE(DMGrowBuf *buf, const Uint32 val); |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 |
1699
f71cd6691e05
Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents:
1697
diff
changeset
|
56 BOOL dmGrowBufGetU8(DMGrowBuf *buf, Uint8 *value); |
f71cd6691e05
Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents:
1697
diff
changeset
|
57 |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 #ifdef __cplusplus |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 #endif |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 #endif // DMGROWBUF_H |