转载一个无依赖的linux的list.h头文件

前端之家收集整理的这篇文章主要介绍了转载一个无依赖的linux的list.h头文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. /**
  2. *
  3. * I grub it from linux kernel source code and fix it for user space
  4. * program. Of course,this is a GPL licensed header file.
  5. *
  6. * Here is a recipe to cook list.h for user space program
  7. *
  8. * 1. copy list.h from linux/include/list.h
  9. * 2. remove
  10. * - #ifdef __KERNE__ and its #endif
  11. * - all #include line
  12. * - prefetch() and rcu related functions
  13. * 3. add macro offsetof() and container_of
  14. *
  15. * - kazutomo@mcs.anl.gov
  16. */
  17. #ifndef _LINUX_LIST_H
  18. #define _LINUX_LIST_H
  19.  
  20. /**
  21. * @name from other kernel headers
  22. */
  23. /*@{*/
  24.  
  25. /**
  26. * Get offset of a member
  27. */
  28. #define offsetof(TYPE,MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  29.  
  30. /**
  31. * Casts a member of a structure out to the containing structure
  32. * @param ptr the pointer to the member.
  33. * @param type the type of the container struct this is embedded in.
  34. * @param member the name of the member within the struct.
  35. *
  36. */
  37. #define container_of(ptr,type,member) ({ \
  38. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  39. (type *)( (char *)__mptr - offsetof(type,member) );})
  40. /*@}*/
  41.  
  42.  
  43. /*
  44. * These are non-NULL pointers that will result in page faults
  45. * under normal circumstances,used to verify that nobody uses
  46. * non-initialized list entries.
  47. */
  48. #define LIST_POISON1 ((void *) 0x00100100)
  49. #define LIST_POISON2 ((void *) 0x00200200)
  50.  
  51. /**
  52. * Simple doubly linked list implementation.
  53. *
  54. * Some of the internal functions ("__xxx") are useful when
  55. * manipulating whole lists rather than single entries,as
  56. * sometimes we already know the next/prev entries and we can
  57. * generate better code by using them directly rather than
  58. * using the generic single-entry routines.
  59. */
  60. struct list_head {
  61. struct list_head *next,*prev;
  62. };
  63.  
  64. #define LIST_HEAD_INIT(name) { &(name),&(name) }
  65.  
  66. #define LIST_HEAD(name) \
  67. struct list_head name = LIST_HEAD_INIT(name)
  68.  
  69. #define INIT_LIST_HEAD(ptr) do { \
  70. (ptr)->next = (ptr); (ptr)->prev = (ptr); \
  71. } while (0)
  72.  
  73. /*
  74. * Insert a new entry between two known consecutive entries.
  75. *
  76. * This is only for internal list manipulation where we know
  77. * the prev/next entries already!
  78. */
  79. static inline void __list_add(struct list_head *new,struct list_head *prev,struct list_head *next)
  80. {
  81. next->prev = new;
  82. new->next = next;
  83. new->prev = prev;
  84. prev->next = new;
  85. }
  86.  
  87. /**
  88. * list_add - add a new entry
  89. * @new: new entry to be added
  90. * @head: list head to add it after
  91. *
  92. * Insert a new entry after the specified head.
  93. * This is good for implementing stacks.
  94. */
  95. static inline void list_add(struct list_head *new,struct list_head *head)
  96. {
  97. __list_add(new,head,head->next);
  98. }
  99.  
  100. /**
  101. * list_add_tail - add a new entry
  102. * @new: new entry to be added
  103. * @head: list head to add it before
  104. *
  105. * Insert a new entry before the specified head.
  106. * This is useful for implementing queues.
  107. */
  108. static inline void list_add_tail(struct list_head *new,head->prev,head);
  109. }
  110.  
  111.  
  112. /*
  113. * Delete a list entry by making the prev/next entries
  114. * point to each other.
  115. *
  116. * This is only for internal list manipulation where we know
  117. * the prev/next entries already!
  118. */
  119. static inline void __list_del(struct list_head * prev,struct list_head * next)
  120. {
  121. next->prev = prev;
  122. prev->next = next;
  123. }
  124.  
  125. /**
  126. * list_del - deletes entry from list.
  127. * @entry: the element to delete from the list.
  128. * Note: list_empty on entry does not return true after this,the entry is
  129. * in an undefined state.
  130. */
  131. static inline void list_del(struct list_head *entry)
  132. {
  133. __list_del(entry->prev,entry->next);
  134. entry->next = LIST_POISON1;
  135. entry->prev = LIST_POISON2;
  136. }
  137.  
  138.  
  139.  
  140. /**
  141. * list_del_init - deletes entry from list and reinitialize it.
  142. * @entry: the element to delete from the list.
  143. */
  144. static inline void list_del_init(struct list_head *entry)
  145. {
  146. __list_del(entry->prev,entry->next);
  147. INIT_LIST_HEAD(entry);
  148. }
  149.  
  150. /**
  151. * list_move - delete from one list and add as another's head
  152. * @list: the entry to move
  153. * @head: the head that will precede our entry
  154. */
  155. static inline void list_move(struct list_head *list,struct list_head *head)
  156. {
  157. __list_del(list->prev,list->next);
  158. list_add(list,head);
  159. }
  160.  
  161. /**
  162. * list_move_tail - delete from one list and add as another's tail
  163. * @list: the entry to move
  164. * @head: the head that will follow our entry
  165. */
  166. static inline void list_move_tail(struct list_head *list,list->next);
  167. list_add_tail(list,head);
  168. }
  169.  
  170. /**
  171. * list_empty - tests whether a list is empty
  172. * @head: the list to test.
  173. */
  174. static inline int list_empty(const struct list_head *head)
  175. {
  176. return head->next == head;
  177. }
  178.  
  179. static inline void __list_splice(struct list_head *list,struct list_head *head)
  180. {
  181. struct list_head *first = list->next;
  182. struct list_head *last = list->prev;
  183. struct list_head *at = head->next;
  184.  
  185. first->prev = head;
  186. head->next = first;
  187.  
  188. last->next = at;
  189. at->prev = last;
  190. }
  191.  
  192. /**
  193. * list_splice - join two lists
  194. * @list: the new list to add.
  195. * @head: the place to add it in the first list.
  196. */
  197. static inline void list_splice(struct list_head *list,struct list_head *head)
  198. {
  199. if (!list_empty(list))
  200. __list_splice(list,head);
  201. }
  202.  
  203. /**
  204. * list_splice_init - join two lists and reinitialise the emptied list.
  205. * @list: the new list to add.
  206. * @head: the place to add it in the first list.
  207. *
  208. * The list at @list is reinitialised
  209. */
  210. static inline void list_splice_init(struct list_head *list,struct list_head *head)
  211. {
  212. if (!list_empty(list)) {
  213. __list_splice(list,head);
  214. INIT_LIST_HEAD(list);
  215. }
  216. }
  217.  
  218. /**
  219. * list_entry - get the struct for this entry
  220. * @ptr: the &struct list_head pointer.
  221. * @type: the type of the struct this is embedded in.
  222. * @member: the name of the list_struct within the struct.
  223. */
  224. #define list_entry(ptr,member) \
  225. container_of(ptr,member)
  226.  
  227. /**
  228. * list_for_each - iterate over a list
  229. * @pos: the &struct list_head to use as a loop counter.
  230. * @head: the head for your list.
  231. */
  232.  
  233. #define list_for_each(pos,head) \
  234. for (pos = (head)->next; pos != (head); \
  235. pos = pos->next)
  236.  
  237. /**
  238. * __list_for_each - iterate over a list
  239. * @pos: the &struct list_head to use as a loop counter.
  240. * @head: the head for your list.
  241. *
  242. * This variant differs from list_for_each() in that it's the
  243. * simplest possible list iteration code,no prefetching is done.
  244. * Use this for code that knows the list to be very short (empty
  245. * or 1 entry) most of the time.
  246. */
  247. #define __list_for_each(pos,head) \
  248. for (pos = (head)->next; pos != (head); pos = pos->next)
  249.  
  250. /**
  251. * list_for_each_prev - iterate over a list backwards
  252. * @pos: the &struct list_head to use as a loop counter.
  253. * @head: the head for your list.
  254. */
  255. #define list_for_each_prev(pos,head) \
  256. for (pos = (head)->prev; prefetch(pos->prev),pos != (head); \
  257. pos = pos->prev)
  258.  
  259. /**
  260. * list_for_each_safe - iterate over a list safe against removal of list entry
  261. * @pos: the &struct list_head to use as a loop counter.
  262. * @n: another &struct list_head to use as temporary storage
  263. * @head: the head for your list.
  264. */
  265. #define list_for_each_safe(pos,n,head) \
  266. for (pos = (head)->next,n = pos->next; pos != (head); \
  267. pos = n,n = pos->next)
  268.  
  269. /**
  270. * list_for_each_entry - iterate over list of given type
  271. * @pos: the type * to use as a loop counter.
  272. * @head: the head for your list.
  273. * @member: the name of the list_struct within the struct.
  274. */
  275. #define list_for_each_entry(pos,member) \
  276. for (pos = list_entry((head)->next,typeof(*pos),member); \
  277. &pos->member != (head); \
  278. pos = list_entry(pos->member.next,member))
  279.  
  280. /**
  281. * list_for_each_entry_reverse - iterate backwards over list of given type.
  282. * @pos: the type * to use as a loop counter.
  283. * @head: the head for your list.
  284. * @member: the name of the list_struct within the struct.
  285. */
  286. #define list_for_each_entry_reverse(pos,member) \
  287. for (pos = list_entry((head)->prev,member); \
  288. &pos->member != (head); \
  289. pos = list_entry(pos->member.prev,member))
  290.  
  291. /**
  292. * list_prepare_entry - prepare a pos entry for use as a start point in
  293. * list_for_each_entry_continue
  294. * @pos: the type * to use as a start point
  295. * @head: the head of the list
  296. * @member: the name of the list_struct within the struct.
  297. */
  298. #define list_prepare_entry(pos,member) \
  299. ((pos) ? : list_entry(head,member))
  300.  
  301. /**
  302. * list_for_each_entry_continue - iterate over list of given type
  303. * continuing after existing point
  304. * @pos: the type * to use as a loop counter.
  305. * @head: the head for your list.
  306. * @member: the name of the list_struct within the struct.
  307. */
  308. #define list_for_each_entry_continue(pos,member) \
  309. for (pos = list_entry(pos->member.next,member); \
  310. &pos->member != (head); \
  311. pos = list_entry(pos->member.next,member))
  312.  
  313. /**
  314. * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  315. * @pos: the type * to use as a loop counter.
  316. * @n: another type * to use as temporary storage
  317. * @head: the head for your list.
  318. * @member: the name of the list_struct within the struct.
  319. */
  320. #define list_for_each_entry_safe(pos,member) \
  321. for (pos = list_entry((head)->next,member),\
  322. n = list_entry(pos->member.next,member); \
  323. &pos->member != (head); \
  324. pos = n,n = list_entry(n->member.next,typeof(*n),member))
  325.  
  326. /**
  327. * list_for_each_entry_safe_continue - iterate over list of given type
  328. * continuing after existing point safe against removal of list entry
  329. * @pos: the type * to use as a loop counter.
  330. * @n: another type * to use as temporary storage
  331. * @head: the head for your list.
  332. * @member: the name of the list_struct within the struct.
  333. */
  334. #define list_for_each_entry_safe_continue(pos,member); \
  335. &pos->member != (head); \
  336. pos = n,member))
  337.  
  338. /**
  339. * list_for_each_entry_safe_reverse - iterate backwards over list of given type safe against
  340. * removal of list entry
  341. * @pos: the type * to use as a loop counter.
  342. * @n: another type * to use as temporary storage
  343. * @head: the head for your list.
  344. * @member: the name of the list_struct within the struct.
  345. */
  346. #define list_for_each_entry_safe_reverse(pos,member) \
  347. for (pos = list_entry((head)->prev,\
  348. n = list_entry(pos->member.prev,n = list_entry(n->member.prev,member))
  349.  
  350.  
  351.  
  352.  
  353. /*
  354. * Double linked lists with a single pointer list head.
  355. * Mostly useful for hash tables where the two pointer list head is
  356. * too wasteful.
  357. * You lose the ability to access the tail in O(1).
  358. */
  359.  
  360. struct hlist_head {
  361. struct hlist_node *first;
  362. };
  363.  
  364. struct hlist_node {
  365. struct hlist_node *next,**pprev;
  366. };
  367.  
  368. #define HLIST_HEAD_INIT { .first = NULL }
  369. #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
  370. #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
  371. #define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL,(ptr)->pprev = NULL)
  372.  
  373. static inline int hlist_unhashed(const struct hlist_node *h)
  374. {
  375. return !h->pprev;
  376. }
  377.  
  378. static inline int hlist_empty(const struct hlist_head *h)
  379. {
  380. return !h->first;
  381. }
  382.  
  383. static inline void __hlist_del(struct hlist_node *n)
  384. {
  385. struct hlist_node *next = n->next;
  386. struct hlist_node **pprev = n->pprev;
  387. *pprev = next;
  388. if (next)
  389. next->pprev = pprev;
  390. }
  391.  
  392. static inline void hlist_del(struct hlist_node *n)
  393. {
  394. __hlist_del(n);
  395. n->next = LIST_POISON1;
  396. n->pprev = LIST_POISON2;
  397. }
  398.  
  399.  
  400. static inline void hlist_del_init(struct hlist_node *n)
  401. {
  402. if (n->pprev) {
  403. __hlist_del(n);
  404. INIT_HLIST_NODE(n);
  405. }
  406. }
  407.  
  408. static inline void hlist_add_head(struct hlist_node *n,struct hlist_head *h)
  409. {
  410. struct hlist_node *first = h->first;
  411. n->next = first;
  412. if (first)
  413. first->pprev = &n->next;
  414. h->first = n;
  415. n->pprev = &h->first;
  416. }
  417.  
  418.  
  419.  
  420. /* next must be != NULL */
  421. static inline void hlist_add_before(struct hlist_node *n,struct hlist_node *next)
  422. {
  423. n->pprev = next->pprev;
  424. n->next = next;
  425. next->pprev = &n->next;
  426. *(n->pprev) = n;
  427. }
  428.  
  429. static inline void hlist_add_after(struct hlist_node *n,struct hlist_node *next)
  430. {
  431. next->next = n->next;
  432. n->next = next;
  433. next->pprev = &n->next;
  434.  
  435. if(next->next)
  436. next->next->pprev = &next->next;
  437. }
  438.  
  439.  
  440.  
  441. #define hlist_entry(ptr,member) container_of(ptr,member)
  442.  
  443. #define hlist_for_each(pos,head) \
  444. for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
  445. pos = pos->next)
  446.  
  447. #define hlist_for_each_safe(pos,head) \
  448. for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
  449. pos = n)
  450.  
  451. /**
  452. * hlist_for_each_entry - iterate over list of given type
  453. * @tpos: the type * to use as a loop counter.
  454. * @pos: the &struct hlist_node to use as a loop counter.
  455. * @head: the head for your list.
  456. * @member: the name of the hlist_node within the struct.
  457. */
  458. #define hlist_for_each_entry(tpos,pos,member) \
  459. for (pos = (head)->first; \
  460. pos && ({ prefetch(pos->next); 1;}) && \
  461. ({ tpos = hlist_entry(pos,typeof(*tpos),member); 1;}); \
  462. pos = pos->next)
  463.  
  464. /**
  465. * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
  466. * @tpos: the type * to use as a loop counter.
  467. * @pos: the &struct hlist_node to use as a loop counter.
  468. * @member: the name of the hlist_node within the struct.
  469. */
  470. #define hlist_for_each_entry_continue(tpos,member) \
  471. for (pos = (pos)->next; \
  472. pos && ({ prefetch(pos->next); 1;}) && \
  473. ({ tpos = hlist_entry(pos,member); 1;}); \
  474. pos = pos->next)
  475.  
  476. /**
  477. * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
  478. * @tpos: the type * to use as a loop counter.
  479. * @pos: the &struct hlist_node to use as a loop counter.
  480. * @member: the name of the hlist_node within the struct.
  481. */
  482. #define hlist_for_each_entry_from(tpos,member) \
  483. for (; pos && ({ prefetch(pos->next); 1;}) && \
  484. ({ tpos = hlist_entry(pos,member); 1;}); \
  485. pos = pos->next)
  486.  
  487. /**
  488. * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  489. * @tpos: the type * to use as a loop counter.
  490. * @pos: the &struct hlist_node to use as a loop counter.
  491. * @n: another &struct hlist_node to use as temporary storage
  492. * @head: the head for your list.
  493. * @member: the name of the hlist_node within the struct.
  494. */
  495. #define hlist_for_each_entry_safe(tpos,member) \
  496. for (pos = (head)->first; \
  497. pos && ({ n = pos->next; 1; }) && \
  498. ({ tpos = hlist_entry(pos,member); 1;}); \
  499. pos = n)
  500.  
  501.  
  502. #endif
PS:使用时遇到list_entry处报错,原因是用到了-std=c99选项,改为-std=gnu99就OK了

猜你在找的设计模式相关文章