Home > Backend Development > PHP Tutorial > nginx source code study notes (11) - basic container - ngx_list

nginx source code study notes (11) - basic container - ngx_list

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 09:10:55
Original
790 people have browsed it

ngx_list.{c|h} The structure is very simple. If you have read the previous array introduction, you can skip this section:

[cpp] view plaincopyprint?

  1. typedefstruct ngx_list_part_s ngx_list_part_t;
  2.                                                                                                                                ngx_uint_t nelts; //The actual number of data
  3. ngx_list_part_t *next; //Next data pointer
  4. }; typedef
  5. struct {
  6. ngx_list_part_t *last;
  7. ngx_list_part_t part; //Data part                                                                                                                                   
  8. ngx_uint_t nalloc; //Default number of data
  9. ngx_pool_t *pool; / /The memory pool to which
  10. } ngx_list_t; list Operation:
  11. [cpp] view plaincopyprint?
    1. //Create list
    2. ngx_list_t *
    3. ngx_list_create(ngx_pool_t *pool, ngx_uint_t n, size_t size)
    4. {
    5. ngx_list_t *list;
    6. list = ngx_palloc(pool, sizeof(ngx_list_t)); //Allocate list memory space
    7. if (list == NULL) {
    8. return NULL;
    9. }
    10. list->part.elts = ngx_palloc(pool, n * size); //Allocate some memory space for list data
    11. if (list->part.elts == NULL) {
    12.                                                                                                                              list->part.nelts = 0; The actual number of data is 0
    13. list->part.next = NULL; list->last = &list->part; /The last one is the data itself
    14. list->size = size; list->nalloc = n; Amount of data
    15. list->pool = pool; return
    16. list;
    17. //If you find that it is similar to creating an array here, then it means you have started to get started
    18. }
  12. //Add data You can see that it is basically the same as adding an array. It also returns the address of the data to be added, and then performs the operation
  13. void *
  14. ngx_list_push( ngx_list_t * l)
  15. {
  16. void last = l->last;
  17. if
  18. (last- >nelts == l->nalloc) {
  19.                                                              ​last = ngx_palloc(l->pool, sizeof(ngx_list_part_t));
  20. NULL;
  21.                                                                                             elts = ngx_palloc(l->pool, l->nalloc * l->size); ​​​​​​​return
  22. NULL;                                                                                                                                                                                                  
  23.                                                                                                                                                                                                                                                                                                                                  last->nelts;
  24. last->nelts++;
  25. return
  26. elt; [cpp] view plaincopyprint?
  27. //How to traverse list
  28. part = &list.part;
  29. data = part->elts;
  30. for
  31. (i = 0 ;; i++) {
  32. if
  33. (i >= part->nelts) {
  34. if
  35. (part->next == NULL ) {
  36. break;
  37. }

Part = part->next;

Data = part->elts;
  1. i = 0; }
  2. ... data[i]...
  3. The above introduces the nginx source code study notes (11) - basic container - ngx_list, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template