linux kernel中的一些utility API---(1)

来源:互联网 发布:mysql 时间小于等于 编辑:程序博客网 时间:2024/04/30 11:20


===========================================================================================================


--- index:


=> How to get current registers? - _RET_IP_, _THIS_IP_


=> sort array - sort() in /lib/sort.c


=> <linux/math64.h> - 64-bit division


=> <linux/textsearch.h> - text search library




===========================================================================================================


@@ How to get current registers? - _RET_IP_, _THIS_IP_


#
# /include/linux/kernel.h
#


#define _RET_IP_(unsigned long)__builtin_return_address(0) # This is the built in support from gcc.
#define _THIS_IP_  ({ __label__ __here; __here: (unsigned long)&&__here; })# A trick




===========================================================================================================


@@ sort array - sort() in /lib/sort.c


#
# <linux/sort.h>
#
# /lib/sort.c
#


/**
 * sort - sort an array of elements
 * @base: pointer to data to sort
 * @num: number of elements
 * @size: size of each element
 * @cmp_func: pointer to comparison function
 * @swap_func: pointer to swap function or NULL
 *
 * This function does a heapsort on the given array. You may provide a
 * swap_func function optimized to your element type.
 *
 * Sorting time is O(n log n) both on average and worst-case. While
 * qsort is about 20% faster on average, it suffers from exploitable
 * O(n*n) worst-case behavior and extra memory requirements that make
 * it less suitable for kernel use.
 */


void sort(void *base, size_t num, size_t size,
 int (*cmp_func)(const void *, const void *),
 void (*swap_func)(void *, void *, int size))


#
# "swap_func" is to swap "src" and "dst" content.
#
# If "swap_func" is NULL, then sort()'s default one is:
# "src" <- memcpy() -> "dst"
#




===========================================================================================================


@@ <linux/math64.h> - 64-bit division


#
# 64-bit division may need to conform to special rules on some $(arch).
#
# [*] For example, Huizhao once fixed a WIFI driver crash bug on mips32 platform, which was 64-bit division:
#
# u64 a, b, c;
#
# c = a / b;
#
# It cause some magic crash with symbol like "_div". It seems to be that gcc may generate some error ASM
# instructions for 64-bit division on some 32-bit $(arch), or cause unaligned exception, or other possiblities.
#
#
# Kernel is aware of that.
#
#
# See the <comment> in /lib/div64.c
#
*
* Generic C version of 64bit/32bit division and modulo, with
* 64bit result and 32bit remainder.
*
* The fast case for (n>>32 == 0) is handled inline by do_div(). 
*
* Code generated for this function might be very inefficient
* for some CPUs. __div64_32() can be overridden by linking arch-specific
* assembly versions such as arch/ppc/lib/div64.S and arch/sh/lib/div64.S.


#
#
# So, the following files:
#
# <linux/math64.h># declare inline function for 64-bit division
#
# <asm/div64.h># $(arch) may provide its own 64-bit division function in ASM.
#
# #if BITS_PER_LONG == 64
#
# ... # For 64-bit $(arch), no need for special handling on 64-bit division.
#
# #elif BITS_PER_LONG == 32
#
# ... # For 32-bit $(arch), if there is no ASM defintion in <asm/div64.h>, then
# # kernel will try to provide C implementation in /lib/div64.c
#
# #endif /* BITS_PER_LONG */
#
#
#
# [?] I only see 64-bit division here, how about 64-bit multiplcation ?
#




===========================================================================================================


@@ <linux/textsearch.h> - text search library


Note these APIs:


textsearch_prepare()


skb_find_text() -> textsearch_find()


textsearch_destroy()




Usage example: <<netfilterIF---MATCH-EXT---common.txt>>- "iptables" - "string" - "xt_string_mt_reg"



===========================================================================================================
0 0
原创粉丝点击