// File: /workspace/github_c_makefile_star2_O3/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 0x27797
xorl %eax, %eax
xorl %ecx, %ecx
lock
xaddl %ecx, 0x25b61(%rip)     # 0x4d0e0
testl %ecx, %ecx
jne 0x27799
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 %r13d, %r13d
xorl %ebx, %ebx
lock
xaddq %rbx, 0x25b30(%rip)     # 0x4d0e8
xorl %edi, %edi
callq 0xb2a0
cmpq %rbx, %rax
jb 0x276b2
movq %rax, %r12
movq %rbx, %rax
lock
cmpxchgq %r12, 0x25b11(%rip)     # 0x4d0e8
sete %al
testb %al, %al
je 0x275ad
testq %rbp, %rbp
setne %cl
cmpq %rbp, %r12
setne %dl
testq %r12, %r12
je 0x27797
movl $0x0, %eax
andb %dl, %cl
jne 0x27799
movq 0x25ae1(%rip), %r15     # 0x4d0e8
movq 0x20(%rsp), %rax
leaq (%rax,%r15), %r14
andq 0x28(%rsp), %r14
cmpq %r15, %r14
jb 0x27797
movq (%rsp), %rax
leaq (%r14,%rax), %rbx
movl $0x0, %eax
cmpq %r15, %rbx
jb 0x27799
leaq 0x25a8d(%rip), %rax     # 0x4d0c8
movq (%rax), %rdi
leaq 0x25a8b(%rip), %rax     # 0x4d0d0
movq (%rax), %rcx
movq %r12, %rax
lock
cmpxchgq %rbx, 0x25a94(%rip)     # 0x4d0e8
sete %al
movl $0x0, %r13d
testb %al, %al
je 0x275ad
movq %rcx, %rax
andq %r15, %rax
subq %rax, %rdi
andq %rcx, %rdi
addq %rdi, %r15
movq %r14, %r13
subq %r15, %r13
addq (%rsp), %rdi
addq %r13, %rdi
callq 0xb2a0
cmpq %r12, %rax
je 0x276ef
movq %rax, %rcx
movq %rbx, %rax
lock
cmpxchgq %r12, 0x25a4e(%rip)     # 0x4d0e8
sete %al
movl $0x0, %r13d
cmpq $-0x1, %rcx
jne 0x275ad
jmp 0x2778c
movq $0x0, 0x30(%rsp)
movq 0x30(%rsp), %rax
movl %r13d, %ecx
shrq %cl, %rax
testq %rax, %rax
jne 0x276e2
movl %r13d, %ecx
pause
incq 0x30(%rsp)
movq 0x30(%rsp), %rax
shrq %cl, %rax
testq %rax, %rax
je 0x276ce
cmpl $0x3f, %r13d
adcl $0x0, %r13d
jmp 0x275ad
cmpq %r15, %r14
je 0x2773d
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rbx
movaps %xmm0, 0x20(%rbx)
movaps %xmm0, 0x10(%rbx)
movaps %xmm0, (%rbx)
movq $0x0, 0x30(%rbx)
movq 0x10(%rsp), %r12
movq %r12, %rdi
callq 0x23da2
movq 0x8(%rsp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq %r15, %rcx
movq %r13, %r8
movq %rax, %r9
pushq $0x1
pushq $0x0
callq 0x27376
addq $0x10, %rsp
movq 0x18(%rsp), %rax
cmpb $0x1, (%rax)
jne 0x27761
leaq 0x24e4e(%rip), %rax     # 0x4c59c
cmpb $0x1, (%rax)
je 0x277a8
movq %r14, %rdi
xorl %esi, %esi
movq (%rsp), %rdx
callq 0xb230
movq 0xa0(%rsp), %rax
cmpb $0x0, (%rax)
movq %r14, %rax
jne 0x27799
movq %r14, %rdi
movq (%rsp), %rsi
callq 0x2e721
movq 0xa0(%rsp), %rcx
movb %al, (%rcx)
movq %r14, %rax
jmp 0x27799
movl $0x1, %eax
xchgl %eax, 0x25949(%rip)     # 0x4d0e0
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
movq (%rsp), %rsi
callq 0x376c2
jmp 0x27753