代码实现:
stddef.h
#ifndef __STDDEF_H__
#define __STDDEF_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#if defined(__cplusplus)
#define NULL (0)
#else
#define NULL ((void *)0)
#endif
#if (defined(__GNUC__) && (__GNUC__ >= 4))
#define offsetof(type, member) __builtin_offsetof(type, member)
#else
#define offsetof(type, field) ((size_t)(&((type *)0)->field))
#endif
#define container_of(ptr, type, member) ({const typeof(((type *)0)->member) *__mptr = (ptr); (type *)((char *)__mptr - offsetof(type,member));})
#if (defined(__GNUC__) && (__GNUC__ >= 3))
#define likely(expr) (__builtin_expect(!!(expr), 1))
#define unlikely(expr) (__builtin_expect(!!(expr), 0))
#else
#define likely(expr) (!!(expr))
#define unlikely(expr) (!!(expr))
#endif
#define min(a, b) ({typeof(a) _amin = (a); typeof(b) _bmin = (b); (void)(&_amin == &_bmin); _amin < _bmin ? _amin : _bmin;})
#define max(a, b) ({typeof(a) _amax = (a); typeof(b) _bmax = (b); (void)(&_amax == &_bmax); _amax > _bmax ? _amax : _bmax;})
#define clamp(v, a, b) min(max(a, v), b)
#define ifloor(x) ((x) > 0 ? (int)(x) : (int)((x) - 0.9999999999))
#define iround(x) ((x) > 0 ? (int)((x) + 0.5) : (int)((x) - 0.5))
#define iceil(x) ((x) > 0 ? (int)((x) + 0.9999999999) : (int)(x))
#define idiv255(x) ((((int)(x) + 1) * 257) >> 16)
#define X(...) ("" #__VA_ARGS__ "")
#define roundup_pow_of_two(n) \
( \
__builtin_constant_p(n) ? ( \
(n == 1) ? 1 : \
(1UL << (ilog2((n) - 1) + 1)) \
) : \
__roundup_pow_of_two(n) \
)
#define rounddown_pow_of_two(n) \
( \
__builtin_constant_p(n) ? ( \
(1UL << ilog2(n))) : \
__rounddown_pow_of_two(n) \
)
static uint32_t shash(const char * s)
{
uint32_t v = 5381;
if(s)
{
while(*s)
v = (v << 5) + v + (*s++);
}
return v;
}
#ifdef __cplusplus
}
#endif
#endif /* __STDDEF_H__ */
log2.h
#ifndef __LOG2_H__
#define __LOG2_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <string.h>
#include <stdint.h>
static inline __attribute__((always_inline)) int fls(int x)
{
return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
}
static inline __attribute__((always_inline)) int fls64(uint64_t x)
{
uint32_t h = x >> 32;
if (h)
return fls(h) + 32;
return fls(x);
}
static inline __attribute__((always_inline)) unsigned fls_long(unsigned long l)
{
if(sizeof(l) == 4)
return fls(l);
return fls64(l);
}
static inline __attribute__((const)) int __ilog2_u32(uint32_t n)
{
return fls(n) - 1;
}
static inline __attribute__((const)) int __ilog2_u64(uint64_t n)
{
return fls64(n) - 1;
}
static inline __attribute__((const)) int is_power_of_2(unsigned long n)
{
return (n != 0 && ((n & (n - 1)) == 0));
}
static inline __attribute__((const)) unsigned long __roundup_pow_of_two(unsigned long n)
{
return 1UL << fls_long(n - 1);
}
static inline __attribute__((const)) unsigned long __rounddown_pow_of_two(unsigned long n)
{
return 1UL << (fls_long(n) - 1);
}
#define ilog2(n) \
( \
__builtin_constant_p(n) ? ( \
(n) < 2 ? 0 : \
(n) & (1ULL << 63) ? 63 : \
(n) & (1ULL << 62) ? 62 : \
(n) & (1ULL << 61) ? 61 : \
(n) & (1ULL << 60) ? 60 : \
(n) & (1ULL << 59) ? 59 : \
(n) & (1ULL << 58) ? 58 : \
(n) & (1ULL << 57) ? 57 : \
(n) & (1ULL << 56) ? 56 : \
(n) & (1ULL << 55) ? 55 : \
(n) & (1ULL << 54) ? 54 : \
(n) & (1ULL << 53) ? 53 : \
(n) & (1ULL << 52) ? 52 : \
(n) & (1ULL << 51) ? 51 : \
(n) & (1ULL << 50) ? 50 : \
(n) & (1ULL << 49) ? 49 : \
(n) & (1ULL << 48) ? 48 : \
(n) & (1ULL << 47) ? 47 : \
(n) & (1ULL << 46) ? 46 : \
(n) & (1ULL << 45) ? 45 : \
(n) & (1ULL << 44) ? 44 : \
(n) & (1ULL << 43) ? 43 : \
(n) & (1ULL << 42) ? 42 : \
(n) & (1ULL << 41) ? 41 : \
(n) & (1ULL << 40) ? 40 : \
(n) & (1ULL << 39) ? 39 : \
(n) & (1ULL << 38) ? 38 : \
(n) & (1ULL << 37) ? 37 : \
(n) & (1ULL << 36) ? 36 : \
(n) & (1ULL << 35) ? 35 : \
(n) & (1ULL << 34) ? 34 : \
(n) & (1ULL << 33) ? 33 : \
(n) & (1ULL << 32) ? 32 : \
(n) & (1ULL << 31) ? 31 : \
(n) & (1ULL << 30) ? 30 : \
(n) & (1ULL << 29) ? 29 : \
(n) & (1ULL << 28) ? 28 : \
(n) & (1ULL << 27) ? 27 : \
(n) & (1ULL << 26) ? 26 : \
(n) & (1ULL << 25) ? 25 : \
(n) & (1ULL << 24) ? 24 : \
(n) & (1ULL << 23) ? 23 : \
(n) & (1ULL << 22) ? 22 : \
(n) & (1ULL << 21) ? 21 : \
(n) & (1ULL << 20) ? 20 : \
(n) & (1ULL << 19) ? 19 : \
(n) & (1ULL << 18) ? 18 : \
(n) & (1ULL << 17) ? 17 : \
(n) & (1ULL << 16) ? 16 : \
(n) & (1ULL << 15) ? 15 : \
(n) & (1ULL << 14) ? 14 : \
(n) & (1ULL << 13) ? 13 : \
(n) & (1ULL << 12) ? 12 : \
(n) & (1ULL << 11) ? 11 : \
(n) & (1ULL << 10) ? 10 : \
(n) & (1ULL << 9) ? 9 : \
(n) & (1ULL << 8) ? 8 : \
(n) & (1ULL << 7) ? 7 : \
(n) & (1ULL << 6) ? 6 : \
(n) & (1ULL << 5) ? 5 : \
(n) & (1ULL << 4) ? 4 : \
(n) & (1ULL << 3) ? 3 : \
(n) & (1ULL << 2) ? 2 : \
1 ) : \
(sizeof(n) <= 4) ? \
__ilog2_u32(n) : \
__ilog2_u64(n) \
)
#define roundup_pow_of_two(n) \
( \
__builtin_constant_p(n) ? ( \
(n == 1) ? 1 : \
(1UL << (ilog2((n) - 1) + 1)) \
) : \
__roundup_pow_of_two(n) \
)
#define rounddown_pow_of_two(n) \
( \
__builtin_constant_p(n) ? ( \
(1UL << ilog2(n))) : \
__rounddown_pow_of_two(n) \
)
#ifdef __cplusplus
}
#endif
#endif /* __LOG2_H__ */
list.h
#ifndef __LIST_H__
#define __LIST_H__
#ifdef __cplusplus
extern "C" {
#endif
#include "stddef.h"
/*
* Simple doubly linked list implementation.
*
* Some of the internal functions ("__xxx") are useful when
* manipulating whole lists rather than single entries, as
* sometimes we already know the next/prev entries and we can
* generate better code by using them directly rather than
* using the generic single-entry routines.
*/
struct list_head {
struct list_head * next, * prev;
};
#define LIST_HEAD(name) \
struct list_head name = { &(name), &(name) }
static inline void init_list_head(struct list_head * list)
{
list->next = list;
list->prev = list;
}
/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_add(struct list_head * new,
struct list_head * prev,
struct list_head * next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
/**
* list_add - add a new entry
* @new: new entry to be added
* @head: list head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
static inline void list_add(struct list_head * new, struct list_head * head)
{
__list_add(new, head, head->next);
}
/**
* list_add_tail - add a new entry
* @new: new entry to be added
* @head: list head to add it before
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/
static inline void list_add_tail(struct list_head * new, struct list_head * head)
{
__list_add(new, head->prev, head);
}
/*
* Delete a list entry by making the prev/next entries
* point to each other.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
/**
* list_del - deletes entry from list.
* @entry: the element to delete from the list.
* Note: list_empty() on entry does not return true after this, the entry is
* in an undefined state.
*/
static inline void __list_del_entry(struct list_head * entry)
{
__list_del(entry->prev, entry->next);
}
static inline void list_del(struct list_head *entry)
{
__list_del_entry(entry);
entry->next = 0;
entry->prev = 0;
}
/**
* list_replace - replace old entry by new one
* @old : the element to be replaced
* @new : the new element to insert
*
* If @old was empty, it will be overwritten.
*/
static inline void list_replace(struct list_head * old,
struct list_head * new)
{
new->next = old->next;
new->next->prev = new;
new->prev = old->prev;
new->prev->next = new;
}
static inline void list_replace_init(struct list_head * old,
struct list_head * new)
{
list_replace(old, new);
init_list_head(old);
}
/**
* list_del_init - deletes entry from list and reinitialize it.
* @entry: the element to delete from the list.
*/
static inline void list_del_init(struct list_head * entry)
{
__list_del_entry(entry);
init_list_head(entry);
}
/**
* list_move - delete from one list and add as another's head
* @list: the entry to move
* @head: the head that will precede our entry
*/
static inline void list_move(struct list_head * list, struct list_head * head)
{
__list_del_entry(list);
list_add(list, head);
}
/**
* list_move_tail - delete from one list and add as another's tail
* @list: the entry to move
* @head: the head that will follow our entry
*/
static inline void list_move_tail(struct list_head * list,
struct list_head * head)
{
__list_del_entry(list);
list_add_tail(list, head);
}
/**
* list_bulk_move_tail - move a subsection of a list to its tail
* @head: the head that will follow our entry
* @first: first entry to move
* @last: last entry to move, can be the same as first
*
* Move all entries between @first and including @last before @head.
* All three entries must belong to the same linked list.
*/
static inline void list_bulk_move_tail(struct list_head * head,
struct list_head * first,
struct list_head * last)
{
first->prev->next = last->next;
last->next->prev = first->prev;
head->prev->next = first;
first->prev = head->prev;
last->next = head;
head->prev = last;
}
/**
* list_is_first - tests whether @list is the first entry in list @head
* @list: the entry to test
* @head: the head of the list
*/
static inline int list_is_first(const struct list_head * list,
const struct list_head * head)
{
return list->prev == head;
}
/**
* list_is_last - tests whether @list is the last entry in list @head
* @list: the entry to test
* @head: the head of the list
*/
static inline int list_is_last(const struct list_head * list,
const struct list_head * head)
{
return list->next == head;
}
/**
* list_empty - tests whether a list is empty
* @head: the list to test.
*/
static inline int list_empty(const struct list_head * head)
{
return head->next == head;
}
/**
* list_empty_careful - tests whether a list is empty and not being modified
* @head: the list to test
*
* Description:
* tests whether a list is empty _and_ checks that no other CPU might be
* in the process of modifying either member (next or prev)
*
* NOTE: using list_empty_careful() without synchronization
* can only be safe if the only activity that can happen
* to the list entry is list_del_init(). Eg. it cannot be used
* if another CPU could re-list_add() it.
*/
static inline int list_empty_careful(const struct list_head * head)
{
struct list_head * next = head->next;
return (next == head) && (next == head->prev);
}
/**
* list_rotate_left - rotate the list to the left
* @head: the head of the list
*/
static inline void list_rotate_left(struct list_head * head)
{
struct list_head * first;
if (!list_empty(head))
{
first = head->next;
list_move_tail(first, head);
}
}
/**
* list_is_singular - tests whether a list has just one entry.
* @head: the list to test.
*/
static inline int list_is_singular(const struct list_head * head)
{
return !list_empty(head) && (head->next == head->prev);
}
static inline void __list_cut_position(struct list_head * list,
struct list_head * head, struct list_head * entry)
{
struct list_head * new_first = entry->next;
list->next = head->next;
list->next->prev = list;
list->prev = entry;
entry->next = list;
head->next = new_first;
new_first->prev = head;
}
/**
* list_cut_position - cut a list into two
* @list: a new list to add all removed entries
* @head: a list with entries
* @entry: an entry within head, could be the head itself
* and if so we won't cut the list
*
* This helper moves the initial part of @head, up to and
* including @entry, from @head to @list. You should
* pass on @entry an element you know is on @head. @list
* should be an empty list or a list you do not care about
* losing its data.
*
*/
static inline void list_cut_position(struct list_head * list,
struct list_head * head, struct list_head * entry)
{
if(list_empty(head))
return;
if(list_is_singular(head) &&
(head->next != entry && head != entry))
return;
if(entry == head)
init_list_head(list);
else
__list_cut_position(list, head, entry);
}
/**
* list_cut_before - cut a list into two, before given entry
* @list: a new list to add all removed entries
* @head: a list with entries
* @entry: an entry within head, could be the head itself
*
* This helper moves the initial part of @head, up to but
* excluding @entry, from @head to @list. You should pass
* in @entry an element you know is on @head. @list should
* be an empty list or a list you do not care about losing
* its data.
* If @entry == @head, all entries on @head are moved to
* @list.
*/
static inline void list_cut_before(struct list_head * list,
struct list_head * head,
struct list_head * entry)
{
if (head->next == entry)
{
init_list_head(list);
return;
}
list->next = head->next;
list->next->prev = list;
list->prev = entry->prev;
list->prev->next = list;
head->next = entry;
entry->prev = head;
}
static inline void __list_splice(const struct list_head * list,
struct list_head * prev,
struct list_head * next)
{
struct list_head * first = list->next;
struct list_head * last = list->prev;
first->prev = prev;
prev->next = first;
last->next = next;
next->prev = last;
}
/**
* list_splice - join two lists, this is designed for stacks
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice(const struct list_head * list,
struct list_head * head)
{
if(!list_empty(list))
__list_splice(list, head, head->next);
}
/**
* list_splice_tail - join two lists, each list being a queue
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice_tail(struct list_head * list,
struct list_head * head)
{
if(!list_empty(list))
__list_splice(list, head->prev, head);
}
/**
* list_splice_init - join two lists and reinitialise the emptied list.
* @list: the new list to add.
* @head: the place to add it in the first list.
*
* The list at @list is reinitialised
*/
static inline void list_splice_init(struct list_head * list,
struct list_head * head)
{
if(!list_empty(list))
{
__list_splice(list, head, head->next);
init_list_head(list);
}
}
/**
* list_splice_tail_init - join two lists and reinitialise the emptied list
* @list: the new list to add.
* @head: the place to add it in the first list.
*
* Each of the lists is a queue.
* The list at @list is reinitialised
*/
static inline void list_splice_tail_init(struct list_head * list,
struct list_head * head)
{
if(!list_empty(list))
{
__list_splice(list, head->prev, head);
init_list_head(list);
}
}
/**
* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*/
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
/**
* list_first_entry - get the first element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note, that list is expected to be not empty.
*/
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)
/**
* list_last_entry - get the last element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note, that list is expected to be not empty.
*/
#define list_last_entry(ptr, type, member) \
list_entry((ptr)->prev, type, member)
/**
* list_first_entry_or_null - get the first element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note that if the list is empty, it returns NULL.
*/
#define list_first_entry_or_null(ptr, type, member) ({ \
struct list_head * head__ = (ptr); \
struct list_head * pos__ = head__->next; \
pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
})
/**
* list_next_entry - get the next element in list
* @pos: the type * to cursor
* @member: the name of the list_head within the struct.
*/
#define list_next_entry(pos, member) \
list_entry((pos)->member.next, typeof(*(pos)), member)
/**
* list_prev_entry - get the prev element in list
* @pos: the type * to cursor
* @member: the name of the list_head within the struct.
*/
#define list_prev_entry(pos, member) \
list_entry((pos)->member.prev, typeof(*(pos)), member)
/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
/**
* list_for_each_prev - iterate over a list backwards
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; pos != (head); pos = pos->prev)
/**
* list_for_each_safe - iterate over a list safe against removal of list entry
* @pos: the &struct list_head to use as a loop cursor.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
/**
* list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
* @pos: the &struct list_head to use as a loop cursor.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
#define list_for_each_prev_safe(pos, n, head) \
for (pos = (head)->prev, n = pos->prev; \
pos != (head); \
pos = n, n = pos->prev)
/**
* list_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*/
#define list_for_each_entry(pos, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member); \
&pos->member != (head); \
pos = list_next_entry(pos, member))
/**
* list_for_each_entry_reverse - iterate backwards over list of given type.
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*/
#define list_for_each_entry_reverse(pos, head, member) \
for (pos = list_last_entry(head, typeof(*pos), member); \
&pos->member != (head); \
pos = list_prev_entry(pos, member))
/**
* list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
* @pos: the type * to use as a start point
* @head: the head of the list
* @member: the name of the list_head within the struct.
*
* Prepares a pos entry for use as a start point in list_for_each_entry_continue().
*/
#define list_prepare_entry(pos, head, member) \
((pos) ? : list_entry(head, typeof(*pos), member))
/**
* list_for_each_entry_continue - continue iteration over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Continue to iterate over list of given type, continuing after
* the current position.
*/
#define list_for_each_entry_continue(pos, head, member) \
for (pos = list_next_entry(pos, member); \
&pos->member != (head); \
pos = list_next_entry(pos, member))
/**
* list_for_each_entry_continue_reverse - iterate backwards from the given point
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Start to iterate over list of given type backwards, continuing after
* the current position.
*/
#define list_for_each_entry_continue_reverse(pos, head, member) \
for (pos = list_prev_entry(pos, member); \
&pos->member != (head); \
pos = list_prev_entry(pos, member))
/**
* list_for_each_entry_from - iterate over list of given type from the current point
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Iterate over list of given type, continuing from current position.
*/
#define list_for_each_entry_from(pos, head, member) \
for (; &pos->member != (head); \
pos = list_next_entry(pos, member))
/**
* list_for_each_entry_from_reverse - iterate backwards over list of given type
* from the current point
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Iterate backwards over list of given type, continuing from current position.
*/
#define list_for_each_entry_from_reverse(pos, head, member) \
for (; &pos->member != (head); \
pos = list_prev_entry(pos, member))
/**
* list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*/
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member), \
n = list_next_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_next_entry(n, member))
/**
* list_for_each_entry_safe_continue - continue list iteration safe against removal
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Iterate over list of given type, continuing after current point,
* safe against removal of list entry.
*/
#define list_for_each_entry_safe_continue(pos, n, head, member) \
for (pos = list_next_entry(pos, member), \
n = list_next_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_next_entry(n, member))
/**
* list_for_each_entry_safe_from - iterate over list from current point safe against removal
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Iterate over list of given type from current point, safe against
* removal of list entry.
*/
#define list_for_each_entry_safe_from(pos, n, head, member) \
for (n = list_next_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_next_entry(n, member))
/**
* list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Iterate backwards over list of given type, safe against removal
* of list entry.
*/
#define list_for_each_entry_safe_reverse(pos, n, head, member) \
for (pos = list_last_entry(head, typeof(*pos), member), \
n = list_prev_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_prev_entry(n, member))
/**
* list_safe_reset_next - reset a stale list_for_each_entry_safe loop
* @pos: the loop cursor used in the list_for_each_entry_safe loop
* @n: temporary storage used in list_for_each_entry_safe
* @member: the name of the list_head within the struct.
*
* list_safe_reset_next is not safe to use in general if the list may be
* modified concurrently (eg. the lock is dropped in the loop body). An
* exception to this is if the cursor element (pos) is pinned in the list,
* and list_safe_reset_next is called after re-taking the lock and before
* completing the current iteration of the loop body.
*/
#define list_safe_reset_next(pos, n, member) \
n = list_next_entry(pos, member)
/*
* Double linked lists with a single pointer list head.
* Mostly useful for hash tables where the two pointer list head is
* too wasteful.
* You lose the ability to access the tail in O(1).
*/
struct hlist_head {
struct hlist_node * first;
};
struct hlist_node {
struct hlist_node * next, ** pprev;
};
#define HLIST_HEAD(name) \
struct hlist_head name = { .first = NULL }
static inline void init_hlist_head(struct hlist_head * hlist)
{
hlist->first = NULL;
}
static inline void init_hlist_node(struct hlist_node * h)
{
h->next = NULL;
h->pprev = NULL;
}
static inline int hlist_unhashed(const struct hlist_node * h)
{
return !h->pprev;
}
static inline int hlist_empty(const struct hlist_head * h)
{
return !h->first;
}
static inline void __hlist_del(struct hlist_node * n)
{
struct hlist_node * next = n->next;
struct hlist_node ** pprev = n->pprev;
*pprev = next;
if(next)
next->pprev = pprev;
}
static inline void hlist_del(struct hlist_node * n)
{
__hlist_del(n);
n->next = 0;
n->pprev = 0;
}
static inline void hlist_del_init(struct hlist_node * n)
{
if(!hlist_unhashed(n))
{
__hlist_del(n);
init_hlist_node(n);
}
}
static inline void hlist_add_head(struct hlist_node * n, struct hlist_head * h)
{
struct hlist_node * first = h->first;
n->next = first;
if(first)
first->pprev = &n->next;
h->first = n;
n->pprev = &h->first;
}
/* next must be != NULL */
static inline void hlist_add_before(struct hlist_node * n,
struct hlist_node * next)
{
n->pprev = next->pprev;
n->next = next;
next->pprev = &n->next;
*(n->pprev) = n;
}
static inline void hlist_add_behind(struct hlist_node * n,
struct hlist_node * prev)
{
n->next = prev->next;
prev->next = n;
n->pprev = &prev->next;
if(n->next)
n->next->pprev = &n->next;
}
/* after that we'll appear to be on some hlist and hlist_del will work */
static inline void hlist_add_fake(struct hlist_node * n)
{
n->pprev = &n->next;
}
static inline int hlist_fake(struct hlist_node * h)
{
return h->pprev == &h->next;
}
/*
* Check whether the node is the only node of the head without
* accessing head:
*/
static inline int hlist_is_singular_node(struct hlist_node * n, struct hlist_head * h)
{
return !n->next && n->pprev == &h->first;
}
/*
* Move a list from one list head to another. Fixup the pprev
* reference of the first entry if it exists.
*/
static inline void hlist_move_list(struct hlist_head * old,
struct hlist_head * new)
{
new->first = old->first;
if(new->first)
new->first->pprev = &new->first;
old->first = NULL;
}
#define hlist_entry(ptr, type, member) \
container_of(ptr, type, member)
#define hlist_for_each(pos, head) \
for (pos = (head)->first; pos; pos = pos->next)
#define hlist_for_each_safe(pos, n, head) \
for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
pos = n)
#define hlist_entry_safe(ptr, type, member) \
({ typeof(ptr) ____ptr = (ptr); \
____ptr ? hlist_entry(____ptr, type, member) : NULL; \
})
/**
* hlist_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry(pos, head, member) \
for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member); \
pos; \
pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
/**
* hlist_for_each_entry_continue - iterate over a hlist continuing after current point
* @pos: the type * to use as a loop cursor.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry_continue(pos, member) \
for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member); \
pos; \
pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
/**
* hlist_for_each_entry_from - iterate over a hlist continuing from current point
* @pos: the type * to use as a loop cursor.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry_from(pos, member) \
for (; pos; \
pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
/**
* hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
* @pos: the type * to use as a loop cursor.
* @n: another &struct hlist_node to use as temporary storage
* @head: the head for your list.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry_safe(pos, n, head, member) \
for (pos = hlist_entry_safe((head)->first, typeof(*pos), member); \
pos && ({ n = pos->member.next; 1; }); \
pos = hlist_entry_safe(n, typeof(*pos), member))
#ifdef __cplusplus
}
#endif
#endif /* __LIST_H__ */
hmap.h
#ifndef __HMAP_H__
#define __HMAP_H__
#ifdef __cplusplus
extern "C" {
#endif
#include "list.h"
struct hmap_entry_t {
struct hlist_node node;
struct list_head head;
char * key;
void * value;
};
struct hmap_t {
struct hlist_head * hash;
struct list_head list;
unsigned int size;
unsigned int n;
void (*callback)(struct hmap_t * m, struct hmap_entry_t * e);
};
#define hmap_for_each_entry(entry, m) \
list_for_each_entry(entry, &(m)->list, head)
#define hmap_for_each_entry_reverse(entry, m) \
list_for_each_entry_reverse(entry, &(m)->list, head)
struct hmap_t * hmap_alloc(int size, void (*cb)(struct hmap_t *, struct hmap_entry_t *));
void hmap_free(struct hmap_t * m);
void hmap_clear(struct hmap_t * m);
void hmap_add(struct hmap_t * m, const char * key, void * value);
void hmap_remove(struct hmap_t * m, const char * key);
void hmap_sort(struct hmap_t * m);
void * hmap_search(struct hmap_t * m, const char * key);
#ifdef __cplusplus
}
#endif
#endif /* __HMAP_H__ */
hmap.c
#include "stddef.h"
#include <string.h>
#include "list.h"
#include "hmap.h"
#include <stdlib.h>
#include "log2.h"
struct hmap_t * hmap_alloc(int size, void (*cb)(struct hmap_t *, struct hmap_entry_t *))
{
struct hmap_t * m;
int i;
if(size < 16)
size = 16;
if(size & (size - 1))
size = roundup_pow_of_two(size);
m = malloc(sizeof(struct hmap_t));
if(!m)
return NULL;
m->hash = malloc(sizeof(struct hlist_head) * size);
if(!m->hash)
{
free(m);
return NULL;
}
for(i = 0; i < size; i++)
init_hlist_head(&m->hash[i]);
init_list_head(&m->list);
m->size = size;
m->n = 0;
m->callback = cb;
return m;
}
void hmap_free(struct hmap_t * m)
{
if(m)
{
hmap_clear(m);
free(m->hash);
free(m);
}
}
void hmap_clear(struct hmap_t * m)
{
struct hmap_entry_t * pos, * n;
if(m)
{
list_for_each_entry_safe(pos, n, &m->list, head)
{
hlist_del(&pos->node);
list_del(&pos->head);
m->n--;
if(m->callback)
m->callback(m, pos);
free(pos->key);
free(pos);
}
}
}
static void hmap_resize(struct hmap_t * m, unsigned int size)
{
struct hmap_entry_t * pos, * n;
struct hlist_head * hash;
int i;
if(!m)
return;
if(size < 16)
size = 16;
if(size & (size - 1))
size = roundup_pow_of_two(size);
hash = malloc(sizeof(struct hlist_head) * size);
if(!hash)
return;
for(i = 0; i < size; i++)
init_hlist_head(&hash[i]);
list_for_each_entry_safe(pos, n, &m->list, head)
{
hlist_del(&pos->node);
}
free(m->hash);
m->hash = hash;
m->size = size;
list_for_each_entry_safe(pos, n, &m->list, head)
{
hlist_add_head(&pos->node, &m->hash[shash(pos->key) & (m->size - 1)]);
}
}
void hmap_add(struct hmap_t * m, const char * key, void * value)
{
struct hmap_entry_t * pos;
struct hlist_node * n;
if(!m || !key)
return;
hlist_for_each_entry_safe(pos, n, &m->hash[shash(key) & (m->size - 1)], node)
{
if(strcmp(pos->key, key) == 0)
{
if(pos->value != value)
pos->value = value;
return;
}
}
if(m->n > (m->size >> 1))
hmap_resize(m, m->size << 1);
pos = malloc(sizeof(struct hmap_entry_t));
if(!pos)
return;
pos->key = strdup(key);
pos->value = value;
init_hlist_node(&pos->node);
hlist_add_head(&pos->node, &m->hash[shash(pos->key) & (m->size - 1)]);
init_list_head(&pos->head);
list_add_tail(&pos->head, &m->list);
m->n++;
}
void hmap_remove(struct hmap_t * m, const char * key)
{
struct hmap_entry_t * pos;
struct hlist_node * n;
if(!m || !key)
return;
if((m->size > 16) && (m->n < (m->size >> 1)))
hmap_resize(m, m->size >> 1);
hlist_for_each_entry_safe(pos, n, &m->hash[shash(key) & (m->size - 1)], node)
{
if(strcmp(pos->key, key) == 0)
{
hlist_del(&pos->node);
list_del(&pos->head);
m->n--;
free(pos->key);
free(pos);
return;
}
}
}
void * hmap_search(struct hmap_t * m, const char * key)
{
struct hmap_entry_t * pos;
struct hlist_node * n;
if(!m || !key)
return NULL;
hlist_for_each_entry_safe(pos, n, &m->hash[shash(key) & (m->size - 1)], node)
{
if(strcmp(pos->key, key) == 0)
return pos->value;
}
return NULL;
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "hmap.h"
static void hmap_entry_callback(struct hmap_t * m, struct hmap_entry_t * e)
{
if(e)
{
//free(e->value);
}
}
int main(void)
{
struct hmap_t *map = hmap_alloc(128, hmap_entry_callback);
if(map == NULL)
{
perror("fatal error.\n");
return -1;
}
hmap_add(map, "wife", "weixin");
hmap_add(map, "husband", "caozilong");
hmap_add(map, "son", "caoyikang");
printf("wife %s.\n", (char*)hmap_search(map, "wife"));
printf("son %s.\n", (char*)hmap_search(map, "son"));
printf("husband %s.\n", (char*)hmap_search(map, "husband"));
hmap_remove(map, "husband");
printf("husband %s.\n", (char*)hmap_search(map, "husband"));
hmap_free(map);
return 0;
}
添加释放
#include <stdio.h>
#include <stdlib.h>
#include "hmap.h"
static void hmap_entry_callback(struct hmap_t * m, struct hmap_entry_t * e)
{
if(e)
{
printf("%s line %d, key %s value %s.\n", __func__, __LINE__, (char*)e->key, (char*)e->value);
//free(e->key);
}
}
int main(void)
{
struct hmap_t *map = hmap_alloc(128, hmap_entry_callback);
if(map == NULL)
{
perror("fatal error.\n");
return -1;
}
hmap_add(map, "wife", "weixin");
hmap_add(map, "husband", "caozilong");
hmap_add(map, "son", "caoyikang");
printf("wife %s.\n", (char*)hmap_search(map, "wife"));
printf("son %s.\n", (char*)hmap_search(map, "son"));
printf("husband %s.\n", (char*)hmap_search(map, "husband"));
hmap_remove(map, "husband");
printf("husband %s.\n", (char*)hmap_search(map, "husband"));
hmap_free(map);
return 0;
}
LIST_HEAD
1 #include <stdio.h>
2
3 int main(void)
4 {
5 int i = 0;
6
7 for(i = 0, printf("%s line %d, loop left.\n", __func__, __LINE__) ; \
8 printf("%s line %d, loop mid.\n", __func__, __LINE__), i < 3; \
9 i ++, printf("%s line %d, loop right.\n", __func__, __LINE__))
10 {
11 printf("%s line %d, loop body, i %d.\n", __func__, __LINE__, i);
12 }
13
14 return 0;
15 }
~/Workspace/for$ ./a.out
main line 7, loop left.
main line 8, loop mid.
main line 11, loop body, i 0.
main line 9, loop right.
main line 8, loop mid.
main line 11, loop body, i 1.
main line 9, loop right.
main line 8, loop mid.
main line 11, loop body, i 2.
main line 9, loop right.
main line 8, loop mid.
~/Workspace/for$
list_for_each_entry/list_for_each
1.list_for_each和list_for_each_entry都是遍历链表的两个宏,本质上都是for循环。
2.他们做的事情本质上都一样,判断链表项是不是链表头,指向链表的下一项。
3.他们的区别:list_for_each遍历的链表,其链表项不属于某个结构体。或者说不关心它是不是包含在某个结构体中,head为起始位置和判断位置,但是HEAD本身并不会作为循环体中的引用成员。
list_for_each_entry遍历的链表,其每一项都是某个结构体中的成员,单纯遍历链表还不行,还要找到包含这个链表项的结构体的地址,从而为下一步应用该结构体做好准备。和list_for_each类似,head可以属于某个结构体,也可以不属于,作为起始索引位置和结束判断条件,循环体中不会出现此对象。
如果一个已经挂入链表的ENTRY再次加入,会不会有BUG?
当然会,这样做会破坏链表结构,导致不可预知的错误,而且这种错误都非常难查。如果把链表堪称连接件,连接件被挪动位置了,但是原来的位置指向连接件的指针仍然没有变,变化的只是连接件自己发出的指针换了位置,这样的破碎链表是完不成任务的。
Linux内核既有HASH框架实现
Linux hashtable既有实现在头文件include/linux/hashtable.h中。
定义:
DEFINE_HASHTABLE
DEFINE_READ_MOSTLY_HASHTABLE
DECLARE_HASHTABLE
操作
hash_init
hash_add
hash_add_rcu
hash_empty
hash_del
hash_del_rcu
为何HASH表的长度最好是质数?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int hash_func(int idx, int tblsize)
{
return idx % tblsize;
}
#define DATA_NUM (1000000UL)
int main(int argc, char **argv)
{
int i;
int *hash_table;
int interval, table_size, data_size;
float ratio, sum = 0.0f;
if (argc != 3) {
fprintf(stderr, "%s line %d, please use like this ./program interval hashsize.\n",
__func__, __LINE__);
return -1;
}
interval = atoi(argv[1]);
table_size = atoi(argv[2]);
printf("%s line %d, interval %d, table size %d.\n", __func__, __LINE__, interval, table_size);
data_size = sizeof(int) * table_size;
hash_table = malloc(data_size);
if (hash_table == NULL) {
fprintf(stderr, "%s line %d, alloc buffer failure.\n",
__func__, __LINE__);
return -1;
}
memset(hash_table, 0x00, data_size);
for (i = 0; i < DATA_NUM; i++) {
int entry = hash_func(i * interval, table_size);
hash_table[entry]++;
}
for (i = 0; i < table_size; i++) {
float r = hash_table[i] / (double)DATA_NUM;
printf("hash_table[%d]: %f.\n", i, r);
sum += r;
}
printf("sum %f.\n", sum);
free(hash_table);
return 0;
}
./program 程序key间隔 HASH表长度:
如果key间隔是1,则HASH表长度是不是素数无所谓。
但是当key间隔不是1的时候,素数长度的HASH表将会减少冲突可能:
参考文档
算法分析:哈希表的大小为何是素数_哈希函数为什么要质数-优快云博客