Xenomai  3.0-rc7
ancillaries.h
1 /*
2  * Copyright (C) 2014 Philippe Gerum <rpm@xenomai.org>.
3  *
4  * Xenomai is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  *
9  * Xenomai is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Xenomai; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17  * 02111-1307, USA.
18  */
19 #ifndef _COBALT_KERNEL_ANCILLARIES_H
20 #define _COBALT_KERNEL_ANCILLARIES_H
21 
22 #include <linux/kernel.h>
23 #include <linux/string.h>
24 #include <cobalt/uapi/kernel/limits.h>
25 
26 #define ksformat(__dst, __len, __fmt, __args...) \
27  ({ \
28  size_t __ret; \
29  __ret = snprintf(__dst, __len, __fmt, ##__args); \
30  if (__ret >= __len) \
31  __dst[__len-1] = '\0'; \
32  __ret; \
33  })
34 
35 #define kasformat(__fmt, __args...) \
36  ({ \
37  kasprintf(GFP_KERNEL, __fmt, ##__args); \
38  })
39 
40 #define kvsformat(__dst, __len, __fmt, __ap) \
41  ({ \
42  size_t __ret; \
43  __ret = vsnprintf(__dst, __len, __fmt, __ap); \
44  if (__ret >= __len) \
45  __dst[__len-1] = '\0'; \
46  __ret; \
47  })
48 
49 #define kvasformat(__fmt, __ap) \
50  ({ \
51  kvasprintf(GFP_KERNEL, __fmt, __ap); \
52  })
53 
54 void __knamecpy_requires_character_array_as_destination(void);
55 
56 #define knamecpy(__dst, __src) \
57  ({ \
58  if (!__builtin_types_compatible_p(typeof(__dst), char[])) \
59  __knamecpy_requires_character_array_as_destination(); \
60  strncpy((__dst), __src, sizeof(__dst) - 1); \
61  __dst[sizeof(__dst) - 1] = '\0'; \
62  __dst; \
63  })
64 
65 #endif /* !_COBALT_KERNEL_ANCILLARIES_H */