77 return -1; |
77 return -1; |
78 } |
78 } |
79 } |
79 } |
80 |
80 |
81 int flib_vector_resize(flib_vector *vec, size_t newSize) { |
81 int flib_vector_resize(flib_vector *vec, size_t newSize) { |
82 if(!vec) { |
82 if(log_badargs_if(vec==NULL)) { |
83 flib_log_e("null parameter in flib_vector_resize"); |
|
84 return -1; |
83 return -1; |
85 } |
84 } |
86 |
85 |
87 if(vec->capacity < newSize) { |
86 if(vec->capacity < newSize) { |
88 // Resize exponentially for constant amortized time, |
87 // Resize exponentially for constant amortized time, |
111 return -1; |
110 return -1; |
112 } |
111 } |
113 } |
112 } |
114 |
113 |
115 int flib_vector_append(flib_vector *vec, const void *data, size_t len) { |
114 int flib_vector_append(flib_vector *vec, const void *data, size_t len) { |
116 if(!log_badparams_if(!vec || (!data && len>0)) |
115 if(!log_badargs_if2(vec==NULL, data==NULL && len>0) |
117 && !log_oom_if(len > SIZE_MAX-vec->size)) { |
116 && !log_oom_if(len > SIZE_MAX-vec->size)) { |
118 size_t oldSize = vec->size; |
117 size_t oldSize = vec->size; |
119 if(!log_oom_if(flib_vector_resize(vec, vec->size+len))) { |
118 if(!log_oom_if(flib_vector_resize(vec, vec->size+len))) { |
120 memmove(((uint8_t*)vec->data) + oldSize, data, len); |
119 memmove(((uint8_t*)vec->data) + oldSize, data, len); |
121 return 0; |
120 return 0; |
140 } |
139 } |
141 return result; |
140 return result; |
142 } |
141 } |
143 |
142 |
144 flib_buffer flib_vector_as_buffer(flib_vector *vec) { |
143 flib_buffer flib_vector_as_buffer(flib_vector *vec) { |
145 if(!vec) { |
144 if(log_badargs_if(vec==NULL)) { |
146 flib_log_e("null parameter in flib_vector_as_buffer"); |
|
147 flib_buffer result = {NULL, 0}; |
145 flib_buffer result = {NULL, 0}; |
148 return result; |
146 return result; |
149 } else { |
147 } else { |
150 flib_buffer result = {vec->data, vec->size}; |
148 flib_buffer result = {vec->data, vec->size}; |
151 return result; |
149 return result; |
152 } |
150 } |
153 } |
151 } |
154 |
152 |
155 flib_constbuffer flib_vector_as_constbuffer(flib_vector *vec) { |
153 flib_constbuffer flib_vector_as_constbuffer(flib_vector *vec) { |
156 if(!vec) { |
154 if(log_badargs_if(vec==NULL)) { |
157 flib_log_e("null parameter in flib_vector_as_constbuffer"); |
|
158 flib_constbuffer result = {NULL, 0}; |
155 flib_constbuffer result = {NULL, 0}; |
159 return result; |
156 return result; |
160 } else { |
157 } else { |
161 flib_constbuffer result = {vec->data, vec->size}; |
158 flib_constbuffer result = {vec->data, vec->size}; |
162 return result; |
159 return result; |
163 } |
160 } |
164 } |
161 } |
165 |
162 |
166 void *flib_vector_data(flib_vector *vec) { |
163 void *flib_vector_data(flib_vector *vec) { |
167 if(!vec) { |
164 if(log_badargs_if(vec==NULL)) { |
168 flib_log_e("null parameter in flib_vector_data"); |
|
169 return NULL; |
165 return NULL; |
170 } else { |
166 } else { |
171 return vec->data; |
167 return vec->data; |
172 } |
168 } |
173 } |
169 } |
174 |
170 |
175 size_t flib_vector_size(flib_vector *vec) { |
171 size_t flib_vector_size(flib_vector *vec) { |
176 if(!vec) { |
172 if(log_badargs_if(vec==NULL)) { |
177 flib_log_e("null parameter in flib_vector_size"); |
|
178 return 0; |
173 return 0; |
179 } else { |
174 } else { |
180 return vec->size; |
175 return vec->size; |
181 } |
176 } |
182 } |
177 } |