PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/sys/lib/libkern/libkern.h

https://gitlab.com/storedmirrors/minix
C Header | 487 lines | 369 code | 51 blank | 67 comment | 31 complexity | a333ce4d4116ccac13aeb45bb3264610 MD5 | raw file
  1. /* $NetBSD: libkern.h,v 1.121 2015/08/30 07:55:45 uebayasi Exp $ */
  2. /*-
  3. * Copyright (c) 1992, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * @(#)libkern.h 8.2 (Berkeley) 8/5/94
  31. */
  32. #ifndef _LIB_LIBKERN_LIBKERN_H_
  33. #define _LIB_LIBKERN_LIBKERN_H_
  34. #ifdef _KERNEL_OPT
  35. #include "opt_diagnostic.h"
  36. #endif
  37. #include <sys/types.h>
  38. #include <sys/inttypes.h>
  39. #include <sys/null.h>
  40. #ifndef LIBKERN_INLINE
  41. #define LIBKERN_INLINE static __inline
  42. #define LIBKERN_BODY
  43. #endif
  44. LIBKERN_INLINE int imax(int, int) __unused;
  45. LIBKERN_INLINE int imin(int, int) __unused;
  46. LIBKERN_INLINE u_int max(u_int, u_int) __unused;
  47. LIBKERN_INLINE u_int min(u_int, u_int) __unused;
  48. LIBKERN_INLINE long lmax(long, long) __unused;
  49. LIBKERN_INLINE long lmin(long, long) __unused;
  50. LIBKERN_INLINE u_long ulmax(u_long, u_long) __unused;
  51. LIBKERN_INLINE u_long ulmin(u_long, u_long) __unused;
  52. LIBKERN_INLINE int abs(int) __unused;
  53. LIBKERN_INLINE long labs(long) __unused;
  54. LIBKERN_INLINE long long llabs(long long) __unused;
  55. LIBKERN_INLINE intmax_t imaxabs(intmax_t) __unused;
  56. LIBKERN_INLINE int isspace(int) __unused;
  57. LIBKERN_INLINE int isascii(int) __unused;
  58. LIBKERN_INLINE int isupper(int) __unused;
  59. LIBKERN_INLINE int islower(int) __unused;
  60. LIBKERN_INLINE int isalpha(int) __unused;
  61. LIBKERN_INLINE int isalnum(int) __unused;
  62. LIBKERN_INLINE int isdigit(int) __unused;
  63. LIBKERN_INLINE int isxdigit(int) __unused;
  64. LIBKERN_INLINE int iscntrl(int) __unused;
  65. LIBKERN_INLINE int isgraph(int) __unused;
  66. LIBKERN_INLINE int isprint(int) __unused;
  67. LIBKERN_INLINE int ispunct(int) __unused;
  68. LIBKERN_INLINE int toupper(int) __unused;
  69. LIBKERN_INLINE int tolower(int) __unused;
  70. #ifdef LIBKERN_BODY
  71. LIBKERN_INLINE int
  72. imax(int a, int b)
  73. {
  74. return (a > b ? a : b);
  75. }
  76. LIBKERN_INLINE int
  77. imin(int a, int b)
  78. {
  79. return (a < b ? a : b);
  80. }
  81. LIBKERN_INLINE long
  82. lmax(long a, long b)
  83. {
  84. return (a > b ? a : b);
  85. }
  86. LIBKERN_INLINE long
  87. lmin(long a, long b)
  88. {
  89. return (a < b ? a : b);
  90. }
  91. LIBKERN_INLINE u_int
  92. max(u_int a, u_int b)
  93. {
  94. return (a > b ? a : b);
  95. }
  96. LIBKERN_INLINE u_int
  97. min(u_int a, u_int b)
  98. {
  99. return (a < b ? a : b);
  100. }
  101. LIBKERN_INLINE u_long
  102. ulmax(u_long a, u_long b)
  103. {
  104. return (a > b ? a : b);
  105. }
  106. LIBKERN_INLINE u_long
  107. ulmin(u_long a, u_long b)
  108. {
  109. return (a < b ? a : b);
  110. }
  111. LIBKERN_INLINE int
  112. abs(int j)
  113. {
  114. return(j < 0 ? -j : j);
  115. }
  116. LIBKERN_INLINE long
  117. labs(long j)
  118. {
  119. return(j < 0 ? -j : j);
  120. }
  121. LIBKERN_INLINE long long
  122. llabs(long long j)
  123. {
  124. return(j < 0 ? -j : j);
  125. }
  126. LIBKERN_INLINE intmax_t
  127. imaxabs(intmax_t j)
  128. {
  129. return(j < 0 ? -j : j);
  130. }
  131. LIBKERN_INLINE int
  132. isspace(int ch)
  133. {
  134. return (ch == ' ' || (ch >= '\t' && ch <= '\r'));
  135. }
  136. LIBKERN_INLINE int
  137. isascii(int ch)
  138. {
  139. return ((ch & ~0x7f) == 0);
  140. }
  141. LIBKERN_INLINE int
  142. isupper(int ch)
  143. {
  144. return (ch >= 'A' && ch <= 'Z');
  145. }
  146. LIBKERN_INLINE int
  147. islower(int ch)
  148. {
  149. return (ch >= 'a' && ch <= 'z');
  150. }
  151. LIBKERN_INLINE int
  152. isalpha(int ch)
  153. {
  154. return (isupper(ch) || islower(ch));
  155. }
  156. LIBKERN_INLINE int
  157. isalnum(int ch)
  158. {
  159. return (isalpha(ch) || isdigit(ch));
  160. }
  161. LIBKERN_INLINE int
  162. isdigit(int ch)
  163. {
  164. return (ch >= '0' && ch <= '9');
  165. }
  166. LIBKERN_INLINE int
  167. isxdigit(int ch)
  168. {
  169. return (isdigit(ch) ||
  170. (ch >= 'A' && ch <= 'F') ||
  171. (ch >= 'a' && ch <= 'f'));
  172. }
  173. LIBKERN_INLINE int
  174. iscntrl(int ch)
  175. {
  176. return ((ch >= 0x00 && ch <= 0x1F) || ch == 0x7F);
  177. }
  178. LIBKERN_INLINE int
  179. isgraph(int ch)
  180. {
  181. return (ch != ' ' && isprint(ch));
  182. }
  183. LIBKERN_INLINE int
  184. isprint(int ch)
  185. {
  186. return (ch >= 0x20 && ch <= 0x7E);
  187. }
  188. LIBKERN_INLINE int
  189. ispunct(int ch)
  190. {
  191. return (isprint(ch) && ch != ' ' && !isalnum(ch));
  192. }
  193. LIBKERN_INLINE int
  194. toupper(int ch)
  195. {
  196. if (islower(ch))
  197. return (ch - 0x20);
  198. return (ch);
  199. }
  200. LIBKERN_INLINE int
  201. tolower(int ch)
  202. {
  203. if (isupper(ch))
  204. return (ch + 0x20);
  205. return (ch);
  206. }
  207. #endif
  208. #define __NULL_STMT do { } while (/* CONSTCOND */ 0)
  209. #define __KASSERTSTR "kernel %sassertion \"%s\" failed: file \"%s\", line %d "
  210. #ifdef NDEBUG /* tradition! */
  211. #define assert(e) ((void)0)
  212. #else
  213. #define assert(e) (__predict_true((e)) ? (void)0 : \
  214. kern_assert(__KASSERTSTR, "", #e, __FILE__, __LINE__))
  215. #endif
  216. #ifdef __COVERITY__
  217. #ifndef DIAGNOSTIC
  218. #define DIAGNOSTIC
  219. #endif
  220. #endif
  221. #ifndef CTASSERT
  222. #define CTASSERT(x) __CTASSERT(x)
  223. #endif
  224. #ifndef CTASSERT_SIGNED
  225. #define CTASSERT_SIGNED(x) __CTASSERT(((typeof(x))-1) < 0)
  226. #endif
  227. #ifndef CTASSERT_UNSIGNED
  228. #define CTASSERT_UNSIGNED(x) __CTASSERT(((typeof(x))-1) >= 0)
  229. #endif
  230. #ifndef DIAGNOSTIC
  231. #define _DIAGASSERT(a) (void)0
  232. #ifdef lint
  233. #define KASSERTMSG(e, msg, ...) /* NOTHING */
  234. #define KASSERT(e) /* NOTHING */
  235. #else /* !lint */
  236. #define KASSERTMSG(e, msg, ...) ((void)0)
  237. #define KASSERT(e) ((void)0)
  238. #endif /* !lint */
  239. #else /* DIAGNOSTIC */
  240. #define _DIAGASSERT(a) assert(a)
  241. #define KASSERTMSG(e, msg, ...) \
  242. (__predict_true((e)) ? (void)0 : \
  243. kern_assert(__KASSERTSTR msg, "diagnostic ", #e, \
  244. __FILE__, __LINE__, ## __VA_ARGS__))
  245. #define KASSERT(e) (__predict_true((e)) ? (void)0 : \
  246. kern_assert(__KASSERTSTR, "diagnostic ", #e, \
  247. __FILE__, __LINE__))
  248. #endif
  249. #ifndef DEBUG
  250. #ifdef lint
  251. #define KDASSERTMSG(e,msg, ...) /* NOTHING */
  252. #define KDASSERT(e) /* NOTHING */
  253. #else /* lint */
  254. #define KDASSERTMSG(e,msg, ...) ((void)0)
  255. #define KDASSERT(e) ((void)0)
  256. #endif /* lint */
  257. #else
  258. #define KDASSERTMSG(e, msg, ...) \
  259. (__predict_true((e)) ? (void)0 : \
  260. kern_assert(__KASSERTSTR msg, "debugging ", #e, \
  261. __FILE__, __LINE__, ## __VA_ARGS__))
  262. #define KDASSERT(e) (__predict_true((e)) ? (void)0 : \
  263. kern_assert(__KASSERTSTR, "debugging ", #e, \
  264. __FILE__, __LINE__))
  265. #endif
  266. /*
  267. * XXX: For compatibility we use SMALL_RANDOM by default.
  268. */
  269. #define SMALL_RANDOM
  270. #ifndef offsetof
  271. #if __GNUC_PREREQ__(4, 0)
  272. #define offsetof(type, member) __builtin_offsetof(type, member)
  273. #else
  274. #define offsetof(type, member) \
  275. ((size_t)(unsigned long)(&(((type *)0)->member)))
  276. #endif
  277. #endif
  278. /*
  279. * Return the container of an embedded struct. Given x = &c->f,
  280. * container_of(x, T, f) yields c, where T is the type of c. Example:
  281. *
  282. * struct foo { ... };
  283. * struct bar {
  284. * int b_x;
  285. * struct foo b_foo;
  286. * ...
  287. * };
  288. *
  289. * struct bar b;
  290. * struct foo *fp = b.b_foo;
  291. *
  292. * Now we can get at b from fp by:
  293. *
  294. * struct bar *bp = container_of(fp, struct bar, b_foo);
  295. *
  296. * The 0*sizeof((PTR) - ...) causes the compiler to warn if the type of
  297. * *fp does not match the type of struct bar::b_foo.
  298. * We skip the validation for coverity runs to avoid warnings.
  299. */
  300. #ifdef __COVERITY__
  301. #define __validate_container_of(PTR, TYPE, FIELD) 0
  302. #else
  303. #define __validate_container_of(PTR, TYPE, FIELD) \
  304. (0 * sizeof((PTR) - &((TYPE *)(((char *)(PTR)) - \
  305. offsetof(TYPE, FIELD)))->FIELD))
  306. #endif
  307. #define container_of(PTR, TYPE, FIELD) \
  308. ((TYPE *)(((char *)(PTR)) - offsetof(TYPE, FIELD)) \
  309. + __validate_container_of(PTR, TYPE, FIELD))
  310. #define MTPRNG_RLEN 624
  311. struct mtprng_state {
  312. unsigned int mt_idx;
  313. uint32_t mt_elem[MTPRNG_RLEN];
  314. uint32_t mt_count;
  315. uint32_t mt_sparse[3];
  316. };
  317. /* Prototypes for which GCC built-ins exist. */
  318. void *memcpy(void *, const void *, size_t);
  319. int memcmp(const void *, const void *, size_t);
  320. void *memset(void *, int, size_t);
  321. #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
  322. #define memcpy(d, s, l) __builtin_memcpy(d, s, l)
  323. #define memcmp(a, b, l) __builtin_memcmp(a, b, l)
  324. #endif
  325. #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
  326. #define memset(d, v, l) __builtin_memset(d, v, l)
  327. #endif
  328. char *strcpy(char *, const char *);
  329. int strcmp(const char *, const char *);
  330. size_t strlen(const char *);
  331. size_t strnlen(const char *, size_t);
  332. char *strsep(char **, const char *);
  333. #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
  334. #define strcpy(d, s) __builtin_strcpy(d, s)
  335. #define strcmp(a, b) __builtin_strcmp(a, b)
  336. #define strlen(a) __builtin_strlen(a)
  337. #endif
  338. /* Functions for which we always use built-ins. */
  339. #ifdef __GNUC__
  340. #define alloca(s) __builtin_alloca(s)
  341. #endif
  342. /* These exist in GCC 3.x, but we don't bother. */
  343. char *strcat(char *, const char *);
  344. size_t strcspn(const char *, const char *);
  345. char *strncpy(char *, const char *, size_t);
  346. char *strncat(char *, const char *, size_t);
  347. int strncmp(const char *, const char *, size_t);
  348. char *strchr(const char *, int);
  349. char *strrchr(const char *, int);
  350. char *strstr(const char *, const char *);
  351. char *strpbrk(const char *, const char *);
  352. size_t strspn(const char *, const char *);
  353. /*
  354. * ffs is an instruction on vax.
  355. */
  356. int ffs(int);
  357. #if __GNUC_PREREQ__(2, 95) && (!defined(__vax__) || __GNUC_PREREQ__(4,1))
  358. #define ffs(x) __builtin_ffs(x)
  359. #endif
  360. void kern_assert(const char *, ...)
  361. __attribute__((__format__(__printf__, 1, 2)));
  362. u_int32_t
  363. inet_addr(const char *);
  364. struct in_addr;
  365. int inet_aton(const char *, struct in_addr *);
  366. char *intoa(u_int32_t);
  367. #define inet_ntoa(a) intoa((a).s_addr)
  368. void *memchr(const void *, int, size_t);
  369. void *memmove(void *, const void *, size_t);
  370. int pmatch(const char *, const char *, const char **);
  371. #ifndef SMALL_RANDOM
  372. void srandom(unsigned long);
  373. char *initstate(unsigned long, char *, size_t);
  374. char *setstate(char *);
  375. #endif /* SMALL_RANDOM */
  376. long random(void);
  377. void mi_vector_hash(const void * __restrict, size_t, uint32_t,
  378. uint32_t[3]);
  379. void mtprng_init32(struct mtprng_state *, uint32_t);
  380. void mtprng_initarray(struct mtprng_state *, const uint32_t *, size_t);
  381. uint32_t mtprng_rawrandom(struct mtprng_state *);
  382. uint32_t mtprng_random(struct mtprng_state *);
  383. int scanc(u_int, const u_char *, const u_char *, int);
  384. int skpc(int, size_t, u_char *);
  385. int strcasecmp(const char *, const char *);
  386. size_t strlcpy(char *, const char *, size_t);
  387. size_t strlcat(char *, const char *, size_t);
  388. int strncasecmp(const char *, const char *, size_t);
  389. u_long strtoul(const char *, char **, int);
  390. long long strtoll(const char *, char **, int);
  391. unsigned long long strtoull(const char *, char **, int);
  392. intmax_t strtoimax(const char *, char **, int);
  393. uintmax_t strtoumax(const char *, char **, int);
  394. intmax_t strtoi(const char * __restrict, char ** __restrict, int, intmax_t,
  395. intmax_t, int *);
  396. uintmax_t strtou(const char * __restrict, char ** __restrict, int, uintmax_t,
  397. uintmax_t, int *);
  398. int snprintb(char *, size_t, const char *, uint64_t);
  399. int snprintb_m(char *, size_t, const char *, uint64_t, size_t);
  400. int kheapsort(void *, size_t, size_t, int (*)(const void *, const void *),
  401. void *);
  402. uint32_t crc32(uint32_t, const uint8_t *, size_t);
  403. #if __GNUC_PREREQ__(4, 5) \
  404. && (defined(__alpha_cix__) || defined(__mips_popcount))
  405. #define popcount __builtin_popcount
  406. #define popcountl __builtin_popcountl
  407. #define popcountll __builtin_popcountll
  408. #define popcount32 __builtin_popcount
  409. #define popcount64 __builtin_popcountll
  410. #else
  411. unsigned int popcount(unsigned int) __constfunc;
  412. unsigned int popcountl(unsigned long) __constfunc;
  413. unsigned int popcountll(unsigned long long) __constfunc;
  414. unsigned int popcount32(uint32_t) __constfunc;
  415. unsigned int popcount64(uint64_t) __constfunc;
  416. #endif
  417. void *explicit_memset(void *, int, size_t);
  418. int consttime_memequal(const void *, const void *, size_t);
  419. #ifdef notyet
  420. /*
  421. * LZF hashtable/state size: on uncompressible data and on a system with
  422. * a sufficiently large d-cache, a larger table produces a considerable
  423. * speed benefit. On systems with small memory and caches, however...
  424. */
  425. #if defined(__vax__) || defined(__m68k__)
  426. #define LZF_HLOG 14
  427. #else
  428. #define LZF_HLOG 15
  429. #endif
  430. typedef const uint8_t *LZF_STATE[1 << LZF_HLOG];
  431. unsigned int lzf_compress_r (const void *const, unsigned int, void *,
  432. unsigned int, LZF_STATE);
  433. unsigned int lzf_decompress (const void *const, unsigned int, void *,
  434. unsigned int);
  435. #endif
  436. #endif /* !_LIB_LIBKERN_LIBKERN_H_ */