Conditional asserts as public API

This commit is contained in:
Alex Kotov 2021-12-18 04:43:19 +05:00
parent ac31273c8d
commit ecba475c06
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
7 changed files with 58 additions and 64 deletions

View File

@ -49,22 +49,22 @@ AM_CONDITIONAL([ENABLE_LIBC_STRCPY], [test "$enable_libc_strcpy" = yes])
AM_CONDITIONAL([ENABLE_LIBC_STRLEN], [test "$enable_libc_strlen" = yes])
AM_CONDITIONAL([ENABLE_NULL_GUARD], [test "$enable_null_guard" = yes])
AS_IF([test "$host_cpu" = i386], [AC_DEFINE([ARCH_I386], [1], [architecture is i386])])
AS_IF([test "$host_cpu" = x86_64], [AC_DEFINE([ARCH_X86_64], [1], [architecture is x86_64])])
AS_IF([test "$host_cpu" = i386], [AC_DEFINE([ARCH_I386], [1], [architecture is i386])])
AS_IF([test "$host_cpu" = x86_64], [AC_DEFINE([ARCH_X86_64], [1], [architecture is x86_64])])
AS_IF([test "$enable_cmdline" != no], [AC_DEFINE([ENABLE_CMDLINE], [1], [enabled command line parser])])
AS_IF([test "$enable_console" != no], [AC_DEFINE([ENABLE_CONSOLE], [1], [enabled serial console])])
AS_IF([test "$enable_elf" != no], [AC_DEFINE([ENABLE_ELF], [1], [enabled ELF utils])])
AS_IF([test "$enable_multiboot2" != no], [AC_DEFINE([ENABLE_MULTIBOOT2], [1], [enabled Multiboot 2 information parser])])
AS_IF([test "$enable_pfa" != no], [AC_DEFINE([ENABLE_PFA], [1], [enabled Page Frame Allocator])])
AS_IF([test "$enable_units", != no], [AC_DEFINE([ENABLE_UNITS], [1], [enabled measurement units utils])])
AS_IF([test "$enable_cmdline" != no], [AC_DEFINE([ENABLE_CMDLINE], [1], [enabled command line parser])])
AS_IF([test "$enable_console" != no], [AC_DEFINE([ENABLE_CONSOLE], [1], [enabled serial console])])
AS_IF([test "$enable_elf" != no], [AC_DEFINE([ENABLE_ELF], [1], [enabled ELF utils])])
AS_IF([test "$enable_multiboot2" != no], [AC_DEFINE([ENABLE_MULTIBOOT2], [1], [enabled Multiboot 2 information parser])])
AS_IF([test "$enable_pfa" != no], [AC_DEFINE([ENABLE_PFA], [1], [enabled Page Frame Allocator])])
AS_IF([test "$enable_units", != no], [AC_DEFINE([ENABLE_UNITS], [1], [enabled measurement units utils])])
AS_IF([test "$enable_assert" = yes], [AC_DEFINE([ENABLE_ASSERT], [1], [enabled runtime assertions])])
AS_IF([test "$enable_libc" = yes], [AC_DEFINE([ENABLE_LIBC], [1], [enabled libc replacement])])
AS_IF([test "$enable_libc_memset" = yes], [AC_DEFINE([ENABLE_LIBC_MEMSET], [1], [enabled memset replacement])])
AS_IF([test "$enable_libc_strcpy" = yes], [AC_DEFINE([ENABLE_LIBC_STRCPY], [1], [enabled strcpy replacement])])
AS_IF([test "$enable_libc_strlen" = yes], [AC_DEFINE([ENABLE_LIBC_STRLEN], [1], [enabled strlen replacement])])
AS_IF([test "$enable_null_guard" = yes], [AC_DEFINE([ENABLE_NULL_GUARD], [1], [enabled NULL-guard])])
AS_IF([test "$enable_assert" = yes], [AC_DEFINE([KERNAUX_ENABLE_ASSERT], [1], [enabled runtime assertions])])
AS_IF([test "$enable_libc" = yes], [AC_DEFINE([ENABLE_LIBC], [1], [enabled libc replacement])])
AS_IF([test "$enable_libc_memset" = yes], [AC_DEFINE([ENABLE_LIBC_MEMSET], [1], [enabled memset replacement])])
AS_IF([test "$enable_libc_strcpy" = yes], [AC_DEFINE([ENABLE_LIBC_STRCPY], [1], [enabled strcpy replacement])])
AS_IF([test "$enable_libc_strlen" = yes], [AC_DEFINE([ENABLE_LIBC_STRLEN], [1], [enabled strlen replacement])])
AS_IF([test "$enable_null_guard" = yes], [AC_DEFINE([KERNAUX_ENABLE_NULL_GUARD], [1], [enabled NULL-guard])])
AM_INIT_AUTOMAKE([1.9 subdir-objects -Wall -Werror])

View File

@ -1,3 +1,4 @@
#define KERNAUX_ENABLE_ASSERT
#include <kernaux/assert.h>
#include <assert.h>
@ -24,21 +25,21 @@ int main()
{
kernaux_assert_cb = assert_cb;
kernaux_assert(1 == 1);
KERNAUX_ASSERT(1 == 1);
assert(count == 0);
assert(last_file == NULL);
assert(last_line == 0);
assert(last_str == NULL);
kernaux_assert(1 != 1);
KERNAUX_ASSERT(1 != 1);
assert(count == 1);
assert(strcmp(last_file, __FILE__) == 0);
assert(last_line == __LINE__ - 4);
assert(strcmp(last_str, "1 != 1") == 0);
kernaux_assert(strcmp("qwe", "rty") == 0);
KERNAUX_ASSERT(strcmp("qwe", "rty") == 0);
assert(count == 2);
assert(strcmp(last_file, __FILE__) == 0);

View File

@ -5,8 +5,25 @@
extern "C" {
#endif
#define kernaux_assert(cond) \
#ifdef KERNAUX_ENABLE_ASSERT
#define KERNAUX_ASSERT(cond) \
((cond) ? (void)0 : kernaux_assert_do(__FILE__, __LINE__, #cond))
#else
#define KERNAUX_ASSERT(cond) ((void)sizeof((cond)))
#endif
#define KERNAUX_ASSERT_RETURN(cond) \
{ KERNAUX_ASSERT(cond); if (!(cond)) return; }
#define KERNAUX_ASSERT_RETVAL(cond, val) \
{ KERNAUX_ASSERT(cond); if (!(cond)) return (val); }
#ifdef KERNAUX_ENABLE_NULL_GUARD
#define KERNAUX_NOTNULL_RETURN(cond) { KERNAUX_ASSERT_RETURN(cond); }
#define KERNAUX_NOTNULL_RETVAL(cond, val) { KERNAUX_ASSERT_RETVAL(cond, val); }
#else
#define KERNAUX_NOTNULL_RETURN(cond) { KERNAUX_ASSERT(cond); }
#define KERNAUX_NOTNULL_RETVAL(cond, val) { KERNAUX_ASSERT(cond); }
#endif
void kernaux_assert_do(const char *file, int line, const char *str);

View File

@ -9,20 +9,20 @@
void (*kernaux_assert_cb)(const char *file, int line, const char *str) = NULL;
void kernaux_assert_do(
#ifndef ENABLE_ASSERT
#ifndef KERNAUX_ENABLE_ASSERT
__attribute__((unused))
#endif
const char *const file,
#ifndef ENABLE_ASSERT
#ifndef KERNAUX_ENABLE_ASSERT
__attribute__((unused))
#endif
const int line,
#ifndef ENABLE_ASSERT
#ifndef KERNAUX_ENABLE_ASSERT
__attribute__((unused))
#endif
const char *const str
) {
#ifdef ENABLE_ASSERT
#ifdef KERNAUX_ENABLE_ASSERT
if (kernaux_assert_cb) kernaux_assert_cb(file, line, str);
#endif
}

View File

@ -1,23 +0,0 @@
#ifndef _ASSERT_H
#define _ASSERT_H
#include <kernaux/assert.h>
#ifdef ENABLE_ASSERT
#define assert(cond) kernaux_assert(cond)
#else
#define assert(cond) ((void)sizeof((cond)))
#endif
#define assert_return(cond) { assert(cond); if (!(cond)) return; }
#define assert_retval(cond, val) { assert(cond); if (!(cond)) return (val); }
#ifdef ENABLE_NULL_GUARD
#define notnull_return(cond) { assert_return(cond); }
#define notnull_retval(cond, val) { assert_retval(cond, val); }
#else
#define notnull_return(cond) { assert(cond); }
#define notnull_retval(cond, val) { assert(cond); }
#endif
#endif

View File

@ -2,11 +2,10 @@
#include "config.h"
#endif
#include <kernaux/assert.h>
#include <kernaux/libc.h>
#include <kernaux/pfa.h>
#include "assert.h"
#define PAGE_INDEX(page_addr) ((page_addr) / KERNAUX_PFA_PAGE_SIZE)
#define FLAG_INDEX_FROM_INDEX(page_index) ((page_index) / 8)
@ -40,14 +39,14 @@ static void KernAux_PFA_mark(
void KernAux_PFA_initialize(const KernAux_PFA pfa)
{
notnull_return(pfa);
KERNAUX_NOTNULL_RETURN(pfa);
memset(pfa->flags, 0, sizeof(pfa->flags));
}
bool KernAux_PFA_is_available(const KernAux_PFA pfa, const size_t page_addr)
{
notnull_retval(pfa, false);
assert_retval(page_addr % KERNAUX_PFA_PAGE_SIZE == 0, false);
KERNAUX_NOTNULL_RETVAL(pfa, false);
KERNAUX_ASSERT_RETVAL(page_addr % KERNAUX_PFA_PAGE_SIZE == 0, false);
return GET_FLAG_FROM_ADDR(pfa, page_addr);
}
@ -74,8 +73,8 @@ void KernAux_PFA_mark(
size_t start,
size_t end
) {
notnull_return(pfa);
assert_return(start < end);
KERNAUX_NOTNULL_RETURN(pfa);
KERNAUX_ASSERT_RETURN(start < end);
const size_t start_rem = start % KERNAUX_PFA_PAGE_SIZE;
const size_t end_rem = (end + 1) % KERNAUX_PFA_PAGE_SIZE;
@ -102,7 +101,7 @@ void KernAux_PFA_mark(
size_t KernAux_PFA_alloc_pages(const KernAux_PFA pfa, size_t mem_size)
{
notnull_retval(pfa, 0);
KERNAUX_NOTNULL_RETVAL(pfa, 0);
const size_t mem_rem = mem_size % KERNAUX_PFA_PAGE_SIZE;
@ -141,8 +140,8 @@ void KernAux_PFA_free_pages(
const size_t page_addr,
size_t mem_size
) {
notnull_return(pfa);
assert_return(page_addr % KERNAUX_PFA_PAGE_SIZE == 0);
KERNAUX_NOTNULL_RETURN(pfa);
KERNAUX_ASSERT_RETURN(page_addr % KERNAUX_PFA_PAGE_SIZE == 0);
const size_t mem_rem = mem_size % KERNAUX_PFA_PAGE_SIZE;

View File

@ -12,7 +12,7 @@ static unsigned int count = 0;
static void test();
#ifdef ENABLE_ASSERT
#ifdef KERNAUX_ENABLE_ASSERT
static void assert_cb(
__attribute__((unused))
const char *const file,
@ -30,8 +30,8 @@ int main()
kernaux_assert_cb = NULL;
test();
#if defined(ENABLE_ASSERT) || defined(ENABLE_NULL_GUARD)
#ifdef ENABLE_ASSERT
#if defined(KERNAUX_ENABLE_ASSERT) || defined(KERNAUX_ENABLE_NULL_GUARD)
#ifdef KERNAUX_ENABLE_ASSERT
kernaux_assert_cb = assert_cb;
#endif
test();
@ -46,12 +46,12 @@ void test()
struct KernAux_PFA pfa;
KernAux_PFA_initialize(&pfa);
#ifdef ENABLE_NULL_GUARD
#ifdef KERNAUX_ENABLE_NULL_GUARD
KernAux_PFA_initialize(NULL);
if (kernaux_assert_cb) assert(count == ++acc);
#endif
#ifdef ENABLE_NULL_GUARD
#ifdef KERNAUX_ENABLE_NULL_GUARD
assert(!KernAux_PFA_is_available(NULL, KERNAUX_PFA_PAGE_SIZE));
if (kernaux_assert_cb) assert(count == ++acc);
#endif
@ -59,7 +59,7 @@ void test()
assert(!KernAux_PFA_is_available(&pfa, 123));
if (kernaux_assert_cb) assert(count == ++acc);
#ifdef ENABLE_NULL_GUARD
#ifdef KERNAUX_ENABLE_NULL_GUARD
KernAux_PFA_mark_available(NULL, 0, KERNAUX_PFA_PAGE_SIZE);
if (kernaux_assert_cb) assert(count == ++acc);
#endif
@ -67,7 +67,7 @@ void test()
KernAux_PFA_mark_available(&pfa, KERNAUX_PFA_PAGE_SIZE, 0);
if (kernaux_assert_cb) assert(count == ++acc);
#ifdef ENABLE_NULL_GUARD
#ifdef KERNAUX_ENABLE_NULL_GUARD
KernAux_PFA_mark_unavailable(NULL, 0, KERNAUX_PFA_PAGE_SIZE);
if (kernaux_assert_cb) assert(count == ++acc);
#endif
@ -75,12 +75,12 @@ void test()
KernAux_PFA_mark_unavailable(&pfa, KERNAUX_PFA_PAGE_SIZE, 0);
if (kernaux_assert_cb) assert(count == ++acc);
#ifdef ENABLE_NULL_GUARD
#ifdef KERNAUX_ENABLE_NULL_GUARD
assert(KernAux_PFA_alloc_pages(NULL, KERNAUX_PFA_PAGE_SIZE) == 0);
if (kernaux_assert_cb) assert(count == ++acc);
#endif
#ifdef ENABLE_NULL_GUARD
#ifdef KERNAUX_ENABLE_NULL_GUARD
KernAux_PFA_free_pages(NULL, KERNAUX_PFA_PAGE_SIZE, KERNAUX_PFA_PAGE_SIZE);
if (kernaux_assert_cb) assert(count == ++acc);
#endif