pipewire/spa/include/spa/utils/ratelimit.h
Sam James b943c31fd8
*: don't include standard C headers inside of extern "C"
Including C headers inside of `extern "C"` breaks use from C++. Hoist
the includes of standard C headers above the block so we don't try
to mangle the stdlib.

I initially tried to scope this with a targeted change but it's too
hard to do correctly that way. This way, we avoid whack-a-mole.

Firefox is working around this in their e21461b7b8b39cc31ba53c47d4f6f310c673ff2f
commit.

Bug: https://bugzilla.mozilla.org/1953080
2025-05-30 09:48:28 +01:00

53 lines
982 B
C

/* Ratelimit */
/* SPDX-FileCopyrightText: Copyright © 2023 Wim Taymans */
/* SPDX-License-Identifier: MIT */
#ifndef SPA_RATELIMIT_H
#define SPA_RATELIMIT_H
#include <inttypes.h>
#include <stddef.h>
#include <spa/utils/defs.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef SPA_API_RATELIMIT
#ifdef SPA_API_IMPL
#define SPA_API_RATELIMIT SPA_API_IMPL
#else
#define SPA_API_RATELIMIT static inline
#endif
#endif
struct spa_ratelimit {
uint64_t interval;
uint64_t begin;
unsigned burst;
unsigned n_printed;
unsigned n_suppressed;
};
SPA_API_RATELIMIT int spa_ratelimit_test(struct spa_ratelimit *r, uint64_t now)
{
unsigned suppressed = 0;
if (r->begin + r->interval < now) {
suppressed = r->n_suppressed;
r->begin = now;
r->n_printed = 0;
r->n_suppressed = 0;
} else if (r->n_printed >= r->burst) {
r->n_suppressed++;
return -1;
}
r->n_printed++;
return suppressed;
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* SPA_RATELIMIT_H */