xref: /openbsd-src/sys/dev/pci/drm/include/linux/string.h (revision fcde59b201a29a2b4570b00b71e7aa25d61cb5c1)
1 /* Public domain. */
2 
3 #ifndef _LINUX_STRING_H
4 #define _LINUX_STRING_H
5 
6 #include <sys/types.h>
7 #include <sys/systm.h>
8 #include <sys/malloc.h>
9 #include <sys/stdint.h>
10 #include <sys/errno.h>
11 
12 void *memchr_inv(const void *, int, size_t);
13 
14 static inline void *
15 memset32(uint32_t *b, uint32_t c, size_t len)
16 {
17 	uint32_t *dst = b;
18 	while (len--)
19 		*dst++ = c;
20 	return b;
21 }
22 
23 static inline void *
24 memset64(uint64_t *b, uint64_t c, size_t len)
25 {
26 	uint64_t *dst = b;
27 	while (len--)
28 		*dst++ = c;
29 	return b;
30 }
31 
32 static inline void *
33 memset_p(void **p, void *v, size_t n)
34 {
35 #ifdef __LP64__
36 	return memset64((uint64_t *)p, (uintptr_t)v, n);
37 #else
38 	return memset32((uint32_t *)p, (uintptr_t)v, n);
39 #endif
40 }
41 
42 static inline void *
43 kmemdup(const void *src, size_t len, int flags)
44 {
45 	void *p = malloc(len, M_DRM, flags);
46 	if (p)
47 		memcpy(p, src, len);
48 	return (p);
49 }
50 
51 static inline void *
52 kstrdup(const char *str, int flags)
53 {
54 	size_t len;
55 	char *p;
56 
57 	if (str == NULL)
58 		return NULL;
59 
60 	len = strlen(str) + 1;
61 	p = malloc(len, M_DRM, flags);
62 	if (p)
63 		memcpy(p, str, len);
64 	return (p);
65 }
66 
67 static inline int
68 match_string(const char * const *array,  size_t n, const char *str)
69 {
70 	int i;
71 
72 	for (i = 0; i < n; i++) {
73 		if (array[i] == NULL)
74 			break;
75 		if (!strcmp(array[i], str))
76 			return i;
77 	}
78 
79 	return -EINVAL;
80 }
81 
82 #endif
83