diff options
Diffstat (limited to 'util')
| -rw-r--r-- | util/array.c | 40 | ||||
| -rw-r--r-- | util/env.c | 38 | ||||
| -rw-r--r-- | util/meson.build | 5 | ||||
| -rw-r--r-- | util/time.c | 32 | 
4 files changed, 115 insertions, 0 deletions
| diff --git a/util/array.c b/util/array.c new file mode 100644 index 0000000..ec16a7b --- /dev/null +++ b/util/array.c @@ -0,0 +1,40 @@ +#include "util/array.h" +#include <assert.h> +#include <string.h> + +void array_remove_at(struct wl_array *arr, size_t offset, size_t size) { +	assert(arr->size >= offset + size); + +	char *data = arr->data; +	memmove(&data[offset], &data[offset + size], arr->size - offset - size); +	arr->size -= size; +} + +bool array_realloc(struct wl_array *arr, size_t size) { +	// If the size is less than 1/4th of the allocation size, we shrink it. +	// 1/4th is picked to provide hysteresis, without which an array with size +	// arr->alloc would constantly reallocate if an element is added and then +	// removed continously. +	size_t alloc; +	if (arr->alloc > 0 && size > arr->alloc / 4) { +		alloc = arr->alloc; +	} else { +		alloc = 16; +	} + +	while (alloc < size) { +		alloc *= 2; +	} + +	if (alloc == arr->alloc) { +		return true; +	} + +	void *data = realloc(arr->data, alloc); +	if (data == NULL) { +		return false; +	} +	arr->data = data; +	arr->alloc = alloc; +	return true; +} diff --git a/util/env.c b/util/env.c new file mode 100644 index 0000000..b0a9efd --- /dev/null +++ b/util/env.c @@ -0,0 +1,38 @@ +#include <stdlib.h> +#include <string.h> +#include <wlr/util/log.h> +#include "util/env.h" + +bool env_parse_bool(const char *option) { +	const char *env = getenv(option); +	if (env) { +		wlr_log(WLR_INFO, "Loading %s option: %s", option, env); +	} + +	if (!env || strcmp(env, "0") == 0) { +		return false; +	} else if (strcmp(env, "1") == 0) { +		return true; +	} + +	wlr_log(WLR_ERROR, "Unknown %s option: %s", option, env); +	return false; +} + +ssize_t env_parse_switch(const char *option, const char **switches) { +	const char *env = getenv(option); +	if (env) { +		wlr_log(WLR_INFO, "Loading %s option: %s", option, env); +	} else { +		return 0; +	} + +	for (ssize_t i = 0; switches[i]; i++) { +		if (strcmp(env, switches[i]) == 0) { +			return i; +		} +	} + +	wlr_log(WLR_ERROR, "Unknown %s option: %s", option, env); +	return 0; +} diff --git a/util/meson.build b/util/meson.build new file mode 100644 index 0000000..b54835c --- /dev/null +++ b/util/meson.build @@ -0,0 +1,5 @@ +wlr_files += files( +	'array.c', +	'env.c', +	'time.c', +) diff --git a/util/time.c b/util/time.c new file mode 100644 index 0000000..06e42b4 --- /dev/null +++ b/util/time.c @@ -0,0 +1,32 @@ +#define _POSIX_C_SOURCE 200809L +#include <stdint.h> +#include <time.h> + +#include "util/time.h" + +static const long NSEC_PER_SEC = 1000000000; + +int64_t timespec_to_msec(const struct timespec *a) { +	return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000; +} + +void timespec_from_nsec(struct timespec *r, int64_t nsec) { +	r->tv_sec = nsec / NSEC_PER_SEC; +	r->tv_nsec = nsec % NSEC_PER_SEC; +} + +uint32_t get_current_time_msec(void) { +	struct timespec now; +	clock_gettime(CLOCK_MONOTONIC, &now); +	return timespec_to_msec(&now); +} + +void timespec_sub(struct timespec *r, const struct timespec *a, +		const struct timespec *b) { +	r->tv_sec = a->tv_sec - b->tv_sec; +	r->tv_nsec = a->tv_nsec - b->tv_nsec; +	if (r->tv_nsec < 0) { +		r->tv_sec--; +		r->tv_nsec += NSEC_PER_SEC; +	} +} | 
