2017-04-11 09:17:55 +08:00
|
|
|
#ifndef JEMALLOC_INTERNAL_INLINES_C_H
|
|
|
|
#define JEMALLOC_INTERNAL_INLINES_C_H
|
|
|
|
|
2018-04-24 09:07:40 +08:00
|
|
|
#include "jemalloc/internal/hook.h"
|
2017-04-18 06:52:44 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_internal_types.h"
|
2017-05-31 01:45:37 +08:00
|
|
|
#include "jemalloc/internal/sz.h"
|
2017-05-23 10:32:04 +08:00
|
|
|
#include "jemalloc/internal/witness.h"
|
2017-04-18 06:52:44 +08:00
|
|
|
|
2017-08-15 04:32:28 +08:00
|
|
|
/*
|
|
|
|
* Translating the names of the 'i' functions:
|
|
|
|
* Abbreviations used in the first part of the function name (before
|
|
|
|
* alloc/dalloc) describe what that function accomplishes:
|
|
|
|
* a: arena (query)
|
|
|
|
* s: size (query, or sized deallocation)
|
|
|
|
* e: extent (query)
|
|
|
|
* p: aligned (allocates)
|
|
|
|
* vs: size (query, without knowing that the pointer is into the heap)
|
|
|
|
* r: rallocx implementation
|
|
|
|
* x: xallocx implementation
|
|
|
|
* Abbreviations used in the second part of the function name (after
|
|
|
|
* alloc/dalloc) describe the arguments it takes
|
|
|
|
* z: whether to return zeroed memory
|
|
|
|
* t: accepts a tcache_t * parameter
|
|
|
|
* m: accepts an arena_t * parameter
|
|
|
|
*/
|
|
|
|
|
2017-04-11 09:17:55 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE arena_t *
|
|
|
|
iaalloc(tsdn_t *tsdn, const void *ptr) {
|
|
|
|
assert(ptr != NULL);
|
|
|
|
|
|
|
|
return arena_aalloc(tsdn, ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE size_t
|
|
|
|
isalloc(tsdn_t *tsdn, const void *ptr) {
|
|
|
|
assert(ptr != NULL);
|
|
|
|
|
|
|
|
return arena_salloc(tsdn, ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE void *
|
|
|
|
iallocztm(tsdn_t *tsdn, size_t size, szind_t ind, bool zero, tcache_t *tcache,
|
|
|
|
bool is_internal, arena_t *arena, bool slow_path) {
|
|
|
|
void *ret;
|
|
|
|
|
|
|
|
assert(size != 0);
|
|
|
|
assert(!is_internal || tcache == NULL);
|
2017-04-21 06:19:02 +08:00
|
|
|
assert(!is_internal || arena == NULL || arena_is_auto(arena));
|
2017-12-23 03:19:50 +08:00
|
|
|
if (!tsdn_null(tsdn) && tsd_reentrancy_level_get(tsdn_tsd(tsdn)) == 0) {
|
|
|
|
witness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),
|
|
|
|
WITNESS_RANK_CORE, 0);
|
|
|
|
}
|
2017-04-11 09:17:55 +08:00
|
|
|
|
|
|
|
ret = arena_malloc(tsdn, arena, size, ind, zero, tcache, slow_path);
|
|
|
|
if (config_stats && is_internal && likely(ret != NULL)) {
|
|
|
|
arena_internal_add(iaalloc(tsdn, ret), isalloc(tsdn, ret));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE void *
|
|
|
|
ialloc(tsd_t *tsd, size_t size, szind_t ind, bool zero, bool slow_path) {
|
|
|
|
return iallocztm(tsd_tsdn(tsd), size, ind, zero, tcache_get(tsd), false,
|
|
|
|
NULL, slow_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE void *
|
|
|
|
ipallocztm(tsdn_t *tsdn, size_t usize, size_t alignment, bool zero,
|
|
|
|
tcache_t *tcache, bool is_internal, arena_t *arena) {
|
|
|
|
void *ret;
|
|
|
|
|
|
|
|
assert(usize != 0);
|
2017-05-31 01:45:37 +08:00
|
|
|
assert(usize == sz_sa2u(usize, alignment));
|
2017-04-11 09:17:55 +08:00
|
|
|
assert(!is_internal || tcache == NULL);
|
2017-04-21 06:19:02 +08:00
|
|
|
assert(!is_internal || arena == NULL || arena_is_auto(arena));
|
2017-05-23 10:32:04 +08:00
|
|
|
witness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),
|
|
|
|
WITNESS_RANK_CORE, 0);
|
2017-04-11 09:17:55 +08:00
|
|
|
|
|
|
|
ret = arena_palloc(tsdn, arena, usize, alignment, zero, tcache);
|
|
|
|
assert(ALIGNMENT_ADDR2BASE(ret, alignment) == ret);
|
|
|
|
if (config_stats && is_internal && likely(ret != NULL)) {
|
|
|
|
arena_internal_add(iaalloc(tsdn, ret), isalloc(tsdn, ret));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE void *
|
|
|
|
ipalloct(tsdn_t *tsdn, size_t usize, size_t alignment, bool zero,
|
|
|
|
tcache_t *tcache, arena_t *arena) {
|
|
|
|
return ipallocztm(tsdn, usize, alignment, zero, tcache, false, arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE void *
|
|
|
|
ipalloc(tsd_t *tsd, size_t usize, size_t alignment, bool zero) {
|
|
|
|
return ipallocztm(tsd_tsdn(tsd), usize, alignment, zero,
|
|
|
|
tcache_get(tsd), false, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE size_t
|
|
|
|
ivsalloc(tsdn_t *tsdn, const void *ptr) {
|
|
|
|
return arena_vsalloc(tsdn, ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
2017-04-12 09:13:10 +08:00
|
|
|
idalloctm(tsdn_t *tsdn, void *ptr, tcache_t *tcache, alloc_ctx_t *alloc_ctx,
|
2017-04-11 09:17:55 +08:00
|
|
|
bool is_internal, bool slow_path) {
|
|
|
|
assert(ptr != NULL);
|
|
|
|
assert(!is_internal || tcache == NULL);
|
2017-04-21 06:19:02 +08:00
|
|
|
assert(!is_internal || arena_is_auto(iaalloc(tsdn, ptr)));
|
2017-05-23 10:32:04 +08:00
|
|
|
witness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),
|
|
|
|
WITNESS_RANK_CORE, 0);
|
2017-04-11 09:17:55 +08:00
|
|
|
if (config_stats && is_internal) {
|
|
|
|
arena_internal_sub(iaalloc(tsdn, ptr), isalloc(tsdn, ptr));
|
|
|
|
}
|
2017-12-23 03:22:16 +08:00
|
|
|
if (!is_internal && !tsdn_null(tsdn) &&
|
|
|
|
tsd_reentrancy_level_get(tsdn_tsd(tsdn)) != 0) {
|
2017-04-13 07:16:27 +08:00
|
|
|
assert(tcache == NULL);
|
2017-04-11 09:17:55 +08:00
|
|
|
}
|
2017-04-12 09:13:10 +08:00
|
|
|
arena_dalloc(tsdn, ptr, tcache, alloc_ctx, slow_path);
|
2017-04-11 09:17:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
|
|
|
idalloc(tsd_t *tsd, void *ptr) {
|
|
|
|
idalloctm(tsd_tsdn(tsd), ptr, tcache_get(tsd), NULL, false, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE void
|
2017-04-12 05:56:43 +08:00
|
|
|
isdalloct(tsdn_t *tsdn, void *ptr, size_t size, tcache_t *tcache,
|
2017-04-12 09:13:10 +08:00
|
|
|
alloc_ctx_t *alloc_ctx, bool slow_path) {
|
2017-05-23 10:32:04 +08:00
|
|
|
witness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),
|
|
|
|
WITNESS_RANK_CORE, 0);
|
2017-04-12 09:13:10 +08:00
|
|
|
arena_sdalloc(tsdn, ptr, size, tcache, alloc_ctx, slow_path);
|
2017-04-11 09:17:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE void *
|
|
|
|
iralloct_realign(tsdn_t *tsdn, void *ptr, size_t oldsize, size_t size,
|
2018-04-24 09:07:40 +08:00
|
|
|
size_t alignment, bool zero, tcache_t *tcache, arena_t *arena,
|
|
|
|
hook_ralloc_args_t *hook_args) {
|
2017-05-23 10:32:04 +08:00
|
|
|
witness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),
|
|
|
|
WITNESS_RANK_CORE, 0);
|
2017-04-11 09:17:55 +08:00
|
|
|
void *p;
|
|
|
|
size_t usize, copysize;
|
|
|
|
|
2018-04-24 09:07:40 +08:00
|
|
|
usize = sz_sa2u(size, alignment);
|
2017-04-11 09:17:55 +08:00
|
|
|
if (unlikely(usize == 0 || usize > LARGE_MAXCLASS)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p = ipalloct(tsdn, usize, alignment, zero, tcache, arena);
|
|
|
|
if (p == NULL) {
|
2018-04-24 09:07:40 +08:00
|
|
|
return NULL;
|
2017-04-11 09:17:55 +08:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Copy at most size bytes (not size+extra), since the caller has no
|
|
|
|
* expectation that the extra bytes will be reliably preserved.
|
|
|
|
*/
|
|
|
|
copysize = (size < oldsize) ? size : oldsize;
|
|
|
|
memcpy(p, ptr, copysize);
|
2018-04-24 09:07:40 +08:00
|
|
|
hook_invoke_alloc(hook_args->is_realloc
|
|
|
|
? hook_alloc_realloc : hook_alloc_rallocx, p, (uintptr_t)p,
|
|
|
|
hook_args->args);
|
|
|
|
hook_invoke_dalloc(hook_args->is_realloc
|
|
|
|
? hook_dalloc_realloc : hook_dalloc_rallocx, ptr, hook_args->args);
|
2017-04-12 05:56:43 +08:00
|
|
|
isdalloct(tsdn, ptr, oldsize, tcache, NULL, true);
|
2017-04-11 09:17:55 +08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2018-04-24 09:07:40 +08:00
|
|
|
/*
|
|
|
|
* is_realloc threads through the knowledge of whether or not this call comes
|
|
|
|
* from je_realloc (as opposed to je_rallocx); this ensures that we pass the
|
|
|
|
* correct entry point into any hooks.
|
|
|
|
* Note that these functions are all force-inlined, so no actual bool gets
|
|
|
|
* passed-around anywhere.
|
|
|
|
*/
|
2017-04-11 09:17:55 +08:00
|
|
|
JEMALLOC_ALWAYS_INLINE void *
|
|
|
|
iralloct(tsdn_t *tsdn, void *ptr, size_t oldsize, size_t size, size_t alignment,
|
2018-04-24 09:07:40 +08:00
|
|
|
bool zero, tcache_t *tcache, arena_t *arena, hook_ralloc_args_t *hook_args)
|
|
|
|
{
|
2017-04-11 09:17:55 +08:00
|
|
|
assert(ptr != NULL);
|
|
|
|
assert(size != 0);
|
2017-05-23 10:32:04 +08:00
|
|
|
witness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),
|
|
|
|
WITNESS_RANK_CORE, 0);
|
2017-04-11 09:17:55 +08:00
|
|
|
|
|
|
|
if (alignment != 0 && ((uintptr_t)ptr & ((uintptr_t)alignment-1))
|
|
|
|
!= 0) {
|
|
|
|
/*
|
|
|
|
* Existing object alignment is inadequate; allocate new space
|
|
|
|
* and copy.
|
|
|
|
*/
|
2018-04-24 09:07:40 +08:00
|
|
|
return iralloct_realign(tsdn, ptr, oldsize, size, alignment,
|
|
|
|
zero, tcache, arena, hook_args);
|
2017-04-11 09:17:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return arena_ralloc(tsdn, arena, ptr, oldsize, size, alignment, zero,
|
2018-04-24 09:07:40 +08:00
|
|
|
tcache, hook_args);
|
2017-04-11 09:17:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE void *
|
|
|
|
iralloc(tsd_t *tsd, void *ptr, size_t oldsize, size_t size, size_t alignment,
|
2018-04-24 09:07:40 +08:00
|
|
|
bool zero, hook_ralloc_args_t *hook_args) {
|
2017-04-11 09:17:55 +08:00
|
|
|
return iralloct(tsd_tsdn(tsd), ptr, oldsize, size, alignment, zero,
|
2018-04-24 09:07:40 +08:00
|
|
|
tcache_get(tsd), NULL, hook_args);
|
2017-04-11 09:17:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JEMALLOC_ALWAYS_INLINE bool
|
|
|
|
ixalloc(tsdn_t *tsdn, void *ptr, size_t oldsize, size_t size, size_t extra,
|
2018-06-05 04:36:06 +08:00
|
|
|
size_t alignment, bool zero, size_t *newsize) {
|
2017-04-11 09:17:55 +08:00
|
|
|
assert(ptr != NULL);
|
|
|
|
assert(size != 0);
|
2017-05-23 10:32:04 +08:00
|
|
|
witness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),
|
|
|
|
WITNESS_RANK_CORE, 0);
|
2017-04-11 09:17:55 +08:00
|
|
|
|
|
|
|
if (alignment != 0 && ((uintptr_t)ptr & ((uintptr_t)alignment-1))
|
|
|
|
!= 0) {
|
|
|
|
/* Existing object alignment is inadequate. */
|
2018-06-05 04:36:06 +08:00
|
|
|
*newsize = oldsize;
|
2017-04-11 09:17:55 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-05 04:36:06 +08:00
|
|
|
return arena_ralloc_no_move(tsdn, ptr, oldsize, size, extra, zero,
|
|
|
|
newsize);
|
2017-04-11 09:17:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* JEMALLOC_INTERNAL_INLINES_C_H */
|