Mercurial > hg > dmlib
annotate src/dmgrowbuf.c @ 1693:ce8d4552ffd5
Get rid of dmGrowBufResize().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 05 Jun 2018 09:52:53 +0300 |
parents | 9611ecd2c4fb |
children | e568535e1a96 |
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 |
1692
9611ecd2c4fb
Get rid of dmGrowBufInit().
Matti Hamalainen <ccr@tnsp.org>
parents:
1687
diff
changeset
|
18 int dmGrowBufAlloc(DMGrowBuf *buf, const size_t initial, const size_t mingrow) |
1458
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
19 { |
1692
9611ecd2c4fb
Get rid of dmGrowBufInit().
Matti Hamalainen <ccr@tnsp.org>
parents:
1687
diff
changeset
|
20 DM_DBG("dmGrowBufAlloc(%p, %" DM_PRIu_SIZE_T ", %" DM_PRIu_SIZE_T ")\n", |
9611ecd2c4fb
Get rid of dmGrowBufInit().
Matti Hamalainen <ccr@tnsp.org>
parents:
1687
diff
changeset
|
21 buf, initial, mingrow); |
1687 | 22 |
1458
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
23 if (buf == NULL) |
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
24 return DMERR_NULLPTR; |
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
25 |
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
26 memset(buf, 0, sizeof(DMGrowBuf)); |
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
27 |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
28 buf->len = 0; |
1531
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
29 buf->offs = 0; |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
30 buf->size = initial; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 buf->mingrow = mingrow; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
32 buf->allocated = FALSE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
33 |
1509
15afe578f0ae
Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents:
1508
diff
changeset
|
34 // Allocate the data |
1531
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
35 if ((buf->adata = buf->data = dmMalloc0(initial)) == NULL) |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
36 return DMERR_MALLOC; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
38 return DMERR_OK; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
39 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
41 |
1458
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
42 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
|
43 { |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 int res; |
1458
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
45 if (pbuf == NULL) |
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
46 return DMERR_NULLPTR; |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 |
1687 | 48 DM_DBG("dmGrowBufNew(%p, %" DM_PRIu_SIZE_T ", %" DM_PRIu_SIZE_T ")\n", |
49 pbuf, initial, mingrow); | |
50 | |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 if ((*pbuf = dmMalloc0(sizeof(DMGrowBuf))) == NULL) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 return DMERR_MALLOC; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
53 |
1458
b2dd6a72d162
Adjust semantics of growbuf and add new function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1455
diff
changeset
|
54 if ((res = dmGrowBufAlloc(*pbuf, initial, mingrow)) != DMERR_OK) |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 { |
1459 | 56 // The "allocated" flag has not yet been set |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 dmGrowBufFree(*pbuf); |
1459 | 58 |
59 // .. thus free the allocated struct here | |
60 dmFreeR(pbuf); | |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 return res; |
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 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
64 (*pbuf)->allocated = TRUE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
65 return res; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
66 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
67 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
69 void dmGrowBufFree(DMGrowBuf *buf) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
70 { |
1536
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
71 DM_DBG("dmGrowBufFree(%p)\n", buf); |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
72 if (buf != NULL) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
73 { |
1536
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
74 DM_DBG( |
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
75 " buf->adata = %p\n" |
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
76 " buf->data = %p\n" |
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
77 " buf->allocated = %s\n", |
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
78 buf->adata, buf->data, buf->allocated ? "YES" : "NO"); |
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
79 |
1531
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
80 dmFreeR(&buf->adata); |
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
81 buf->data = NULL; |
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
82 |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
83 if (buf->allocated) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
84 dmFree(buf); |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
85 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
86 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
87 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
88 |
1531
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
89 void dmGrowBufPush(DMGrowBuf *buf) |
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
90 { |
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
91 if (buf != NULL && buf->adata != NULL) |
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
92 { |
1536
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
93 DM_DBG("dmGrowBufPush(%p): size=%" DM_PRIu_SIZE_T "\n" |
1687 | 94 " nstack=%d [offs=%" DM_PRIu_SIZE_T ", len=%" DM_PRIu_SIZE_T "]\n", |
95 buf, buf->size, buf->nstack, buf->offs, buf->len); | |
1536
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
96 |
1531
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
97 buf->stack[buf->nstack].offs = buf->offs; |
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
98 buf->stack[buf->nstack].len = buf->len; |
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
99 buf->nstack++; |
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
100 |
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
101 buf->offs = buf->len; |
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
102 buf->data = buf->adata + buf->offs; |
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
103 buf->len = 0; |
1536
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
104 |
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
105 DM_DBG( |
1687 | 106 " nstack=%d [offs=%" DM_PRIu_SIZE_T ", len=%" DM_PRIu_SIZE_T "]\n", |
107 buf->nstack, buf->offs, buf->len); | |
1531
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
108 } |
1536
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
109 else |
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
110 DM_DBG("dmGrowBufPush(%p)\n", buf); |
1531
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
111 } |
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
112 |
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
113 |
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
114 void dmGrowBufPop(DMGrowBuf *buf) |
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
115 { |
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
116 if (buf != NULL && buf->adata != NULL && buf->nstack > 0) |
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
117 { |
1536
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
118 DM_DBG("dmGrowBufPop(%p): size=%" DM_PRIu_SIZE_T "\n" |
1687 | 119 " nstack=%d [offs=%" DM_PRIu_SIZE_T ", len=%" DM_PRIu_SIZE_T "]\n", |
120 buf, buf->size, buf->nstack, buf->offs, buf->len); | |
1536
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
121 |
1531
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
122 buf->nstack--; |
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
123 buf->offs = buf->stack[buf->nstack].offs; |
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
124 buf->len += buf->stack[buf->nstack].len; |
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
125 |
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
126 buf->data = buf->adata + buf->offs; |
1536
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
127 |
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
128 DM_DBG( |
1687 | 129 " nstack=%d [offs=%" DM_PRIu_SIZE_T ", len=%" DM_PRIu_SIZE_T "]\n", |
130 buf->nstack, buf->offs, buf->len); | |
1536
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
131 |
1531
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
132 } |
1536
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
133 else |
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
134 DM_DBG("dmGrowBufPop(%p)\n", buf); |
1531
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
135 } |
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
136 |
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
137 |
1530
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
138 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
|
139 { |
1536
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
140 DM_DBG("dmGrowBufRealloc(%p): size=%" DM_PRIu_SIZE_T "\n" |
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
141 " nstack=%d [offs=%" DM_PRIu_SIZE_T ", len=%" DM_PRIu_SIZE_T "]\n", |
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
142 buf, buf->size, buf->nstack, buf->offs, buf->len); |
064fc2e3ee64
Add compile-time debugging information to DMGrowBuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1531
diff
changeset
|
143 |
1531
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
144 if ((buf->adata = dmRealloc(buf->adata, nsize)) == NULL) |
1530
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
145 return FALSE; |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
146 |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
147 if (clear) |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
148 memset(buf->adata + buf->size, 0, nsize - buf->size); |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
149 |
1531
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
150 buf->data = buf->adata + buf->offs; |
1530
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
151 buf->size = nsize; |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
152 |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
153 return TRUE; |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
154 } |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
155 |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
156 |
1509
15afe578f0ae
Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents:
1508
diff
changeset
|
157 // |
15afe578f0ae
Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents:
1508
diff
changeset
|
158 // 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
|
159 // 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
|
160 // current buffer "len". |
15afe578f0ae
Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents:
1508
diff
changeset
|
161 // |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
162 BOOL dmGrowBufGrow(DMGrowBuf *buf, const size_t amount) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
163 { |
1531
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
164 if (buf->adata == NULL || buf->offs + buf->len + amount >= buf->size) |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
165 { |
1530
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
166 size_t grow = (amount > buf->mingrow) ? amount : buf->mingrow; |
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
167 if (!dmGrowBufRealloc(buf, buf->offs + buf->len + grow, TRUE)) |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
168 return FALSE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
169 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
170 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
171 return TRUE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
172 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
173 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
174 |
1509
15afe578f0ae
Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents:
1508
diff
changeset
|
175 // |
15afe578f0ae
Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents:
1508
diff
changeset
|
176 // 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
|
177 // Buffer is enlarged to nsize + mingrow. |
15afe578f0ae
Add few comments to clarify things.
Matti Hamalainen <ccr@tnsp.org>
parents:
1508
diff
changeset
|
178 // |
1455
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
179 BOOL dmGrowBufCheckGrow(DMGrowBuf *buf, const size_t nsize) |
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
180 { |
1531
260bf529a8f2
Implement current len/offs push/pop for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents:
1530
diff
changeset
|
181 if (buf->adata == NULL || buf->offs + nsize > buf->size) |
1455
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
182 { |
1530
94eb6a8a7d56
Add helper function dmGrowBufRealloc() to handle the common case of
Matti Hamalainen <ccr@tnsp.org>
parents:
1509
diff
changeset
|
183 if (!dmGrowBufRealloc(buf, buf->offs + nsize + buf->mingrow, TRUE)) |
1455
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
184 return FALSE; |
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
185 } |
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
186 |
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
187 return TRUE; |
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
188 } |
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
189 |
a957b318fbe2
Add dmGrowBufCheckGrow() function.
Matti Hamalainen <ccr@tnsp.org>
parents:
1454
diff
changeset
|
190 |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
191 BOOL dmGrowBufPutU8(DMGrowBuf *buf, const Uint8 value) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
192 { |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
193 if (!dmGrowBufGrow(buf, sizeof(Uint8))) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
194 return FALSE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
195 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
196 buf->data[buf->len++] = value; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
197 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
198 return TRUE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
199 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
200 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
201 |
1477
e8fe529f4341
Rename one of the growbuf functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
1459
diff
changeset
|
202 BOOL dmGrowBufPut(DMGrowBuf *buf, const void *str, const size_t len) |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
203 { |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
204 if (str == NULL) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
205 return FALSE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
206 |
1484
a0166c4050be
Fix dmGrowBufPut() to not include extra byte.
Matti Hamalainen <ccr@tnsp.org>
parents:
1477
diff
changeset
|
207 if (!dmGrowBufGrow(buf, len)) |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
208 return FALSE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
209 |
1484
a0166c4050be
Fix dmGrowBufPut() to not include extra byte.
Matti Hamalainen <ccr@tnsp.org>
parents:
1477
diff
changeset
|
210 memcpy(buf->data + buf->len, str, len); |
1454
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
211 buf->len += len; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
212 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
213 return TRUE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
214 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
215 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
216 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
217 BOOL dmGrowBufPutU16BE(DMGrowBuf *buf, const Uint16 val) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
218 { |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
219 if (!dmGrowBufGrow(buf, sizeof(Uint16))) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
220 return FALSE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
221 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
222 buf->data[buf->len++] = (val >> 8) & 0xff; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
223 buf->data[buf->len++] = val & 0xff; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
224 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
225 return TRUE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
226 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
227 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
228 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
229 BOOL dmGrowBufPutU16LE(DMGrowBuf *buf, const Uint16 val) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
230 { |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
231 if (!dmGrowBufGrow(buf, sizeof(Uint16))) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
232 return FALSE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
233 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
234 buf->data[buf->len++] = val & 0xff; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
235 buf->data[buf->len++] = (val >> 8) & 0xff; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
236 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
237 return TRUE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
238 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
239 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
240 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
241 BOOL dmGrowBufPutU32BE(DMGrowBuf *buf, const Uint32 val) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
242 { |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
243 if (!dmGrowBufGrow(buf, sizeof(Uint32))) |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
244 return FALSE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
245 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
246 buf->data[buf->len++] = (val >> 24) & 0xff; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
247 buf->data[buf->len++] = (val >> 16) & 0xff; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
248 buf->data[buf->len++] = (val >> 8) & 0xff; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
249 buf->data[buf->len++] = val & 0xff; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
250 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
251 return TRUE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
252 } |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
253 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
254 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
255 BOOL dmGrowBufPutU32LE(DMGrowBuf *buf, const Uint32 val) |
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(Uint32))) |
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 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
260 buf->data[buf->len++] = val & 0xff; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
261 buf->data[buf->len++] = (val >> 8) & 0xff; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
262 buf->data[buf->len++] = (val >> 16) & 0xff; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
263 buf->data[buf->len++] = (val >> 24) & 0xff; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
264 |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
265 return TRUE; |
fff3b58d031c
Add a growable buffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
266 } |