Home > Article > Backend Development > Nginx data structure-integer and string
Nginx data structure – integer and string
Tags: Nginx data structure
Taking into account cross-platform, high efficiency, and unified specifications, Nginx encapsulates many data structures, most of which we use in other development projects Of course, there are some frequently used containers, and of course there are some complex containers. In each article, the author will analyze and practice one or two points.
Integer encapsulation
<code><span>typedef</span> intptr_t ngx_int_t; <span>typedef</span> uintptr_t ngx_uint_t'</code>
String type
In Nginx, Ngx_str_t is used to represent a string, and its definition is as follows:
<code><span>typedef</span> struct { size_t len; u_char *<span><span>data</span>;</span> } ngx_str_t;</code>
We can see that it is a simple structure with only two members, the data pointer Points to the starting address of the string, and len represents the length of the string.
You may be confused here. A string in C language only needs a pointer to represent it. Why is a length needed here? This is because the strings we often talk about in C language actually start with '
So what are the benefits of doing this? As a network server, Nginx certainly considers the need to facilitate development. In network requests, what we are most exposed to are URL addresses, request header information, request entities, etc. Take URL addresses, for example, user requests:
<code>GET /test/<span>string</span>?<span>a</span>=<span>1</span>&b=<span>2</span><span>http</span>/<span>1.1</span>\r\n</code>Then if we use a Ngx_str_t structure to store this value, now we want to get the request type, is it GET, POST or PUT? We don't need to copy a memory. All we have to do is make a new ngx_str_t. The data pointer inside points to the same address as the original ngx_str_t, and then change len to 3.
Of course, this is just the simplest application. The string type is a basic type that is widely used in almost all business systems and network frameworks. A good design structure is an important guarantee for Nginx's low memory consumption.