Friday, December 19, 2014

Memory Management in H2O

This blogpost (as part of the H2O Advent Calendar 2014) provides a high-level overview of the memory management functions in H2O that can be categorized into four groups.

h2o_mem_alloc, h2o_mem_realloc

They are wrappers of malloc(3) / realloc(3), that calls abort(3) if memory allocation fails. The returned chunks should be freed by calling free(3).

h2o_mem_init_pool, h2o_mem_clear_pool, h2o_mem_alloc_pool

The functions create, clear, and allocate from a memory pool. The term memory pool has several meanings, but in case of H2O the term has been borrowed from Apache; it refers to a memory allocator that frees all associated chunks at once when the destructor (h2o_mem_clear_pool) is being called.

The primary use-case of the functions is to allocate memory that relates to a HTTP request. The request object h2o_req_t has a memory pool associated to it; small chunks of memory that need to be allocated while handling a request should be obtained by calling h2o_mem_alloc_pool instead of h2o_mem_alloc, since the former is generally faster than the latter.

h2o_mem_alloc_shared, h2o_mem_link_shared, h2o_mem_addref_shared, h2o_mem_release_shared

They are the functions to handle ref-counted chunks of memory. Eeach shared chunk has its own dispose callback that gets called when the reference counter reaches zero. A chunk can be optionally associated to a memory pool, so that the reference counter gets decremented when the pool gets flushed.

The functions are used for handling things like headers transferred via HTTP/2, or to for associating a resource that needs a custom dispose callback to a HTTP request through the use of the memory pool.

h2o_buffer_init, h2o_buffer_dispose, h2o_buffer_reserve, h2o_buffer_consume, h2o_buffer_link_to_pool

The functions provide access to buffer, that can hold any length of octets. They internally use malloc(3) / realloc(3) for handling short buffers, and switch to using temporary-file-backed mmap(2) when the length of the buffer reaches a predefined threshold (default: 32MB). A buffer can also be associated to memory pool by calling the h2o_buffer_link_to_pool function.

The primary use-case of the buffer is to store incoming HTTP requests and POST contents (as it can be used to hold huge chunks on 64-bit systems since it switches to temporary-file-backed memory as described).

h2o_vector_reserve

The function reserves given number of slots for H2O_VECTOR which is a variable length array of an arbitrary type of data. Either h2o_mem_realloc or the memory pool can be used as the underlying memory allocator (in the former case, the allocated memory should be manually freed by the caller). The structure is initialized by zero-filling it.

The vector is used everywhere, from storing a list of HTTP headers to a list of configuration directives.

For details, please refer to their doc-comment and the definitions in include/h2o/memory.h and lib/memory.c.

17 comments:

  1. I like this a lot. Thank you for sharing. I'm always looking for upcycles like this. In the end, you don't know it was a shipping pallet to begin with!
    t20 worldcup 2016 Schedule
    KKR New Players List
    IPL Live Streaming 2016 Score Watch Online

    ReplyDelete
  2. A couple of months ago I discovered another website that talked in depth about this topic. I am glad you were able to shed some light on what?s really happening out there. Some webistes are overtly biased towards things like this. Where do you think the industry is going in response to this? 5tb external hard drive

    ReplyDelete
  3. This is very interesting, You’re a very skilled blogger. I have joined your feed and look forward to seeking more of your fantastic post. Also, I have shared your web site in my social networks!
    Regards - www.office.com/setup
    www.office.com/setup

    ReplyDelete
  4. Good post. Thanks for sharing very useful information.
    happy eid mubarak quotes for friends

    ReplyDelete
  5. i am for the first time here. I found this board and I in finding It truly helpful & it helped me out a lot. I hope to present something back and help others such as you helped me. Thanks for share. By the way Visit here to know Eid Mubarak 2018 Date & Time for USA,UK, Dubai and others country.
    Also Download Eid Mubarak 2018 Images
    Eid Mubarak 2018 Wallpapers

    ReplyDelete
  6. Pick the correct name for your business; make business cards, stationary, recommendations, customer understandings and a site. Delta V

    ReplyDelete
  7. Sample Assignment, for about a decade, has offered a year-round online assignment. To look at our services from a different perspective, we are also there as online academic assistants working providing reference assignments on students’ behalves. With our Accounting, Nursing, Management, Marketing, Law, IT assignment help services, we have been, since then, providing excellent and outclassing assignment help. Be it any package you choose on our website, you would find yourself opting for a convenient and affordable assignment help Australia package. The research, industry, and academic experts we possess have dealt with a number of pan-industry scenarios and, at the same time, dealt with thesis, CDRs, Essays, Assignments, Homework, Dissertations, Case Study analysis, Projects, etc. Ever since our inception, students have found our services reliable and hence voted us the No.1 Assignment help online  provider in Australia. We have made sure that the process remains grounded and already slashed just because we value the money in a student’s already-shallow pocket. With our services, you can avail of our convenient online assignment help and samples available on our website for free. Are you ready, for the HD?

    ReplyDelete
  8. For students enrolled in Nursing, it gets a bit difficult to collect everything and prepare perfect assignments. That is why we at Online Assignment Expert aims at providing effective nursing assignment help  by experts relating to the same domain. Not just sticking to a single domain, we have extended our academic services to accounting by delivering supreme quality accounting assignment help  services at pocket friendly prices.
    Finding assistance is easy with Online Assignment Expert. So, contact us by mailing us at contact@onlineassignmentexpert and get promodel simulation assignment help alongside with our unique value added services that includes proofreading, plagiarism check, expert guidance sessions, etc. in no time.

    ReplyDelete

Note: Only a member of this blog may post a comment.