summaryrefslogtreecommitdiff
path: root/inc/cad/util.h
blob: 440d29bd0de9768cf84a422b37b8e6d429a8af31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once

#include <assert.h>
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>

static void resize_buffer(void **buffer, size_t *capacity, size_t elems, size_t elemSize) {
	assert(elems > 0);
	uint64_t newpower = (sizeof(elems) * CHAR_BIT) - __builtin_clzl(elems-1);
	elems = 1 << newpower;

	if(elems != *capacity) {
		*capacity = elems;
		*buffer = realloc(*buffer, *capacity * elemSize);
	}
}