Re: allocate memory in userspace

From: Erik Andersen (andersen@codepoet.org)
Date: Tue Jul 02 2002 - 04:00:03 EST


On Tue Jul 02, 2002 at 01:44:18AM -0700, William Lee Irwin III wrote:
> On Mon, Jul 01, 2002 at 11:49:13AM -0600, Erik Andersen wrote:
> >> void *malloc(size_t size)
> >> {
> >> void *result;
> >> if (size == 0)
> >> return NULL;
> >> result = mmap((void *) 0, size + sizeof(size_t), PROT_READ | PROT_WRITE,
> >> MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
> >> if (result == MAP_FAILED)
> >> return 0;
> >> * (size_t *) result = size;
> >> return(result + sizeof(size_t));
> >> }
>
> On Tue, Jul 02, 2002 at 01:37:37AM -0700, William Lee Irwin III wrote:
> > This looks like a very bad idea. Userspace allocators should make some
> > attempt at avoiding diving into the kernel at every allocation like this.
>
> Sorry, I also forgot the rather severe internal fragmentation this is
> likely to suffer due to page-level restrictions on mmap's operation.

Of course. No question there -- actually using such an allocator
(with a standard linux kernel) would be a terrible idea. I was
merely providing a trivial answer to his question on how to use
mmap to allocate memory.

 -Erik

--
Erik B. Andersen             http://codepoet-consulting.com/
--This message was written using 73% post-consumer electrons--
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Sun Jul 07 2002 - 22:00:09 EST