{"id":2717,"date":"2013-11-08T15:20:36","date_gmt":"2013-11-08T04:20:36","guid":{"rendered":"http:\/\/blog.mozilla.org\/nnethercote\/?p=2717"},"modified":"2013-11-09T08:54:22","modified_gmt":"2013-11-08T21:54:22","slug":"libraries-should-permit-custom-allocators","status":"publish","type":"post","link":"https:\/\/blog.mozilla.org\/nnethercote\/2013\/11\/08\/libraries-should-permit-custom-allocators\/","title":{"rendered":"Libraries should permit custom allocators"},"content":{"rendered":"<p>Some C and C++ libraries permit the use of custom allocators, which are registered through some kind of external API.\u00a0 For example, the following libraries used by Firefox provide this facility.<\/p>\n<ul>\n<li>FreeType provides this via the FT_MemoryRec_ argument of the FT_New_Library() function.<\/li>\n<li>ICU provides this via the u_setMemoryFunctions() function.<\/li>\n<li>SQLite provides this via the sqlite3_config() function.<\/li>\n<\/ul>\n<p>This gives the users of these libraries additional flexibility that can be very helpful.\u00a0 For example, in Firefox we provide custom allocators that measure the size of all the live allocations done by the library;\u00a0 these measurements are shown in about:memory.<\/p>\n<p>In contrast, libraries that don&#8217;t allow custom allocator are very hard to account for in about:memory.\u00a0 Such libraries are major contributors to the dreaded &#8220;heap-unclassified&#8221; value in about:memory.\u00a0 These include Cairo and the WebRTC libraries.<\/p>\n<p>Now, supporting custom allocators in a library takes some effort.\u00a0 You have to be careful to always allocate in a fashion that will use the custom allocators if they have been registered.\u00a0 Direct calls to vanilla allocation\/free functions like malloc(), realloc(), and free() must be avoided.\u00a0 For example, SpiderMonkey allows custom allocators (although Firefox doesn&#8217;t need to use that functionality), and I just fixed a <a href=\"https:\/\/bugzilla.mozilla.org\/show_bug.cgi?id=634417\">handful of cases<\/a> where it was accidentally using vanilla allocation\/free functions.<\/p>\n<p>But, it&#8217;s a very useful facility to provide, and I encourage all library writers to consider it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some C and C++ libraries permit the use of custom allocators, which are registered through some kind of external API.\u00a0 For example, the following libraries used by Firefox provide this facility. FreeType provides this via the FT_MemoryRec_ argument of the FT_New_Library() function. ICU provides this via the u_setMemoryFunctions() function. SQLite provides this via the sqlite3_config() [&hellip;]<\/p>\n","protected":false},"author":139,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4550,4546,617],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/posts\/2717"}],"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=2717"}],"version-history":[{"count":0,"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/posts\/2717\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/media?parent=2717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/categories?post=2717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/tags?post=2717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}