From: Christopher Friedt <chrisfriedt@gmail.com>
To: musl@lists.openwall.com
Subject: Re: malloc implementation survey: omalloc
Date: Tue, 31 Jul 2018 10:49:53 -0400 [thread overview]
Message-ID: <CAF4BF-TEAY7OVKsqW_Bn5pabB-2nmqh5--CW=0iMW7oHfcGfTA@mail.gmail.com> (raw)
In-Reply-To: <20180731074929.GB22386@voyager>
[-- Attachment #1: Type: text/plain, Size: 2462 bytes --]
On Tue, Jul 31, 2018, 3:49 AM Markus Wichmann, <nullplan@gmx.net> wrote:
> On Mon, Jul 30, 2018 at 11:09:05AM -0400, Christopher Friedt wrote:
> > I wrote it originally for using on the kernel-side of an rtos, but it
> could
> > easily be extended to make a syscall when a process runs out of ram.
> >
> > Obviously, a shortcoming is that the memory blocks must be PoT and there
> is
> > the potential for fragmentation. Otherwise though, the meta-information
> is
> > intrinsic within the pointer, which obliviates the need for external
> > storage.
> >
>
> That does sound interesting, but I don't really follow. The meta-info
> which we need to save is the length of the object returned. How can you
> encode that in the pointer without any external storage? Using internal
> storage? Yeah, that is where we are right now, and that means there's
> only one buffer overflow between us and oblivion at any given time.
>
> Or do you essentially have a large block of memory, with one area for
> the 16-byte objects, one for the 32-byte ones, &c. But then what do you
> do if your given area runs out and the application still wants more?
https://en.wikipedia.org/wiki/Buddy_memory_allocation
The 'bins' do overlap (e.g. a 64 byte chunk can also be used as 2 32-byte
chunks or "buddies"), but their size (when using a bitmap for metadata) is
encoded by the position of the bit in the map. This also encodes if a
parent (or child) of a specific memory region is already in use or free.
My original implementation was for an rtos with a compile-time-fixed memory
size. In user space (since doubling ram usage is obviously bad) it would be
best to use some container (e.g. an interval tree) to contain buddy-malloc
nodes and general slabs of mmap. Current usage, a configurable
fragmentation threshold, and the system pagesize, would determine if an
allocation would use an existing buddy-malloc node, a new buddy-malloc node
(PoT # of pages) or a sequence of pages. Pages would, of course, be
allocated with mmap.
I've personally not implemented malloc debugging for a buddy allocator
though, since bits are precious enough, and there is no fundamental
requirement for it, although technically, it would mean adding some padding
with a computable bit pattern. I suppose if checking is enabled, it can be
run before free(3) returns or before realloc(3) does anything. A global
check on every function call would be more thorough, but obviously more
expensive.
[-- Attachment #2: Type: text/html, Size: 3102 bytes --]
next prev parent reply other threads:[~2018-07-31 14:49 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-07-29 19:26 Markus Wichmann
2018-07-30 15:09 ` Christopher Friedt
2018-07-30 19:14 ` Christopher Friedt
2018-07-30 19:22 ` Christopher Friedt
2018-07-30 19:34 ` Christopher Friedt
2018-07-31 7:49 ` Markus Wichmann
2018-07-31 14:25 ` Rich Felker
2018-07-31 14:49 ` Christopher Friedt [this message]
2018-07-31 0:47 ` Rich Felker
2018-07-31 1:44 ` Rich Felker
2018-08-30 18:16 ` A. Wilcox
2018-08-30 18:46 ` Rich Felker
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAF4BF-TEAY7OVKsqW_Bn5pabB-2nmqh5--CW=0iMW7oHfcGfTA@mail.gmail.com' \
--to=chrisfriedt@gmail.com \
--cc=musl@lists.openwall.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).