bitarray.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (C) 2006-2020 B.A.T.M.A.N. contributors:
  3. *
  4. * Simon Wunderlich, Marek Lindner
  5. */
  6. #ifndef _NET_BATMAN_ADV_BITARRAY_H_
  7. #define _NET_BATMAN_ADV_BITARRAY_H_
  8. #include "main.h"
  9. #include <linux/bitops.h>
  10. #include <linux/compiler.h>
  11. #include <linux/stddef.h>
  12. #include <linux/types.h>
  13. /**
  14. * batadv_test_bit() - check if bit is set in the current window
  15. *
  16. * @seq_bits: pointer to the sequence number receive packet
  17. * @last_seqno: latest sequence number in seq_bits
  18. * @curr_seqno: sequence number to test for
  19. *
  20. * Return: true if the corresponding bit in the given seq_bits indicates true
  21. * and curr_seqno is within range of last_seqno. Otherwise returns false.
  22. */
  23. static inline bool batadv_test_bit(const unsigned long *seq_bits,
  24. u32 last_seqno, u32 curr_seqno)
  25. {
  26. s32 diff;
  27. diff = last_seqno - curr_seqno;
  28. if (diff < 0 || diff >= BATADV_TQ_LOCAL_WINDOW_SIZE)
  29. return false;
  30. return test_bit(diff, seq_bits) != 0;
  31. }
  32. /**
  33. * batadv_set_bit() - Turn corresponding bit on, so we can remember that we got
  34. * the packet
  35. * @seq_bits: bitmap of the packet receive window
  36. * @n: relative sequence number of newly received packet
  37. */
  38. static inline void batadv_set_bit(unsigned long *seq_bits, s32 n)
  39. {
  40. /* if too old, just drop it */
  41. if (n < 0 || n >= BATADV_TQ_LOCAL_WINDOW_SIZE)
  42. return;
  43. set_bit(n, seq_bits); /* turn the position on */
  44. }
  45. bool batadv_bit_get_packet(void *priv, unsigned long *seq_bits,
  46. s32 seq_num_diff, int set_mark);
  47. #endif /* _NET_BATMAN_ADV_BITARRAY_H_ */