public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
* Why doesn't it directly invoke malloc(3) to allocate a big block of memory other than calling sbrk() or map() under different conditions?
@ 2020-08-13  8:35 孙世龙 sunshilong
  2020-08-13  8:45 ` Siddhesh Poyarekar
  2020-08-13 10:00 ` Florian Weimer
  0 siblings, 2 replies; 3+ messages in thread
From: 孙世龙 sunshilong @ 2020-08-13  8:35 UTC (permalink / raw)
  To: 孙世龙 sunshilong via Libc-help

Hi, list

As the subject, here is the related code snippet(which could be seen
on https://github.com/ros2/tlsf/blob/master/tlsf/src/tlsf.c):
#if USE_SBRK || USE_MMAP
static __inline__ void *get_new_area(size_t * size)
{
    void *area;

#if USE_SBRK
    area = (void *)sbrk(0);
    if (((void *)sbrk(*size)) != ((void *) -1))
return area;
#endif

#ifndef MAP_ANONYMOUS
/* https://dev.openwrt.org/ticket/322 */
# define MAP_ANONYMOUS MAP_ANON
#endif


#if USE_MMAP
    *size = ROUNDUP(*size, getpagesize());
    if ((area = __STD(mmap(0, *size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0))) != MAP_FAILED)
return area;
#endif
    return ((void *) ~0);
}
#endif

I would be grateful to have some help with this question.
Best Regards.
Sunshilong

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Why doesn't it directly invoke malloc(3) to allocate a big block of memory other than calling sbrk() or map() under different conditions?
  2020-08-13  8:35 Why doesn't it directly invoke malloc(3) to allocate a big block of memory other than calling sbrk() or map() under different conditions? 孙世龙 sunshilong
@ 2020-08-13  8:45 ` Siddhesh Poyarekar
  2020-08-13 10:00 ` Florian Weimer
  1 sibling, 0 replies; 3+ messages in thread
From: Siddhesh Poyarekar @ 2020-08-13  8:45 UTC (permalink / raw)
  To: 孙世龙 sunshilong
  Cc: 孙世龙 sunshilong via Libc-help

On Thu, 13 Aug 2020 at 14:06, 孙世龙 sunshilong via Libc-help
<libc-help@sourceware.org> wrote:
> I would be grateful to have some help with this question.

It is a question for the tlsf project, not for glibc.  Projects make
such decisions for a variety of reasons and the most common one is
that system malloc does not perform the way they would like it.

Siddhesh
-- 
http://siddhesh.in

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Why doesn't it directly invoke malloc(3) to allocate a big block of memory other than calling sbrk() or map() under different conditions?
  2020-08-13  8:35 Why doesn't it directly invoke malloc(3) to allocate a big block of memory other than calling sbrk() or map() under different conditions? 孙世龙 sunshilong
  2020-08-13  8:45 ` Siddhesh Poyarekar
@ 2020-08-13 10:00 ` Florian Weimer
  1 sibling, 0 replies; 3+ messages in thread
From: Florian Weimer @ 2020-08-13 10:00 UTC (permalink / raw)
  To: 孙世龙 sunshilong via Libc-help

* 孙世龙 sunshilong via Libc-help:

> As the subject, here is the related code snippet(which could be seen
> on https://github.com/ros2/tlsf/blob/master/tlsf/src/tlsf.c):
> #if USE_SBRK || USE_MMAP
> static __inline__ void *get_new_area(size_t * size)
> {
>     void *area;
>
> #if USE_SBRK
>     area = (void *)sbrk(0);
>     if (((void *)sbrk(*size)) != ((void *) -1))
> return area;
> #endif
>
> #ifndef MAP_ANONYMOUS
> /* https://dev.openwrt.org/ticket/322 */
> # define MAP_ANONYMOUS MAP_ANON
> #endif
>
>
> #if USE_MMAP
>     *size = ROUNDUP(*size, getpagesize());
>     if ((area = __STD(mmap(0, *size, PROT_READ | PROT_WRITE,
> MAP_PRIVATE | MAP_ANONYMOUS, -1, 0))) != MAP_FAILED)
> return area;
> #endif
>     return ((void *) ~0);
> }
> #endif

Calling malloc here would lead to infinite recursion if TLSF is used
as a malloc replacement:

  <https://www.gnu.org/software/libc/manual/html_node/Replacing-malloc.html>

It would call into TLSF again, never actually allocating any memory.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-08-13 10:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-13  8:35 Why doesn't it directly invoke malloc(3) to allocate a big block of memory other than calling sbrk() or map() under different conditions? 孙世龙 sunshilong
2020-08-13  8:45 ` Siddhesh Poyarekar
2020-08-13 10:00 ` Florian Weimer

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 read-only IMAP folder(s) and NNTP newsgroup(s).