summaryrefslogtreecommitdiff
path: root/include/asterisk/utils.h
diff options
context:
space:
mode:
authorWalter Doekes <walter+asterisk@wjd.nu>2011-11-02 22:02:07 +0000
committerWalter Doekes <walter+asterisk@wjd.nu>2011-11-02 22:02:07 +0000
commit7fd2a68b0ecc0261e9090915dafcd65fee9cc6b8 (patch)
treec30bd204b03e58ed781b196f344020fa6a7e8b14 /include/asterisk/utils.h
parent55ffab4cd934e923cfe5c4fb1030325591b942fa (diff)
Ensure that string field lengths are properly aligned
Integers should always be aligned. For some platforms (ARM, SPARC) this is more important than for others. This changeset ensures that the string field string lengths are aligned on *all* platforms, not just on the SPARC for which there was a workaround. It also fixes that the length integer can be resized to 32 bits without problems if needed. (closes issue ASTERISK-17310) Reported by: radael, S Adrian Reviewed by: Tzafrir Cohen, Terry Wilson Tested by: S Adrian Review: https://reviewboard.asterisk.org/r/1549 ........ Merged revisions 343157 from http://svn.asterisk.org/svn/asterisk/branches/1.8 ........ Merged revisions 343158 from http://svn.asterisk.org/svn/asterisk/branches/10 git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@343163 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'include/asterisk/utils.h')
-rw-r--r--include/asterisk/utils.h32
1 files changed, 32 insertions, 0 deletions
diff --git a/include/asterisk/utils.h b/include/asterisk/utils.h
index 44eeb5f8e..4d015f3ee 100644
--- a/include/asterisk/utils.h
+++ b/include/asterisk/utils.h
@@ -741,6 +741,38 @@ static void force_inline _ast_assert(int condition, const char *condition_str,
#include "asterisk/strings.h"
/*!
+ * \brief Add space and let result be a multiple of space.
+ * \param initial A number to add space to.
+ * \param space The space to add, this would typically be sizeof(sometype).
+ * \return The sum of initial plus space plus at most space-1.
+ *
+ * Many systems prefer integers to be stored on aligned on memory locations.
+ * A use case for this is when prepending length fields of type int to a buffer.
+ * If you keep the total used bytes a multiple of the size of the integer type,
+ * a next block of length+buffer will have the length field automatically
+ * aligned.
+ *
+ * It looks kind of ugly, but the compiler will optimize this down to 4 or 5
+ * inexpensive instructions (on x86_64).
+ *
+ * Examples:
+ * ast_add_and_make_multiple_of(0x18, sizeof(int64_t)) ==> 0x20
+ * ast_add_and_make_multiple_of(0x19, sizeof(int64_t)) ==> 0x28
+ */
+#define ast_add_and_make_multiple_of(initial, space) (((initial + (2 * space - 1)) / space) * space)
+
+/*!
+ * \brief Add bytes so that result is a multiple of size.
+ * \param initial A number to enlarge.
+ * \param size The block size the number should be a multiple of.
+ * \return The sum of initial plus at most size-1.
+ *
+ * Similar ast_add_and_make_multiple_of, except that this doesn't add the room
+ * for the length object, it only ensures that the total is aligned.
+ */
+#define ast_make_multiple_of(initial, size) (((initial + size - 1) / size) * size)
+
+/*!
* \brief An Entity ID is essentially a MAC address, brief and unique
*/
struct ast_eid {