comparison src/dmgrowbuf.c @ 1530:94eb6a8a7d56

Add helper function dmGrowBufRealloc() to handle the common case of reallocating the grow buffer in various enlargement functions.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 12 May 2018 03:10:47 +0300
parents 15afe578f0ae
children 260bf529a8f2
comparison
equal deleted inserted replaced
1529:4068d87ca884 1530:94eb6a8a7d56
71 dmFree(buf); 71 dmFree(buf);
72 } 72 }
73 } 73 }
74 74
75 75
76 static BOOL dmGrowBufRealloc(DMGrowBuf *buf, const size_t nsize, const BOOL clear)
77 {
78 if ((buf->data = dmRealloc(buf->data, nsize)) == NULL)
79 return FALSE;
80
81 if (clear)
82 memset(buf->adata + buf->size, 0, nsize - buf->size);
83
84 buf->size = nsize;
85
86 return TRUE;
87 }
88
89
76 // 90 //
77 // Grow the buffer by "amount" bytes, but at least by buf->mingrow, 91 // Grow the buffer by "amount" bytes, but at least by buf->mingrow,
78 // if there is not enough space for at least that amount compared to 92 // if there is not enough space for at least that amount compared to
79 // current buffer "len". 93 // current buffer "len".
80 // 94 //
81 BOOL dmGrowBufGrow(DMGrowBuf *buf, const size_t amount) 95 BOOL dmGrowBufGrow(DMGrowBuf *buf, const size_t amount)
82 { 96 {
83 size_t grow = (amount > buf->mingrow) ? amount : buf->mingrow; 97 if (buf->data == NULL || buf->offs + buf->len + amount >= buf->size)
84 if (buf->data == NULL || buf->len + grow > buf->size) 98 {
85 { 99 size_t grow = (amount > buf->mingrow) ? amount : buf->mingrow;
86 size_t prev = buf->size; 100 if (!dmGrowBufRealloc(buf, buf->offs + buf->len + grow, TRUE))
87 buf->size += grow;
88 if ((buf->data = dmRealloc(buf->data, buf->size)) == NULL)
89 return FALSE; 101 return FALSE;
90
91 memset(buf->data + prev, 0, buf->size - prev);
92 } 102 }
93 103
94 return TRUE; 104 return TRUE;
95 } 105 }
96 106
99 // Grow the buffer if "nsize" is larger than the current buffer size. 109 // Grow the buffer if "nsize" is larger than the current buffer size.
100 // Buffer is enlarged to nsize + mingrow. 110 // Buffer is enlarged to nsize + mingrow.
101 // 111 //
102 BOOL dmGrowBufCheckGrow(DMGrowBuf *buf, const size_t nsize) 112 BOOL dmGrowBufCheckGrow(DMGrowBuf *buf, const size_t nsize)
103 { 113 {
104 if (buf->data == NULL || nsize > buf->size) 114 if (buf->data == NULL || buf->offs + nsize > buf->size)
105 { 115 {
106 size_t prev = buf->size; 116 if (!dmGrowBufRealloc(buf, buf->offs + nsize + buf->mingrow, TRUE))
107 buf->size = nsize + buf->mingrow;
108 if ((buf->data = dmRealloc(buf->data, buf->size)) == NULL)
109 return FALSE; 117 return FALSE;
110
111 memset(buf->data + prev, 0, buf->size - prev);
112 } 118 }
113 119
114 return TRUE; 120 return TRUE;
115 } 121 }
116 122
122 128
123 buf->size = buf->len; 129 buf->size = buf->len;
124 if (buf->len == 0) 130 if (buf->len == 0)
125 return DMERR_OK; 131 return DMERR_OK;
126 132
127 if ((buf->data = dmRealloc(buf->data, buf->len)) == NULL) 133 if (!dmGrowBufRealloc(buf, buf->size, FALSE))
128 return DMERR_MALLOC; 134 return DMERR_MALLOC;
129 135
130 return DMERR_OK; 136 return DMERR_OK;
131 } 137 }
132 138