aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/kernel.h
diff options
context:
space:
mode:
authorHarvey Harrison <harvey.harrison@gmail.com>2008-04-30 03:54:55 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2008-04-30 11:29:53 -0400
commitbdf4bbaaee3d4b8f555658333cbce1affe9070fb (patch)
treee85af55f310d5bd9391cc374bc32def7c5458046 /include/linux/kernel.h
parent354a1f4d99240f53980275416ca3e1ac2ee73d5d (diff)
Add macros similar to min/max/min_t/max_t
Also, change the variable names used in the min/max macros to avoid shadowed variable warnings when min/max min_t/max_t are nested. Small formatting changes to make all the macros have a similar form. [akpm@linux-foundation.org: coding-style fixes] [akpm@linux-foundation.org: fix v4l build] Signed-off-by: Harvey Harrison <harvey.harrison@gmail.com> Cc: Mauro Carvalho Chehab <mchehab@infradead.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Cc: Bartlomiej Zolnierkiewicz <bzolnier@gmail.com> Cc: Jeff Garzik <jeff@garzik.org> Cc: Tejun Heo <htejun@gmail.com> Cc: Michael Buesch <mb@bu3sch.de> Cc: "John W. Linville" <linville@tuxdriver.com> Cc: Miklos Szeredi <miklos@szeredi.hu> Cc: Dmitry Torokhov <dtor@mail.ru> Cc: Jiri Kosina <jkosina@suse.cz> Cc: Arnaldo Carvalho de Melo <acme@ghostprotocols.net> Cc: Randy Dunlap <randy.dunlap@oracle.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'include/linux/kernel.h')
-rw-r--r--include/linux/kernel.h91
1 files changed, 74 insertions, 17 deletions
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 53839ba265ec..4d46e299afb5 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -338,33 +338,90 @@ extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
338#endif /* __LITTLE_ENDIAN */ 338#endif /* __LITTLE_ENDIAN */
339 339
340/* 340/*
341 * min()/max() macros that also do 341 * min()/max()/clamp() macros that also do
342 * strict type-checking.. See the 342 * strict type-checking.. See the
343 * "unnecessary" pointer comparison. 343 * "unnecessary" pointer comparison.
344 */ 344 */
345#define min(x,y) ({ \ 345#define min(x, y) ({ \
346 typeof(x) _x = (x); \ 346 typeof(x) _min1 = (x); \
347 typeof(y) _y = (y); \ 347 typeof(y) _min2 = (y); \
348 (void) (&_x == &_y); \ 348 (void) (&_min1 == &_min2); \
349 _x < _y ? _x : _y; }) 349 _min1 < _min2 ? _min1 : _min2; })
350 350
351#define max(x,y) ({ \ 351#define max(x, y) ({ \
352 typeof(x) _x = (x); \ 352 typeof(x) _max1 = (x); \
353 typeof(y) _y = (y); \ 353 typeof(y) _max2 = (y); \
354 (void) (&_x == &_y); \ 354 (void) (&_max1 == &_max2); \
355 _x > _y ? _x : _y; }) 355 _max1 > _max2 ? _max1 : _max2; })
356
357/**
358 * clamp - return a value clamped to a given range with strict typechecking
359 * @val: current value
360 * @min: minimum allowable value
361 * @max: maximum allowable value
362 *
363 * This macro does strict typechecking of min/max to make sure they are of the
364 * same type as val. See the unnecessary pointer comparisons.
365 */
366#define clamp(val, min, max) ({ \
367 typeof(val) __val = (val); \
368 typeof(min) __min = (min); \
369 typeof(max) __max = (max); \
370 (void) (&__val == &__min); \
371 (void) (&__val == &__max); \
372 __val = __val < __min ? __min: __val; \
373 __val > __max ? __max: __val; })
356 374
357/* 375/*
358 * ..and if you can't take the strict 376 * ..and if you can't take the strict
359 * types, you can specify one yourself. 377 * types, you can specify one yourself.
360 * 378 *
361 * Or not use min/max at all, of course. 379 * Or not use min/max/clamp at all, of course.
380 */
381#define min_t(type, x, y) ({ \
382 type __min1 = (x); \
383 type __min2 = (y); \
384 __min1 < __min2 ? __min1: __min2; })
385
386#define max_t(type, x, y) ({ \
387 type __max1 = (x); \
388 type __max2 = (y); \
389 __max1 > __max2 ? __max1: __max2; })
390
391/**
392 * clamp_t - return a value clamped to a given range using a given type
393 * @type: the type of variable to use
394 * @val: current value
395 * @min: minimum allowable value
396 * @max: maximum allowable value
397 *
398 * This macro does no typechecking and uses temporary variables of type
399 * 'type' to make all the comparisons.
362 */ 400 */
363#define min_t(type,x,y) \ 401#define clamp_t(type, val, min, max) ({ \
364 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; }) 402 type __val = (val); \
365#define max_t(type,x,y) \ 403 type __min = (min); \
366 ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; }) 404 type __max = (max); \
405 __val = __val < __min ? __min: __val; \
406 __val > __max ? __max: __val; })
367 407
408/**
409 * clamp_val - return a value clamped to a given range using val's type
410 * @val: current value
411 * @min: minimum allowable value
412 * @max: maximum allowable value
413 *
414 * This macro does no typechecking and uses temporary variables of whatever
415 * type the input argument 'val' is. This is useful when val is an unsigned
416 * type and min and max are literals that will otherwise be assigned a signed
417 * integer type.
418 */
419#define clamp_val(val, min, max) ({ \
420 typeof(val) __val = (val); \
421 typeof(val) __min = (min); \
422 typeof(val) __max = (max); \
423 __val = __val < __min ? __min: __val; \
424 __val > __max ? __max: __val; })
368 425
369/** 426/**
370 * container_of - cast a member of a structure out to the containing structure 427 * container_of - cast a member of a structure out to the containing structure