// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movq %r9, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdi, 0x20(%rsp)
movq %rcx, (%rsp)
testq %rcx, %rcx
js 0x275e4
xorl %eax, %eax
lock
xaddl %eax, 0x25b00(%rip)     # 0x4d0e0
testl %eax, %eax
je 0x275f5
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r8, %r15
movq %rdx, %r13
leaq -0x1(%r8), %rax
movq %rax, 0x38(%rsp)
negq %r15
movq %r15, 0x10(%rsp)
movq %rdx, 0x8(%rsp)
xorl %ebp, %ebp
xorl %r14d, %r14d
lock
xaddq %r14, 0x25ac9(%rip)     # 0x4d0e8
xorl %edi, %edi
callq 0xb2a0
movq %rax, %rbx
cmpq %r14, %rax
jae 0x27666
movq $0x0, 0x40(%rsp)
movq 0x40(%rsp), %rax
movl %ebp, %ecx
shrq %cl, %rax
testq %rax, %rax
jne 0x2765c
movl %ebp, %ecx
pause
incq 0x40(%rsp)
movq 0x40(%rsp), %rax
shrq %cl, %rax
testq %rax, %rax
je 0x27648
cmpl $0x3f, %ebp
adcl $0x0, %ebp
xorl %eax, %eax
jmp 0x2767a
movq %r14, %rax
lock
cmpxchgq %rbx, 0x25a76(%rip)     # 0x4d0e8
sete %al
testb %al, %al
setne %al
testb %al, %al
je 0x27613
testq %r13, %r13
setne %al
cmpq %r13, %rbx
setne %cl
movl $0xa, %r12d
testq %rbx, %rbx
je 0x27808
andb %cl, %al
jne 0x27808
movq 0x25a40(%rip), %r13     # 0x4d0e8
movq 0x38(%rsp), %rax
leaq (%rax,%r13), %rbp
andq %r15, %rbp
movq (%rsp), %rax
leaq (%rax,%rbp), %r14
cmpq %r13, %rbp
setb %al
cmpq %r13, %r14
setb %cl
orb %al, %cl
jne 0x27748
leaq 0x259f5(%rip), %rax     # 0x4d0c8
movq (%rax), %rdi
leaq 0x259f3(%rip), %rax     # 0x4d0d0
movq (%rax), %rcx
movq %rbx, %rax
lock
cmpxchgq %r14, 0x259fc(%rip)     # 0x4d0e8
sete %al
testb %al, %al
je 0x27742
movq %rcx, %rax
andq %r13, %rax
subq %rax, %rdi
andq %rcx, %rdi
addq %rdi, %r13
movq %rbp, %r15
subq %r13, %r15
addq (%rsp), %rdi
addq %r15, %rdi
callq 0xb2a0
cmpq %rbx, %rax
je 0x27752
movq %rax, %rcx
movq %r14, %rax
lock
cmpxchgq %rbx, 0x259c0(%rip)     # 0x4d0e8
sete %al
cmpq $-0x1, %rcx
movq 0x8(%rsp), %r13
je 0x277f8
xorl %r12d, %r12d
jmp 0x27803
movl $0x8, %r12d
movq 0x8(%rsp), %r13
jmp 0x27808
cmpq %r13, %rbp
je 0x277a2
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x70(%rsp)
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x23e89
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
leaq 0x40(%rsp), %rdx
movq %r13, %rcx
movq %r15, %r8
movq %rax, %r9
pushq $0x1
pushq $0x0
callq 0x273ec
addq $0x10, %rsp
movq 0x30(%rsp), %rax
cmpb $0x1, (%rax)
movq 0x10(%rsp), %r15
movq 0x8(%rsp), %r13
movq 0xb0(%rsp), %rbx
jne 0x277d8
leaq 0x24dd7(%rip), %rax     # 0x4c59c
cmpb $0x1, (%rax)
je 0x27828
movq %rbp, %rdi
xorl %esi, %esi
movq (%rsp), %rdx
callq 0xb230
movl $0x1, %r12d
cmpb $0x0, (%rbx)
jne 0x277f1
movq %rbp, %rdi
movq (%rsp), %rsi
callq 0x2e8bd
movb %al, (%rbx)
movq %rbp, 0x18(%rsp)
jmp 0x27808
movl $0x1, %eax
xchgl %eax, 0x258dd(%rip)     # 0x4d0e0
movq 0x10(%rsp), %r15
cmpl $0x7, %r12d
jg 0x27819
testl %r12d, %r12d
je 0x27611
jmp 0x27836
cmpl $0x8, %r12d
je 0x27611
jmp 0x275e4
movq %rbp, %rdi
movq (%rsp), %rsi
callq 0x37116
jmp 0x277ca
movq 0x18(%rsp), %rax
jmp 0x275e6