Lely core libraries  2.3.4
util.h File Reference

This is the public header file of the utilities library. More...

#include <lely/features.h>
#include <stddef.h>
Include dependency graph for util.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define ABS(a)   ((a) < 0 ? -(a) : (a))
 Returns the absolute value of a.
 
#define ALIGN(x, a)   ALIGN_MASK((x), (__typeof__(x))(a)-1)
 Rounds x up to the nearest multiple of a. More...
 
#define MIN(a, b)   ((a) < (b) ? (a) : (b))
 Returns the minimum of a and b. More...
 
#define MAX(a, b)   ((a) < (b) ? (b) : (a))
 Returns the maximum of a and b. More...
 
#define countof(a)   (sizeof(a) / sizeof(0 [a]))
 Returns the number of array members in a.
 
#define powerof2(x)   (!((x) & ((x)-1)))
 Returns 1 if x is a power of two, and 0 otherwise. More...
 
#define structof(ptr, type, member)
 Obtains the address of a structure from the address of one of its members. More...
 

Detailed Description

This is the public header file of the utilities library.

Author
J. S. Seldenthuis jseld.nosp@m.enth.nosp@m.uis@l.nosp@m.ely..nosp@m.com

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Definition in file util.h.

Macro Definition Documentation

◆ ALIGN

#define ALIGN (   x,
 
)    ALIGN_MASK((x), (__typeof__(x))(a)-1)

Rounds x up to the nearest multiple of a.

Since the rounding is performed with a bitmask, a MUST be a power of two.

Definition at line 41 of file util.h.

◆ MIN

#define MIN (   a,
 
)    ((a) < (b) ? (a) : (b))

Returns the minimum of a and b.

Guaranteed to return the opposite of MAX(), i.e., if MAX() returns a then MIN() returns b and vice versa.

Definition at line 57 of file util.h.

◆ MAX

#define MAX (   a,
 
)    ((a) < (b) ? (b) : (a))

Returns the maximum of a and b.

Guaranteed to return the opposite of MIN().

Definition at line 65 of file util.h.

◆ powerof2

#define powerof2 (   x)    (!((x) & ((x)-1)))

Returns 1 if x is a power of two, and 0 otherwise.

Note that zero is considered to be a power of two.

Definition at line 78 of file util.h.

◆ structof

#define structof (   ptr,
  type,
  member 
)
Value:
__extension__({ \
const __typeof__(((type *)NULL)->member) *_tmp_ = (ptr); \
((type *)((char *)(_tmp_) - offsetof(type, member))); \
})

Obtains the address of a structure from the address of one of its members.

This macro only works for plain old data structures (PODSs) and performs no type or NULL checking.

Parameters
ptra pointer to the member.
typethe type of the structure.
memberthe name of member.

Definition at line 93 of file util.h.