Mercurial > hg > dmlib
comparison src/dmgrowbuf.c @ 1747:5e928618fdc8
Change DMGrowBuf API somewhat and implement more copy operations.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 11 Jun 2018 13:57:07 +0300 |
parents | e40227e994e2 |
children | 22b6fa1a2ee4 |
comparison
equal
deleted
inserted
replaced
1746:dd57dd9430cb | 1747:5e928618fdc8 |
---|---|
40 | 40 |
41 buf->len = 0; | 41 buf->len = 0; |
42 buf->offs = 0; | 42 buf->offs = 0; |
43 buf->size = initial; | 43 buf->size = initial; |
44 buf->mingrow = mingrow; | 44 buf->mingrow = mingrow; |
45 buf->allocated = FALSE; | |
46 buf->backwards = FALSE; | 45 buf->backwards = FALSE; |
47 buf->is_const = FALSE; | 46 buf->is_const = FALSE; |
48 | 47 |
49 // Allocate the data | 48 // Allocate the data |
50 if ((buf->data = dmMalloc0(initial)) == NULL) | 49 if ((buf->data = dmMalloc0(initial)) == NULL) |
52 | 51 |
53 return DMERR_OK; | 52 return DMERR_OK; |
54 } | 53 } |
55 | 54 |
56 | 55 |
57 int dmGrowBufNew(DMGrowBuf **pbuf, const size_t initial, const size_t mingrow) | |
58 { | |
59 int res; | |
60 if (pbuf == NULL) | |
61 return DMERR_NULLPTR; | |
62 | |
63 DM_DBG("dmGrowBufNew(%p, %" DM_PRIu_SIZE_T ", %" DM_PRIu_SIZE_T ")\n", | |
64 pbuf, initial, mingrow); | |
65 | |
66 if ((*pbuf = dmMalloc0(sizeof(DMGrowBuf))) == NULL) | |
67 return DMERR_MALLOC; | |
68 | |
69 if ((res = dmGrowBufAlloc(*pbuf, initial, mingrow)) != DMERR_OK) | |
70 { | |
71 // The "allocated" flag has not yet been set | |
72 dmGrowBufFree(*pbuf); | |
73 | |
74 // .. thus free the allocated struct here | |
75 dmFreeR(pbuf); | |
76 return res; | |
77 } | |
78 | |
79 (*pbuf)->allocated = TRUE; | |
80 return res; | |
81 } | |
82 | |
83 | |
84 void dmGrowBufFree(DMGrowBuf *buf) | 56 void dmGrowBufFree(DMGrowBuf *buf) |
85 { | 57 { |
86 DM_DBG("dmGrowBufFree(%p)\n", buf); | 58 DM_DBG("dmGrowBufFree(%p)\n", buf); |
87 if (buf != NULL) | 59 if (buf != NULL) |
88 { | 60 { |
89 DM_DBG( | 61 DM_DBG( |
90 " buf->data = %p\n" | 62 " buf->data = %p\n" |
91 " buf->allocated = %s\n" | |
92 " buf->is_const = %s\n", | 63 " buf->is_const = %s\n", |
93 buf->data, | 64 buf->data, |
94 buf->allocated ? "YES" : "NO", | |
95 buf->is_const ? "YES" : "NO"); | 65 buf->is_const ? "YES" : "NO"); |
96 | 66 |
97 if (buf->is_const) | 67 if (buf->is_const) |
98 return; | 68 return; |
99 | 69 |
100 dmFreeR(&buf->data); | 70 dmFreeR(&buf->data); |
101 | 71 } |
102 if (buf->allocated) | 72 } |
103 dmFree(buf); | 73 |
104 } | 74 |
75 DMGrowBuf * dmGrowBufCopy(DMGrowBuf *dst, const DMGrowBuf *src, const size_t enlarge) | |
76 { | |
77 if (dst == NULL) | |
78 return NULL; | |
79 | |
80 DM_DBG("dmGrowBufCopy(dst=%p, src=%p, enlarge=" DM_PRIu_SIZE_T "):\n" | |
81 " data=%p, size=%" DM_PRIu_SIZE_T ", len=%" DM_PRIu_SIZE_T ", offs=%" DM_PRIu_SIZE_T "\n", | |
82 dst, src, enlarge, src->data, src->size, src->len, src->offs); | |
83 | |
84 // Copy the struct here | |
85 memcpy(dst, src, sizeof(DMGrowBuf)); | |
86 dst->size += enlarge; | |
87 | |
88 // Allocate new memory for the data | |
89 if ((dst->data = dmMalloc(dst->size)) == NULL) | |
90 { | |
91 // If that fails, clear the struct | |
92 memset(dst, 0, sizeof(DMGrowBuf)); | |
93 return NULL; | |
94 } | |
95 | |
96 // Copy data | |
97 memcpy(dst->data, src->data, src->size); | |
98 if (enlarge > 0) | |
99 memset(dst->data + src->size, 0, enlarge); | |
100 | |
101 // And reset some struct information | |
102 dst->is_const = FALSE; | |
103 | |
104 return dst; | |
105 } | 105 } |
106 | 106 |
107 | 107 |
108 DMGrowBuf * dmGrowBufConstCopy(DMGrowBuf *dst, const DMGrowBuf *src) | 108 DMGrowBuf * dmGrowBufConstCopy(DMGrowBuf *dst, const DMGrowBuf *src) |
109 { | 109 { |
119 | 119 |
120 return dst; | 120 return dst; |
121 } | 121 } |
122 | 122 |
123 | 123 |
124 DMGrowBuf * dmGrowBufCreateFrom(DMGrowBuf *dst, Uint8 *data, size_t len) | 124 DMGrowBuf * dmGrowBufConstCreateFrom(DMGrowBuf *dst, Uint8 *data, size_t len) |
125 { | 125 { |
126 if (dmGrowBufInit(dst) != DMERR_OK) | 126 if (dmGrowBufInit(dst) != DMERR_OK) |
127 return NULL; | 127 return NULL; |
128 | 128 |
129 DM_DBG("dmGrowBufCreateFrom(dst=%p, data=%p, len=%" DM_PRIu_SIZE_T ")\n", | 129 DM_DBG("dmGrowBufConstCreateFrom(dst=%p, data=%p, len=%" DM_PRIu_SIZE_T ")\n", |
130 dst, data, len); | 130 dst, data, len); |
131 | 131 |
132 dst->data = data; | 132 dst->data = data; |
133 dst->len = dst->size = len; | 133 dst->len = dst->size = len; |
134 dst->is_const = TRUE; | 134 dst->is_const = TRUE; |
135 | 135 |
136 return dst; | 136 return dst; |
137 } | |
138 | |
139 | |
140 DMGrowBuf * dmGrowBufConstCopyOffs(DMGrowBuf *dst, const DMGrowBuf *src, const size_t offs) | |
141 { | |
142 return dmGrowBufConstCreateFrom(dst, src->data + offs, src->len - offs); | |
143 } | |
144 | |
145 | |
146 DMGrowBuf * dmGrowBufCopyOffs(DMGrowBuf *dst, const DMGrowBuf *src, const size_t offs, const size_t enlarge) | |
147 { | |
148 DMGrowBuf tmp; | |
149 return dmGrowBufCopy(dst, | |
150 dmGrowBufConstCreateFrom(&tmp, src->data + offs, src->len - offs), enlarge); | |
137 } | 151 } |
138 | 152 |
139 | 153 |
140 static BOOL dmGrowBufRealloc(DMGrowBuf *buf, const size_t nsize, const BOOL clear) | 154 static BOOL dmGrowBufRealloc(DMGrowBuf *buf, const size_t nsize, const BOOL clear) |
141 { | 155 { |