diff --git a/nasmlib.c b/nasmlib.c index fda0c7a5..2dd3a658 100644 --- a/nasmlib.c +++ b/nasmlib.c @@ -95,7 +95,7 @@ void nasm_set_malloc_error(efunc error) } #ifdef LOGALLOC -void *nasm_malloc_log(char *file, int line, size_t size) +void *nasm_malloc_log(const char *file, int line, size_t size) #else void *nasm_malloc(size_t size) #endif @@ -112,7 +112,7 @@ void *nasm_malloc(size_t size) } #ifdef LOGALLOC -void *nasm_zalloc_log(char *file, int line, size_t size) +void *nasm_zalloc_log(const char *file, int line, size_t size) #else void *nasm_zalloc(size_t size) #endif @@ -129,7 +129,7 @@ void *nasm_zalloc(size_t size) } #ifdef LOGALLOC -void *nasm_realloc_log(char *file, int line, void *q, size_t size) +void *nasm_realloc_log(const char *file, int line, void *q, size_t size) #else void *nasm_realloc(void *q, size_t size) #endif @@ -149,7 +149,7 @@ void *nasm_realloc(void *q, size_t size) } #ifdef LOGALLOC -void nasm_free_log(char *file, int line, void *q) +void nasm_free_log(const char *file, int line, void *q) #else void nasm_free(void *q) #endif @@ -163,7 +163,7 @@ void nasm_free(void *q) } #ifdef LOGALLOC -char *nasm_strdup_log(char *file, int line, const char *s) +char *nasm_strdup_log(const char *file, int line, const char *s) #else char *nasm_strdup(const char *s) #endif @@ -184,9 +184,9 @@ char *nasm_strdup(const char *s) } #ifdef LOGALLOC -char *nasm_strndup_log(char *file, int line, char *s, size_t len) +char *nasm_strndup_log(const char *file, int line, const char *s, size_t len) #else -char *nasm_strndup(char *s, size_t len) +char *nasm_strndup(const char *s, size_t len) #endif { char *p; @@ -205,6 +205,13 @@ char *nasm_strndup(char *s, size_t len) return p; } +noreturn nasm_assert_failed(const char *file, int line, const char *msg) +{ + nasm_malloc_error(ERR_FATAL, "assertion %s failed at %s:%d", + msg, file, line); + exit(1); +} + #ifndef nasm_stricmp int nasm_stricmp(const char *s1, const char *s2) { diff --git a/nasmlib.h b/nasmlib.h index 995c9612..6776c91d 100644 --- a/nasmlib.h +++ b/nasmlib.h @@ -152,14 +152,14 @@ void *nasm_zalloc(size_t); void *nasm_realloc(void *, size_t); void nasm_free(void *); char *nasm_strdup(const char *); -char *nasm_strndup(char *, size_t); +char *nasm_strndup(const char *, size_t); #else -void *nasm_malloc_log(char *, int, size_t); -void *nasm_zalloc_log(char *, int, size_t); -void *nasm_realloc_log(char *, int, void *, size_t); -void nasm_free_log(char *, int, void *); -char *nasm_strdup_log(char *, int, const char *); -char *nasm_strndup_log(char *, int, char *, size_t); +void *nasm_malloc_log(const char *, int, size_t); +void *nasm_zalloc_log(const char *, int, size_t); +void *nasm_realloc_log(const char *, int, void *, size_t); +void nasm_free_log(const char *, int, void *); +char *nasm_strdup_log(const char *, int, const char *); +char *nasm_strndup_log(const char *, int, const char *, size_t); #define nasm_malloc(x) nasm_malloc_log(__FILE__,__LINE__,x) #define nasm_zalloc(x) nasm_zalloc_log(__FILE__,__LINE__,x) #define nasm_realloc(x,y) nasm_realloc_log(__FILE__,__LINE__,x,y) @@ -168,6 +168,13 @@ char *nasm_strndup_log(char *, int, char *, size_t); #define nasm_strndup(x,y) nasm_strndup_log(__FILE__,__LINE__,x,y) #endif +/* + * NASM assert failure + */ +noreturn nasm_assert_failed(const char *, int, const char *); +#define nasm_assert(x) \ + do { if (!(x)) nasm_assert_failed(__FILE__,__LINE__,#x); } while (0) + /* * ANSI doesn't guarantee the presence of `stricmp' or * `strcasecmp'.