History

misc/mp_assert: add mp_assert and mp_require macros

Kacper Michajłow committed 1 year ago

Commit c0dbdcf1 parent 16bbfbf1 file · +57 −0Browse files

Unified · Split

misc/mp_assert.h+57−0
@@ -0,0 +1,57 @@
1/*
2 * This file is part of mpv.
3 *
4 * mpv is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * mpv is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with mpv. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20/**
21 * @brief This file provides two macros, mp_assert and mp_require, used to check
22 * for conditions that should never occur.
23 *
24 * The mp_assert macro is a convenience wrapper around the standard assert,
25 * which also sinks unused variables to prevent compiler warnings. It should be
26 * used for debugging purposes only to check for conditions that should never
27 * occur. This macro can be disabled in release builds by defining NDEBUG and
28 * should not be used for error handling.
29 *
30 * The mp_require macro is similar to mp_assert but more strict. It enforces
31 * critical assertions even in release builds. If the condition is not met, this
32 * macro will abort the program, making it suitable for error handling in cases
33 * where recovery is impossible. Note that this macro does not log any messages
34 * to keep as small footprint as possible in NDEBUG builds.
35 */
36
37#ifndef NDEBUG
38
39#include <assert.h>
40
41#define mp_assert assert
42#define mp_require assert
43
44#else
45
46#include <stdio.h>
47#include <stdlib.h>
48
49#define mp_require(expr) \
50 do { \
51 if (!(expr)) \
52 abort(); \
53 } while (0)
54
55#define mp_assert(expr) do { (void)sizeof(expr);} while (0)
56
57#endif

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t