annotate src/dmgrowbuf.c @ 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 31a1f710e342
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 #include "dmgrowbuf.h"
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8
1536
064fc2e3ee64 Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1531
diff changeset
9 //#define DM_GROWBUF_DEBUG 1
064fc2e3ee64 Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1531
diff changeset
10
064fc2e3ee64 Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1531
diff changeset
11 #ifdef DM_GROWBUF_DEBUG
064fc2e3ee64 Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1531
diff changeset
12 # define DM_DBG(...) do { fprintf(stderr, __VA_ARGS__); } while (0)
064fc2e3ee64 Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1531
diff changeset
13 #else
064fc2e3ee64 Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1531
diff changeset
14 # define DM_DBG(...) do { } while (0)
064fc2e3ee64 Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1531
diff changeset
15 #endif
064fc2e3ee64 Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1531
diff changeset
16
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17
1694
e568535e1a96 Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents: 1693
diff changeset
18 int dmGrowBufInit(DMGrowBuf *buf)
1458
b2dd6a72d162 Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1455
diff changeset
19 {
1694
e568535e1a96 Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents: 1693
diff changeset
20 DM_DBG("dmGrowBufInit(%p)\n", buf);
1687
383ca5f6e78b Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1536
diff changeset
21
1458
b2dd6a72d162 Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1455
diff changeset
22 if (buf == NULL)
b2dd6a72d162 Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1455
diff changeset
23 return DMERR_NULLPTR;
b2dd6a72d162 Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1455
diff changeset
24
b2dd6a72d162 Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1455
diff changeset
25 memset(buf, 0, sizeof(DMGrowBuf));
b2dd6a72d162 Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1455
diff changeset
26
1694
e568535e1a96 Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents: 1693
diff changeset
27 return DMERR_OK;
e568535e1a96 Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents: 1693
diff changeset
28 }
e568535e1a96 Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents: 1693
diff changeset
29
e568535e1a96 Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents: 1693
diff changeset
30
e568535e1a96 Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents: 1693
diff changeset
31 int dmGrowBufAlloc(DMGrowBuf *buf, const size_t initial, const size_t mingrow)
e568535e1a96 Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents: 1693
diff changeset
32 {
e568535e1a96 Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents: 1693
diff changeset
33 int res;
e568535e1a96 Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents: 1693
diff changeset
34
e568535e1a96 Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents: 1693
diff changeset
35 DM_DBG("dmGrowBufAlloc(%p, %" DM_PRIu_SIZE_T ", %" DM_PRIu_SIZE_T ")\n",
e568535e1a96 Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents: 1693
diff changeset
36 buf, initial, mingrow);
e568535e1a96 Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents: 1693
diff changeset
37
e568535e1a96 Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents: 1693
diff changeset
38 if ((res = dmGrowBufInit(buf)) != DMERR_OK)
e568535e1a96 Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents: 1693
diff changeset
39 return res;
e568535e1a96 Backed out changeset 9611ecd2c4fb
Matti Hamalainen <ccr@tnsp.org>
parents: 1693
diff changeset
40
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 buf->len = 0;
1531
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
42 buf->offs = 0;
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 buf->size = initial;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 buf->mingrow = mingrow;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 buf->allocated = FALSE;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46
1509
15afe578f0ae Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents: 1508
diff changeset
47 // Allocate the data
1697
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
48 if ((buf->data = dmMalloc0(initial)) == NULL)
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 return DMERR_MALLOC;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 return DMERR_OK;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 }
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54
1458
b2dd6a72d162 Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1455
diff changeset
55 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
56 {
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 int res;
1458
b2dd6a72d162 Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1455
diff changeset
58 if (pbuf == NULL)
b2dd6a72d162 Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1455
diff changeset
59 return DMERR_NULLPTR;
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60
1687
383ca5f6e78b Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1536
diff changeset
61 DM_DBG("dmGrowBufNew(%p, %" DM_PRIu_SIZE_T ", %" DM_PRIu_SIZE_T ")\n",
383ca5f6e78b Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1536
diff changeset
62 pbuf, initial, mingrow);
383ca5f6e78b Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1536
diff changeset
63
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 if ((*pbuf = dmMalloc0(sizeof(DMGrowBuf))) == NULL)
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 return DMERR_MALLOC;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66
1458
b2dd6a72d162 Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1455
diff changeset
67 if ((res = dmGrowBufAlloc(*pbuf, initial, mingrow)) != DMERR_OK)
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 {
1459
1155f4bc4afc More work on growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1458
diff changeset
69 // The "allocated" flag has not yet been set
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 dmGrowBufFree(*pbuf);
1459
1155f4bc4afc More work on growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1458
diff changeset
71
1155f4bc4afc More work on growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1458
diff changeset
72 // .. thus free the allocated struct here
1155f4bc4afc More work on growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1458
diff changeset
73 dmFreeR(pbuf);
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 return res;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 }
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 (*pbuf)->allocated = TRUE;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 return res;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 }
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 void dmGrowBufFree(DMGrowBuf *buf)
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 {
1536
064fc2e3ee64 Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1531
diff changeset
84 DM_DBG("dmGrowBufFree(%p)\n", buf);
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 if (buf != NULL)
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 {
1536
064fc2e3ee64 Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1531
diff changeset
87 DM_DBG(
064fc2e3ee64 Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1531
diff changeset
88 " buf->data = %p\n"
064fc2e3ee64 Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1531
diff changeset
89 " buf->allocated = %s\n",
1700
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
90 " buf->is_const = %s\n",
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
91 buf->data,
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
92 buf->allocated ? "YES" : "NO",
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
93 buf->is_const ? "YES" : "NO");
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
94
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
95 if (buf->is_const)
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
96 return;
1536
064fc2e3ee64 Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1531
diff changeset
97
1697
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
98 dmFreeR(&buf->data);
1531
260bf529a8f2 Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1530
diff changeset
99
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 if (buf->allocated)
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 dmFree(buf);
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 }
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 }
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105
1700
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
106 DMGrowBuf * dmGrowBufConstCopy(DMGrowBuf *dst, const DMGrowBuf *src)
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
107 {
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
108 memcpy(dst, src, sizeof(DMGrowBuf));
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
109 dst->is_const = TRUE;
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
110 return dst;
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
111 }
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
112
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
113
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
114 DMGrowBuf * dmGrowBufCreateFrom(DMGrowBuf *dst, Uint8 *data, size_t len)
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
115 {
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
116 dmGrowBufInit(dst);
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
117
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
118 dst->data = data;
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
119 dst->len = dst->size = len;
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
120 dst->is_const = TRUE;
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
121
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
122 return dst;
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
123 }
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
124
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
125
1530
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
126 static BOOL dmGrowBufRealloc(DMGrowBuf *buf, const size_t nsize, const BOOL clear)
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
127 {
1697
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
128 DM_DBG("dmGrowBufRealloc(%p):\n"
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
129 " size=%" DM_PRIu_SIZE_T ", nsize=" DM_PRIu_SIZE_T
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
130 ", nstack=%d, offs=%" DM_PRIu_SIZE_T "\n",
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
131 buf, buf->size, nsize, buf->nstack, buf->offs);
1536
064fc2e3ee64 Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 1531
diff changeset
132
1700
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
133 if (buf->is_const)
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
134 return FALSE;
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
135
1697
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
136 // Can't be smaller than current size!
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
137 if (nsize < buf->size)
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
138 return FALSE;
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
139
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
140 if ((buf->data = dmRealloc(buf->data, nsize)) == NULL)
1530
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
141 return FALSE;
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
142
1697
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
143 // For buffers growing backwards, we must move the
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
144 // current data to the end of the buffer ..
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
145 size_t clrsize = nsize - buf->size;
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
146 if (buf->backwards)
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
147 {
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
148 memmove(buf->data + clrsize, buf->data, clrsize);
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
149 buf->offs += clrsize;
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
150 }
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
151
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
152 // Check if we need to clear the newly allocated area?
1530
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
153 if (clear)
1697
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
154 {
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
155 if (buf->backwards)
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
156 memset(buf->data, 0, clrsize);
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
157 else
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
158 memset(buf->data + buf->size, 0, clrsize);
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
159 }
1530
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
160
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
161 buf->size = nsize;
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
162
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
163 return TRUE;
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
164 }
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
165
94eb6a8a7d56 Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents: 1509
diff changeset
166
1509
15afe578f0ae Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents: 1508
diff changeset
167 //
15afe578f0ae Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents: 1508
diff changeset
168 // Grow the buffer by "amount" bytes, but at least by buf->mingrow,
15afe578f0ae Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents: 1508
diff changeset
169 // if there is not enough space for at least that amount compared to
15afe578f0ae Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents: 1508
diff changeset
170 // current buffer "len".
15afe578f0ae Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents: 1508
diff changeset
171 //
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172 BOOL dmGrowBufGrow(DMGrowBuf *buf, const size_t amount)
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173 {
1697
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
174 size_t grow = (amount > buf->mingrow) ? amount : buf->mingrow;
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
175
1700
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
176 if (buf->is_const)
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
177 return FALSE;
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
178
1697
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
179 if (buf->data == NULL ||
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
180 (buf->backwards && amount >= buf->offs) ||
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
181 (!buf->backwards && buf->offs + amount >= buf->size))
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
182 {
1697
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
183 if (!dmGrowBufRealloc(buf, buf->size + grow, TRUE))
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
184 return FALSE;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
185 }
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
186
1697
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
187 buf->len += amount;
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
188 return TRUE;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
189 }
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191
1509
15afe578f0ae Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents: 1508
diff changeset
192 //
15afe578f0ae Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents: 1508
diff changeset
193 // Grow the buffer if "nsize" is larger than the current buffer size.
15afe578f0ae Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents: 1508
diff changeset
194 // Buffer is enlarged to nsize + mingrow.
15afe578f0ae Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents: 1508
diff changeset
195 //
1455
a957b318fbe2 Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1454
diff changeset
196 BOOL dmGrowBufCheckGrow(DMGrowBuf *buf, const size_t nsize)
a957b318fbe2 Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1454
diff changeset
197 {
1700
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
198 if (buf->is_const)
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
199 return FALSE;
a2e65aa47554 Implement dmGrowBufConstCopy() and dmGrowBufCreateFrom(), and also
Matti Hamalainen <ccr@tnsp.org>
parents: 1699
diff changeset
200
1697
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
201 if (buf->data == NULL || nsize > buf->size)
1455
a957b318fbe2 Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1454
diff changeset
202 {
1697
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
203 if (!dmGrowBufRealloc(buf, nsize + buf->mingrow, TRUE))
1455
a957b318fbe2 Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1454
diff changeset
204 return FALSE;
a957b318fbe2 Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1454
diff changeset
205 }
a957b318fbe2 Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1454
diff changeset
206
1697
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
207 buf->len = nsize;
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
208 return TRUE;
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
209 }
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
210
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
211
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
212 static void dmGrowBufUpdate(DMGrowBuf *buf)
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
213 {
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
214 if (buf->offs < buf->min_offs)
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
215 buf->min_offs = buf->offs;
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
216
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
217 if (buf->offs > buf->max_offs)
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
218 buf->max_offs = buf->offs;
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
219 }
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
220
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
221
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
222 BOOL dmGrowBufPut(DMGrowBuf *buf, const Uint8 *data, const size_t len)
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
223 {
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
224 if (data == NULL)
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
225 return FALSE;
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
226
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
227 if (!dmGrowBufGrow(buf, len))
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
228 return FALSE;
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
229
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
230 if (buf->backwards)
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
231 {
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
232 if (buf->literal)
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
233 {
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
234 buf->offs -= len;
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
235 memcpy(buf->data + buf->offs, data, len);
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
236 }
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
237 else
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
238 {
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
239 for (size_t n = 0; n < len; n++)
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
240 buf->data[buf->offs--] = data[n];
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
241 }
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
242 }
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
243 else
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
244 {
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
245 memcpy(buf->data + buf->offs, data, len);
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
246 buf->offs += len;
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
247 }
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
248
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
249 dmGrowBufUpdate(buf);
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
250
1455
a957b318fbe2 Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1454
diff changeset
251 return TRUE;
a957b318fbe2 Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1454
diff changeset
252 }
a957b318fbe2 Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1454
diff changeset
253
a957b318fbe2 Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents: 1454
diff changeset
254
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
255 BOOL dmGrowBufPutU8(DMGrowBuf *buf, const Uint8 value)
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
256 {
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
257 if (!dmGrowBufGrow(buf, sizeof(Uint8)))
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258 return FALSE;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259
1697
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
260 buf->data[buf->offs] = value;
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
261 if (buf->backwards)
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
262 buf->offs--;
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
263 else
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
264 buf->offs++;
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
265
1697
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
266 dmGrowBufUpdate(buf);
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
267
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
268 return TRUE;
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
269 }
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
270
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
271
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
272 BOOL dmGrowBufPutU16BE(DMGrowBuf *buf, const Uint16 val)
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
273 {
1697
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
274 if (buf->literal && buf->backwards)
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
275 {
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
276 return
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
277 !dmGrowBufPutU8(buf, val & 0xff) ||
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
278 !dmGrowBufPutU8(buf, (val >> 8) & 0xff);
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
279 }
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
280 else
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
281 {
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
282 return
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
283 !dmGrowBufPutU8(buf, (val >> 8) & 0xff) ||
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
284 !dmGrowBufPutU8(buf, val & 0xff);
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
285 }
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
286 }
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
287
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
288
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
289 BOOL dmGrowBufPutU16LE(DMGrowBuf *buf, const Uint16 val)
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
290 {
1697
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
291 if (buf->literal && buf->backwards)
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
292 {
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
293 return
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
294 !dmGrowBufPutU8(buf, (val >> 8) & 0xff) ||
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
295 !dmGrowBufPutU8(buf, val & 0xff);
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
296 }
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
297 else
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
298 {
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
299 return
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
300 !dmGrowBufPutU8(buf, val & 0xff) ||
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
301 !dmGrowBufPutU8(buf, (val >> 8) & 0xff);
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
302 }
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
303 }
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
304
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
305
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
306 BOOL dmGrowBufPutU32BE(DMGrowBuf *buf, const Uint32 val)
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
307 {
1697
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
308 if (buf->literal && buf->backwards)
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
309 {
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
310 return
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
311 !dmGrowBufPutU8(buf, (val >> 24) & 0xff) ||
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
312 !dmGrowBufPutU8(buf, (val >> 16) & 0xff) ||
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
313 !dmGrowBufPutU8(buf, (val >> 8) & 0xff) ||
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
314 !dmGrowBufPutU8(buf, val & 0xff);
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
315 }
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
316 else
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
317 {
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
318 return
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
319 !dmGrowBufPutU8(buf, val & 0xff) ||
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
320 !dmGrowBufPutU8(buf, (val >> 8) & 0xff) ||
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
321 !dmGrowBufPutU8(buf, (val >> 16) & 0xff) ||
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
322 !dmGrowBufPutU8(buf, (val >> 24) & 0xff);
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
323 }
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
324 }
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
325
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
326
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
327 BOOL dmGrowBufPutU32LE(DMGrowBuf *buf, const Uint32 val)
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
328 {
1697
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
329 if (buf->literal && buf->backwards)
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
330 {
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
331 return
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
332 !dmGrowBufPutU8(buf, val & 0xff) ||
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
333 !dmGrowBufPutU8(buf, (val >> 8) & 0xff) ||
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
334 !dmGrowBufPutU8(buf, (val >> 16) & 0xff) ||
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
335 !dmGrowBufPutU8(buf, (val >> 24) & 0xff);
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
336 }
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
337 else
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
338 {
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
339 return
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
340 !dmGrowBufPutU8(buf, (val >> 24) & 0xff) ||
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
341 !dmGrowBufPutU8(buf, (val >> 16) & 0xff) ||
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
342 !dmGrowBufPutU8(buf, (val >> 8) & 0xff) ||
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
343 !dmGrowBufPutU8(buf, val & 0xff);
1036b0dcccb5 Refactor DMGrowBuf so that there can be buffers that grow "backwards".
Matti Hamalainen <ccr@tnsp.org>
parents: 1694
diff changeset
344 }
1454
fff3b58d031c Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
345 }
1699
f71cd6691e05 Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents: 1697
diff changeset
346
f71cd6691e05 Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents: 1697
diff changeset
347
f71cd6691e05 Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents: 1697
diff changeset
348 BOOL dmGrowBufGetU8(DMGrowBuf *buf, Uint8 *value)
f71cd6691e05 Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents: 1697
diff changeset
349 {
f71cd6691e05 Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents: 1697
diff changeset
350 if (buf->backwards && buf->offs > 0)
f71cd6691e05 Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents: 1697
diff changeset
351 {
f71cd6691e05 Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents: 1697
diff changeset
352 *value = buf->data[buf->offs];
f71cd6691e05 Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents: 1697
diff changeset
353 buf->offs--;
f71cd6691e05 Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents: 1697
diff changeset
354 return TRUE;
f71cd6691e05 Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents: 1697
diff changeset
355 }
f71cd6691e05 Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents: 1697
diff changeset
356 else
f71cd6691e05 Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents: 1697
diff changeset
357 if (!buf->backwards && buf->offs < buf->len)
f71cd6691e05 Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents: 1697
diff changeset
358 {
f71cd6691e05 Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents: 1697
diff changeset
359 *value = buf->data[buf->offs];
f71cd6691e05 Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents: 1697
diff changeset
360 buf->offs++;
f71cd6691e05 Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents: 1697
diff changeset
361 return TRUE;
f71cd6691e05 Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents: 1697
diff changeset
362 }
f71cd6691e05 Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents: 1697
diff changeset
363
f71cd6691e05 Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents: 1697
diff changeset
364 return FALSE;
f71cd6691e05 Implement dmGrowBufGetU8().
Matti Hamalainen <ccr@tnsp.org>
parents: 1697
diff changeset
365 }