weston/shared/helpers.h

97 lines
2.8 KiB
C
Raw Normal View History

/*
* Copyright © 2015 Samsung Electronics Co., Ltd
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef WESTON_HELPERS_H
#define WESTON_HELPERS_H
#ifdef __cplusplus
extern "C" {
#endif
/**
* @file
* Simple misc helper macros.
*/
/**
* Compile-time computation of number of items in a hardcoded array.
*
* @param a the array being measured.
* @return the number of items hardcoded into the array.
*/
#ifndef ARRAY_LENGTH
#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
#endif
/**
* Returns the smaller of two values.
*
* @param x the first item to compare.
* @param y the second item to compare.
* @return the value that evaluates to lesser than the other.
*/
#ifndef MIN
#define MIN(x,y) (((x) < (y)) ? (x) : (y))
#endif
/**
* Returns a pointer the the containing struct of a given member item.
*
* To demonstrate, the following example retrieves a pointer to
* `example_container` given only its `destroy_listener` member:
*
* @code
* struct example_container {
* struct wl_listener destroy_listener;
* // other members...
* };
*
* void example_container_destroy(struct wl_listener *listener, void *data)
* {
* struct example_container *ctr;
*
* ctr = wl_container_of(listener, ctr, destroy_listener);
* // destroy ctr...
* }
* @endcode
*
* @param ptr A valid pointer to the contained item.
*
* @param type A pointer to the type of content that the list item
* stores. Type does not need be a valid pointer; a null or
* an uninitialised pointer will suffice.
*
* @param member The named location of ptr within the sample type.
*
* @return The container for the specified pointer.
*/
#ifndef container_of
#define container_of(ptr, type, member) ({ \
const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#endif
#ifdef __cplusplus
}
#endif
#endif /* WESTON_HELPERS_H */