分享

Nginx源码学习(2)

 waitingnothing 2017-06-22

Ngnix对一些基础的数据结构进行了自己的封装。我首先从最简单的string 这个最基础的方法下手。

主要包括了

Nginx自己的字符串API
内存操作相关
字符串格式化
字符串查找
格式转换
Base64/URL/UTF-8
字符串和数字之间的转换

 

首先看一下他的基础的结构

typedef struct { size_t len; u_char *data; } ngx_str_t;


结构体ngx_str_t 中两个变量:data和len,因此,在使用的时候,其长度也要记住赋值。

 

那么下面有如下方法供其使用。

#define ngx_string(str) { sizeof(str) - 1, (u_char *) str } #define ngx_null_string { 0, NULL } #define ngx_str_set(str, text) (str)->len = sizeof(text) - 1; (str)->data = (u_char *) text #define ngx_str_null(str) (str)->len = 0; (str)->data = NULL #define ngx_tolower(c) (u_char) ((c >= 'A' && c <= 'Z') ? (c | 0x20) : c) #define ngx_toupper(c) (u_char) ((c >= 'a' && c <= 'z') ? (c & ~0x20) : c)


 

#define ngx_strncmp(s1, s2, n) strncmp((const char *) s1, (const char *) s2, n) /* msvc and icc7 compile strcmp() to inline loop */ #define ngx_strcmp(s1, s2) strcmp((const char *) s1, (const char *) s2) #define ngx_strstr(s1, s2) strstr((const char *) s1, (const char *) s2) #define ngx_strlen(s) strlen((const char *) s) #define ngx_strchr(s1, c) strchr((const char *) s1, (int) c)


 

#define ngx_memzero(buf, n) (void) memset(buf, 0, n) #define ngx_memset(buf, c, n) (void) memset(buf, c, n) #if (NGX_MEMCPY_LIMIT) void *ngx_memcpy(void *dst, void *src, size_t n);
#define ngx_cpymem(dst, src, n) (((u_char *) ngx_memcpy(dst, src, n)) (n)) #else

 

这些函数都是加了自己的前缀,可能是作者想让自己的使用标准吧,还有就是发掘几个自己的功能函数,组合一起。


这几个函数都是利用了标准C里的函数直接定义的。

这个函数作用就是找到c在p后边的数据,前提是长度小于last

static ngx_inline u_char * ngx_strlchr(u_char *p, u_char *last, u_char c) { while (p < last) { if (*p == c) { return p; } p ; } return NULL; }

/* * the simple inline cycle copies the variable length strings up to 16 * bytes faster than icc8 autodetecting _intel_fast_memcpy() */ static ngx_inline u_char * ngx_copy(u_char *dst, u_char *src, size_t len) { if (len < 17) { while (len) { *dst = *src ; len--; } return dst; } else { return ngx_cpymem(dst, src, len); } }


取得src在dst后边的地址值。

/* * the simple inline cycle copies the variable length strings up to 16 * bytes faster than icc8 autodetecting _intel_fast_memcpy() */ static ngx_inline u_char * ngx_copy(u_char *dst, u_char *src, size_t len) { if (len < 17) { while (len) { *dst = *src ; len--; } return dst; } else { return ngx_cpymem(dst, src, len); } }

下面的这个函数就是常用的字符串转int型的一个常用函数,核心语句value = value * 10 (*line - '0');

ngx_int_t ngx_atoi(u_char *line, size_t n) { ngx_int_t value; if (n == 0) { return NGX_ERROR; } for (value = 0; n--; line ) { if (*line < '0' || *line > '9') { return NGX_ERROR; } value = value * 10 (*line - '0'); } if (value < 0) { return NGX_ERROR; } else { return value; } }

 

还有很多方法,这里就不一一列举了,不过有几个方法,是对红黑树使用的一些方法。

void ngx_str_rbtree_insert_value(ngx_rbtree_node_t *temp, ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel); ngx_str_node_t *ngx_str_rbtree_lookup(ngx_rbtree_t *rbtree, ngx_str_t *name, uint32_t hash);


跟多文章欢迎反问:http://blog.csdn.net/wallwind

 

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多