{"id":718,"date":"2011-05-12T12:43:01","date_gmt":"2011-05-12T01:43:01","guid":{"rendered":"http:\/\/blog.mozilla.org\/nnethercote\/?p=718"},"modified":"2011-05-12T12:43:01","modified_gmt":"2011-05-12T01:43:01","slug":"duplicated-abstraction-layers-in-firefox","status":"publish","type":"post","link":"https:\/\/blog.mozilla.org\/nnethercote\/2011\/05\/12\/duplicated-abstraction-layers-in-firefox\/","title":{"rendered":"Duplicated abstraction layers in Firefox"},"content":{"rendered":"<p>Just about every operating system provides a mechanism for directly allocating and deallocating memory at the page level (ie. not malloc\/free or new\/delete).\u00a0 The functions to do this vary from OS to OS:<\/p>\n<ul>\n<li>Windows: VirtualAlloc\/FreeAlloc.<\/li>\n<li>Posix (e.g. Mac and Linux): mmap\/munmap.<\/li>\n<li>Mac also has: vm_allocate\/vm_deallocate.<\/li>\n<\/ul>\n<p>So it&#8217;s very natural to add an abstraction layer: your own functions (let&#8217;s call them Map and Unmap) that use conditional compilation to choose the appropriate OS-specific call.<\/p>\n<p>An abstraction layer like this appears in lots of software projects.\u00a0 Firefox happens to incorporate code from a lot of other projects, and so what happens is you end up with lots of duplicate abstraction layers.\u00a0 For example, in the JS engine alone we have <strong>five<\/strong> Map\/Unmap abstraction layers.<\/p>\n<ol>\n<li>In js\/src\/jsgcchunk.cpp, used to allocate chunks for the GC heap.<\/li>\n<li>in js\/src\/vm\/Stack.cpp, used to allocate some stack space.<\/li>\n<li>In js\/src\/nanojit\/avmplus.cpp, used to allocate space for code generated by the trace JIT.<\/li>\n<li>In js\/src\/assembler\/jit\/ExecutableAllocator*.cpp, used to allocate space for code generated by the method JIT.<\/li>\n<li>In js\/src\/ctypes\/libffi\/src\/dlmalloc.c, used to allocate chunks of memory that are handed out in pieces by the heap allocator defined in that file.<\/li>\n<\/ol>\n<p>The duplication of 3, 4 and 5 are understandable &#8212; they all involve large chunks of code that were imported from other projects.\u00a0 (Furthermore, you can see that ctypes\/ has its own heap allocator, thus duplicating jemalloc&#8217;s functionality.)\u00a0 The duplication between 1 and 2 is less forgiveable;\u00a0 neither of those cases were imported and so they should share an abstraction layer.<\/p>\n<p>How many other Map\/Unmap abstraction layers are there in the rest of Firefox?\u00a0 The JS engine may be more guilty of this than other parts of the code.\u00a0 Is there a sane way to avoid this duplication in a world where we import code from other projects?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just about every operating system provides a mechanism for directly allocating and deallocating memory at the page level (ie. not malloc\/free or new\/delete).\u00a0 The functions to do this vary from OS to OS: Windows: VirtualAlloc\/FreeAlloc. Posix (e.g. Mac and Linux): mmap\/munmap. Mac also has: vm_allocate\/vm_deallocate. So it&#8217;s very natural to add an abstraction layer: your [&hellip;]<\/p>\n","protected":false},"author":139,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,617,4549],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/posts\/718"}],"collection":[{"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/users\/139"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/comments?post=718"}],"version-history":[{"count":0,"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/posts\/718\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/media?parent=718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/categories?post=718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/tags?post=718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}