// File: /workspace/github_c_makefile_star2_O2/zhou-yuxin[P]redis-aof-no-cow/deps/jemalloc/src/chunk_dss.c
// Function: je_chunk_alloc_dss
void *
chunk_alloc_dss(tsdn_t *tsdn, arena_t *arena, void *new_addr, size_t size,
    size_t alignment, bool *zero, bool *commit)
{
	cassert(have_dss);
	assert(size > 0 && (size & chunksize_mask) == 0);
	assert(alignment > 0 && (alignment & chunksize_mask) == 0);

	/*
	 * sbrk() uses a signed increment argument, so take care not to
	 * interpret a huge allocation request as a negative increment.
	 */
	if ((intptr_t)size < 0)
		return (NULL);

	if (!atomic_read_u(&dss_exhausted)) {
		/*
		 * The loop is necessary to recover from races with other
		 * threads that are using the DSS for something other than
		 * malloc.
		 */
		while (true) {
			void *ret, *cpad, *max_cur, *dss_next, *dss_prev;
			size_t gap_size, cpad_size;
			intptr_t incr;

			max_cur = chunk_dss_max_update(new_addr);
			if (max_cur == NULL)
				goto label_oom;

			/*
			 * Calculate how much padding is necessary to
			 * chunk-align the end of the DSS.
			 */
			gap_size = (chunksize - CHUNK_ADDR2OFFSET(dss_max)) &
			    chunksize_mask;
			/*
			 * Compute how much chunk-aligned pad space (if any) is
			 * necessary to satisfy alignment.  This space can be
			 * recycled for later use.
			 */
			cpad = (void *)((uintptr_t)dss_max + gap_size);
			ret = (void *)ALIGNMENT_CEILING((uintptr_t)dss_max,
			    alignment);
			cpad_size = (uintptr_t)ret - (uintptr_t)cpad;
			dss_next = (void *)((uintptr_t)ret + size);
			if ((uintptr_t)ret < (uintptr_t)dss_max ||
			    (uintptr_t)dss_next < (uintptr_t)dss_max)
				goto label_oom; /* Wrap-around. */
			incr = gap_size + cpad_size + size;

			/*
			 * Optimistically update dss_max, and roll back below if
			 * sbrk() fails.  No other thread will try to extend the
			 * DSS while dss_max is greater than the current DSS
			 * max reported by sbrk(0).
			 */
			if (atomic_cas_p(&dss_max, max_cur, dss_next))
				continue;

			/* Try to allocate. */
			dss_prev = chunk_dss_sbrk(incr);
			if (dss_prev == max_cur) {
				/* Success. */
				if (cpad_size != 0) {
					chunk_hooks_t chunk_hooks =
					    CHUNK_HOOKS_INITIALIZER;
					chunk_dalloc_wrapper(tsdn, arena,
					    &chunk_hooks, cpad, cpad_size,
					    arena_extent_sn_next(arena), false,
					    true);
				}
				if (*zero) {
					JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED(
					    ret, size);
					memset(ret, 0, size);
				}
				if (!*commit)
					*commit = pages_decommit(ret, size);
				return (ret);
			}

			/*
			 * Failure, whether due to OOM or a race with a raw
			 * sbrk() call from outside the allocator.  Try to roll
			 * back optimistic dss_max update; if rollback fails,
			 * it's due to another caller of this function having
			 * succeeded since this invocation started, in which
			 * case rollback is not necessary.
			 */
			atomic_cas_p(&dss_max, dss_next, max_cur);
			if (dss_prev == (void *)-1) {
				/* OOM. */
				atomic_write_u(&dss_exhausted, (unsigned)true);
				goto label_oom;
			}
		}
	}
label_oom:
	return (NULL);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, (%rsp)
testq %rcx, %rcx
js 0x2235d
xorl %eax, %eax
xorl %ecx, %ecx
lock
xaddl %ecx, 0x26f8d(%rip)     # 0x490e0
testl %ecx, %ecx
jne 0x2235f
movq %rdx, %rbp
movq %rdi, 0x8(%rsp)
movq %rsi, 0x10(%rsp)
movq %r9, 0x18(%rsp)
leaq -0x1(%r8), %rax
movq %rax, 0x20(%rsp)
negq %r8
movq %r8, 0x28(%rsp)
xorl %r14d, %r14d
xorl %ebx, %ebx
lock
xaddq %rbx, 0x26f5c(%rip)     # 0x490e8
xorl %edi, %edi
callq 0xb2a0
cmpq %rbx, %rax
jb 0x22286
movq %rax, %r12
movq %rbx, %rax
lock
cmpxchgq %r12, 0x26f3d(%rip)     # 0x490e8
sete %al
testb %al, %al
je 0x22181
testq %rbp, %rbp
setne %cl
cmpq %rbp, %r12
setne %dl
testq %r12, %r12
je 0x2235d
movl $0x0, %eax
andb %dl, %cl
jne 0x2235f
movq 0x26f0d(%rip), %r15     # 0x490e8
movq 0x20(%rsp), %rax
leaq (%rax,%r15), %rbx
andq 0x28(%rsp), %rbx
cmpq %r15, %rbx
jb 0x2235d
movq (%rsp), %rax
leaq (%rbx,%rax), %r13
movl $0x0, %eax
cmpq %r15, %r13
jb 0x2235f
leaq 0x26eb9(%rip), %rax     # 0x490c8
movq (%rax), %rdi
leaq 0x26eb7(%rip), %rax     # 0x490d0
movq (%rax), %rcx
movq %r12, %rax
lock
cmpxchgq %r13, 0x26ec0(%rip)     # 0x490e8
sete %al
movl $0x0, %r14d
testb %al, %al
je 0x22181
movq %rcx, %rax
andq %r15, %rax
subq %rax, %rdi
andq %rcx, %rdi
addq %rdi, %r15
movq %rbx, %r14
subq %r15, %r14
addq (%rsp), %rdi
addq %r14, %rdi
callq 0xb2a0
cmpq %r12, %rax
je 0x222b5
movq %rax, %rcx
movq %r13, %rax
lock
cmpxchgq %r12, 0x26e7a(%rip)     # 0x490e8
sete %al
movl $0x0, %r14d
cmpq $-0x1, %rcx
jne 0x22181
jmp 0x22354
movq $0x0, 0x30(%rsp)
movl %r14d, %ecx
movq 0x30(%rsp), %rax
shrq %cl, %rax
testq %rax, %rax
jne 0x222a8
pause
incq 0x30(%rsp)
jmp 0x22292
cmpl $0x3f, %r14d
adcl $0x0, %r14d
jmp 0x22181
cmpq %r15, %rbx
je 0x22305
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %r12
movaps %xmm0, 0x20(%r12)
movaps %xmm0, 0x10(%r12)
movaps %xmm0, (%r12)
andq $0x0, 0x30(%r12)
movq 0x10(%rsp), %r13
movq %r13, %rdi
callq 0x1e135
movq 0x8(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r15, %rcx
movq %r14, %r8
movq %rax, %r9
pushq $0x1
pushq $0x0
callq 0x21eb3
popq %rax
popq %rcx
movq 0x18(%rsp), %rax
cmpb $0x1, (%rax)
jne 0x22329
leaq 0x26286(%rip), %rax     # 0x4859c
cmpb $0x1, (%rax)
je 0x2236e
movq %rbx, %rdi
xorl %esi, %esi
movq (%rsp), %rdx
callq 0xb230
movq 0xa0(%rsp), %rax
cmpb $0x0, (%rax)
movq %rbx, %rax
jne 0x2235f
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x296cf
movq 0xa0(%rsp), %rcx
movb %al, (%rcx)
movq %rbx, %rax
jmp 0x2235f
pushq $0x1
popq %rax
xchgl %eax, 0x26d83(%rip)     # 0x490e0
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x32496
jmp 0x2231b