Blame jemalloc/include/jemalloc/internal/ticker.h

Packit 345191
#ifndef JEMALLOC_INTERNAL_TICKER_H
Packit 345191
#define JEMALLOC_INTERNAL_TICKER_H
Packit 345191
Packit 345191
#include "jemalloc/internal/util.h"
Packit 345191
Packit 345191
/**
Packit 345191
 * A ticker makes it easy to count-down events until some limit.  You
Packit 345191
 * ticker_init the ticker to trigger every nticks events.  You then notify it
Packit 345191
 * that an event has occurred with calls to ticker_tick (or that nticks events
Packit 345191
 * have occurred with a call to ticker_ticks), which will return true (and reset
Packit 345191
 * the counter) if the countdown hit zero.
Packit 345191
 */
Packit 345191
Packit 345191
typedef struct {
Packit 345191
	int32_t tick;
Packit 345191
	int32_t nticks;
Packit 345191
} ticker_t;
Packit 345191
Packit 345191
static inline void
Packit 345191
ticker_init(ticker_t *ticker, int32_t nticks) {
Packit 345191
	ticker->tick = nticks;
Packit 345191
	ticker->nticks = nticks;
Packit 345191
}
Packit 345191
Packit 345191
static inline void
Packit 345191
ticker_copy(ticker_t *ticker, const ticker_t *other) {
Packit 345191
	*ticker = *other;
Packit 345191
}
Packit 345191
Packit 345191
static inline int32_t
Packit 345191
ticker_read(const ticker_t *ticker) {
Packit 345191
	return ticker->tick;
Packit 345191
}
Packit 345191
Packit 345191
/*
Packit 345191
 * Not intended to be a public API.  Unfortunately, on x86, neither gcc nor
Packit 345191
 * clang seems smart enough to turn
Packit 345191
 *   ticker->tick -= nticks;
Packit 345191
 *   if (unlikely(ticker->tick < 0)) {
Packit 345191
 *     fixup ticker
Packit 345191
 *     return true;
Packit 345191
 *   }
Packit 345191
 *   return false;
Packit 345191
 * into
Packit 345191
 *   subq %nticks_reg, (%ticker_reg)
Packit 345191
 *   js fixup ticker
Packit 345191
 *
Packit 345191
 * unless we force "fixup ticker" out of line.  In that case, gcc gets it right,
Packit 345191
 * but clang now does worse than before.  So, on x86 with gcc, we force it out
Packit 345191
 * of line, but otherwise let the inlining occur.  Ordinarily this wouldn't be
Packit 345191
 * worth the hassle, but this is on the fast path of both malloc and free (via
Packit 345191
 * tcache_event).
Packit 345191
 */
Packit 345191
#if defined(__GNUC__) && !defined(__clang__)				\
Packit 345191
    && (defined(__x86_64__) || defined(__i386__))
Packit 345191
JEMALLOC_NOINLINE
Packit 345191
#endif
Packit 345191
static bool
Packit 345191
ticker_fixup(ticker_t *ticker) {
Packit 345191
	ticker->tick = ticker->nticks;
Packit 345191
	return true;
Packit 345191
}
Packit 345191
Packit 345191
static inline bool
Packit 345191
ticker_ticks(ticker_t *ticker, int32_t nticks) {
Packit 345191
	ticker->tick -= nticks;
Packit 345191
	if (unlikely(ticker->tick < 0)) {
Packit 345191
		return ticker_fixup(ticker);
Packit 345191
	}
Packit 345191
	return false;
Packit 345191
}
Packit 345191
Packit 345191
static inline bool
Packit 345191
ticker_tick(ticker_t *ticker) {
Packit 345191
	return ticker_ticks(ticker, 1);
Packit 345191
}
Packit 345191
Packit 345191
/*
Packit 345191
 * Try to tick.  If ticker would fire, return true, but rely on
Packit 345191
 * slowpath to reset ticker.
Packit 345191
 */
Packit 345191
static inline bool
Packit 345191
ticker_trytick(ticker_t *ticker) {
Packit 345191
	--ticker->tick;
Packit 345191
	if (unlikely(ticker->tick < 0)) {
Packit 345191
		return true;
Packit 345191
	}
Packit 345191
	return false;
Packit 345191
}
Packit 345191
Packit 345191
#endif /* JEMALLOC_INTERNAL_TICKER_H */