feat: (common) next_pow_of_two, min, max

This commit is contained in:
thetek 2023-02-12 18:28:44 +01:00
parent 0732dc4717
commit d13172d535
2 changed files with 17 additions and 5 deletions

View File

@ -96,7 +96,7 @@ debug_free (void *ptr, const char *file, size_t line)
* automatically inserts filename and line.
*/
void *
debug_malloc (size_t size, const char *file, size_t line)
debug_smalloc (size_t size, const char *file, size_t line)
{
void *ptr;
@ -117,13 +117,11 @@ debug_malloc (size_t size, const char *file, size_t line)
*
* @return the pointer to the reallocated memory, or NULL in case of error
*
* @errno see realloc(3)
*
* @usage the header `debug.h` defines a macro `realloc(ptr, size)` that
* @usage the header `debug.h` defines a macro `srealloc(ptr, size)` that
* automatically inserts filename and line.
*/
void *
debug_realloc (void *ptr, size_t size, const char *file, size_t line)
debug_srealloc (void *ptr, size_t size, const char *file, size_t line)
{
void *new_ptr;
uintptr_t old_ptr;

View File

@ -1,6 +1,20 @@
#ifndef CUTILS_COMMON_H_
#define CUTILS_COMMON_H_
#include <stddef.h>
#include <stdint.h>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#if UINTPTR_MAX == 0xffffffffffffffff
# define next_pow_of_two(n) (((size_t) 1) << (64 - __builtin_clzll (n)))
#elif UINTPTR_MAX == 0xffffffff
# define next_pow_of_two(n) (((size_t) 1) << (64 - __builtin_clz (n)))
#else
# error unsupported architecture: size_t expected to be 64-bit or 32-bit.
#endif
void *smalloc (size_t size);
void *srealloc (void *ptr, size_t size);