Nginx Source Learning Notes (11)--Basic container--ngx_list

Source: Internet
Author: User
ngx_list. The {C|H} structure is very simple, and if you have seen the previous array introduction, this section can be used around:

[CPP] View plaincopyprint?

  1. typedef struct ngx_list_part_s ngx_list_part_t;
  2. struct ngx_list_part_s {
  3. void *elts; //Data area pointer
  4. ngx_uint_t Nelts; //Data actual number
  5. ngx_list_part_t *next; //Next data pointer
  6. };
  7. typedef struct {
  8. ngx_list_part_t *last;
  9. ngx_list_part_t part; //Data section
  10. size_t size; //Single Data size
  11. ngx_uint_t Nalloc; //Preset number of data
  12. ngx_pool_t *pool; //memory pool to which it belongs
  13. } ngx_list_t;


Operation of List:

[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 partial memory space for list data
  11. if (List->part.elts = = NULL) {
  12. return NULL;
  13. }
  14. list->part.nelts = 0; ///Actual data number is 0
  15. List->part.next = NULL; //No next node
  16. List->last = &list->part; //The last one is the data itself
  17. list->size = size; //Initialize the size of each data
  18. List->nalloc = n; //Preset data volume
  19. List->pool = pool; //memory pool to which it belongs
  20. return list; //Return address
  21. //Here if you find it similar to creating an array, then you're getting started.
  22. }
  23. //Add data you can see and add an array basically the same, but also return the address to add the data, and then take action
  24. void *
  25. Ngx_list_push (ngx_list_t *l)
  26. {
  27. void *elt;
  28. ngx_list_part_t *last;
  29. Last = l->last;
  30. if (Last->nelts = = L->nalloc) {
  31. / * The last part was full, allocate a new list part * /
  32. Last = Ngx_palloc (l->pool, sizeof(ngx_list_part_t));
  33. if (last = = NULL) {
  34. return NULL;
  35. }
  36. Last->elts = Ngx_palloc (L->pool, L->nalloc * l->size);
  37. if (Last->elts = = NULL) {
  38. return NULL;
  39. }
  40. last->nelts = 0;
  41. Last->next = NULL;
  42. L->last->next = Last;
  43. L->last = Last;
  44. }
  45. ELT = (char *) Last->elts + l->size * last->nelts;
  46. last->nelts++;
  47. return ELT;
  48. }


[CPP] View plaincopyprint?

    1. //How to Traverse list
    2. Part = &list.part;
    3. data = part->elts;
    4. for (i = 0;; i++) {
    5. if (i >= part->nelts) {
    6. if (Part->next = = NULL) {
    7. Break ;
    8. }
    9. Part = part->next;
    10. data = part->elts;
    11. i = 0;
    12. }
    13. ... data[i] ...
    14. }

The above describes the Nginx source learning notes (11)-The basic container--ngx_list, including the content, I hope the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.