Переглянути джерело

[libc] Rewrite unrelicensable portions of stddef.h

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 9 роки тому
джерело
коміт
5d1f351278
1 змінених файлів з 29 додано та 11 видалено
  1. 29
    11
      src/include/stddef.h

+ 29
- 11
src/include/stddef.h Переглянути файл

@@ -1,25 +1,43 @@
1 1
 #ifndef STDDEF_H
2 2
 #define STDDEF_H
3 3
 
4
-FILE_LICENCE ( GPL2_ONLY );
4
+FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
5 5
 
6
-/* for size_t */
7 6
 #include <stdint.h>
8 7
 
8
+/** EFI headers also define NULL */
9 9
 #undef NULL
10
-#define NULL ((void *)0)
11 10
 
12
-#undef offsetof
13
-#if ( defined ( __GNUC__ ) && ( __GNUC__ > 3 ) )
14
-#define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER)
11
+/** Null pointer */
12
+#define NULL ( ( void * ) 0 )
13
+
14
+/**
15
+ * Get offset of a field within a structure
16
+ *
17
+ * @v type		Structure type
18
+ * @v field		Field within structure
19
+ * @ret offset		Offset within structure
20
+ */
21
+#if defined ( __GNUC__ ) && ( __GNUC__ > 3 )
22
+#define offsetof( type, field ) __builtin_offsetof ( type, field )
15 23
 #else
16
-#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
24
+#define offsetof( type, field ) ( ( size_t ) &( ( ( type * ) NULL )->field ) )
17 25
 #endif
18 26
 
19
-#undef container_of
20
-#define container_of(ptr, type, member) ({                      \
21
-	const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
22
-	(type *)( (char *)__mptr - offsetof(type,member) );})
27
+/**
28
+ * Get containing structure
29
+ *
30
+ * @v ptr		Pointer to contained field
31
+ * @v type		Containing structure type
32
+ * @v field		Field within containing structure
33
+ * @ret container	Pointer to containing structure
34
+ */
35
+#define container_of( ptr, type, field ) ( {				\
36
+	type *__container;						\
37
+	const typeof ( __container->field ) *__field = (ptr);		\
38
+	__container = ( ( ( void * ) __field ) -			\
39
+			offsetof ( type, field ) );			\
40
+	__container; } )
23 41
 
24 42
 /* __WCHAR_TYPE__ is defined by gcc and will change if -fshort-wchar is used */
25 43
 #ifndef __WCHAR_TYPE__

Завантаження…
Відмінити
Зберегти