Sha256: 66967a866e494d588d851da4784828e56b953423e0cd4e851d4f8815c28f0193

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

#if (_MSC_VER > 1000)
#pragma once
#endif

#ifndef COMMUTIL_H_INCLUDED
#define COMMUTIL_H_INCLUDED

/*	The following macroses are defined:

	MIN(A, B)			- minimum
	MAX(A, B)			- maximum
	IS_POWER_OF_2(NUMBER)		- whether a number is power of 2 and > 0
	PTR_DISTANCE(PTRBEGIN, PTREND)	- the length of [PTRBEGIN, PTREND] range, in bytes
	ALIGN_PTR(PTR, ALIGNMENT)	- syn. for RALIGN_PTR(...)
	RALIGN_PTR(PTR, ALIGNMENT)	- make pointer aligned by moving it towards higher addresses 
	LALIGN_PTR(PTR, ALIGNMENT)	- make pointer aligned by moving it towards lower addresses
	OFFSET_PTR(PTR, DISTANCE)	- offset pointer by given distance in bytes
	ROUND_SIZE_UP(SIZE, K)		- round size up
	ROUND_SIZE_DOWN(SIZE, K)	- round size down

	Note: ALIGN_* and ROUND_* macros are defined with % (integer remainder) hence
	the alignment is not required to be power of 2. Any sane compiler will replace
	% with bitwize operators when alignment is power of 2 (the most common scenario).
*/ 

#include <stddef.h>
#include <stdint.h>

#ifndef MIN
#define MIN(A, B) \
	((A)<(B)?(A):(B))
#endif

#ifndef MAX
#define MAX(A, B) \
	((A)>(B)?(A):(B))
#endif

#define SET_FLAG(f, b) (f) = ((f) & ~(0x1UL << (b))) | (0x1UL << (b))
#define CLEAR_FLAG(f, b) (f) = ((f) & ~(0x1UL << (b)))
#define GET_FLAG(f, b) (((f) >> (b)) & 1)

#define IS_POWER_OF_2(NUMBER) \
	((NUMBER)&((NUMBER)-1)==0 && (NUMBER)>0)

#define PTR_DISTANCE(PTRBEGIN, PTREND) \
	(ptrdiff_t)((uintptr_t)PTREND-(uintptr_t)PTRBEGIN)

#define ALIGN_PTR(PTR, ALIGNMENT) \
	RALIGN_PTR(PTR, ALIGNMENT)

#define RALIGN_PTR(PTR, ALIGNMENT) \
	(void *)((uintptr_t)(PTR)+(ALIGNMENT)-1-((uintptr_t)(PTR)+(ALIGNMENT)-1)%(ALIGNMENT))

#define LALIGN_PTR(PTR, ALIGNMENT) \
	(void *)((uintptr_t)(PTR)-(uintptr_t)(PTR)%(ALIGNMENT))

#define OFFSET_PTR(PTR, OFFSET) \
	(void *)((uintptr_t)(PTR)+(OFFSET))

#define ROUND_SIZE_UP(SIZE, K) \
	((size_t)(SIZE)+(K)-1-((size_t)(SIZE)+(K)-1)%(K))

#define ROUND_SIZE_DOWN(SIZE, K) \
	((size_t)(SIZE)-(size_t)(SIZE)%(K))

#endif

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sedna-0.6.0 vendor/sedna/kernel/common/commutil.h