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