<p dir="ltr">Also 2^31 == 29, not 1<<31</p>
<p dir="ltr">Bradley C Kuszmaul - via snartphone</p>
<div class="gmail_quote">On Jul 28, 2014 7:04 PM, "meng" <<a href="mailto:xqmeng@gmail.com">xqmeng@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr"><div>Yes, the address returned from the custom chunk_alloc() is got from mmap(0,2^31,...). Because 2^31 is a multiple of 2^22 (the default chunk size), it must be aligned with the chunk size.</div><div><br>
</div>
<div>Below is a simplified version of my test program:</div><div><br></div><div>void *space = NULL;</div><div><div>static unsigned _arena;</div><div>static chunk_alloc_t *_alloc;</div><div>static chunk_dalloc_t *_dalloc;</div>
<div><br></div><div>static void *_chunk_alloc(size_t, size_t, bool *, unsigned);</div><div>static bool _chunk_dalloc(void *, size_t, unsigned);</div></div><div><br></div>
<div>void * _chunk_alloc(size_t size, size_t alignment, bool *zero, unsigned arena_ind)<br></div><div>
<div>{</div><div> return space;<br></div></div><div>}</div><div><br></div><div><div>bool _chunk_dalloc(void *chunk, size_t size, unsigned arena_ind)</div><div>{</div></div><div> return true;</div><div>}</div><div><br></div>
<div><div>int main(void)</div><div>{</div><div> space = mmap (NULL, 2^31, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);<br></div><div><div> assert (space != (void *) -1);</div></div><div> <br></div><div>
size_t sz = sizeof(_arena);</div>
<div> int ret = mallctl("arenas.extend", &_arena, &sz, NULL, 0);</div><div> assert (ret == 0);</div><div><br></div><div> sz = sizeof(_alloc);</div><div> char path[128];</div><div> snprintf(path, sizeof(path), "arena.%u.chunk.alloc", _arena);</div>
<div> chunk_alloc_t *alloc = _chunk_alloc;</div><div> ret = mallctl(path, (void*)&_alloc, &sz, (void*)&alloc, sizeof(alloc));</div><div> assert (ret == 0);</div><div><br></div><div> snprintf(path, sizeof(path), "arena.%u.chunk.dalloc", _arena);</div>
<div> chunk_dalloc_t *dalloc = _chunk_dalloc;</div><div> ret = mallctl(path, (void*)&_dalloc, &sz, (void*)&dalloc, sizeof(dalloc));</div><div> assert (ret == 0);</div><div><br></div><div> void *p = mallocx(1024, MALLOCX_ARENA(_arena));</div>
<div> assert (p != 0);</div><div> dallocx(p, MALLOCX_ARENA(_arena));</div><div><br></div><div> //unmap space</div><div> return 1;</div><div>}</div></div><div><br></div><div>The seg. fault occurs in mallocx, and here is the stack frame in GDB:</div>
<div><div>#0 0x00007ffff7d6b7a2 in je_arena_mapbitsp_write (mapbits=4, mapbitsp=0x7ffff7cb0008) at include/jemalloc/internal/arena.h:774</div><div>#1 je_arena_mapbits_unzeroed_set (unzeroed=4, pageind=345, chunk=0x7ffff7cae000) at include/jemalloc/internal/arena.h:852</div>
<div>#2 arena_chunk_init_hard (arena=0x7ffff7822140) at src/arena.c:662</div><div>#3 0x00007ffff7d6bea8 in arena_chunk_alloc (arena=0x7ffff7822140) at src/arena.c:689</div><div>#4 0x00007ffff7d6ce9b in arena_run_alloc_small (arena=0x7ffff7822140, size=65536, binind=20) at src/arena.c:854</div>
<div>#5 0x00007ffff7d74852 in arena_bin_nonfull_run_get (arena=0x7ffff7822140, bin=0x7ffff7822e70) at src/arena.c:1470</div><div>#6 0x00007ffff7d749aa in arena_bin_malloc_hard (arena=0x7ffff7822140, bin=0x7ffff7822e70) at src/arena.c:1516</div>
<div>#7 0x00007ffff7d7558a in je_arena_malloc_small (arena=0x7ffff7822140, size=1024, zero=false) at src/arena.c:1713</div><div>#8 0x00007ffff7d17121 in je_arena_malloc (try_tcache=false, zero=false, size=1024, arena=0x7ffff7822140) at include/jemalloc/internal/arena.h:1076</div>
<div>#9 je_imalloct (arena=0x7ffff7822140, try_tcache=false, size=1024) at include/jemalloc/internal/jemalloc_internal.h:647</div><div>#10 imallocx (arena=0x7ffff7822140, try_tcache=false, zero=false, alignment=0, usize=1024) at src/jemalloc.c:1377</div>
<div>#11 mallocx (size=1024, flags=512) at src/jemalloc.c:1456</div><div>#12 0x0000000000400c06 in main () at test_jemalloc.c:102</div></div><div><br></div><div><br></div><div><br></div></div><div class="gmail_extra"><br>
<br><div class="gmail_quote">On Mon, Jul 28, 2014 at 5:52 PM, Jason Evans <span dir="ltr"><<a href="mailto:jasone@canonware.com" target="_blank">jasone@canonware.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div>On Jul 28, 2014, at 2:17 PM, meng <<a href="mailto:xqmeng@gmail.com" target="_blank">xqmeng@gmail.com</a>> wrote:<br>
> I used the new chunk allocator feature to allocate memory from a fixed 2G memory region. Nevertheless, I got a seg. fault.<br>
><br>
> The flow of my code is as following: I first use "arenas.extend" mallctl to create a custom arena. Then I defined custom chunk_alloc() and chunk_dalloc() on this arena. In the initialization phase of my code, I use mmap() to reserve a memory region of size 2^32. In the custom chunk_alloc(), I return the pointer of the 2^32B memory region. Because lg_chunk is 2^22, I thought this should be fine. But the program ran into seg. fault within arena_mapbits_unzeroed_set() called by arena_chunk_init_hard(). On the other hand, if the mmap() reserved a memory region of size 2^22, everything works fine.<br>
><br>
> My question is: why does the custom chunk_alloc() always expect a memory block returned from mmap()/malloc() with the requested size equal to lg_chunk? I can't figure out what wrong it could be if the returned block is a multiple of lg_chunk<br>
><br>
> B.T.W. My code only uses mallocx() for a single 1024B buffer from the custom. Memory alignment problem shouldn't exist.<br>
<br>
</div></div>Is the address you're returning from the custom chunk_alloc() aligned at a multiple of the chunk size?<br>
<span><font color="#888888"><br>
Jason</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br>Best<br>-Xiaoqiao
</div>
<br>_______________________________________________<br>
jemalloc-discuss mailing list<br>
<a href="mailto:jemalloc-discuss@canonware.com">jemalloc-discuss@canonware.com</a><br>
<a href="http://www.canonware.com/mailman/listinfo/jemalloc-discuss" target="_blank">http://www.canonware.com/mailman/listinfo/jemalloc-discuss</a><br>
<br></blockquote></div>