diff options
author | Nick Mathewson <nickm@torproject.org> | 2017-12-08 10:29:01 -0500 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2017-12-08 14:47:19 -0500 |
commit | 7ca5f4bf0365b853cdb0bab5cc9cb0ce6deaec26 (patch) | |
tree | 32ddb5479eda51c3e65ee67136c88fc1630f4749 /doc/HACKING/CodingStandards.md | |
parent | fa0d24286b1dac3959c338f6b76fc15dbe1559e5 (diff) | |
download | tor-7ca5f4bf0365b853cdb0bab5cc9cb0ce6deaec26.tar.gz tor-7ca5f4bf0365b853cdb0bab5cc9cb0ce6deaec26.zip |
document our allocator conventions
Diffstat (limited to 'doc/HACKING/CodingStandards.md')
-rw-r--r-- | doc/HACKING/CodingStandards.md | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/doc/HACKING/CodingStandards.md b/doc/HACKING/CodingStandards.md index dd21d6fd2c..4b1bf4718c 100644 --- a/doc/HACKING/CodingStandards.md +++ b/doc/HACKING/CodingStandards.md @@ -346,6 +346,46 @@ macro, as in: if (BUG(ptr == NULL)) return -1; +Allocator conventions +--------------------- + +By convention, any tor type with a name like `abc_t` should be allocated +by a function named `abc_new()`. This function should never return +NULL. + +Also, a type named `abc_t` should be freed by a function named `abc_free_()`. +Don't call this `abc_free_()` function directly -- instead, wrap it in a +macro called `abc_free()`, using the `FREE_AND_NULL` macro: + + void abc_free_(abc_t *obj); + #define abc_free(obj) FREE_AND_NULL(abc_t, abc_free_, (abc)) + +This macro will free the underlying `abc_t` object, and will also set +the object pointer to NULL. + +You should define all `abc_free_()` functions to accept NULL inputs: + + void + abc_free_(abc_t *obj) + { + if (!obj) + return; + tor_free(obj->name); + thing_free(obj->thing); + tor_free(obj); + } + +If you need a free function that takes a `void *` argument (for example, +to use it as a function callback), define it with a name like +`abc_free_void()`: + + static void + abc_free_void_(void *obj) + { + abc_free_(obj); + } + + Doxygen comment conventions --------------------------- |