2010-01-17 01:53:50 +08:00
|
|
|
#define JEMALLOC_HUGE_C_
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_internal.h"
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* Data. */
|
|
|
|
|
2014-05-16 13:22:27 +08:00
|
|
|
/* Protects chunk-related data structures. */
|
|
|
|
static malloc_mutex_t huge_mtx;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
/* Tree of chunks that are stand-alone huge allocations. */
|
|
|
|
static extent_tree_t huge;
|
|
|
|
|
|
|
|
void *
|
2014-09-23 12:09:23 +08:00
|
|
|
huge_malloc(tsd_t *tsd, arena_t *arena, size_t size, bool zero)
|
2012-04-11 01:50:33 +08:00
|
|
|
{
|
2014-10-06 08:54:10 +08:00
|
|
|
size_t usize;
|
2012-04-11 01:50:33 +08:00
|
|
|
|
2014-10-06 08:54:10 +08:00
|
|
|
usize = s2u(size);
|
|
|
|
if (usize == 0) {
|
|
|
|
/* size_t overflow. */
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (huge_palloc(tsd, arena, usize, chunksize, zero));
|
2012-04-11 01:50:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2014-10-06 08:54:10 +08:00
|
|
|
huge_palloc(tsd_t *tsd, arena_t *arena, size_t usize, size_t alignment,
|
2014-09-23 12:09:23 +08:00
|
|
|
bool zero)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
|
|
|
void *ret;
|
|
|
|
size_t csize;
|
|
|
|
extent_node_t *node;
|
2012-04-22 07:04:51 +08:00
|
|
|
bool is_zeroed;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
/* Allocate one or more contiguous chunks for this request. */
|
|
|
|
|
2014-10-06 08:54:10 +08:00
|
|
|
csize = CHUNK_CEILING(usize);
|
|
|
|
assert(csize >= usize);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
/* Allocate an extent node with which to track the chunk. */
|
|
|
|
node = base_node_alloc();
|
|
|
|
if (node == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
2012-04-22 07:04:51 +08:00
|
|
|
/*
|
|
|
|
* Copy zero into is_zeroed and pass the copy to chunk_alloc(), so that
|
|
|
|
* it is possible to make correct junk/zero fill decisions below.
|
|
|
|
*/
|
|
|
|
is_zeroed = zero;
|
2014-09-23 12:09:23 +08:00
|
|
|
arena = choose_arena(tsd, arena);
|
Attempt to expand huge allocations in-place.
This adds support for expanding huge allocations in-place by requesting
memory at a specific address from the chunk allocator.
It's currently only implemented for the chunk recycling path, although
in theory it could also be done by optimistically allocating new chunks.
On Linux, it could attempt an in-place mremap. However, that won't work
in practice since the heap is grown downwards and memory is not unmapped
(in a normal build, at least).
Repeated vector reallocation micro-benchmark:
#include <string.h>
#include <stdlib.h>
int main(void) {
for (size_t i = 0; i < 100; i++) {
void *ptr = NULL;
size_t old_size = 0;
for (size_t size = 4; size < (1 << 30); size *= 2) {
ptr = realloc(ptr, size);
if (!ptr) return 1;
memset(ptr + old_size, 0xff, size - old_size);
old_size = size;
}
free(ptr);
}
}
The glibc allocator fails to do any in-place reallocations on this
benchmark once it passes the M_MMAP_THRESHOLD (default 128k) but it
elides the cost of copies via mremap, which is currently not something
that jemalloc can use.
With this improvement, jemalloc still fails to do any in-place huge
reallocations for the first outer loop, but then succeeds 100% of the
time for the remaining 99 iterations. The time spent doing allocations
and copies drops down to under 5%, with nearly all of it spent doing
purging + faulting (when huge pages are disabled) and the array memset.
An improved mremap API (MREMAP_RETAIN - #138) would be far more general
but this is a portable optimization and would still be useful on Linux
for xallocx.
Numbers with transparent huge pages enabled:
glibc (copies elided via MREMAP_MAYMOVE): 8.471s
jemalloc: 17.816s
jemalloc + no-op madvise: 13.236s
jemalloc + this commit: 6.787s
jemalloc + this commit + no-op madvise: 6.144s
Numbers with transparent huge pages disabled:
glibc (copies elided via MREMAP_MAYMOVE): 15.403s
jemalloc: 39.456s
jemalloc + no-op madvise: 12.768s
jemalloc + this commit: 15.534s
jemalloc + this commit + no-op madvise: 6.354s
Closes #137
2014-10-04 13:39:32 +08:00
|
|
|
ret = arena_chunk_alloc_huge(arena, NULL, csize, alignment, &is_zeroed);
|
2010-01-17 01:53:50 +08:00
|
|
|
if (ret == NULL) {
|
2014-05-16 13:22:27 +08:00
|
|
|
base_node_dalloc(node);
|
2010-01-17 01:53:50 +08:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Insert node into huge. */
|
|
|
|
node->addr = ret;
|
2014-10-06 08:54:10 +08:00
|
|
|
node->size = usize;
|
2014-05-06 06:16:56 +08:00
|
|
|
node->arena = arena;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
malloc_mutex_lock(&huge_mtx);
|
|
|
|
extent_tree_ad_insert(&huge, node);
|
|
|
|
malloc_mutex_unlock(&huge_mtx);
|
|
|
|
|
2014-10-04 01:16:09 +08:00
|
|
|
if (config_fill && !zero) {
|
2014-09-12 07:20:44 +08:00
|
|
|
if (unlikely(opt_junk))
|
2014-10-06 08:54:10 +08:00
|
|
|
memset(ret, 0xa5, usize);
|
2014-10-04 01:16:09 +08:00
|
|
|
else if (unlikely(opt_zero) && !is_zeroed)
|
2014-10-06 08:54:10 +08:00
|
|
|
memset(ret, 0, usize);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
Implement in-place huge allocation shrinking.
Trivial example:
#include <stdlib.h>
int main(void) {
void *ptr = malloc(1024 * 1024 * 8);
if (!ptr) return 1;
ptr = realloc(ptr, 1024 * 1024 * 4);
if (!ptr) return 1;
}
Before:
mmap(NULL, 8388608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcfff000000
mmap(NULL, 4194304, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcffec00000
madvise(0x7fcfff000000, 8388608, MADV_DONTNEED) = 0
After:
mmap(NULL, 8388608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1934800000
madvise(0x7f1934c00000, 4194304, MADV_DONTNEED) = 0
Closes #134
2014-09-30 22:33:46 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
#undef huge_dalloc_junk
|
|
|
|
#define huge_dalloc_junk JEMALLOC_N(huge_dalloc_junk_impl)
|
|
|
|
#endif
|
|
|
|
static void
|
|
|
|
huge_dalloc_junk(void *ptr, size_t usize)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (config_fill && have_dss && unlikely(opt_junk)) {
|
|
|
|
/*
|
|
|
|
* Only bother junk filling if the chunk isn't about to be
|
|
|
|
* unmapped.
|
|
|
|
*/
|
2014-10-04 01:16:09 +08:00
|
|
|
if (!config_munmap || (have_dss && chunk_in_dss(ptr)))
|
Implement in-place huge allocation shrinking.
Trivial example:
#include <stdlib.h>
int main(void) {
void *ptr = malloc(1024 * 1024 * 8);
if (!ptr) return 1;
ptr = realloc(ptr, 1024 * 1024 * 4);
if (!ptr) return 1;
}
Before:
mmap(NULL, 8388608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcfff000000
mmap(NULL, 4194304, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcffec00000
madvise(0x7fcfff000000, 8388608, MADV_DONTNEED) = 0
After:
mmap(NULL, 8388608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1934800000
madvise(0x7f1934c00000, 4194304, MADV_DONTNEED) = 0
Closes #134
2014-09-30 22:33:46 +08:00
|
|
|
memset(ptr, 0x5a, usize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
#undef huge_dalloc_junk
|
|
|
|
#define huge_dalloc_junk JEMALLOC_N(huge_dalloc_junk)
|
|
|
|
huge_dalloc_junk_t *huge_dalloc_junk = JEMALLOC_N(huge_dalloc_junk_impl);
|
|
|
|
#endif
|
|
|
|
|
Attempt to expand huge allocations in-place.
This adds support for expanding huge allocations in-place by requesting
memory at a specific address from the chunk allocator.
It's currently only implemented for the chunk recycling path, although
in theory it could also be done by optimistically allocating new chunks.
On Linux, it could attempt an in-place mremap. However, that won't work
in practice since the heap is grown downwards and memory is not unmapped
(in a normal build, at least).
Repeated vector reallocation micro-benchmark:
#include <string.h>
#include <stdlib.h>
int main(void) {
for (size_t i = 0; i < 100; i++) {
void *ptr = NULL;
size_t old_size = 0;
for (size_t size = 4; size < (1 << 30); size *= 2) {
ptr = realloc(ptr, size);
if (!ptr) return 1;
memset(ptr + old_size, 0xff, size - old_size);
old_size = size;
}
free(ptr);
}
}
The glibc allocator fails to do any in-place reallocations on this
benchmark once it passes the M_MMAP_THRESHOLD (default 128k) but it
elides the cost of copies via mremap, which is currently not something
that jemalloc can use.
With this improvement, jemalloc still fails to do any in-place huge
reallocations for the first outer loop, but then succeeds 100% of the
time for the remaining 99 iterations. The time spent doing allocations
and copies drops down to under 5%, with nearly all of it spent doing
purging + faulting (when huge pages are disabled) and the array memset.
An improved mremap API (MREMAP_RETAIN - #138) would be far more general
but this is a portable optimization and would still be useful on Linux
for xallocx.
Numbers with transparent huge pages enabled:
glibc (copies elided via MREMAP_MAYMOVE): 8.471s
jemalloc: 17.816s
jemalloc + no-op madvise: 13.236s
jemalloc + this commit: 6.787s
jemalloc + this commit + no-op madvise: 6.144s
Numbers with transparent huge pages disabled:
glibc (copies elided via MREMAP_MAYMOVE): 15.403s
jemalloc: 39.456s
jemalloc + no-op madvise: 12.768s
jemalloc + this commit: 15.534s
jemalloc + this commit + no-op madvise: 6.354s
Closes #137
2014-10-04 13:39:32 +08:00
|
|
|
static bool
|
|
|
|
huge_ralloc_no_move_expand(void *ptr, size_t oldsize, size_t size, bool zero) {
|
2014-10-06 08:54:10 +08:00
|
|
|
size_t usize;
|
Attempt to expand huge allocations in-place.
This adds support for expanding huge allocations in-place by requesting
memory at a specific address from the chunk allocator.
It's currently only implemented for the chunk recycling path, although
in theory it could also be done by optimistically allocating new chunks.
On Linux, it could attempt an in-place mremap. However, that won't work
in practice since the heap is grown downwards and memory is not unmapped
(in a normal build, at least).
Repeated vector reallocation micro-benchmark:
#include <string.h>
#include <stdlib.h>
int main(void) {
for (size_t i = 0; i < 100; i++) {
void *ptr = NULL;
size_t old_size = 0;
for (size_t size = 4; size < (1 << 30); size *= 2) {
ptr = realloc(ptr, size);
if (!ptr) return 1;
memset(ptr + old_size, 0xff, size - old_size);
old_size = size;
}
free(ptr);
}
}
The glibc allocator fails to do any in-place reallocations on this
benchmark once it passes the M_MMAP_THRESHOLD (default 128k) but it
elides the cost of copies via mremap, which is currently not something
that jemalloc can use.
With this improvement, jemalloc still fails to do any in-place huge
reallocations for the first outer loop, but then succeeds 100% of the
time for the remaining 99 iterations. The time spent doing allocations
and copies drops down to under 5%, with nearly all of it spent doing
purging + faulting (when huge pages are disabled) and the array memset.
An improved mremap API (MREMAP_RETAIN - #138) would be far more general
but this is a portable optimization and would still be useful on Linux
for xallocx.
Numbers with transparent huge pages enabled:
glibc (copies elided via MREMAP_MAYMOVE): 8.471s
jemalloc: 17.816s
jemalloc + no-op madvise: 13.236s
jemalloc + this commit: 6.787s
jemalloc + this commit + no-op madvise: 6.144s
Numbers with transparent huge pages disabled:
glibc (copies elided via MREMAP_MAYMOVE): 15.403s
jemalloc: 39.456s
jemalloc + no-op madvise: 12.768s
jemalloc + this commit: 15.534s
jemalloc + this commit + no-op madvise: 6.354s
Closes #137
2014-10-04 13:39:32 +08:00
|
|
|
void *expand_addr;
|
|
|
|
size_t expand_size;
|
|
|
|
extent_node_t *node, key;
|
|
|
|
arena_t *arena;
|
|
|
|
bool is_zeroed;
|
|
|
|
void *ret;
|
|
|
|
|
2014-10-06 08:54:10 +08:00
|
|
|
usize = s2u(size);
|
|
|
|
if (usize == 0) {
|
|
|
|
/* size_t overflow. */
|
Attempt to expand huge allocations in-place.
This adds support for expanding huge allocations in-place by requesting
memory at a specific address from the chunk allocator.
It's currently only implemented for the chunk recycling path, although
in theory it could also be done by optimistically allocating new chunks.
On Linux, it could attempt an in-place mremap. However, that won't work
in practice since the heap is grown downwards and memory is not unmapped
(in a normal build, at least).
Repeated vector reallocation micro-benchmark:
#include <string.h>
#include <stdlib.h>
int main(void) {
for (size_t i = 0; i < 100; i++) {
void *ptr = NULL;
size_t old_size = 0;
for (size_t size = 4; size < (1 << 30); size *= 2) {
ptr = realloc(ptr, size);
if (!ptr) return 1;
memset(ptr + old_size, 0xff, size - old_size);
old_size = size;
}
free(ptr);
}
}
The glibc allocator fails to do any in-place reallocations on this
benchmark once it passes the M_MMAP_THRESHOLD (default 128k) but it
elides the cost of copies via mremap, which is currently not something
that jemalloc can use.
With this improvement, jemalloc still fails to do any in-place huge
reallocations for the first outer loop, but then succeeds 100% of the
time for the remaining 99 iterations. The time spent doing allocations
and copies drops down to under 5%, with nearly all of it spent doing
purging + faulting (when huge pages are disabled) and the array memset.
An improved mremap API (MREMAP_RETAIN - #138) would be far more general
but this is a portable optimization and would still be useful on Linux
for xallocx.
Numbers with transparent huge pages enabled:
glibc (copies elided via MREMAP_MAYMOVE): 8.471s
jemalloc: 17.816s
jemalloc + no-op madvise: 13.236s
jemalloc + this commit: 6.787s
jemalloc + this commit + no-op madvise: 6.144s
Numbers with transparent huge pages disabled:
glibc (copies elided via MREMAP_MAYMOVE): 15.403s
jemalloc: 39.456s
jemalloc + no-op madvise: 12.768s
jemalloc + this commit: 15.534s
jemalloc + this commit + no-op madvise: 6.354s
Closes #137
2014-10-04 13:39:32 +08:00
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
2014-10-06 08:54:10 +08:00
|
|
|
expand_addr = ptr + CHUNK_CEILING(oldsize);
|
|
|
|
expand_size = CHUNK_CEILING(usize) - CHUNK_CEILING(oldsize);
|
Attempt to expand huge allocations in-place.
This adds support for expanding huge allocations in-place by requesting
memory at a specific address from the chunk allocator.
It's currently only implemented for the chunk recycling path, although
in theory it could also be done by optimistically allocating new chunks.
On Linux, it could attempt an in-place mremap. However, that won't work
in practice since the heap is grown downwards and memory is not unmapped
(in a normal build, at least).
Repeated vector reallocation micro-benchmark:
#include <string.h>
#include <stdlib.h>
int main(void) {
for (size_t i = 0; i < 100; i++) {
void *ptr = NULL;
size_t old_size = 0;
for (size_t size = 4; size < (1 << 30); size *= 2) {
ptr = realloc(ptr, size);
if (!ptr) return 1;
memset(ptr + old_size, 0xff, size - old_size);
old_size = size;
}
free(ptr);
}
}
The glibc allocator fails to do any in-place reallocations on this
benchmark once it passes the M_MMAP_THRESHOLD (default 128k) but it
elides the cost of copies via mremap, which is currently not something
that jemalloc can use.
With this improvement, jemalloc still fails to do any in-place huge
reallocations for the first outer loop, but then succeeds 100% of the
time for the remaining 99 iterations. The time spent doing allocations
and copies drops down to under 5%, with nearly all of it spent doing
purging + faulting (when huge pages are disabled) and the array memset.
An improved mremap API (MREMAP_RETAIN - #138) would be far more general
but this is a portable optimization and would still be useful on Linux
for xallocx.
Numbers with transparent huge pages enabled:
glibc (copies elided via MREMAP_MAYMOVE): 8.471s
jemalloc: 17.816s
jemalloc + no-op madvise: 13.236s
jemalloc + this commit: 6.787s
jemalloc + this commit + no-op madvise: 6.144s
Numbers with transparent huge pages disabled:
glibc (copies elided via MREMAP_MAYMOVE): 15.403s
jemalloc: 39.456s
jemalloc + no-op madvise: 12.768s
jemalloc + this commit: 15.534s
jemalloc + this commit + no-op madvise: 6.354s
Closes #137
2014-10-04 13:39:32 +08:00
|
|
|
|
|
|
|
malloc_mutex_lock(&huge_mtx);
|
|
|
|
|
|
|
|
key.addr = ptr;
|
|
|
|
node = extent_tree_ad_search(&huge, &key);
|
|
|
|
assert(node != NULL);
|
|
|
|
assert(node->addr == ptr);
|
|
|
|
|
|
|
|
/* Find the current arena. */
|
|
|
|
arena = node->arena;
|
|
|
|
|
|
|
|
malloc_mutex_unlock(&huge_mtx);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy zero into is_zeroed and pass the copy to chunk_alloc(), so that
|
|
|
|
* it is possible to make correct junk/zero fill decisions below.
|
|
|
|
*/
|
|
|
|
is_zeroed = zero;
|
|
|
|
ret = arena_chunk_alloc_huge(arena, expand_addr, expand_size, chunksize,
|
|
|
|
&is_zeroed);
|
|
|
|
if (ret == NULL)
|
|
|
|
return (true);
|
|
|
|
|
|
|
|
assert(ret == expand_addr);
|
|
|
|
|
|
|
|
malloc_mutex_lock(&huge_mtx);
|
|
|
|
/* Update the size of the huge allocation. */
|
2014-10-06 08:54:10 +08:00
|
|
|
node->size = usize;
|
Attempt to expand huge allocations in-place.
This adds support for expanding huge allocations in-place by requesting
memory at a specific address from the chunk allocator.
It's currently only implemented for the chunk recycling path, although
in theory it could also be done by optimistically allocating new chunks.
On Linux, it could attempt an in-place mremap. However, that won't work
in practice since the heap is grown downwards and memory is not unmapped
(in a normal build, at least).
Repeated vector reallocation micro-benchmark:
#include <string.h>
#include <stdlib.h>
int main(void) {
for (size_t i = 0; i < 100; i++) {
void *ptr = NULL;
size_t old_size = 0;
for (size_t size = 4; size < (1 << 30); size *= 2) {
ptr = realloc(ptr, size);
if (!ptr) return 1;
memset(ptr + old_size, 0xff, size - old_size);
old_size = size;
}
free(ptr);
}
}
The glibc allocator fails to do any in-place reallocations on this
benchmark once it passes the M_MMAP_THRESHOLD (default 128k) but it
elides the cost of copies via mremap, which is currently not something
that jemalloc can use.
With this improvement, jemalloc still fails to do any in-place huge
reallocations for the first outer loop, but then succeeds 100% of the
time for the remaining 99 iterations. The time spent doing allocations
and copies drops down to under 5%, with nearly all of it spent doing
purging + faulting (when huge pages are disabled) and the array memset.
An improved mremap API (MREMAP_RETAIN - #138) would be far more general
but this is a portable optimization and would still be useful on Linux
for xallocx.
Numbers with transparent huge pages enabled:
glibc (copies elided via MREMAP_MAYMOVE): 8.471s
jemalloc: 17.816s
jemalloc + no-op madvise: 13.236s
jemalloc + this commit: 6.787s
jemalloc + this commit + no-op madvise: 6.144s
Numbers with transparent huge pages disabled:
glibc (copies elided via MREMAP_MAYMOVE): 15.403s
jemalloc: 39.456s
jemalloc + no-op madvise: 12.768s
jemalloc + this commit: 15.534s
jemalloc + this commit + no-op madvise: 6.354s
Closes #137
2014-10-04 13:39:32 +08:00
|
|
|
malloc_mutex_unlock(&huge_mtx);
|
|
|
|
|
|
|
|
if (config_fill && !zero) {
|
|
|
|
if (unlikely(opt_junk))
|
2014-10-06 08:54:10 +08:00
|
|
|
memset(ptr + oldsize, 0xa5, usize - oldsize);
|
Attempt to expand huge allocations in-place.
This adds support for expanding huge allocations in-place by requesting
memory at a specific address from the chunk allocator.
It's currently only implemented for the chunk recycling path, although
in theory it could also be done by optimistically allocating new chunks.
On Linux, it could attempt an in-place mremap. However, that won't work
in practice since the heap is grown downwards and memory is not unmapped
(in a normal build, at least).
Repeated vector reallocation micro-benchmark:
#include <string.h>
#include <stdlib.h>
int main(void) {
for (size_t i = 0; i < 100; i++) {
void *ptr = NULL;
size_t old_size = 0;
for (size_t size = 4; size < (1 << 30); size *= 2) {
ptr = realloc(ptr, size);
if (!ptr) return 1;
memset(ptr + old_size, 0xff, size - old_size);
old_size = size;
}
free(ptr);
}
}
The glibc allocator fails to do any in-place reallocations on this
benchmark once it passes the M_MMAP_THRESHOLD (default 128k) but it
elides the cost of copies via mremap, which is currently not something
that jemalloc can use.
With this improvement, jemalloc still fails to do any in-place huge
reallocations for the first outer loop, but then succeeds 100% of the
time for the remaining 99 iterations. The time spent doing allocations
and copies drops down to under 5%, with nearly all of it spent doing
purging + faulting (when huge pages are disabled) and the array memset.
An improved mremap API (MREMAP_RETAIN - #138) would be far more general
but this is a portable optimization and would still be useful on Linux
for xallocx.
Numbers with transparent huge pages enabled:
glibc (copies elided via MREMAP_MAYMOVE): 8.471s
jemalloc: 17.816s
jemalloc + no-op madvise: 13.236s
jemalloc + this commit: 6.787s
jemalloc + this commit + no-op madvise: 6.144s
Numbers with transparent huge pages disabled:
glibc (copies elided via MREMAP_MAYMOVE): 15.403s
jemalloc: 39.456s
jemalloc + no-op madvise: 12.768s
jemalloc + this commit: 15.534s
jemalloc + this commit + no-op madvise: 6.354s
Closes #137
2014-10-04 13:39:32 +08:00
|
|
|
else if (unlikely(opt_zero) && !is_zeroed)
|
2014-10-06 08:54:10 +08:00
|
|
|
memset(ptr + oldsize, 0, usize - oldsize);
|
Attempt to expand huge allocations in-place.
This adds support for expanding huge allocations in-place by requesting
memory at a specific address from the chunk allocator.
It's currently only implemented for the chunk recycling path, although
in theory it could also be done by optimistically allocating new chunks.
On Linux, it could attempt an in-place mremap. However, that won't work
in practice since the heap is grown downwards and memory is not unmapped
(in a normal build, at least).
Repeated vector reallocation micro-benchmark:
#include <string.h>
#include <stdlib.h>
int main(void) {
for (size_t i = 0; i < 100; i++) {
void *ptr = NULL;
size_t old_size = 0;
for (size_t size = 4; size < (1 << 30); size *= 2) {
ptr = realloc(ptr, size);
if (!ptr) return 1;
memset(ptr + old_size, 0xff, size - old_size);
old_size = size;
}
free(ptr);
}
}
The glibc allocator fails to do any in-place reallocations on this
benchmark once it passes the M_MMAP_THRESHOLD (default 128k) but it
elides the cost of copies via mremap, which is currently not something
that jemalloc can use.
With this improvement, jemalloc still fails to do any in-place huge
reallocations for the first outer loop, but then succeeds 100% of the
time for the remaining 99 iterations. The time spent doing allocations
and copies drops down to under 5%, with nearly all of it spent doing
purging + faulting (when huge pages are disabled) and the array memset.
An improved mremap API (MREMAP_RETAIN - #138) would be far more general
but this is a portable optimization and would still be useful on Linux
for xallocx.
Numbers with transparent huge pages enabled:
glibc (copies elided via MREMAP_MAYMOVE): 8.471s
jemalloc: 17.816s
jemalloc + no-op madvise: 13.236s
jemalloc + this commit: 6.787s
jemalloc + this commit + no-op madvise: 6.144s
Numbers with transparent huge pages disabled:
glibc (copies elided via MREMAP_MAYMOVE): 15.403s
jemalloc: 39.456s
jemalloc + no-op madvise: 12.768s
jemalloc + this commit: 15.534s
jemalloc + this commit + no-op madvise: 6.354s
Closes #137
2014-10-04 13:39:32 +08:00
|
|
|
}
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
2014-01-13 07:05:44 +08:00
|
|
|
bool
|
Attempt to expand huge allocations in-place.
This adds support for expanding huge allocations in-place by requesting
memory at a specific address from the chunk allocator.
It's currently only implemented for the chunk recycling path, although
in theory it could also be done by optimistically allocating new chunks.
On Linux, it could attempt an in-place mremap. However, that won't work
in practice since the heap is grown downwards and memory is not unmapped
(in a normal build, at least).
Repeated vector reallocation micro-benchmark:
#include <string.h>
#include <stdlib.h>
int main(void) {
for (size_t i = 0; i < 100; i++) {
void *ptr = NULL;
size_t old_size = 0;
for (size_t size = 4; size < (1 << 30); size *= 2) {
ptr = realloc(ptr, size);
if (!ptr) return 1;
memset(ptr + old_size, 0xff, size - old_size);
old_size = size;
}
free(ptr);
}
}
The glibc allocator fails to do any in-place reallocations on this
benchmark once it passes the M_MMAP_THRESHOLD (default 128k) but it
elides the cost of copies via mremap, which is currently not something
that jemalloc can use.
With this improvement, jemalloc still fails to do any in-place huge
reallocations for the first outer loop, but then succeeds 100% of the
time for the remaining 99 iterations. The time spent doing allocations
and copies drops down to under 5%, with nearly all of it spent doing
purging + faulting (when huge pages are disabled) and the array memset.
An improved mremap API (MREMAP_RETAIN - #138) would be far more general
but this is a portable optimization and would still be useful on Linux
for xallocx.
Numbers with transparent huge pages enabled:
glibc (copies elided via MREMAP_MAYMOVE): 8.471s
jemalloc: 17.816s
jemalloc + no-op madvise: 13.236s
jemalloc + this commit: 6.787s
jemalloc + this commit + no-op madvise: 6.144s
Numbers with transparent huge pages disabled:
glibc (copies elided via MREMAP_MAYMOVE): 15.403s
jemalloc: 39.456s
jemalloc + no-op madvise: 12.768s
jemalloc + this commit: 15.534s
jemalloc + this commit + no-op madvise: 6.354s
Closes #137
2014-10-04 13:39:32 +08:00
|
|
|
huge_ralloc_no_move(void *ptr, size_t oldsize, size_t size, size_t extra,
|
|
|
|
bool zero)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
2014-10-06 08:54:10 +08:00
|
|
|
size_t usize;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
Implement in-place huge allocation shrinking.
Trivial example:
#include <stdlib.h>
int main(void) {
void *ptr = malloc(1024 * 1024 * 8);
if (!ptr) return 1;
ptr = realloc(ptr, 1024 * 1024 * 4);
if (!ptr) return 1;
}
Before:
mmap(NULL, 8388608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcfff000000
mmap(NULL, 4194304, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcffec00000
madvise(0x7fcfff000000, 8388608, MADV_DONTNEED) = 0
After:
mmap(NULL, 8388608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1934800000
madvise(0x7f1934c00000, 4194304, MADV_DONTNEED) = 0
Closes #134
2014-09-30 22:33:46 +08:00
|
|
|
/* Both allocations must be huge to avoid a move. */
|
2014-10-06 08:54:10 +08:00
|
|
|
if (oldsize < chunksize)
|
Implement in-place huge allocation shrinking.
Trivial example:
#include <stdlib.h>
int main(void) {
void *ptr = malloc(1024 * 1024 * 8);
if (!ptr) return 1;
ptr = realloc(ptr, 1024 * 1024 * 4);
if (!ptr) return 1;
}
Before:
mmap(NULL, 8388608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcfff000000
mmap(NULL, 4194304, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcffec00000
madvise(0x7fcfff000000, 8388608, MADV_DONTNEED) = 0
After:
mmap(NULL, 8388608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1934800000
madvise(0x7f1934c00000, 4194304, MADV_DONTNEED) = 0
Closes #134
2014-09-30 22:33:46 +08:00
|
|
|
return (true);
|
|
|
|
|
2014-10-06 08:54:10 +08:00
|
|
|
assert(s2u(oldsize) == oldsize);
|
|
|
|
usize = s2u(size);
|
|
|
|
if (usize == 0) {
|
|
|
|
/* size_t overflow. */
|
|
|
|
return (true);
|
|
|
|
}
|
Implement in-place huge allocation shrinking.
Trivial example:
#include <stdlib.h>
int main(void) {
void *ptr = malloc(1024 * 1024 * 8);
if (!ptr) return 1;
ptr = realloc(ptr, 1024 * 1024 * 4);
if (!ptr) return 1;
}
Before:
mmap(NULL, 8388608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcfff000000
mmap(NULL, 4194304, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcffec00000
madvise(0x7fcfff000000, 8388608, MADV_DONTNEED) = 0
After:
mmap(NULL, 8388608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1934800000
madvise(0x7f1934c00000, 4194304, MADV_DONTNEED) = 0
Closes #134
2014-09-30 22:33:46 +08:00
|
|
|
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
/*
|
2014-10-06 08:54:10 +08:00
|
|
|
* Avoid moving the allocation if the existing chunk size accommodates
|
|
|
|
* the new size.
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
*/
|
2014-10-06 08:54:10 +08:00
|
|
|
if (CHUNK_CEILING(oldsize) >= CHUNK_CEILING(usize)
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
&& CHUNK_CEILING(oldsize) <= CHUNK_CEILING(size+extra)) {
|
2014-10-06 08:54:10 +08:00
|
|
|
size_t usize_next;
|
|
|
|
|
|
|
|
/* Increase usize to incorporate extra. */
|
|
|
|
while (usize < s2u(size+extra) && (usize_next = s2u(usize+1)) <
|
|
|
|
oldsize)
|
|
|
|
usize = usize_next;
|
|
|
|
|
|
|
|
/* Update the size of the huge allocation if it changed. */
|
|
|
|
if (oldsize != usize) {
|
|
|
|
extent_node_t *node, key;
|
|
|
|
|
|
|
|
malloc_mutex_lock(&huge_mtx);
|
|
|
|
|
|
|
|
key.addr = ptr;
|
|
|
|
node = extent_tree_ad_search(&huge, &key);
|
|
|
|
assert(node != NULL);
|
|
|
|
assert(node->addr == ptr);
|
|
|
|
|
|
|
|
assert(node->size != usize);
|
|
|
|
node->size = usize;
|
|
|
|
|
|
|
|
malloc_mutex_unlock(&huge_mtx);
|
|
|
|
|
|
|
|
if (oldsize < usize) {
|
|
|
|
if (zero || (config_fill &&
|
|
|
|
unlikely(opt_zero))) {
|
|
|
|
memset(ptr + oldsize, 0, usize -
|
|
|
|
oldsize);
|
|
|
|
} else if (config_fill && unlikely(opt_junk)) {
|
|
|
|
memset(ptr + oldsize, 0xa5, usize -
|
|
|
|
oldsize);
|
|
|
|
}
|
|
|
|
} else if (config_fill && unlikely(opt_junk) && oldsize
|
|
|
|
> usize)
|
|
|
|
memset(ptr + usize, 0x5a, oldsize - usize);
|
|
|
|
}
|
2014-01-13 07:05:44 +08:00
|
|
|
return (false);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2014-10-06 08:54:10 +08:00
|
|
|
if (CHUNK_CEILING(oldsize) >= CHUNK_CEILING(size)
|
|
|
|
&& CHUNK_CEILING(oldsize) <= CHUNK_CEILING(size+extra)) {
|
|
|
|
return (false);
|
|
|
|
}
|
Implement in-place huge allocation shrinking.
Trivial example:
#include <stdlib.h>
int main(void) {
void *ptr = malloc(1024 * 1024 * 8);
if (!ptr) return 1;
ptr = realloc(ptr, 1024 * 1024 * 4);
if (!ptr) return 1;
}
Before:
mmap(NULL, 8388608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcfff000000
mmap(NULL, 4194304, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcffec00000
madvise(0x7fcfff000000, 8388608, MADV_DONTNEED) = 0
After:
mmap(NULL, 8388608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1934800000
madvise(0x7f1934c00000, 4194304, MADV_DONTNEED) = 0
Closes #134
2014-09-30 22:33:46 +08:00
|
|
|
|
|
|
|
/* Shrink the allocation in-place. */
|
2014-10-06 08:54:10 +08:00
|
|
|
if (CHUNK_CEILING(oldsize) > CHUNK_CEILING(usize)) {
|
Implement in-place huge allocation shrinking.
Trivial example:
#include <stdlib.h>
int main(void) {
void *ptr = malloc(1024 * 1024 * 8);
if (!ptr) return 1;
ptr = realloc(ptr, 1024 * 1024 * 4);
if (!ptr) return 1;
}
Before:
mmap(NULL, 8388608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcfff000000
mmap(NULL, 4194304, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcffec00000
madvise(0x7fcfff000000, 8388608, MADV_DONTNEED) = 0
After:
mmap(NULL, 8388608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1934800000
madvise(0x7f1934c00000, 4194304, MADV_DONTNEED) = 0
Closes #134
2014-09-30 22:33:46 +08:00
|
|
|
extent_node_t *node, key;
|
|
|
|
void *excess_addr;
|
|
|
|
size_t excess_size;
|
|
|
|
|
|
|
|
malloc_mutex_lock(&huge_mtx);
|
|
|
|
|
|
|
|
key.addr = ptr;
|
|
|
|
node = extent_tree_ad_search(&huge, &key);
|
|
|
|
assert(node != NULL);
|
|
|
|
assert(node->addr == ptr);
|
|
|
|
|
|
|
|
/* Update the size of the huge allocation. */
|
2014-10-06 08:54:10 +08:00
|
|
|
node->size = usize;
|
Implement in-place huge allocation shrinking.
Trivial example:
#include <stdlib.h>
int main(void) {
void *ptr = malloc(1024 * 1024 * 8);
if (!ptr) return 1;
ptr = realloc(ptr, 1024 * 1024 * 4);
if (!ptr) return 1;
}
Before:
mmap(NULL, 8388608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcfff000000
mmap(NULL, 4194304, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcffec00000
madvise(0x7fcfff000000, 8388608, MADV_DONTNEED) = 0
After:
mmap(NULL, 8388608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1934800000
madvise(0x7f1934c00000, 4194304, MADV_DONTNEED) = 0
Closes #134
2014-09-30 22:33:46 +08:00
|
|
|
|
|
|
|
malloc_mutex_unlock(&huge_mtx);
|
|
|
|
|
2014-10-06 08:54:10 +08:00
|
|
|
excess_addr = node->addr + CHUNK_CEILING(usize);
|
|
|
|
excess_size = CHUNK_CEILING(oldsize) - CHUNK_CEILING(usize);
|
Implement in-place huge allocation shrinking.
Trivial example:
#include <stdlib.h>
int main(void) {
void *ptr = malloc(1024 * 1024 * 8);
if (!ptr) return 1;
ptr = realloc(ptr, 1024 * 1024 * 4);
if (!ptr) return 1;
}
Before:
mmap(NULL, 8388608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcfff000000
mmap(NULL, 4194304, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcffec00000
madvise(0x7fcfff000000, 8388608, MADV_DONTNEED) = 0
After:
mmap(NULL, 8388608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1934800000
madvise(0x7f1934c00000, 4194304, MADV_DONTNEED) = 0
Closes #134
2014-09-30 22:33:46 +08:00
|
|
|
|
|
|
|
/* Zap the excess chunks. */
|
2014-10-06 08:54:10 +08:00
|
|
|
huge_dalloc_junk(ptr + usize, oldsize - usize);
|
Implement in-place huge allocation shrinking.
Trivial example:
#include <stdlib.h>
int main(void) {
void *ptr = malloc(1024 * 1024 * 8);
if (!ptr) return 1;
ptr = realloc(ptr, 1024 * 1024 * 4);
if (!ptr) return 1;
}
Before:
mmap(NULL, 8388608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcfff000000
mmap(NULL, 4194304, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcffec00000
madvise(0x7fcfff000000, 8388608, MADV_DONTNEED) = 0
After:
mmap(NULL, 8388608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1934800000
madvise(0x7f1934c00000, 4194304, MADV_DONTNEED) = 0
Closes #134
2014-09-30 22:33:46 +08:00
|
|
|
arena_chunk_dalloc_huge(node->arena, excess_addr, excess_size);
|
|
|
|
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
Attempt to expand huge allocations in-place.
This adds support for expanding huge allocations in-place by requesting
memory at a specific address from the chunk allocator.
It's currently only implemented for the chunk recycling path, although
in theory it could also be done by optimistically allocating new chunks.
On Linux, it could attempt an in-place mremap. However, that won't work
in practice since the heap is grown downwards and memory is not unmapped
(in a normal build, at least).
Repeated vector reallocation micro-benchmark:
#include <string.h>
#include <stdlib.h>
int main(void) {
for (size_t i = 0; i < 100; i++) {
void *ptr = NULL;
size_t old_size = 0;
for (size_t size = 4; size < (1 << 30); size *= 2) {
ptr = realloc(ptr, size);
if (!ptr) return 1;
memset(ptr + old_size, 0xff, size - old_size);
old_size = size;
}
free(ptr);
}
}
The glibc allocator fails to do any in-place reallocations on this
benchmark once it passes the M_MMAP_THRESHOLD (default 128k) but it
elides the cost of copies via mremap, which is currently not something
that jemalloc can use.
With this improvement, jemalloc still fails to do any in-place huge
reallocations for the first outer loop, but then succeeds 100% of the
time for the remaining 99 iterations. The time spent doing allocations
and copies drops down to under 5%, with nearly all of it spent doing
purging + faulting (when huge pages are disabled) and the array memset.
An improved mremap API (MREMAP_RETAIN - #138) would be far more general
but this is a portable optimization and would still be useful on Linux
for xallocx.
Numbers with transparent huge pages enabled:
glibc (copies elided via MREMAP_MAYMOVE): 8.471s
jemalloc: 17.816s
jemalloc + no-op madvise: 13.236s
jemalloc + this commit: 6.787s
jemalloc + this commit + no-op madvise: 6.144s
Numbers with transparent huge pages disabled:
glibc (copies elided via MREMAP_MAYMOVE): 15.403s
jemalloc: 39.456s
jemalloc + no-op madvise: 12.768s
jemalloc + this commit: 15.534s
jemalloc + this commit + no-op madvise: 6.354s
Closes #137
2014-10-04 13:39:32 +08:00
|
|
|
/* Attempt to expand the allocation in-place. */
|
|
|
|
if (huge_ralloc_no_move_expand(ptr, oldsize, size + extra, zero)) {
|
|
|
|
if (extra == 0)
|
|
|
|
return (true);
|
|
|
|
|
|
|
|
/* Try again, this time without extra. */
|
|
|
|
return (huge_ralloc_no_move_expand(ptr, oldsize, size, zero));
|
|
|
|
}
|
|
|
|
return (false);
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2014-09-23 12:09:23 +08:00
|
|
|
huge_ralloc(tsd_t *tsd, arena_t *arena, void *ptr, size_t oldsize, size_t size,
|
2014-05-16 13:22:27 +08:00
|
|
|
size_t extra, size_t alignment, bool zero, bool try_tcache_dalloc)
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
{
|
|
|
|
void *ret;
|
|
|
|
size_t copysize;
|
|
|
|
|
|
|
|
/* Try to avoid moving the allocation. */
|
Attempt to expand huge allocations in-place.
This adds support for expanding huge allocations in-place by requesting
memory at a specific address from the chunk allocator.
It's currently only implemented for the chunk recycling path, although
in theory it could also be done by optimistically allocating new chunks.
On Linux, it could attempt an in-place mremap. However, that won't work
in practice since the heap is grown downwards and memory is not unmapped
(in a normal build, at least).
Repeated vector reallocation micro-benchmark:
#include <string.h>
#include <stdlib.h>
int main(void) {
for (size_t i = 0; i < 100; i++) {
void *ptr = NULL;
size_t old_size = 0;
for (size_t size = 4; size < (1 << 30); size *= 2) {
ptr = realloc(ptr, size);
if (!ptr) return 1;
memset(ptr + old_size, 0xff, size - old_size);
old_size = size;
}
free(ptr);
}
}
The glibc allocator fails to do any in-place reallocations on this
benchmark once it passes the M_MMAP_THRESHOLD (default 128k) but it
elides the cost of copies via mremap, which is currently not something
that jemalloc can use.
With this improvement, jemalloc still fails to do any in-place huge
reallocations for the first outer loop, but then succeeds 100% of the
time for the remaining 99 iterations. The time spent doing allocations
and copies drops down to under 5%, with nearly all of it spent doing
purging + faulting (when huge pages are disabled) and the array memset.
An improved mremap API (MREMAP_RETAIN - #138) would be far more general
but this is a portable optimization and would still be useful on Linux
for xallocx.
Numbers with transparent huge pages enabled:
glibc (copies elided via MREMAP_MAYMOVE): 8.471s
jemalloc: 17.816s
jemalloc + no-op madvise: 13.236s
jemalloc + this commit: 6.787s
jemalloc + this commit + no-op madvise: 6.144s
Numbers with transparent huge pages disabled:
glibc (copies elided via MREMAP_MAYMOVE): 15.403s
jemalloc: 39.456s
jemalloc + no-op madvise: 12.768s
jemalloc + this commit: 15.534s
jemalloc + this commit + no-op madvise: 6.354s
Closes #137
2014-10-04 13:39:32 +08:00
|
|
|
if (!huge_ralloc_no_move(ptr, oldsize, size, extra, zero))
|
2014-01-13 07:05:44 +08:00
|
|
|
return (ptr);
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
/*
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
* size and oldsize are different enough that we need to use a
|
|
|
|
* different size class. In that case, fall back to allocating new
|
|
|
|
* space and copying.
|
2010-01-17 01:53:50 +08:00
|
|
|
*/
|
2011-02-01 11:58:22 +08:00
|
|
|
if (alignment > chunksize)
|
2014-09-23 12:09:23 +08:00
|
|
|
ret = huge_palloc(tsd, arena, size + extra, alignment, zero);
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
else
|
2014-09-23 12:09:23 +08:00
|
|
|
ret = huge_malloc(tsd, arena, size + extra, zero);
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
|
|
|
|
if (ret == NULL) {
|
|
|
|
if (extra == 0)
|
|
|
|
return (NULL);
|
|
|
|
/* Try again, this time without extra. */
|
2011-02-01 11:58:22 +08:00
|
|
|
if (alignment > chunksize)
|
2014-09-23 12:09:23 +08:00
|
|
|
ret = huge_palloc(tsd, arena, size, alignment, zero);
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
else
|
2014-09-23 12:09:23 +08:00
|
|
|
ret = huge_malloc(tsd, arena, size, zero);
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
|
|
|
|
if (ret == NULL)
|
|
|
|
return (NULL);
|
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
/*
|
|
|
|
* Copy at most size bytes (not size+extra), since the caller has no
|
|
|
|
* expectation that the extra bytes will be reliably preserved.
|
|
|
|
*/
|
2010-01-17 01:53:50 +08:00
|
|
|
copysize = (size < oldsize) ? size : oldsize;
|
2014-05-16 13:22:27 +08:00
|
|
|
memcpy(ret, ptr, copysize);
|
2014-09-23 12:09:23 +08:00
|
|
|
iqalloc(tsd, ptr, try_tcache_dalloc);
|
2010-01-17 01:53:50 +08:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-16 13:22:27 +08:00
|
|
|
huge_dalloc(void *ptr)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
|
|
|
extent_node_t *node, key;
|
|
|
|
|
|
|
|
malloc_mutex_lock(&huge_mtx);
|
|
|
|
|
|
|
|
/* Extract from tree of huge allocations. */
|
|
|
|
key.addr = ptr;
|
|
|
|
node = extent_tree_ad_search(&huge, &key);
|
|
|
|
assert(node != NULL);
|
|
|
|
assert(node->addr == ptr);
|
|
|
|
extent_tree_ad_remove(&huge, node);
|
|
|
|
|
|
|
|
malloc_mutex_unlock(&huge_mtx);
|
|
|
|
|
2014-05-16 13:22:27 +08:00
|
|
|
huge_dalloc_junk(node->addr, node->size);
|
2014-10-06 08:54:10 +08:00
|
|
|
arena_chunk_dalloc_huge(node->arena, node->addr,
|
|
|
|
CHUNK_CEILING(node->size));
|
2014-05-16 13:22:27 +08:00
|
|
|
base_node_dalloc(node);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
huge_salloc(const void *ptr)
|
|
|
|
{
|
|
|
|
size_t ret;
|
|
|
|
extent_node_t *node, key;
|
|
|
|
|
|
|
|
malloc_mutex_lock(&huge_mtx);
|
|
|
|
|
|
|
|
/* Extract from tree of huge allocations. */
|
|
|
|
key.addr = __DECONST(void *, ptr);
|
|
|
|
node = extent_tree_ad_search(&huge, &key);
|
|
|
|
assert(node != NULL);
|
|
|
|
|
|
|
|
ret = node->size;
|
|
|
|
|
|
|
|
malloc_mutex_unlock(&huge_mtx);
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_tctx_t *
|
|
|
|
huge_prof_tctx_get(const void *ptr)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
2014-08-19 07:22:13 +08:00
|
|
|
prof_tctx_t *ret;
|
2010-02-11 02:37:56 +08:00
|
|
|
extent_node_t *node, key;
|
|
|
|
|
|
|
|
malloc_mutex_lock(&huge_mtx);
|
|
|
|
|
|
|
|
/* Extract from tree of huge allocations. */
|
|
|
|
key.addr = __DECONST(void *, ptr);
|
|
|
|
node = extent_tree_ad_search(&huge, &key);
|
|
|
|
assert(node != NULL);
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
ret = node->prof_tctx;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
malloc_mutex_unlock(&huge_mtx);
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-08-19 07:22:13 +08:00
|
|
|
huge_prof_tctx_set(const void *ptr, prof_tctx_t *tctx)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
|
|
|
extent_node_t *node, key;
|
|
|
|
|
|
|
|
malloc_mutex_lock(&huge_mtx);
|
|
|
|
|
|
|
|
/* Extract from tree of huge allocations. */
|
|
|
|
key.addr = __DECONST(void *, ptr);
|
|
|
|
node = extent_tree_ad_search(&huge, &key);
|
|
|
|
assert(node != NULL);
|
|
|
|
|
2014-08-19 07:22:13 +08:00
|
|
|
node->prof_tctx = tctx;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
malloc_mutex_unlock(&huge_mtx);
|
|
|
|
}
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
bool
|
|
|
|
huge_boot(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Initialize chunks data. */
|
|
|
|
if (malloc_mutex_init(&huge_mtx))
|
|
|
|
return (true);
|
|
|
|
extent_tree_ad_new(&huge);
|
|
|
|
|
|
|
|
return (false);
|
|
|
|
}
|
2012-03-14 07:31:41 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
huge_prefork(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
malloc_mutex_prefork(&huge_mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
huge_postfork_parent(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
malloc_mutex_postfork_parent(&huge_mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
huge_postfork_child(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
malloc_mutex_postfork_child(&huge_mtx);
|
|
|
|
}
|