[Reprinted] linux kernel library functions

 

category

Function name

Features

Function formation

parameter

description

String conversion

simple_strtol

Convert a string to a signed long integer

long simple_strtol (const char * cp, char ** endp, unsigned int base)

cp points to the beginning of the string, endp refers to the position at the end of the string to be analyzed, and base is the base to be used.

 

 

simple_strtoll

Convert a string to a signed long integer

long long simple_strtoll (const char * cp, char ** endp, unsigned int base)

cp points to the beginning of the string, endp refers to the position at the end of the string to be analyzed, and base is the base to be used.

 

 

simple_strtoul

Convert a string to an unsigned long integer

long long simple_strtoul (const char * cp, char ** endp, unsigned int base)

cp points to the beginning of the string, endp refers to the position at the end of the string to be analyzed, and base is the base to be used.

 

 

simple_strtoull

Convert a string to an unsigned long integer

long long simple_strtoull (const char * cp, char ** endp, unsigned int base)

cp points to the beginning of the string, endp refers to the position at the end of the string to be analyzed, and base is the base to be used.

 

 

vsnprintf

Format a string and put it in the cache.

int vsnprintf (char * buf, size_t size, const char * fmt, va_list args)

buf is the buffer to store the result , size is the size of the buffer, fmt is the format string to be used, and args is the parameter of the format string.

 

snprintf

Format a string and put it in the cache.

int snprintf (char * buf, size_t size, const char * fmt, ... ...)

buf is the buffer to store the result , size is the size of the buffer, fmt is the format string, use @ ... to format the format string, ... is a variable parameter.

 

vsprintf

Format a string and put it in the cache.

int vsprintf (char * buf, const char * fmt, va_list args)

 

buf is the buffer to store the result , size is the size of the buffer, fmt is the format string to be used, and args is the parameter of the format string.

 

sprintf

Format a string and put it in the cache.

int sprintf (char * buf, const char * fmt, ... ...)

buf is the buffer to store the result , size is the size of the buffer, fmt is the format string, use @ ... to format the format string, ... is a variable parameter.

 

String manipulation

strcpy

Copy a string ending in NUL

char * strcpy (char * dest, const char * src)

dest is the location of the destination string, and src is the location of the source string.

 

strncpy

Copy a fixed-length NUL- terminated string

char * strncpy (char * dest, const char * src, size_t count)

dest为目的字符串的位置, src为源字符串的位置,count为要拷贝的最大字节数

与用户空间的strncpy不同,这个函数并不用NUL填充缓冲区,如果与源串超过count,则结果以非NUL结束

strcat

把一个以NUL结束的字符串添加到另一个串的末尾

char * strcat (char * dest, const char * src)

 

dest为要添加的字符串, src为源字符串。

 

strncat

把一个定长的、以NUL结束的字符串添加到另一个串的末尾

char * strncat (char * dest, const char * src, size_t count)

dest为要添加的字符串, src为源字符串,count为要拷贝的最大字节数

注意,与strncpy,形成对照, strncat正常结束。

strchr

在一个字符串中查找第一次出现的某个字符

char * strchr (const char * s, int c)

s为被搜索的字符串,c为待搜索的字符。

 

strrchr

在一个字符串中查找最后一次出现的某个字符

char * strrchr (const char * s, int c)

s为被搜索的字符串,c为待搜索的字符。

 

strlen

给出一个字符串的长度

size_t strlen (const char * s)

s为给定的字符串

 

strnlen

给出给定长度字符串的长度

size_t strnlen (const char * s, size_t count)

s为给定的字符串

 

strpbrk

在一个字符串中查找第一次出现的一组字符

char * strpbrk (const char * cs, const char * ct)

cs为被搜索的字符串,ct为待搜索的一组字符

 

strtok

把一个字符串分割为子串

char * strtok (char * s, const char * ct)

 

s为被搜索的字符串,ct为待搜索的子串

注意,一般不提倡用这个函数,而应当用strsep

memset

用给定的值填充内存区

void * memset (void * s, int c, size_t count)

 

s为指向内存区起始的指针,c 要填充的内容,count为内存区的大小

 

I/O空间的访问不能使用memset,而应当使用memset_io

bcopy

把内存的一个区域拷贝到另一个区域

char * bcopy (const char * src, char * dest, int count)

src为源字符串,dest为目的字符串,而count为内存区的大小

注意,这个函数的功能与memcpy相同,这是从BSD遗留下来的,对I/O空间的访问应当用memcpy_toio memcpy_fromio

 

memcpy

把内存的一个区域拷贝到另一个区域

void * memcpy (void * dest, const void * src, size_t count)

dest为目的字符串,Src为源字符串,而count为内存区的大小

I/O空间的访问应当用memcpy_toio memcpy_fromio

 

memmove

把内存的一个区域拷贝到另一个区域

void * memmove (void * dest, const void * src, size_t count)

dest为目的字符串,Src为源字符串,而count为内存区的大小

memcpymemmove处理重叠的区域,而该函数不处理。

memcmp

比较内存的两个区域

int memcmp (const void * cs, const void * ct, size_t count)

cs为一个内存区,ct为另一个内存区,而count为内存区的大小

 

memscan

在一个内存区中查找一个字符

void * memscan (void * addr, int c, size_t size)

addr为内存区,c为要搜索的字符,而size为内存区的大小

返回c第一次出现的地址,如果没有找到c,则向该内存区传递一个字节。

strstr

在以NUL结束的串中查找第一个出现的子串

char * strstr (const char * s1, const char * s2)

s1为被搜索的串,s2为待搜索的串。

 

 

memchr

在一个内存区中查找一个字符

void * memchr (const void * s, int c, size_t n)

s为内存区,为待搜索的字符,n为内存的大小

返回c第一次出现的位置,如果没有找到c,则返回空。

位操作

set_bit

在位图中原子地设置某一位

void set_bit (int nr, volatile void * addr)

nr为要设置的位,addr为位图的起始地址

这个函数是原子操作,如果不需要原子操作,则调用__set_bit函数,nr可以任意大,位图的大小不限于一个字。

__set_bit

在位图中设置某一位

void __set_bit (int nr, volatile void * addr)

nr为要设置的位,addr为位图的起始地址

 

clear_bit

在位图中清某一位

void clear_bit (int nr, volatile void * addr)

nr为要清的位,addr为位图的起始地址

该函数是原子操作,但不具有加锁功能,如果要用于加锁目的,应当调用smp_mb__before_clear_bit smp_mb__after_clear_bit函数,以确保任何改变在其他的处理器上是可见的。

__change_bit

在位图中改变某一位

void __change_bit (int nr, volatile void * addr)

nr为要设置的位,addr为位图的起始地址。

change_bit不同,该函数是非原子操作。

change_bit

在位图中改变某一位

void change_bit (int nr, volatile void * addr)

nr为要设置的位,addr为位图的起始地址。

 

test_and_set_bit

设置某一位并返回该位原来的值

int test_and_set_bit (int nr, volatile void * addr)

nr为要设置的位,addr为位图的起始地址。

该函数是原子操作

 __test_and_set_bit

设置某一位并返回该位原来的值

 

int __test_and_set_bit (int nr, volatile void * addr)

 

nr为要设置的位,addr为位图的起始地址。

该函数是非原子操作,如果这个操作的两个实例发生竞争,则一个成功而另一个失败,因此应当用一个锁来保护对某一位的多个访问。

 

 test_and_clear_bit

 

清某一位,并返回原来的值

int test_and_clear_bit (int nr, volatile void * addr);

nr为要设置的位,addr为位图的起始地址。

该函数是原子操作

__test_and_clear_bit

 

清某一位,并返回原来的值

int __test_and_clear_bit (int nr, volatile void * addr);

nr为要设置的位,addr为位图的起始地址。

该函数为非原子操作

test_and_change_bit

改变某一位并返回该位的新值

int test_and_change_bit (int nr, volatile void * addr)

nr为要设置的位,addr为位图的起始地址。

该函数为原子操作

test_bit

确定某位是否被设置

int test_bit (int nr, const volatile void * addr)

nr为要测试的第几位,addr为位图的起始地址。

 

 find_first_zero_bit

在内存区中查找第一个值为0的位

int find_first_zero_bit (void * addr, unsigned size)

addr为内存区的起始地址,size为要查找的最大长度

返回第一个位为0的位号

find_next_zero_bit

在内存区中查找第一个值为0的位

int find_next_zero_bit (void * addr, int size, int offset)

addr为内存区的起始地址,size为要查找的最大长度,offset开始搜索的起始位号。

 

ffz

在字中查找第一个0

unsigned long ffz (unsigned long word);

word为要搜索的字。

 

ffs

查找第一个已设置的位

 

int ffs (int x)

 

x为要搜索的字。

这个函数的定义方式与Libc中的一样。

hweight32

返回一个N位字的加权平衡值

 

hweight32 ( x)

 

x为要加权的字

个数的加权平衡是这个数所有位的总和。

转自:linux 内核库函数

发布了91 篇原创文章 · 获赞 17 · 访问量 5万+

Guess you like

Origin blog.csdn.net/qq_23327993/article/details/105455727