{"id":279,"date":"2024-10-22T01:00:34","date_gmt":"2024-10-22T04:00:34","guid":{"rendered":"https:\/\/suspensao.blog.br\/disbelief\/?p=279"},"modified":"2024-10-30T12:23:12","modified_gmt":"2024-10-30T15:23:12","slug":"implementing-malloc-and-free-splitting-large-blocks","status":"publish","type":"post","link":"https:\/\/suspensao.blog.br\/disbelief\/implementing-malloc-and-free-splitting-large-blocks\/","title":{"rendered":"Implementing malloc() and free() \u2014 splitting large blocks"},"content":{"rendered":"\n<p>In the <a href=\"https:\/\/suspensao.blog.br\/disbelief\/implementing-malloc-and-free-old-memory-reused-first\/\">previous post of this series<\/a>, we saw how the order in which we choose memory blocks to reuse can lead to greater or lesser memory consumption, and we changed our functions to avoid this waste. But we need to solve another, even more serious, problem: sometimes, a very large memory block can occupy the space that several smaller blocks could use. Consider the case below, where we allocate a large chunk of memory, deallocate it, and then allocate two much smaller blocks:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void *ptr1 = abmalloc(128);\nvoid *ptr2 = abmalloc(8);\nabfree(ptr1);\nvoid *ptr3 = abmalloc(8);\nvoid *ptr4 = abmalloc(8);<\/code><\/pre>\n\n\n\n<p>Here, we have a free 128-byte memory block, and when we allocate a block of just 8 bytes, all 128 bytes become unavailable. When we allocate another 8-byte block, the heap needs to grow again. This is not an efficient use of memory.<\/p>\n\n\n\n<p>There are at least two popular solutions for this case. One, more efficient, is to use <a href=\"https:\/\/sourceware.org\/glibc\/wiki\/MallocInternals#Arenas_and_Heaps\"><em>bins<\/em><\/a>: lists that group blocks by size. This is a more sophisticated and efficient approach, but more complex. Another option, simpler, is to find a large block and split it into smaller blocks. We&#8217;ll follow this approach.<\/p>\n\n\n\n<p>But remember: simpler doesn&#8217;t exactly mean simple \ud83d\ude09<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Initial Refactoring<\/h3>\n\n\n\n<p>Before we begin, let&#8217;s do a small refactoring. Currently, the <code>header_new()<\/code> function does two things: it allocates more memory for a new block and initializes its header, setting the metadata and pointers to the previous block. The part of initializing the header might be useful, so let&#8217;s extract it. We&#8217;ll create two new functions to improve readability:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>header_plug()<\/code> function, which &#8220;plugs&#8221; the initialized block to the previous and next blocks.<\/li>\n\n\n\n<li>The <code>header_init()<\/code> function, which sets the initial values of the block&#8217;s metadata (size and availability).<\/li>\n<\/ul>\n\n\n\n<p>Here&#8217;s how they look:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void header_init(Header *header, size_t size, bool available) {\n    header-&gt;size = size;\n    header-&gt;available = available;\n}\n\nvoid header_plug(Header *header, Header *previous, Header *next) {\n    header-&gt;previous = previous;\n    if (previous != NULL) {\n        previous-&gt;next = header;\n    }\n    header-&gt;next = next;\n    if (next != NULL) {\n        next-&gt;previous = header;\n    }\n}<\/code><\/pre>\n\n\n\n<p>Now, we just need to modify <code>header_new()<\/code> to use these new functions:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Header *header_new(Header *previous, size_t size, bool available) {\n    Header *header = sbrk(sizeof(Header) + size);\n    header_init(header, size, available);\n    header_plug(header, previous, NULL);\n    return header;\n}<\/code><\/pre>\n\n\n\n<p>(Additionally, we can remove the line <code>last-&gt;previous-&gt;next = last;<\/code> from the <code>abmalloc()<\/code> function, since <code>header_plug()<\/code> now takes care of that.)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Splitting Blocks<\/h3>\n\n\n\n<p>With these tools in hand, let&#8217;s create the <code>header_split()<\/code> function. Given a header and a minimum required size, this function splits the memory block into two if the original block is large enough to contain<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>the required size,<\/li>\n\n\n\n<li>a new header for the new block, and<\/li>\n\n\n\n<li>a bit of extra memory.<\/li>\n<\/ul>\n\n\n\n<p>First, we check if the block is large enough:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Header *header_split(Header *header, size_t size) {\n    size_t original_size = header-&gt;size;\n    if (original_size &gt;= size + sizeof(Header)) {<\/code><\/pre>\n\n\n\n<p>If this condition is met, we split the block. First, we reduce the size of the current block by subtracting the size of a header and the space requested by <code>abmalloc<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>header-&gt;size = original_size - size - sizeof(Header);<\/code><\/pre>\n\n\n\n<p>This leaves a memory space after the current block, which we&#8217;ll use to create the new block. We calculate the pointer for this new block:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Header *new_header = header + sizeof(Header) + header-&gt;size;<\/code><\/pre>\n\n\n\n<p>Now that we have the pointer to the new block, we initialize its header with <code>header_init()<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>header_init(new_header, size, true);<\/code><\/pre>\n\n\n\n<p>And we connect the new block to the previous and next blocks using <code>header_plug()<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>header_plug(new_header, header, header-&gt;next);<\/code><\/pre>\n\n\n\n<p>If the original block was the last one, the new block will now be the last, so we update the <code>last<\/code> pointer:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (header == last) {\n    last = new_header;\n}<\/code><\/pre>\n\n\n\n<p>Finally, we return the new block:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>return new_header;<\/code><\/pre>\n\n\n\n<p>If the original block is not large enough, we simply return the original block:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>} else {\n    return header;\n}\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Updating <code>abmalloc()<\/code><\/h3>\n\n\n\n<p>Now, we just need to go back to the <code>abmalloc()<\/code> function, and in the place where we find a usable block, we invoke <code>header_split()<\/code> to try to split it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (header-&gt;available &amp;&amp; (header-&gt;size &gt;= size)) {\n    header = header_split(header, size);\n    header-&gt;available = false;\n    return header + 1;\n}<\/code><\/pre>\n\n\n\n<p>If the block can be split, the new block will be returned. Otherwise, the original block will be kept and returned as before.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Note on Block Splitting<\/h3>\n\n\n\n<p>Notice that we created the new block at the end of the original block. We could have created it at the beginning, but by creating the new used block at the end, the new free block stays closer to older blocks. This way, it will be found first the next time <code>abmalloc()<\/code> is invoked.<\/p>\n\n\n\n<p>Splitting large memory blocks is a step forward, but there\u2019s an opposite problem: small memory blocks can cause fragmentation, making larger requests cause the heap to grow. We&#8217;ll see how to solve this in the next post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When implementing malloc(), it is important to consider the size of the allocated blocks. For example, reusing blocks too big for smaller requests  cause memory waste. How to solve that? Let&#8217;s see on this post of our series on malloc() and free().<\/p>\n","protected":false},"author":1,"featured_media":280,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"When implementing malloc(), it is important to consider the size of the allocated blocks. For example, reusing blocks too big for smaller requests  cause memory waste. How to solve that? Let's see on this post of our series on malloc() and free().","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[75,76],"tags":[77,82,80,78,47],"class_list":["post-279","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c","category-linux","tag-c","tag-linux","tag-malloc","tag-memory-management","tag-software-development"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/suspensao.blog.br\/disbelief\/wp-content\/uploads\/2024\/10\/Owner_of_general_store_slicing_baloney_Jarreau_Louisiana_October_1938_crop.jpg?fit=1019%2C768&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9Ru6q-4v","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/suspensao.blog.br\/disbelief\/wp-json\/wp\/v2\/posts\/279","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/suspensao.blog.br\/disbelief\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/suspensao.blog.br\/disbelief\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/suspensao.blog.br\/disbelief\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/suspensao.blog.br\/disbelief\/wp-json\/wp\/v2\/comments?post=279"}],"version-history":[{"count":2,"href":"https:\/\/suspensao.blog.br\/disbelief\/wp-json\/wp\/v2\/posts\/279\/revisions"}],"predecessor-version":[{"id":308,"href":"https:\/\/suspensao.blog.br\/disbelief\/wp-json\/wp\/v2\/posts\/279\/revisions\/308"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/suspensao.blog.br\/disbelief\/wp-json\/wp\/v2\/media\/280"}],"wp:attachment":[{"href":"https:\/\/suspensao.blog.br\/disbelief\/wp-json\/wp\/v2\/media?parent=279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/suspensao.blog.br\/disbelief\/wp-json\/wp\/v2\/categories?post=279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/suspensao.blog.br\/disbelief\/wp-json\/wp\/v2\/tags?post=279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}