comparison 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
comparison
equal deleted inserted replaced
1699:f71cd6691e05 1700:a2e65aa47554
85 if (buf != NULL) 85 if (buf != NULL)
86 { 86 {
87 DM_DBG( 87 DM_DBG(
88 " buf->data = %p\n" 88 " buf->data = %p\n"
89 " buf->allocated = %s\n", 89 " buf->allocated = %s\n",
90 buf->data, buf->allocated ? "YES" : "NO"); 90 " buf->is_const = %s\n",
91 buf->data,
92 buf->allocated ? "YES" : "NO",
93 buf->is_const ? "YES" : "NO");
94
95 if (buf->is_const)
96 return;
91 97
92 dmFreeR(&buf->data); 98 dmFreeR(&buf->data);
93 99
94 if (buf->allocated) 100 if (buf->allocated)
95 dmFree(buf); 101 dmFree(buf);
96 } 102 }
103 }
104
105
106 DMGrowBuf * dmGrowBufConstCopy(DMGrowBuf *dst, const DMGrowBuf *src)
107 {
108 memcpy(dst, src, sizeof(DMGrowBuf));
109 dst->is_const = TRUE;
110 return dst;
111 }
112
113
114 DMGrowBuf * dmGrowBufCreateFrom(DMGrowBuf *dst, Uint8 *data, size_t len)
115 {
116 dmGrowBufInit(dst);
117
118 dst->data = data;
119 dst->len = dst->size = len;
120 dst->is_const = TRUE;
121
122 return dst;
97 } 123 }
98 124
99 125
100 static BOOL dmGrowBufRealloc(DMGrowBuf *buf, const size_t nsize, const BOOL clear) 126 static BOOL dmGrowBufRealloc(DMGrowBuf *buf, const size_t nsize, const BOOL clear)
101 { 127 {
102 DM_DBG("dmGrowBufRealloc(%p):\n" 128 DM_DBG("dmGrowBufRealloc(%p):\n"
103 " size=%" DM_PRIu_SIZE_T ", nsize=" DM_PRIu_SIZE_T 129 " size=%" DM_PRIu_SIZE_T ", nsize=" DM_PRIu_SIZE_T
104 ", nstack=%d, offs=%" DM_PRIu_SIZE_T "\n", 130 ", nstack=%d, offs=%" DM_PRIu_SIZE_T "\n",
105 buf, buf->size, nsize, buf->nstack, buf->offs); 131 buf, buf->size, nsize, buf->nstack, buf->offs);
132
133 if (buf->is_const)
134 return FALSE;
106 135
107 // Can't be smaller than current size! 136 // Can't be smaller than current size!
108 if (nsize < buf->size) 137 if (nsize < buf->size)
109 return FALSE; 138 return FALSE;
110 139
142 // 171 //
143 BOOL dmGrowBufGrow(DMGrowBuf *buf, const size_t amount) 172 BOOL dmGrowBufGrow(DMGrowBuf *buf, const size_t amount)
144 { 173 {
145 size_t grow = (amount > buf->mingrow) ? amount : buf->mingrow; 174 size_t grow = (amount > buf->mingrow) ? amount : buf->mingrow;
146 175
176 if (buf->is_const)
177 return FALSE;
178
147 if (buf->data == NULL || 179 if (buf->data == NULL ||
148 (buf->backwards && amount >= buf->offs) || 180 (buf->backwards && amount >= buf->offs) ||
149 (!buf->backwards && buf->offs + amount >= buf->size)) 181 (!buf->backwards && buf->offs + amount >= buf->size))
150 { 182 {
151 if (!dmGrowBufRealloc(buf, buf->size + grow, TRUE)) 183 if (!dmGrowBufRealloc(buf, buf->size + grow, TRUE))
161 // Grow the buffer if "nsize" is larger than the current buffer size. 193 // Grow the buffer if "nsize" is larger than the current buffer size.
162 // Buffer is enlarged to nsize + mingrow. 194 // Buffer is enlarged to nsize + mingrow.
163 // 195 //
164 BOOL dmGrowBufCheckGrow(DMGrowBuf *buf, const size_t nsize) 196 BOOL dmGrowBufCheckGrow(DMGrowBuf *buf, const size_t nsize)
165 { 197 {
198 if (buf->is_const)
199 return FALSE;
200
166 if (buf->data == NULL || nsize > buf->size) 201 if (buf->data == NULL || nsize > buf->size)
167 { 202 {
168 if (!dmGrowBufRealloc(buf, nsize + buf->mingrow, TRUE)) 203 if (!dmGrowBufRealloc(buf, nsize + buf->mingrow, TRUE))
169 return FALSE; 204 return FALSE;
170 } 205 }