-10 +

nginx ngx_buf_t

先来看看相关的数据结构:

  1. struct ngx_buf_s {
  2. u_char *pos;
  3. u_char *last;
  4. off_t file_pos;
  5. off_t file_last;
  6. u_char *start; /* start of buffer */
  7. u_char *end; /* end of buffer */
  8. ngx_buf_tag_t tag;
  9. ngx_file_t *file;
  10. ngx_buf_t *shadow;
  11. /* the buf's content could be changed */
  12. unsigned temporary:1;
  13. /*
  14. * the buf's content is in a memory cache or in a read only memory
  15. * and must not be changed
  16. */
  17. unsigned memory:1;
  18. /* the buf's content is mmap()ed and must not be changed */
  19. unsigned mmap:1;
  20. unsigned recycled:1;
  21. unsigned in_file:1;
  22. unsigned flush:1;
  23. unsigned sync:1;
  24. unsigned last_buf:1;
  25. unsigned last_in_chain:1;
  26. unsigned last_shadow:1;
  27. unsigned temp_file:1;
  28. /* STUB */ int num;
  29. };
  30. struct ngx_chain_s {
  31. ngx_buf_t *buf;
  32. ngx_chain_t *next;
  33. };

内存布局图如下:

注意所有的内存分配都应该从 ngx_pool_t 中分配。分配后的 buf 的各个字段的内存含义如下图:

一般buffer结构体可以表示一块内存,内存的起始和结束地址分别用start和end表示,pos和last表示实际的内容。如果内容已经处理过了,pos的位置就可以往后移动。如果读取到新的内容,last的位置就会往后移动。所以buffer可以在多次调用过程中使用。如果last等于end,就说明这块内存已经用完了。如果pos等于last,说明内存已经处理完了。

参考

关于我

85 后程序员, 比较熟悉 Java,JVM,Golang 相关技术栈, 关注 Liunx kernel,目前痴迷于分布式系统的设计和实践。 研究包括但不限于 Docker Kubernetes eBPF 等相关技术。

Blog

Code

Life

Archive

Related Issues not found

Please contact @hushi55 to initialize the comment