project_files/frontlib/util/buffer.c
changeset 7316 f7b49b2c5d84
parent 7314 6171f0bad318
child 10017 de822cd3df3a
equal deleted inserted replaced
7314:6171f0bad318 7316:f7b49b2c5d84
    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;
   124 	return -1;
   123 	return -1;
   125 }
   124 }
   126 
   125 
   127 int flib_vector_appendf(flib_vector *vec, const char *fmt, ...) {
   126 int flib_vector_appendf(flib_vector *vec, const char *fmt, ...) {
   128 	int result = -1;
   127 	int result = -1;
   129 	if(!log_badparams_if(!vec || !fmt)) {
   128 	if(!log_badargs_if2(vec==NULL, fmt==NULL)) {
   130 		va_list argp;
   129 		va_list argp;
   131 		va_start(argp, fmt);
   130 		va_start(argp, fmt);
   132 		char *formatted = flib_vasprintf(fmt, argp);
   131 		char *formatted = flib_vasprintf(fmt, argp);
   133 		va_end(argp);
   132 		va_end(argp);
   134 
   133 
   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 }