xref: /netbsd-src/sys/external/bsd/libnv/dist/nv_compat.h (revision 32302d25abec5456e298ff05ca4fde499624bc77)
1*32302d25Schristos /*	$NetBSD: nv_compat.h,v 1.1 2018/09/08 14:02:15 christos Exp $	*/
2*32302d25Schristos 
3*32302d25Schristos /*
4*32302d25Schristos  * Copyright (c) 1987, 1991, 1993
5*32302d25Schristos  *	The Regents of the University of California.  All rights reserved.
6*32302d25Schristos  *
7*32302d25Schristos  * Redistribution and use in source and binary forms, with or without
8*32302d25Schristos  * modification, are permitted provided that the following conditions
9*32302d25Schristos  * are met:
10*32302d25Schristos  * 1. Redistributions of source code must retain the above copyright
11*32302d25Schristos  *    notice, this list of conditions and the following disclaimer.
12*32302d25Schristos  * 2. Redistributions in binary form must reproduce the above copyright
13*32302d25Schristos  *    notice, this list of conditions and the following disclaimer in the
14*32302d25Schristos  *    documentation and/or other materials provided with the distribution.
15*32302d25Schristos  * 3. Neither the name of the University nor the names of its contributors
16*32302d25Schristos  *    may be used to endorse or promote products derived from this software
17*32302d25Schristos  *    without specific prior written permission.
18*32302d25Schristos  *
19*32302d25Schristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*32302d25Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*32302d25Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*32302d25Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*32302d25Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*32302d25Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*32302d25Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*32302d25Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*32302d25Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*32302d25Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*32302d25Schristos  * SUCH DAMAGE.
30*32302d25Schristos  *
31*32302d25Schristos  *	@(#)endian.h	8.1 (Berkeley) 6/11/93
32*32302d25Schristos  */
33*32302d25Schristos 
34*32302d25Schristos #ifndef	_NV_COMPAT_H_
35*32302d25Schristos #define	_NV_COMPAT_H_
36*32302d25Schristos 
37*32302d25Schristos #if defined(__NetBSD__) && (defined(_KERNEL) || defined(_STANDALONE))
38*32302d25Schristos #ifdef _KERNEL
39*32302d25Schristos #include <sys/malloc.h>
40*32302d25Schristos #define	M_NVLIST		M_TEMP
41*32302d25Schristos #endif
42*32302d25Schristos #include <sys/stdarg.h>
43*32302d25Schristos #include <lib/libkern/libkern.h>
44*32302d25Schristos 
45*32302d25Schristos #endif
46*32302d25Schristos 
47*32302d25Schristos #ifndef	__unused
48*32302d25Schristos #define	__unused		__attribute__((__unused__))
49*32302d25Schristos #endif
50*32302d25Schristos 
51*32302d25Schristos #ifndef	__DECONST
52*32302d25Schristos #define	__DECONST(type, var)	((type)(uintptr_t)(const void *)(var))
53*32302d25Schristos #endif
54*32302d25Schristos 
55*32302d25Schristos #ifndef	__printflike
56*32302d25Schristos #define __printflike(fmtarg, firstvararg)	\
57*32302d25Schristos 	    __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
58*32302d25Schristos #endif
59*32302d25Schristos 
60*32302d25Schristos #ifdef __linux__
61*32302d25Schristos #include <endian.h>
62*32302d25Schristos #else
63*32302d25Schristos #include <sys/endian.h>
64*32302d25Schristos #endif
65*32302d25Schristos 
66*32302d25Schristos #ifdef __linux__
67*32302d25Schristos static inline uint32_t
be32dec(const void * buf)68*32302d25Schristos be32dec(const void *buf)
69*32302d25Schristos {
70*32302d25Schristos 	uint8_t const *p = (uint8_t const *)buf;
71*32302d25Schristos 	return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
72*32302d25Schristos }
73*32302d25Schristos 
74*32302d25Schristos static inline uint32_t
le32dec(const void * buf)75*32302d25Schristos le32dec(const void *buf)
76*32302d25Schristos {
77*32302d25Schristos 	uint8_t const *p = (uint8_t const *)buf;
78*32302d25Schristos 	return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
79*32302d25Schristos }
80*32302d25Schristos 
81*32302d25Schristos static inline uint64_t
be64dec(const void * buf)82*32302d25Schristos be64dec(const void *buf)
83*32302d25Schristos {
84*32302d25Schristos 	uint8_t const *p = (uint8_t const *)buf;
85*32302d25Schristos 	return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
86*32302d25Schristos }
87*32302d25Schristos 
88*32302d25Schristos static inline uint64_t
le64dec(const void * buf)89*32302d25Schristos le64dec(const void *buf)
90*32302d25Schristos {
91*32302d25Schristos 	uint8_t const *p = (uint8_t const *)buf;
92*32302d25Schristos 	return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
93*32302d25Schristos }
94*32302d25Schristos #endif
95*32302d25Schristos 
96*32302d25Schristos #endif
97