Skip to contentSkip to conversation

Boundary calculation

DISCUSSION Ricardo Biehl Pasquali 3 participants 3 messages Jul 16, 2018 – Jul 17, 2018

lore
Share & tools
lore
#0 lore raw

Hello.

I can't get the tricky boundary calculation. Perhaps I'm missing some obvious thing, or maybe it's really some circumvent for a specific case that was not commented.

The current calculation for boundary is:

	boundary = buffer_size;
	while (boundary * 2 <= LONG_MAX - buffer_size)
		boundary *= 2;

I've checked out some repositories, specially alsa-driver in ALSA git, and I realized this calculation exists at least since 2001. I guess it was Jaroslav who wrote.

Visualization of the calculation (B = buffer_size):

LONG_MAX
-------------------------------
\______________/ \__/
B * 4 B
\______/
B * 2
\__/
B

Why isn't a simpler calculation used instead? e.g.:

	/* closest multiple of buffer size less or equal LONG_MAX */
	boundary = LONG_MAX - LONG_MAX % buffer_size;

                           LONG_MAX
-------------------------------
\__________________________/
           B * 6

Cheers!

pasquali

#1 lore raw
Quoted 10 lines · Ricardo Biehl Pasquali

The current calculation for boundary is:

	boundary = buffer_size;
	while (boundary * 2 <= LONG_MAX - buffer_size)
		boundary *= 2;

Why isn't a simpler calculation used instead? e.g.:

	/* closest multiple of buffer size less or equal LONG_MAX */
	boundary = LONG_MAX - LONG_MAX % buffer_size;

To ensure that the factor is a power of two.

Originally, that was not really necessary, but the algorithm is now part of the kernel/userspace interface.

Regards,
Clemens

#2 lore raw
Quoted 10 lines · Clemens Ladisch
Quoted 8 lines · Ricardo Biehl Pasquali

The current calculation for boundary is:

	boundary = buffer_size;
	while (boundary * 2 <= LONG_MAX - buffer_size)
		boundary *= 2;

Why isn't a simpler calculation used instead? e.g.:

Quoted 7 lines
Quoted 2 lines
	/* closest multiple of buffer size less or equal LONG_MAX */
	boundary = LONG_MAX - LONG_MAX % buffer_size;

To ensure that the factor is a power of two.

Originally, that was not really necessary, but the algorithm is now part of the kernel/userspace interface.

Right.

I don't think we have strictly ABI issue even if relaxing this, but I also don't think it would give so much benefit, either.

thanks,

Takashi