Commit 9b31291e authored by Marek Vavruša's avatar Marek Vavruša Committed by Daniel Salzman

libknot: added GNU C attribs for common functions

annotated some functions where it doesn’t make sense if caller doesn’t check the result, and pure functions which may be used for common subexpression elimination and vectorization
parent cc591ae2
......@@ -32,6 +32,7 @@
#include <stdbool.h>
#include "libknot/consts.h"
#include "libknot/internal/macros.h"
#include "libknot/internal/mempattern.h"
/*! \brief Type representing a domain name in wire format. */
......@@ -50,6 +51,7 @@ typedef uint8_t knot_dname_t;
* \retval KNOT_EMALF
* \retval KNOT_ESPACE
*/
_pure_ _mustcheck_
int knot_dname_wire_check(const uint8_t *name, const uint8_t *endp,
const uint8_t *pkt);
......@@ -63,6 +65,7 @@ int knot_dname_wire_check(const uint8_t *name, const uint8_t *endp,
*
* \return parsed domain name or NULL.
*/
_mustcheck_
knot_dname_t *knot_dname_parse(const uint8_t *pkt, size_t *pos, size_t maxpos,
mm_ctx_t *mm);
......@@ -73,6 +76,7 @@ knot_dname_t *knot_dname_parse(const uint8_t *pkt, size_t *pos, size_t maxpos,
*
* \return New domain name which is an exact copy of \a dname.
*/
_mustcheck_
knot_dname_t *knot_dname_copy(const knot_dname_t *name, mm_ctx_t *mm);
/*!
......@@ -83,6 +87,7 @@ knot_dname_t *knot_dname_copy(const knot_dname_t *name, mm_ctx_t *mm);
*
* \return New domain name which is an partial copy of \a dname.
*/
_mustcheck_
knot_dname_t *knot_dname_copy_part(const knot_dname_t *name, unsigned len,
mm_ctx_t *mm);
......@@ -133,6 +138,7 @@ char *knot_dname_to_str(char *dst, const knot_dname_t *name, size_t maxlen);
* \brief This function is a shortcut for \ref knot_dname_to_str with
* no output buffer parameters.
*/
_mustcheck_
static inline char *knot_dname_to_str_alloc(const knot_dname_t *name)
{
return knot_dname_to_str(NULL, name, 0);
......@@ -158,6 +164,7 @@ knot_dname_t *knot_dname_from_str(uint8_t *dst, const char *name, size_t maxlen)
* \brief This function is a shortcut for \ref knot_dname_from_str with
* no output buffer parameters.
*/
_mustcheck_
static inline knot_dname_t *knot_dname_from_str_alloc(const char *name)
{
return knot_dname_from_str(NULL, name, 0);
......@@ -186,6 +193,7 @@ int knot_dname_to_lower(knot_dname_t *name);
* \retval size of the domain name.
* \retval KNOT_EINVAL
*/
_pure_
int knot_dname_size(const knot_dname_t *name);
/*!
......@@ -197,6 +205,7 @@ int knot_dname_size(const knot_dname_t *name);
* \retval size of the domain name.
* \retval KNOT_EINVAL
*/
_pure_
int knot_dname_realsize(const knot_dname_t *name, const uint8_t *pkt);
/*!
......@@ -208,6 +217,7 @@ int knot_dname_realsize(const knot_dname_t *name, const uint8_t *pkt);
* \retval true \a sub is a subdomain of \a domain.
* \retval false otherwise.
*/
_pure_
bool knot_dname_is_sub(const knot_dname_t *sub, const knot_dname_t *domain);
/*!
......@@ -219,6 +229,7 @@ bool knot_dname_is_sub(const knot_dname_t *sub, const knot_dname_t *domain);
* \retval true \a sub us a subdomain or equal to \a domain.
* \retval false otherwise.
*/
_pure_
bool knot_dname_in(const knot_dname_t *domain, const knot_dname_t *sub);
/*!
......@@ -229,6 +240,7 @@ bool knot_dname_in(const knot_dname_t *domain, const knot_dname_t *sub);
* \retval true if \a dname is a wildcard domain name.
* \retval false otherwise.
*/
_pure_
bool knot_dname_is_wildcard(const knot_dname_t *name);
/*!
......@@ -240,6 +252,7 @@ bool knot_dname_is_wildcard(const knot_dname_t *name);
*
* \return Number of labels common for the two domain names.
*/
_pure_
int knot_dname_matched_labels(const knot_dname_t *d1, const knot_dname_t *d2);
/*!
......@@ -253,6 +266,7 @@ int knot_dname_matched_labels(const knot_dname_t *d1, const knot_dname_t *d2);
* \return New domain name created by replacing suffix of \a dname of size
* \a size with \a suffix.
*/
_mustcheck_
knot_dname_t *knot_dname_replace_suffix(const knot_dname_t *name, unsigned labels,
const knot_dname_t *suffix);
......@@ -279,6 +293,7 @@ void knot_dname_free(knot_dname_t **name, mm_ctx_t *mm);
* \retval > 0 if \a d1 goes after \a d2 in canonical order.
* \retval 0 if the domain names are identical.
*/
_pure_
int knot_dname_cmp(const knot_dname_t *d1, const knot_dname_t *d2);
/*!
......@@ -298,6 +313,7 @@ int knot_dname_cmp(const knot_dname_t *d1, const knot_dname_t *d2);
* \retval 1 if d1 > d2
* \retval -1 if d1 < d2
*/
_pure_
int knot_dname_cmp_wire(const knot_dname_t *d1, const knot_dname_t *d2,
const uint8_t *pkt);
......@@ -310,6 +326,7 @@ int knot_dname_cmp_wire(const knot_dname_t *d1, const knot_dname_t *d2,
* \retval true if the domain names are identical
* \retval false if the domain names are NOT identical
*/
_pure_
bool knot_dname_is_equal(const knot_dname_t *d1, const knot_dname_t *d2);
/*!
......@@ -331,6 +348,7 @@ knot_dname_t *knot_dname_cat(knot_dname_t *d1, const knot_dname_t *d2);
*
* \retval length of the prefix
*/
_pure_
int knot_dname_prefixlen(const uint8_t *name, unsigned nlabels, const uint8_t *pkt);
/*!
......@@ -341,6 +359,7 @@ int knot_dname_prefixlen(const uint8_t *name, unsigned nlabels, const uint8_t *p
* \param name Domain name.
* \param pkt Related packet (or NULL if not compressed).
*/
_pure_
int knot_dname_labels(const uint8_t *name, const uint8_t *pkt);
/*!
......
......@@ -30,6 +30,21 @@
#define _public_ __attribute__((visibility("default")))
#define _hidden_ __attribute__((visibility("hidden")))
/*! \brief GNU C function attributes. */
#if __GNUC__ >= 3
#define _pure_ __attribute__ ((pure))
#define _const_ __attribute__ ((const))
#define _noreturn_ __attribute__ ((noreturn))
#define _malloc_ __attribute__ ((malloc))
#define _mustcheck_ __attribute__ ((warn_unused_result))
#else
#define _pure_
#define _const_
#define _noreturn_
#define _malloc_
#define _mustcheck_
#endif
/*! \brief Eliminate compiler warning with unused parameters. */
#define UNUSED(param) (void)(param)
......
......@@ -29,6 +29,7 @@
#include <stdint.h>
#include <assert.h>
#include "libknot/internal/macros.h"
#include "libknot/internal/utils.h"
/*! \brief Offset of DNS header fields in wireformat. */
......@@ -959,6 +960,7 @@ static inline uint16_t knot_wire_get_pointer(const uint8_t *pos)
return (wire_read_u16(pos) - KNOT_WIRE_PTR_BASE); // Return offset.
}
_pure_ _mustcheck_
static inline const uint8_t *knot_wire_seek_label(const uint8_t *lp, const uint8_t *wire)
{
while (knot_wire_is_pointer(lp)) {
......@@ -969,6 +971,7 @@ static inline const uint8_t *knot_wire_seek_label(const uint8_t *lp, const uint8
return lp;
}
_pure_ _mustcheck_
static inline const uint8_t *knot_wire_next_label(const uint8_t *lp, const uint8_t *wire)
{
if (!lp || !lp[0]) /* No label after final label. */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment