project_files/frontlib/util/buffer.c
author Medo <smaxein@googlemail.com>
Tue, 12 Jun 2012 11:25:05 +0200
changeset 7224 5143861c83bd
parent 7179 f84805e6df03
child 7234 613998625a3c
permissions -rw-r--r--
Cleanup, refactoring and generally more development in the frontlib
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
#include "buffer.h"
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     2
#include "logging.h"
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
     3
#include "util.h"
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     4
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     5
#include <stdlib.h>
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     6
#include <limits.h>
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     7
#include <string.h>
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     8
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
     9
#define MIN_VECTOR_CAPACITY 16
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    10
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
typedef struct _flib_vector {
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
	void *data;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
	size_t size;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
	size_t capacity;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
} _flib_vector;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    17
flib_vector *flib_vector_create() {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    18
	flib_vector *result = NULL;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    19
	flib_vector *tmpVector = flib_calloc(1, sizeof(_flib_vector));
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    20
	if(tmpVector) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    21
		tmpVector->data = flib_malloc(MIN_VECTOR_CAPACITY);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    22
		if(tmpVector->data) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    23
			tmpVector->size = 0;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    24
			tmpVector->capacity = MIN_VECTOR_CAPACITY;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    25
			result = tmpVector;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    26
			tmpVector = NULL;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    27
		}
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
	}
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    29
	flib_vector_destroy(tmpVector);
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
	return result;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
void flib_vector_destroy(flib_vector *vec) {
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    34
	if(vec) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    35
		free(vec->data);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    36
		free(vec);
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
	}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    40
static void try_realloc(flib_vector *vec, size_t newCapacity) {
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
	void *newData = realloc(vec->data, newCapacity);
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
	if(newData) {
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
		vec->data = newData;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
		vec->capacity = newCapacity;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
	}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    48
static size_t getFreeCapacity(flib_vector *vec) {
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
	return vec->capacity - vec->size;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    52
int flib_vector_append(flib_vector *vec, const void *data, size_t len) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    53
	if(!vec) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    54
		flib_log_e("null parameter in flib_vector_append");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    55
		return 0;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    56
	}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    57
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
	if(getFreeCapacity(vec) < len) {
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
		// Resize exponentially for constant amortized time,
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
		// But at least by as much as we need of course,
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    61
		// and be extra careful with integer overflows...
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
		size_t extraCapacity = (vec->capacity)/2;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    63
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
		size_t minExtraCapacity = len - getFreeCapacity(vec);
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
		if(extraCapacity < minExtraCapacity) {
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
			extraCapacity = minExtraCapacity;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    67
		}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    68
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    69
		if(extraCapacity <= SIZE_MAX - vec->capacity) {
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    70
			try_realloc(vec, vec->capacity+extraCapacity);
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    71
		}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    73
		// Check if we were able to resize.
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    74
		// If not, try to allocate at least what we need.
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    75
		if(getFreeCapacity(vec) < len) {
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    76
			try_realloc(vec, vec->capacity+minExtraCapacity);
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
			// Still not working? Then we fail.
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
			if(getFreeCapacity(vec) < len) {
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    80
				return 0;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    81
			}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    82
		}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    83
	}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    84
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    85
	memmove(((uint8_t*)vec->data) + vec->size, data, len);
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    86
	vec->size += len;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    87
	return len;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    88
}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    89
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    90
flib_buffer flib_vector_as_buffer(flib_vector *vec) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    91
	if(!vec) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    92
		flib_log_e("null parameter in flib_vector_as_buffer");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    93
		flib_buffer result = {NULL, 0};
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    94
		return result;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    95
	} else {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    96
		flib_buffer result = {vec->data, vec->size};
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    97
		return result;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
    98
	}
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    99
}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   100
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   101
flib_constbuffer flib_vector_as_constbuffer(flib_vector *vec) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   102
	if(!vec) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   103
		flib_log_e("null parameter in flib_vector_as_constbuffer");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   104
		flib_constbuffer result = {NULL, 0};
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   105
		return result;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   106
	} else {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   107
		flib_constbuffer result = {vec->data, vec->size};
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   108
		return result;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   109
	}
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   110
}
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   111
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   112
void *flib_vector_data(flib_vector *vec) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   113
	if(!vec) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   114
		flib_log_e("null parameter in flib_vector_data");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   115
		return NULL;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   116
	} else {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   117
		return vec->data;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   118
	}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   119
}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   120
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   121
size_t flib_vector_size(flib_vector *vec) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   122
	if(!vec) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   123
		flib_log_e("null parameter in flib_vector_size");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   124
		return 0;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   125
	} else {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   126
		return vec->size;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   127
	}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   128
}