diff options
| author | Edward Thomson <ethomson@edwardthomson.com> | 2022-02-27 09:20:58 -0500 |
|---|---|---|
| committer | Edward Thomson <ethomson@edwardthomson.com> | 2022-02-27 09:20:58 -0500 |
| commit | d9b041e6d7de9e2fc8fcdbb6e28acba45af4d888 (patch) | |
| tree | 6db5e99e79b945b4097b71bef49346f50c9c33c2 /src/util/assert_safe.h | |
| parent | e32db9f2b85685801fb249e35bf62a32ef09dc98 (diff) | |
| download | libgit2-d9b041e6d7de9e2fc8fcdbb6e28acba45af4d888.tar.gz | |
assert: add `ASSERT_WITH_CLEANUP`
Now that we safely assert and return, we may need to be in a place where
we need to unlock mutexes or cleanup resources. Provide
`ASSERT_WITH_CLEANUP` that permits for this behavior by taking a block.
Diffstat (limited to 'src/util/assert_safe.h')
| -rw-r--r-- | src/util/assert_safe.h | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/util/assert_safe.h b/src/util/assert_safe.h index 8c261100f..cc0bac551 100644 --- a/src/util/assert_safe.h +++ b/src/util/assert_safe.h @@ -24,6 +24,8 @@ # define GIT_ASSERT_WITH_RETVAL(expr, fail) assert(expr) # define GIT_ASSERT_ARG_WITH_RETVAL(expr, fail) assert(expr) + +# define GIT_ASSERT_WITH_CLEANUP(expr, cleanup) assert(expr) #else /** Internal consistency check to stop the function. */ @@ -53,6 +55,20 @@ } \ } while(0) +/** + * Go to to the given label on assertion failures; useful when you have + * taken a lock or otherwise need to release a resource. + */ +# define GIT_ASSERT_WITH_CLEANUP(expr, cleanup) \ + GIT_ASSERT__WITH_CLEANUP(expr, GIT_ERROR_INTERNAL, "unrecoverable internal error", cleanup) + +# define GIT_ASSERT__WITH_CLEANUP(expr, code, msg, cleanup) do { \ + if (!(expr)) { \ + git_error_set(code, "%s: '%s'", msg, #expr); \ + cleanup; \ + } \ + } while(0) + #endif /* GIT_ASSERT_HARD */ #endif |
