diff options
| author | Tyge Løvset <[email protected]> | 2022-01-06 11:00:11 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-01-06 11:00:11 +0100 |
| commit | 6de373f4accc3e044a04ba25c05e62897fc46706 (patch) | |
| tree | 13bb1896dc3ca2d301c44cbb31f7c6e2e194dba1 | |
| parent | ead3df255f79f60257ae9626e7d52838de3caa67 (diff) | |
| download | STC-modified-6de373f4accc3e044a04ba25c05e62897fc46706.tar.gz STC-modified-6de373f4accc3e044a04ba25c05e62897fc46706.zip | |
Modified c_autovar() macro to accept var of incomplete pointer type.
| -rw-r--r-- | docs/ccommon_api.md | 13 | ||||
| -rw-r--r-- | include/stc/ccommon.h | 2 |
2 files changed, 12 insertions, 3 deletions
diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 9fc33dc1..46e25737 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -17,6 +17,15 @@ resource where the resource acquisition takes place. Makes it easier to verify t For multiple variables, use either multiple **c_autovar** in sequence, or declare variable outside scope and use **c_autoscope**. Also, **c_auto** support up to 4 variables. ```c +c_autovar (uint8_t* buf = c_alloc_n(uint8_t, N), c_free(buf)) +c_autovar (FILE* f = fopen(fname, "rb"), fclose(f)) +{ + int n = 0; + if (f && buf) + n = fread(buf, 1, N, f); + if (n > 0) doSomething(buf, n); +} + c_autovar (cstr s = cstr_new("Hello"), cstr_drop(&s)) { cstr_append(&s, " world"); @@ -74,8 +83,8 @@ int main() } ``` ### The checkauto utility program (for RAII) -The **checkauto** program will check the source code for any misuses of the `c_auto*` macros that -will lead to resource leakages. The `c_auto*`- macros are implemented as one-time executed **for-loops**, +The **checkauto** program will check the source code for any misuses of the `c_auto*` macros which +may lead to resource leakages. The `c_auto*`- macros are implemented as one-time executed **for-loops**, so any `return` or `break` appearing within such a block will lead to resource leaks, as it will disable the cleanup/drop method to be called. However, a `break` may (originally) been intended to break an immediate loop/switch outside the `c_auto` scope, so it would not work as intended in any case. The **checkauto** diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 2b1f38a2..22caf0ef 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -159,7 +159,7 @@ STC_INLINE uint64_t c_hash64(const void* key, size_t len) { for (type i=start, _c_inc=step, _c_end=(stop) - (0 < _c_inc) \
; (i <= _c_end) == (0 < _c_inc); i += _c_inc)
-#define c_autovar(declvar, ...) for (declvar, *_c_ii = NULL; !_c_ii; ++_c_ii, __VA_ARGS__)
+#define c_autovar(declvar, ...) for (declvar, **_c_ii = NULL; !_c_ii; ++_c_ii, __VA_ARGS__)
#define c_autoscope(init, ...) for (int _c_ii = (init, 0); !_c_ii; ++_c_ii, __VA_ARGS__)
#define c_autodefer(...) for (int _c_ii = 0; !_c_ii; ++_c_ii, __VA_ARGS__)
#define c_breakauto continue
|
