Commit 236ff8ba authored by Daniel Salzman's avatar Daniel Salzman

libknot: move common.h to common/macros.h

parent c08577b8
......@@ -97,6 +97,7 @@ libknotcs_la_SOURCES = \
common/lists.h \
common/log.c \
common/log.h \
common/macros.h \
common/mem.c \
common/mem.h \
common/mempattern.c \
......@@ -144,7 +145,6 @@ libknot_la_LDFLAGS = \
libknot_la_SOURCES = \
libknot/binary.c \
libknot/binary.h \
libknot/common.h \
libknot/consts.c \
libknot/consts.h \
libknot/consts.h \
......
......@@ -40,6 +40,10 @@
* undef'd.
*/
#pragma once
#include "common/macros.h"
typedef unsigned uns;
#ifndef ASORT_LT
......@@ -162,8 +166,7 @@ static void ASORT_PREFIX(sort)(ASORT_ARRAY_ARG uns array_size ASORT_EXTRA_ARGS)
*/
/* Find minimal element which will serve as a barrier */
/*! \todo This requires libknot/common.h!!! Define also in common/ */
r = KNOT_MIN(array_size, ASORT_THRESHOLD);
r = MIN(array_size, ASORT_THRESHOLD);
m = 0;
for (l=1; l<r; l++)
if (ASORT_LT(ASORT_ELT(l),ASORT_ELT(m)))
......
......@@ -7,6 +7,8 @@
* of the GNU Lesser General Public License.
*/
#pragma once
/***
* [[defs]]
* Definitions
......
......@@ -6,7 +6,6 @@
#include "common/binsearch.h"
#include "common/trie/murmurhash3.h"
#include "libknot/errcode.h"
#include "libknot/common.h"
/* UCW array sorting defines. */
static int universal_cmp(uint32_t k1, uint32_t k2, hhash_t *tbl);
......@@ -72,7 +71,7 @@ static int reduce_dist(hhash_t *t, int idx, int *empty)
/*! \brief Item comparator. */
static int key_cmp(const char *k1, uint16_t k1_len, const char *k2, uint16_t k2_len)
{
int ret = memcmp(k1, k2, KNOT_MIN(k1_len, k2_len));
int ret = memcmp(k1, k2, MIN(k1_len, k2_len));
if (ret != 0) {
return ret;
}
......
......@@ -28,6 +28,7 @@
#include "common/log.h"
#include "common/lists.h"
#include "common/macros.h"
#include "common/strlcpy.h"
#include "knot/conf/conf.h"
#include "libknot/errcode.h"
......@@ -123,7 +124,7 @@ static uint8_t sink_levels(struct log_sink *log, int facility, logsrc_t src)
assert(log);
// Check facility
if (knot_unlikely(log->facility_count == 0 || facility >= log->facility_count)) {
if (unlikely(log->facility_count == 0 || facility >= log->facility_count)) {
return 0;
}
......@@ -133,7 +134,7 @@ static uint8_t sink_levels(struct log_sink *log, int facility, logsrc_t src)
static int sink_levels_set(struct log_sink *log, int facility, logsrc_t src, uint8_t levels)
{
// Check facility
if (knot_unlikely(log->facility_count == 0 || facility >= log->facility_count)) {
if (unlikely(log->facility_count == 0 || facility >= log->facility_count)) {
return KNOT_EINVAL;
}
......@@ -204,7 +205,7 @@ bool log_isopen()
static int log_open_file(struct log_sink *log, const char* filename)
{
// Check facility
if (knot_unlikely(log->facility_count == 0 ||
if (unlikely(log->facility_count == 0 ||
LOGT_FILE + log->file_count >= log->facility_count)) {
return KNOT_ERROR;
}
......@@ -461,7 +462,7 @@ int log_update_privileges(int uid, int gid)
int log_reconfigure(const struct conf_t *conf, void *data)
{
// Data not used
KNOT_UNUSED(data);
UNUSED(data);
// Use defaults if no 'log' section is configured.
if (EMPTY_LIST(conf->logs)) {
......
/*!
* \file libknot/common.h
*
* \author Lubos Slovak <lubos.slovak@nic.cz>
*
* \brief Common macros, includes and utilities.
*
* \addtogroup libknot
* @{
*/
/* Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -23,33 +13,44 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*!
* \file common/macros.h
*
* \author Lubos Slovak <lubos.slovak@nic.cz>
*
* \brief Common macros.
*
* \addtogroup common_lib
* @{
*/
#pragma once
/*! \brief Library visibility macros. */
#define _public_ __attribute__((visibility("default")))
#define _hidden_ __attribute__((visibility("hidden")))
/*! \brief Eliminate compiler warning with unused parameters. */
#define KNOT_UNUSED(param) (void)(param)
#define UNUSED(param) (void)(param)
#ifndef KNOT_MIN
#ifndef MIN
/*! \brief Type-safe minimum macro. */
#define KNOT_MIN(a, b) \
#define MIN(a, b) \
({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a < _b ? _a : _b; })
/*! \brief Type-safe maximum macro. */
#define KNOT_MAX(a, b) \
#define MAX(a, b) \
({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a > _b ? _a : _b; })
#endif
/* Optimisation macros. */
#ifndef knot_likely
#ifndef likely
/*! \brief Optimize for x to be true value. */
#define knot_likely(x) __builtin_expect((x),1)
#define likely(x) __builtin_expect((x), 1)
#endif
#ifndef knot_unlikely
#ifndef unlikely
/*! \brief Optimize for x to be false value. */
#define knot_unlikely(x) __builtin_expect((x),0)
#define unlikely(x) __builtin_expect((x), 0)
#endif
/*! @} */
......@@ -20,7 +20,6 @@
#include <string.h> // strdup
#include "libknot/errcode.h"
#include "libknot/common.h"
#include "dnstap/dnstap.h"
#include "dnstap/reader.h"
......@@ -98,7 +97,7 @@ void dt_reader_free_frame(dt_reader_t *reader, Dnstap__Dnstap **frame_ptr)
return;
}
KNOT_UNUSED(reader);
UNUSED(reader);
dnstap__dnstap__free_unpacked(*frame_ptr, NULL);
*frame_ptr = NULL;
......
......@@ -20,7 +20,6 @@
#include <string.h> // strdup
#include "libknot/errcode.h"
#include "libknot/common.h"
#include "dnstap/dnstap.h"
#include "dnstap/writer.h"
......
......@@ -27,6 +27,7 @@
#include <urcu.h>
#include "common-knot/strlcat.h"
#include "common/strlcpy.h"
#include "common/macros.h"
#include "common/mem.h"
#include "knot/conf/conf.h"
#include "knot/conf/extra.h"
......@@ -794,7 +795,7 @@ int conf_open(const char* path)
/* Update hooks. */
conf_update_hooks(nconf);
/* Free old config. */
conf_free(oldconf);
}
......@@ -876,13 +877,13 @@ size_t conf_udp_threads(const conf_t *conf)
size_t conf_tcp_threads(const conf_t *conf)
{
size_t thrcount = conf_udp_threads(conf);
return KNOT_MAX(thrcount * 2, CONFIG_XFERS);
return MAX(thrcount * 2, CONFIG_XFERS);
}
int conf_bg_threads(const conf_t *conf)
{
if (conf->bg_workers < 1) {
return KNOT_MIN(dt_optimal_size(), CONFIG_XFERS);
return MIN(dt_optimal_size(), CONFIG_XFERS);
}
return conf->bg_workers;
......
......@@ -19,6 +19,7 @@
#include "knot/ctl/estimator.h"
#include "libknot/dname.h"
#include "common/lists.h"
#include "common/macros.h"
#include "common/mem.h"
#include "knot/zone/node.h"
#include "libknot/descriptor.h"
......@@ -172,7 +173,7 @@ void estimator_rrset_memsize_wrap(zs_scanner_t *scanner)
int estimator_free_trie_node(value_t *val, void *data)
{
KNOT_UNUSED(data);
UNUSED(data);
list_t *trie_n = (list_t *)(*val);
WALK_LIST_FREE(*trie_n);
free(trie_n);
......
......@@ -26,6 +26,7 @@
#endif
#include "knot/knot.h"
#include "common/macros.h"
#include "common/mem.h"
#include "libknot/descriptor.h"
#include "knot/ctl/process.h"
......@@ -585,25 +586,25 @@ exit:
static int cmd_stop(int argc, char *argv[], unsigned flags)
{
KNOT_UNUSED(argc);
KNOT_UNUSED(argv);
KNOT_UNUSED(flags);
UNUSED(argc);
UNUSED(argv);
UNUSED(flags);
return cmd_remote("stop", KNOT_RRTYPE_TXT, 0, NULL);
}
static int cmd_reload(int argc, char *argv[], unsigned flags)
{
KNOT_UNUSED(argc);
KNOT_UNUSED(argv);
KNOT_UNUSED(flags);
UNUSED(argc);
UNUSED(argv);
UNUSED(flags);
return cmd_remote("reload", KNOT_RRTYPE_TXT, 0, NULL);
}
static int cmd_refresh(int argc, char *argv[], unsigned flags)
{
KNOT_UNUSED(flags);
UNUSED(flags);
if (flags & F_FORCE) {
return cmd_remote("retransfer", KNOT_RRTYPE_NS, argc, argv);
......@@ -614,25 +615,25 @@ static int cmd_refresh(int argc, char *argv[], unsigned flags)
static int cmd_flush(int argc, char *argv[], unsigned flags)
{
KNOT_UNUSED(flags);
UNUSED(flags);
return cmd_remote("flush", KNOT_RRTYPE_NS, argc, argv);
}
static int cmd_status(int argc, char *argv[], unsigned flags)
{
KNOT_UNUSED(argc);
KNOT_UNUSED(argv);
KNOT_UNUSED(flags);
UNUSED(argc);
UNUSED(argv);
UNUSED(flags);
return cmd_remote("status", KNOT_RRTYPE_TXT, 0, NULL);
}
static int cmd_zonestatus(int argc, char *argv[], unsigned flags)
{
KNOT_UNUSED(argc);
KNOT_UNUSED(argv);
KNOT_UNUSED(flags);
UNUSED(argc);
UNUSED(argv);
UNUSED(flags);
return cmd_remote("zonestatus", KNOT_RRTYPE_TXT, 0, NULL);
}
......@@ -644,9 +645,9 @@ static int cmd_signzone(int argc, char *argv[], unsigned flags)
static int cmd_checkconf(int argc, char *argv[], unsigned flags)
{
KNOT_UNUSED(argc);
KNOT_UNUSED(argv);
KNOT_UNUSED(flags);
UNUSED(argc);
UNUSED(argv);
UNUSED(flags);
log_info("configuration is valid");
......@@ -684,7 +685,7 @@ static bool fetch_zone(int argc, char *argv[], conf_zone_t *zone)
static int cmd_checkzone(int argc, char *argv[], unsigned flags)
{
KNOT_UNUSED(flags);
UNUSED(flags);
/* Zone checking */
int rc = 0;
......@@ -723,7 +724,7 @@ static int cmd_checkzone(int argc, char *argv[], unsigned flags)
static int cmd_memstats(int argc, char *argv[], unsigned flags)
{
KNOT_UNUSED(flags);
UNUSED(flags);
/* Zone checking */
double total_size = 0;
......
......@@ -18,6 +18,7 @@
#include <sys/stat.h>
#include "knot/ctl/remote.h"
#include "common/log.h"
#include "common/macros.h"
#include "common/mem.h"
#include "common-knot/fdset.h"
#include "knot/knot.h"
......@@ -222,8 +223,8 @@ static int remote_zone_sign(zone_t *zone)
*/
static int remote_c_stop(server_t *s, remote_cmdargs_t* a)
{
KNOT_UNUSED(a);
KNOT_UNUSED(s);
UNUSED(a);
UNUSED(s);
return KNOT_CTL_STOP;
}
......@@ -235,7 +236,7 @@ static int remote_c_stop(server_t *s, remote_cmdargs_t* a)
*/
static int remote_c_reload(server_t *s, remote_cmdargs_t* a)
{
KNOT_UNUSED(a);
UNUSED(a);
return server_reload(s, conf()->filename);
}
......@@ -247,8 +248,8 @@ static int remote_c_reload(server_t *s, remote_cmdargs_t* a)
*/
static int remote_c_status(server_t *s, remote_cmdargs_t* a)
{
KNOT_UNUSED(s);
KNOT_UNUSED(a);
UNUSED(s);
UNUSED(a);
dbg_server("remote: %s\n", __func__);
return KNOT_EOK;
}
......
......@@ -17,6 +17,7 @@
#include <assert.h>
#include "common/base32hex.h"
#include "common/macros.h"
#include "knot/dnssec/nsec3-chain.h"
#include "libknot/dname.h"
#include "libknot/packet/wire.h"
......@@ -364,7 +365,7 @@ static int connect_nsec3_nodes(zone_node_t *a, zone_node_t *b,
{
assert(a);
assert(b);
KNOT_UNUSED(data);
UNUSED(data);
assert(a->rrset_count == 1);
......@@ -496,7 +497,7 @@ static bool nsec3_is_empty(zone_node_t *node)
*/
static int nsec3_mark_empty(zone_node_t **node_p, void *data)
{
KNOT_UNUSED(data);
UNUSED(data);
zone_node_t *node = *node_p;
if (!(node->flags & NODE_FLAGS_EMPTY) && nsec3_is_empty(node)) {
......@@ -545,7 +546,7 @@ static void mark_empty_nodes_tmp(const zone_contents_t *zone)
*/
static int nsec3_reset(zone_node_t **node_p, void *data)
{
KNOT_UNUSED(data);
UNUSED(data);
zone_node_t *node = *node_p;
if (node->flags & NODE_FLAGS_EMPTY) {
......
......@@ -20,9 +20,9 @@
#include <inttypes.h>
#include "common/debug.h"
#include "common/macros.h"
#include "common/mem.h"
#include "libknot/errcode.h"
#include "libknot/common.h"
#include "libknot/dname.h"
#include "libknot/consts.h"
#include "libknot/rrtype/dnskey.h"
......@@ -206,7 +206,7 @@ int knot_load_zone_keys(const char *keydir_name, const knot_dname_t *zone_name,
int written = snprintf(path, path_len + 1, "%s/%s",
keydir_name, entry->d_name);
KNOT_UNUSED(written);
UNUSED(written);
assert(written == path_len);
knot_key_params_t params = { 0 };
......@@ -339,7 +339,7 @@ uint32_t knot_get_next_zone_key_event(const knot_zone_keys_t *keys)
node_t *node = NULL;
WALK_LIST(node, keys->list) {
knot_zone_key_t *key = (knot_zone_key_t *)node;
result = KNOT_MIN(result, key->next_event);
result = MIN(result, key->next_event);
}
return result;
......
......@@ -21,10 +21,10 @@
#include <time.h>
#include "common/debug.h"
#include "common/macros.h"
#include "libknot/descriptor.h"
#include "libknot/errcode.h"
#include "common/trie/hat-trie.h"
#include "libknot/common.h"
#include "libknot/dname.h"
#include "libknot/rrset.h"
#include "libknot/dnssec/key.h"
......@@ -1151,7 +1151,7 @@ static int sign_changeset_wrap(knot_rrset_t *chg_rrset, changeset_signing_data_t
*/
static int free_helper_trie_node(value_t *val, void *d)
{
KNOT_UNUSED(d);
UNUSED(d);
signed_info_t *info = (signed_info_t *)*val;
if (info->type_list && !EMPTY_LIST(*(info->type_list))) {
WALK_LIST_FREE(*(info->type_list));
......@@ -1215,7 +1215,7 @@ int knot_zone_sign(const zone_contents_t *zone,
}
// renew the signatures a little earlier
uint32_t expiration = KNOT_MIN(normal_tree_expiration, nsec3_tree_expiration);
uint32_t expiration = MIN(normal_tree_expiration, nsec3_tree_expiration);
// DNSKEY updates
uint32_t dnskey_update = knot_get_next_zone_key_event(zone_keys);
......@@ -1354,7 +1354,7 @@ int knot_zone_sign_changeset(const zone_contents_t *zone,
}
changeset_iter_t itt;
changeset_iter_all(&itt, in_ch, false);
knot_rrset_t rr = changeset_iter_next(&itt);
while (!knot_rrset_empty(&rr)) {
int ret = sign_changeset_wrap(&rr, &args);
......@@ -1385,7 +1385,7 @@ int knot_zone_sign_nsecs_in_changeset(const knot_zone_keys_t *zone_keys,
changeset_iter_t itt;
changeset_iter_add(&itt, changeset, false);
knot_rrset_t rr = changeset_iter_next(&itt);
while (!knot_rrset_empty(&rr)) {
if (rr.type == KNOT_RRTYPE_NSEC ||
......@@ -1400,7 +1400,7 @@ int knot_zone_sign_nsecs_in_changeset(const knot_zone_keys_t *zone_keys,
rr = changeset_iter_next(&itt);
}
changeset_iter_clear(&itt);
return KNOT_EOK;
}
......
......@@ -29,7 +29,6 @@
#include <systemd/sd-daemon.h>
#endif
#include "libknot/common.h"
#include "libknot/dnssec/crypto.h"
#include "knot/knot.h"
#include "knot/server/server.h"
......
......@@ -16,7 +16,6 @@
#include "common/debug.h"
#include "libknot/descriptor.h"
#include "libknot/common.h"
#include "libknot/rrtype/rdname.h"
#include "libknot/rrtype/soa.h"
#include "libknot/dnssec/rrset-sign.h"
......
......@@ -25,7 +25,6 @@
#include "libknot/consts.h"
#include "knot/zone/zonedb.h"
#include "knot/zone/timers.h"
#include "libknot/common.h"
#include "libknot/packet/wire.h"
#include "knot/updates/acl.h"
#include "common-knot/evsched.h"
......
......@@ -3,7 +3,6 @@
#include "knot/nameserver/internet.h"
#include "knot/dnssec/zone-nsec.h"
#include "libknot/common.h"
#include "common/debug.h"
#define DNSSEC_ENABLED 1
......
......@@ -16,6 +16,7 @@
#include "libknot/tsig-op.h"
#include "libknot/descriptor.h"
#include "common/debug.h"
#include "common/macros.h"
/*! \brief Accessor to query-specific data. */
#define QUERY_DATA(ctx) ((struct query_data *)(ctx)->data)
......@@ -322,8 +323,8 @@ static int prepare_answer(const knot_pkt_t *query, knot_pkt_t *resp, knot_layer_
if (knot_pkt_has_edns(query)) {
uint16_t client = knot_edns_get_payload(query->opt_rr);
uint16_t server = conf()->max_udp_payload;
uint16_t transfer = KNOT_MIN(client, server);
resp->max_size = KNOT_MAX(resp->max_size, transfer);
uint16_t transfer = MIN(client, server);
resp->max_size = MAX(resp->max_size, transfer);
}
} else {
resp->max_size = KNOT_WIRE_MAX_PKTSIZE;
......
......@@ -20,6 +20,7 @@
#include "knot/updates/apply.h"
#include "knot/dnssec/zone-sign.h"
#include "common/debug.h"
#include "common/macros.h"
#include "knot/dnssec/zone-events.h"
#include "knot/updates/ddns.h"
#include "knot/updates/zone-update.h"
......@@ -548,7 +549,7 @@ int updates_execute(zone_t *zone)
"DDNS, processing %zu updates", update_count);
ret = process_requests(zone, &updates);
}
KNOT_UNUSED(ret); /* Don't care about the Knot code, RCODEs are set. */
UNUSED(ret); /* Don't care about the Knot code, RCODEs are set. */
/* Send responses. */
send_update_responses(zone, &updates);
......
......@@ -25,7 +25,6 @@
#include <assert.h>
#include "common-knot/crc.h"
#include "libknot/common.h"
#include "knot/other/debug.h"
#include "knot/server/journal.h"
#include "knot/server/serialization.h"
......
......@@ -33,6 +33,7 @@
#include "common/sockaddr.h"
#include "common-knot/fdset.h"
#include "common/macros.h"
#include "common/mempool.h"
#include "common/net.h"
......@@ -70,7 +71,7 @@ static inline int tcp_throttle() {
/*! \brief Sweep TCP connection. */
static enum fdset_sweep_state tcp_sweep(fdset_t *set, int i, void *data)
{
KNOT_UNUSED(data);
UNUSED(data);
assert(set && i < set->n && i >= 0);
int fd = set->pfd[i].fd;
......@@ -344,7 +345,7 @@ int tcp_master(dthread_t *thread)
for(;;) {
/* Check handler state. */
if (knot_unlikely(*iostate & ServerReload)) {
if (unlikely(*iostate & ServerReload)) {
*iostate &= ~ServerReload;
/* Cancel client connections. */
......
......@@ -37,6 +37,7 @@
#endif /* HAVE_CAP_NG_H */
#include "common/sockaddr.h"
#include "common/macros.h"
#include "common/mempattern.h"
#include "common/mempool.h"
#include "knot/knot.h"
......@@ -128,7 +129,7 @@ void udp_handle(udp_context_t *udp, int fd, struct sockaddr_storage *ss,
param.thread_id = udp->thread_id;
/* Rate limit is applied? */
if (knot_unlikely(udp->server->rrl != NULL) && udp->server->rrl->rate > 0) {
if (unlikely(udp->server->rrl != NULL) && udp->server->rrl->rate > 0) {
param.proc_flags |= NS_QUERY_LIMIT_RATE;
}
......@@ -304,7 +305,7 @@ static inline int sendmmsg(int fd, struct mmsghdr *mmsg, unsigned vlen,
*/
int udp_sendmmsg(int sock, struct sockaddr *_, struct mmsghdr *msgs, size_t count)
{
KNOT_UNUSED(_);
UNUSED(_);
return sendmmsg(sock, msgs, count, 0);
}
#endif /* ENABLE_SENDMMSG */
......@@ -469,8 +470,8 @@ static int track_ifaces(ifacelist_t *ifaces, fd_set *set, int *maxfd, int *minfd
iface_t *iface = NULL;
WALK_LIST(iface, ifaces->l) {
int fd = iface->fd[IO_UDP];
*maxfd = KNOT_MAX(fd, *maxfd);
*minfd = KNOT_MIN(fd, *minfd);
*maxfd = MAX(fd, *maxfd);
*