Blame include/lttng/align.h

Packit c04fcb
#ifndef _UST_ALIGN_H
Packit c04fcb
#define _UST_ALIGN_H
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * lttng/align.h
Packit c04fcb
 *
Packit c04fcb
 * (C) Copyright 2010-2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Packit c04fcb
 *
Packit c04fcb
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit c04fcb
 * of this software and associated documentation files (the "Software"), to deal
Packit c04fcb
 * in the Software without restriction, including without limitation the rights
Packit c04fcb
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Packit c04fcb
 * copies of the Software, and to permit persons to whom the Software is
Packit c04fcb
 * furnished to do so, subject to the following conditions:
Packit c04fcb
 *
Packit c04fcb
 * The above copyright notice and this permission notice shall be included in
Packit c04fcb
 * all copies or substantial portions of the Software.
Packit c04fcb
 *
Packit c04fcb
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit c04fcb
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit c04fcb
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit c04fcb
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit c04fcb
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Packit c04fcb
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Packit c04fcb
 * SOFTWARE.
Packit c04fcb
 */
Packit c04fcb
Packit c04fcb
#include <lttng/bug.h>
Packit c04fcb
#include <unistd.h>
Packit c04fcb
#include <limits.h>
Packit c04fcb
Packit c04fcb
#ifndef PAGE_SIZE	/* Cygwin limits.h defines its own PAGE_SIZE */
Packit c04fcb
#define PAGE_SIZE		sysconf(_SC_PAGE_SIZE)
Packit c04fcb
#endif
Packit c04fcb
Packit c04fcb
#define PAGE_MASK		(~(PAGE_SIZE - 1))
Packit c04fcb
#define __ALIGN_MASK(v, mask)	(((v) + (mask)) & ~(mask))
Packit c04fcb
#define ALIGN(v, align)		__ALIGN_MASK(v, (__typeof__(v)) (align) - 1)
Packit c04fcb
#define PAGE_ALIGN(addr)	ALIGN(addr, PAGE_SIZE)
Packit c04fcb
Packit c04fcb
/**
Packit c04fcb
 * offset_align - Calculate the offset needed to align an object on its natural
Packit c04fcb
 *                alignment towards higher addresses.
Packit c04fcb
 * @align_drift:  object offset from an "alignment"-aligned address.
Packit c04fcb
 * @alignment:    natural object alignment. Must be non-zero, power of 2.
Packit c04fcb
 *
Packit c04fcb
 * Returns the offset that must be added to align towards higher
Packit c04fcb
 * addresses.
Packit c04fcb
 */
Packit c04fcb
#define offset_align(align_drift, alignment)				       \
Packit c04fcb
	({								       \
Packit c04fcb
		LTTNG_BUILD_RUNTIME_BUG_ON((alignment) == 0		       \
Packit c04fcb
				   || ((alignment) & ((alignment) - 1)));      \
Packit c04fcb
		(((alignment) - (align_drift)) & ((alignment) - 1));	       \
Packit c04fcb
	})
Packit c04fcb
Packit c04fcb
/**
Packit c04fcb
 * offset_align_floor - Calculate the offset needed to align an object
Packit c04fcb
 *                      on its natural alignment towards lower addresses.
Packit c04fcb
 * @align_drift:  object offset from an "alignment"-aligned address.
Packit c04fcb
 * @alignment:    natural object alignment. Must be non-zero, power of 2.
Packit c04fcb
 *
Packit c04fcb
 * Returns the offset that must be substracted to align towards lower addresses.
Packit c04fcb
 */
Packit c04fcb
#define offset_align_floor(align_drift, alignment)			       \
Packit c04fcb
	({								       \
Packit c04fcb
		LTTNG_BUILD_RUNTIME_BUG_ON((alignment) == 0		       \
Packit c04fcb
				   || ((alignment) & ((alignment) - 1)));      \
Packit c04fcb
		(((align_drift) - (alignment)) & ((alignment) - 1));	       \
Packit c04fcb
	})
Packit c04fcb
Packit c04fcb
#endif /* _UST_ALIGN_H */