Mark report_allocation_failure as noreturn
Silence an static analyzer warning Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
parent
1c0770dc66
commit
311fa65840
149
src/utils.h
149
src/utils.h
@ -2,17 +2,17 @@
|
|||||||
// Copyright (c) 2018 Yuxuan Shui <yshuiv7@gmail.com>
|
// Copyright (c) 2018 Yuxuan Shui <yshuiv7@gmail.com>
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "compiler.h"
|
#include "compiler.h"
|
||||||
|
|
||||||
#define ARR_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
|
#define ARR_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
|
||||||
|
|
||||||
#ifdef __FAST_MATH__
|
#ifdef __FAST_MATH__
|
||||||
#warning Use of -ffast-math can cause rendering error or artifacts, \
|
#warning Use of -ffast-math can cause rendering error or artifacts, \
|
||||||
@ -24,8 +24,9 @@ __attribute__((optnone))
|
|||||||
#else
|
#else
|
||||||
__attribute__((optimize("-fno-fast-math")))
|
__attribute__((optimize("-fno-fast-math")))
|
||||||
#endif
|
#endif
|
||||||
static inline bool safe_isnan(double a) {
|
static inline bool
|
||||||
return isnan(a);
|
safe_isnan(double a) {
|
||||||
|
return isnan(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -36,43 +37,40 @@ static inline bool safe_isnan(double a) {
|
|||||||
* @param max maximum value
|
* @param max maximum value
|
||||||
* @return normalized value
|
* @return normalized value
|
||||||
*/
|
*/
|
||||||
static inline int attr_const
|
static inline int attr_const normalize_i_range(int i, int min, int max) {
|
||||||
normalize_i_range(int i, int min, int max) {
|
if (i > max)
|
||||||
if (i > max) return max;
|
return max;
|
||||||
if (i < min) return min;
|
if (i < min)
|
||||||
return i;
|
return min;
|
||||||
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Select the larger integer of two.
|
* Select the larger integer of two.
|
||||||
*/
|
*/
|
||||||
static inline int attr_const
|
static inline int attr_const max_i(int a, int b) {
|
||||||
max_i(int a, int b) {
|
return (a > b ? a : b);
|
||||||
return (a > b ? a : b);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Select the smaller integer of two.
|
* Select the smaller integer of two.
|
||||||
*/
|
*/
|
||||||
static inline int attr_const
|
static inline int attr_const min_i(int a, int b) {
|
||||||
min_i(int a, int b) {
|
return (a > b ? b : a);
|
||||||
return (a > b ? b : a);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Select the larger long integer of two.
|
* Select the larger long integer of two.
|
||||||
*/
|
*/
|
||||||
static inline long attr_const
|
static inline long attr_const max_l(long a, long b) {
|
||||||
max_l(long a, long b) {
|
return (a > b ? a : b);
|
||||||
return (a > b ? a : b);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Select the smaller long integer of two.
|
* Select the smaller long integer of two.
|
||||||
*/
|
*/
|
||||||
static inline long attr_const
|
static inline long attr_const min_l(long a, long b) {
|
||||||
min_l(long a, long b) {
|
return (a > b ? b : a);
|
||||||
return (a > b ? b : a);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -83,11 +81,12 @@ min_l(long a, long b) {
|
|||||||
* @param max maximum value
|
* @param max maximum value
|
||||||
* @return normalized value
|
* @return normalized value
|
||||||
*/
|
*/
|
||||||
static inline double attr_const
|
static inline double attr_const normalize_d_range(double d, double min, double max) {
|
||||||
normalize_d_range(double d, double min, double max) {
|
if (d > max)
|
||||||
if (d > max) return max;
|
return max;
|
||||||
if (d < min) return min;
|
if (d < min)
|
||||||
return d;
|
return min;
|
||||||
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -96,39 +95,39 @@ normalize_d_range(double d, double min, double max) {
|
|||||||
* @param d double value to normalize
|
* @param d double value to normalize
|
||||||
* @return normalized value
|
* @return normalized value
|
||||||
*/
|
*/
|
||||||
static inline double attr_const
|
static inline double attr_const normalize_d(double d) {
|
||||||
normalize_d(double d) {
|
return normalize_d_range(d, 0.0, 1.0);
|
||||||
return normalize_d_range(d, 0.0, 1.0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void report_allocation_failure(const char *func, const char *file, unsigned int line);
|
void attr_noret report_allocation_failure(const char *func, const char *file,
|
||||||
|
unsigned int line);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Quit if the passed-in pointer is empty.
|
* @brief Quit if the passed-in pointer is empty.
|
||||||
*/
|
*/
|
||||||
static inline void *
|
static inline void *
|
||||||
allocchk_(const char *func_name, const char *file, unsigned int line, void *ptr) {
|
allocchk_(const char *func_name, const char *file, unsigned int line, void *ptr) {
|
||||||
if (unlikely(!ptr)) {
|
if (unlikely(!ptr)) {
|
||||||
report_allocation_failure(func_name, file, line);
|
report_allocation_failure(func_name, file, line);
|
||||||
}
|
}
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief Wrapper of allocchk_().
|
/// @brief Wrapper of allocchk_().
|
||||||
#define allocchk(ptr) allocchk_(__func__, __FILE__, __LINE__, ptr)
|
#define allocchk(ptr) allocchk_(__func__, __FILE__, __LINE__, ptr)
|
||||||
|
|
||||||
/// @brief Wrapper of malloc().
|
/// @brief Wrapper of malloc().
|
||||||
#define cmalloc(type) ((type *) allocchk(malloc(sizeof(type))))
|
#define cmalloc(type) ((type *)allocchk(malloc(sizeof(type))))
|
||||||
|
|
||||||
/// @brief Wrapper of malloc() that takes a size
|
/// @brief Wrapper of malloc() that takes a size
|
||||||
#define cvalloc(size) allocchk(malloc(size))
|
#define cvalloc(size) allocchk(malloc(size))
|
||||||
|
|
||||||
/// @brief Wrapper of calloc().
|
/// @brief Wrapper of calloc().
|
||||||
#define ccalloc(nmemb, type) ((type *) allocchk(calloc((nmemb), sizeof(type))))
|
#define ccalloc(nmemb, type) ((type *)allocchk(calloc((nmemb), sizeof(type))))
|
||||||
|
|
||||||
/// @brief Wrapper of ealloc().
|
/// @brief Wrapper of ealloc().
|
||||||
#define crealloc(ptr, nmemb) \
|
#define crealloc(ptr, nmemb) \
|
||||||
((__typeof__(ptr)) allocchk(realloc((ptr), (nmemb) * sizeof(*(ptr)))))
|
((__typeof__(ptr))allocchk(realloc((ptr), (nmemb) * sizeof(*(ptr)))))
|
||||||
|
|
||||||
/// RC_TYPE generates a reference counted type from `type`
|
/// RC_TYPE generates a reference counted type from `type`
|
||||||
///
|
///
|
||||||
@ -148,38 +147,40 @@ allocchk_(const char *func_name, const char *file, unsigned int line, void *ptr)
|
|||||||
/// reference to the object
|
/// reference to the object
|
||||||
/// `name`_unref: decrement the reference counter. take a `type **`
|
/// `name`_unref: decrement the reference counter. take a `type **`
|
||||||
/// because it needs to nullify the reference.
|
/// because it needs to nullify the reference.
|
||||||
#define RC_TYPE(type, name, ctor, dtor, Q) \
|
#define RC_TYPE(type, name, ctor, dtor, Q) \
|
||||||
typedef struct { \
|
typedef struct { \
|
||||||
type inner; \
|
type inner; \
|
||||||
int ref_count; \
|
int ref_count; \
|
||||||
} name##_internal_t; \
|
} name##_internal_t; \
|
||||||
typedef type name##_t; \
|
typedef type name##_t; \
|
||||||
Q type *name##_new(void) { \
|
Q type *name##_new(void) { \
|
||||||
name##_internal_t *ret = cmalloc(name##_internal_t); \
|
name##_internal_t *ret = cmalloc(name##_internal_t); \
|
||||||
ctor((type *)ret); \
|
ctor((type *)ret); \
|
||||||
ret->ref_count = 1; \
|
ret->ref_count = 1; \
|
||||||
return (type *)ret; \
|
return (type *)ret; \
|
||||||
} \
|
} \
|
||||||
Q type *name##_ref(type *a) { \
|
Q type *name##_ref(type *a) { \
|
||||||
__auto_type b = (name##_internal_t *)a; \
|
__auto_type b = (name##_internal_t *)a; \
|
||||||
b->ref_count++; \
|
b->ref_count++; \
|
||||||
return a; \
|
return a; \
|
||||||
} \
|
} \
|
||||||
Q void name##_unref(type **a) { \
|
Q void name##_unref(type **a) { \
|
||||||
__auto_type b = (name##_internal_t *)*a; \
|
__auto_type b = (name##_internal_t *)*a; \
|
||||||
if (!b) \
|
if (!b) \
|
||||||
return; \
|
return; \
|
||||||
b->ref_count--; \
|
b->ref_count--; \
|
||||||
if (!b->ref_count) {\
|
if (!b->ref_count) { \
|
||||||
dtor((type *)b); \
|
dtor((type *)b); \
|
||||||
free(b); \
|
free(b); \
|
||||||
} \
|
} \
|
||||||
*a = NULL; \
|
*a = NULL; \
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generate prototypes for functions generated by RC_TYPE
|
/// Generate prototypes for functions generated by RC_TYPE
|
||||||
#define RC_TYPE_PROTO(type, name) \
|
#define RC_TYPE_PROTO(type, name) \
|
||||||
typedef type name##_t; \
|
typedef type name##_t; \
|
||||||
type *name##_new(void); \
|
type *name##_new(void); \
|
||||||
void name##_ref(type *a); \
|
void name##_ref(type *a); \
|
||||||
void name##_unref(type **a);
|
void name##_unref(type **a);
|
||||||
|
|
||||||
|
// vim: set noet sw=8 ts=8 :
|
||||||
|
Loading…
Reference in New Issue
Block a user