lilzz
Posts: 411
Joined: Sat Nov 30, 2013 5:27 pm

Bitmap in Linux

Wed Oct 08, 2014 8:19 pm

Code: Select all

unsigned long *word;
*w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);


/* shift the packet array by n places. */
static void batadv_bitmap_shift_left(unsigned long *seq_bits, int32_t n)
{
	if (n <= 0 || n >= BATADV_TQ_LOCAL_WINDOW_SIZE)
		return;

	bitmap_shift_left(seq_bits, seq_bits, n, BATADV_TQ_LOCAL_WINDOW_SIZE);
}


/* receive and process one packet within the sequence number window.
 *
 * returns:
 *  1 if the window was moved (either new or very old)
 *  0 if the window was not moved/shifted.
 */
int batadv_bit_get_packet(void *priv, unsigned long *seq_bits,
			  int32_t seq_num_diff, int set_mark)
{
	struct batadv_priv *bat_priv = priv;

	/* sequence number is slightly older. We already got a sequence number
	 * higher than this one, so we just mark it.
	 */
	if (seq_num_diff <= 0 && seq_num_diff > -BATADV_TQ_LOCAL_WINDOW_SIZE) {
		if (set_mark)
			batadv_set_bit(seq_bits, -seq_num_diff);
		return 0;
	}

	/* sequence number is slightly newer, so we shift the window and
	 * set the mark if required
	 */
	if (seq_num_diff > 0 && seq_num_diff < BATADV_TQ_LOCAL_WINDOW_SIZE) {
		batadv_bitmap_shift_left(seq_bits, seq_num_diff);

		if (set_mark)
			batadv_set_bit(seq_bits, 0);
		return 1;
	}

	/* sequence number is much newer, probably missed a lot of packets */
	if (seq_num_diff >= BATADV_TQ_LOCAL_WINDOW_SIZE &&
	    seq_num_diff < BATADV_EXPECTED_SEQNO_RANGE) {
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
			   "We missed a lot of packets (%i) !\n",
			   seq_num_diff - 1);
		bitmap_zero(seq_bits, BATADV_TQ_LOCAL_WINDOW_SIZE);
		if (set_mark)
			batadv_set_bit(seq_bits, 0);
		return 1;
	}

	/* received a much older packet. The other host either restarted
	 * or the old packet got delayed somewhere in the network. The
	 * packet should be dropped without calling this function if the
	 * seqno window is protected.
	 *
	 * seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE
	 * or
	 * seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE
	 */
	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
		   "Other host probably restarted!\n");

	bitmap_zero(seq_bits, BATADV_TQ_LOCAL_WINDOW_SIZE);
	if (set_mark)
		batadv_set_bit(seq_bits, 0);

	return 1;
}
1)what's bitmap_weight? couldn't find a reference
2)why the need to use bitmap from above?

User avatar
PeterO
Posts: 5880
Joined: Sun Jul 22, 2012 4:14 pm

Re: Bitmap in Linux

Wed Oct 08, 2014 8:32 pm

How do you expect people to help when all you post is a random bit of source code with absolutely no context ?

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

lilzz
Posts: 411
Joined: Sat Nov 30, 2013 5:27 pm

Re: Bitmap in Linux

Thu Oct 09, 2014 12:28 am

PeterO wrote:How do you expect people to help when all you post is a random bit of source code with absolutely no context ?

PeterO
Care to comment on this ? Not sure what's the purpose of DECLARE_BITMAP?

Code: Select all

#define DECLARE_BITMAP(name,bits) \
unsigned long name[BITS_TO_LONGS(bits)]

#define BITS_TO_LONGS(nr)       DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
#define BITS_PER_BYTE 8

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Bitmap in Linux

Thu Oct 09, 2014 5:57 am

Without some context, like what program is the code from, what does it do, we cannot even guess unless we are fairly familiar with those particular lines of code out of the hundreds of millions that make up the OS and applications.

User avatar
aTao
Posts: 1093
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: Bitmap in Linux

Thu Oct 09, 2014 7:10 am

lilzz wrote:
Care to comment on this ? Not sure what's the purpose of DECLARE_BITMAP?

Code: Select all

#define DECLARE_BITMAP(name,bits) \
unsigned long name[BITS_TO_LONGS(bits)]

#define BITS_TO_LONGS(nr)       DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
#define BITS_PER_BYTE 8
What e have here is poorly commented code. The author can glean their original meaning from the names used, but to an outsider then this is reverse engineering.
However, substituting names through the defines we get:

Code: Select all

#define DECLARE_BITMAP(name,bits) unsigned long name[(((bits) + ({number of bits in long}) -1) / ({number of bits in long}))]
which, to me looks like
from name select the long that contains the bit with index bits

Well nearly...........
>)))'><'(((<

User avatar
PeterO
Posts: 5880
Joined: Sun Jul 22, 2012 4:14 pm

Re: Bitmap in Linux

Thu Oct 09, 2014 8:44 am

lilzz wrote:Care to comment on this ?
Care to read this ? http://www.catb.org/esr/faqs/smart-questions.html
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
RaTTuS
Posts: 10559
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Bitmap in Linux

Thu Oct 09, 2014 9:03 am

PeterO wrote:
lilzz wrote:Care to comment on this ?
Care to read this ? http://www.catb.org/esr/faqs/smart-questions.html
PeterO
tldr ;-p
seriously read and understand ^
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
jojopi
Posts: 3271
Joined: Tue Oct 11, 2011 8:38 pm

Re: Bitmap in Linux

Thu Oct 09, 2014 10:02 am

lilzz wrote:Not sure what's the purpose of DECLARE_BITMAP?
Do you understand what #define does? If so, it should be pretty clear that when someone uses the DECLARE_BITMAP() macro, its expansion will be the declaration of an array variable. It literally declares a bitmap with the chosen name and size.
lilzz wrote:why the need to use bitmap from above?
A bitmap is a data structure that packs truth values into individual bits. It is slower and more complex to access than an array of integers, but uses much less memory.

Are you using layer2 batman routing, or is this just part of your patented approach of reading a random part of the Linux kernel and hoping to understand it? I think you would be better to read a book or tutorial on C, and then try writing and debugging your own programs.

Return to “C/C++”