#include <iostream> 
#include <vector> 
#include <chrono> 
#include <thread> 

class memory_keeper
{
private: 
    std::vector<void*> _memory;
    const size_t _block_size = 128 * 1024 * 1024; //128MB. 
    size_t _blocks;  

    void allocate_block()
    {
        void *block; 
        cudaMalloc(&block, _block_size); 
        _memory.push_back(block); 
        ++ _blocks; 
    }

    void free_block()
    {
        void *block = _memory.back();
        cudaFree(block); 
        _memory.pop_back();
        -- _blocks;
    }

public: 
    memory_keeper() : _blocks {0}
    {}

    ~memory_keeper()
    {
        if (!_memory.empty())
        {
            void *block = _memory.front();
            cudaFree(block); 
        }
        _blocks = 0; 
    }

    size_t get_blocks()
    {
        return _blocks; 
    }

    bool allocate(size_t blocks)
    {
        for (size_t i=0; i<blocks; ++i)
        {
            allocate_block(); 
        }
        return true; 
    }

    bool free(size_t blocks)
    {
        if (blocks > _blocks)
        {
            return false; 
        }
        
        for (size_t i=0; i<blocks; ++i)
        {
            free_block();
        }

        return true; 
    }

    bool reallocate()
    {
        size_t original_blocks = _blocks;

        while (_blocks > 0)
        {
            free_block();
        }

        for (size_t i=0; i<original_blocks; ++i)
        {
            allocate_block(); 
        }
        return true; 
    }
};

int main()
{
    memory_keeper mk; 
    //c++17, no support. using namespace std::chrono_literals; 
    std::cout << "Try to allocate 1G mem: " << std::endl; 
    mk.allocate(8); 
    std::cout << "Done, sleep for 5 seconds. " << std::endl; 
    std::this_thread::sleep_for(std::chrono::seconds(5));
    std::cout << "Release 512M mem: " << std::endl; 
    mk.free(4); 
    std::cout << "Done, sleep for 5 seconds. " << std::endl; 
    std::this_thread::sleep_for(std::chrono::seconds(5));
    std::cout << "Try to allocate another 1G mem: " << std::endl;
    mk.allocate(8);
    std::cout << "Done, sleep for 5 seconds. " << std::endl; 
    std::this_thread::sleep_for(std::chrono::seconds(5));
    std::cout << "Clean them all. " << std::endl; 
}
	.file	"tmpxft_002805db_00000000-6_cuda_memory_keeper_cpp.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB4233:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE4233:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.type	_ZNSt11this_thread9sleep_forIlSt5ratioILl1ELl1EEEEvRKNSt6chrono8durationIT_T0_EE.isra.0, @function
_ZNSt11this_thread9sleep_forIlSt5ratioILl1ELl1EEEEvRKNSt6chrono8durationIT_T0_EE.isra.0:
.LFB5023:
	.cfi_startproc
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	subq	$32, %rsp
	.cfi_def_cfa_offset 48
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	testq	%rdi, %rdi
	jle	.L2
	xorl	%eax, %eax
	movq	%rdi, 8(%rsp)
	leaq	8(%rsp), %rbx
	movq	%rax, 16(%rsp)
.L5:
	movq	%rbx, %rsi
	movq	%rbx, %rdi
	call	nanosleep@PLT
	incl	%eax
	jne	.L2
	call	__errno_location@PLT
	cmpl	$4, (%rax)
	je	.L5
.L2:
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L6
	call	__stack_chk_fail@PLT
.L6:
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE5023:
	.size	_ZNSt11this_thread9sleep_forIlSt5ratioILl1ELl1EEEEvRKNSt6chrono8durationIT_T0_EE.isra.0, .-_ZNSt11this_thread9sleep_forIlSt5ratioILl1ELl1EEEEvRKNSt6chrono8durationIT_T0_EE.isra.0
	.align 2
	.type	_ZNSolsEPFRSoS_E.isra.0, @function
_ZNSolsEPFRSoS_E.isra.0:
.LFB5024:
	.cfi_startproc
	jmp	*%rsi
	.cfi_endproc
.LFE5024:
	.size	_ZNSolsEPFRSoS_E.isra.0, .-_ZNSolsEPFRSoS_E.isra.0
	.section	.text._ZN13memory_keeperD2Ev,"axG",@progbits,_ZN13memory_keeperD5Ev,comdat
	.align 2
	.weak	_ZN13memory_keeperD2Ev
	.type	_ZN13memory_keeperD2Ev, @function
_ZN13memory_keeperD2Ev:
.LFB4224:
	.cfi_startproc
	.cfi_personality 0x9b,DW.ref.__gxx_personality_v0
	.cfi_lsda 0x1b,.LLSDA4224
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	movq	(%rdi), %rax
	cmpq	8(%rdi), %rax
	je	.L12
	movq	(%rax), %rdi
	call	cudaFree@PLT
.L12:
	movq	(%rbx), %rdi
	xorl	%eax, %eax
	movq	%rax, 32(%rbx)
	testq	%rdi, %rdi
	je	.L11
	movq	16(%rbx), %rsi
	popq	%rbx
	.cfi_remember_state
	.cfi_def_cfa_offset 8
	subq	%rdi, %rsi
	jmp	_ZdlPvm@PLT
.L11:
	.cfi_restore_state
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE4224:
	.globl	__gxx_personality_v0
	.section	.gcc_except_table._ZN13memory_keeperD2Ev,"aG",@progbits,_ZN13memory_keeperD5Ev,comdat
.LLSDA4224:
	.byte	0xff
	.byte	0xff
	.byte	0x1
	.uleb128 .LLSDACSE4224-.LLSDACSB4224
.LLSDACSB4224:
.LLSDACSE4224:
	.section	.text._ZN13memory_keeperD2Ev,"axG",@progbits,_ZN13memory_keeperD5Ev,comdat
	.size	_ZN13memory_keeperD2Ev, .-_ZN13memory_keeperD2Ev
	.weak	_ZN13memory_keeperD1Ev
	.set	_ZN13memory_keeperD1Ev,_ZN13memory_keeperD2Ev
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB4256:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE4256:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.rodata._ZNSt6vectorIPvSaIS0_EE17_M_realloc_insertIJRKS0_EEEvN9__gnu_cxx17__normal_iteratorIPS0_S2_EEDpOT_.str1.1,"aMS",@progbits,1
.LC0:
	.string	"vector::_M_realloc_insert"
	.section	.text._ZNSt6vectorIPvSaIS0_EE17_M_realloc_insertIJRKS0_EEEvN9__gnu_cxx17__normal_iteratorIPS0_S2_EEDpOT_,"axG",@progbits,_ZNSt6vectorIPvSaIS0_EE17_M_realloc_insertIJRKS0_EEEvN9__gnu_cxx17__normal_iteratorIPS0_S2_EEDpOT_,comdat
	.align 2
	.weak	_ZNSt6vectorIPvSaIS0_EE17_M_realloc_insertIJRKS0_EEEvN9__gnu_cxx17__normal_iteratorIPS0_S2_EEDpOT_
	.type	_ZNSt6vectorIPvSaIS0_EE17_M_realloc_insertIJRKS0_EEEvN9__gnu_cxx17__normal_iteratorIPS0_S2_EEDpOT_, @function
_ZNSt6vectorIPvSaIS0_EE17_M_realloc_insertIJRKS0_EEEvN9__gnu_cxx17__normal_iteratorIPS0_S2_EEDpOT_:
.LFB4765:
	.cfi_startproc
	endbr64
	movabsq	$1152921504606846975, %rcx
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	movq	8(%rdi), %rax
	movq	(%rdi), %r14
	movq	%rax, %rbx
	movq	%rax, (%rsp)
	subq	%r14, %rbx
	sarq	$3, %rbx
	cmpq	%rcx, %rbx
	jne	.L18
	leaq	.LC0(%rip), %rdi
	call	_ZSt20__throw_length_errorPKc@PLT
.L18:
	cmpq	%r14, (%rsp)
	movl	$1, %eax
	movq	%rsi, %r15
	movq	%rdi, %r13
	cmovne	%rbx, %rax
	movq	%rsi, %rbp
	addq	%rax, %rbx
	setc	%al
	subq	%r14, %r15
	movzbl	%al, %eax
	testq	%rax, %rax
	jne	.L28
	xorl	%r12d, %r12d
	testq	%rbx, %rbx
	je	.L23
	movabsq	$1152921504606846975, %rax
	cmpq	%rax, %rbx
	cmova	%rax, %rbx
	jmp	.L22
.L28:
	movq	%rcx, %rbx
.L22:
	leaq	0(,%rbx,8), %rdi
	movq	%rdx, 8(%rsp)
	call	_Znwm@PLT
	movq	8(%rsp), %rdx
	movq	%rax, %r12
.L23:
	movq	(%rdx), %rax
	movq	%rax, (%r12,%r15)
	testq	%r15, %r15
	jle	.L24
	movq	%r15, %rdx
	movq	%r14, %rsi
	movq	%r12, %rdi
	call	memmove@PLT
.L24:
	movq	(%rsp), %rax
	leaq	8(%r12,%r15), %rdx
	subq	%rbp, %rax
	testq	%rax, %rax
	jle	.L25
	movq	%rdx, %rdi
	movq	%rbp, %rsi
	movq	%rax, %rcx
	rep movsb
.L25:
	leaq	(%rdx,%rax), %rbp
	testq	%r14, %r14
	je	.L26
	movq	16(%r13), %rsi
	movq	%r14, %rdi
	subq	%r14, %rsi
	call	_ZdlPvm@PLT
.L26:
	leaq	(%r12,%rbx,8), %rax
	movq	%r12, 0(%r13)
	movq	%rbp, 8(%r13)
	movq	%rax, 16(%r13)
	addq	$24, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE4765:
	.size	_ZNSt6vectorIPvSaIS0_EE17_M_realloc_insertIJRKS0_EEEvN9__gnu_cxx17__normal_iteratorIPS0_S2_EEDpOT_, .-_ZNSt6vectorIPvSaIS0_EE17_M_realloc_insertIJRKS0_EEEvN9__gnu_cxx17__normal_iteratorIPS0_S2_EEDpOT_
	.text
	.align 2
	.type	_ZN13memory_keeper8allocateEm.isra.0, @function
_ZN13memory_keeper8allocateEm.isra.0:
.LFB5032:
	.cfi_startproc
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movq	%rsi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movq	%rdi, %rbx
	subq	$24, %rsp
	.cfi_def_cfa_offset 64
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	movq	%rsp, %r13
.L36:
	cmpq	%rbp, %r12
	je	.L42
	movq	24(%rbx), %rsi
	movq	%r13, %rdi
	call	cudaMalloc@PLT
	movq	8(%rbx), %rsi
	cmpq	16(%rbx), %rsi
	je	.L37
	movq	(%rsp), %rax
	movq	%rax, (%rsi)
	addq	$8, 8(%rbx)
	jmp	.L38
.L37:
	movq	%r13, %rdx
	movq	%rbx, %rdi
	call	_ZNSt6vectorIPvSaIS0_EE17_M_realloc_insertIJRKS0_EEEvN9__gnu_cxx17__normal_iteratorIPS0_S2_EEDpOT_
.L38:
	incq	32(%rbx)
	incq	%r12
	jmp	.L36
.L42:
	movq	8(%rsp), %rax
	subq	%fs:40, %rax
	je	.L40
	call	__stack_chk_fail@PLT
.L40:
	addq	$24, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE5032:
	.size	_ZN13memory_keeper8allocateEm.isra.0, .-_ZN13memory_keeper8allocateEm.isra.0
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"Try to allocate 1G mem: "
.LC2:
	.string	"Done, sleep for 5 seconds. "
.LC3:
	.string	"Release 512M mem: "
.LC4:
	.string	"Try to allocate another 1G mem: "
.LC5:
	.string	"Clean them all. "
	.section	.text.startup
	.globl	main
	.type	main, @function
main:
.LFB4230:
	.cfi_startproc
	.cfi_personality 0x9b,DW.ref.__gxx_personality_v0
	.cfi_lsda 0x1b,.LLSDA4230
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	xorl	%edx, %edx
	leaq	.LC1(%rip), %rsi
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	leaq	_ZSt4cout(%rip), %rbx
	movq	%rbx, %rdi
	subq	$48, %rsp
	.cfi_def_cfa_offset 96
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	xorl	%eax, %eax
	movq	%rdx, 32(%rsp)
	movq	%rsp, %rbp
	movq	%rax, (%rsp)
	movq	%rax, 8(%rsp)
	movq	%rax, 16(%rsp)
	movq	$134217728, 24(%rsp)
.LEHB0:
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	movl	$8, %esi
	movq	%rbp, %rdi
	call	_ZN13memory_keeper8allocateEm.isra.0
	leaq	.LC2(%rip), %r13
	movq	%rbx, %rdi
	movq	%r13, %rsi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	movl	$5, %edi
	call	_ZNSt11this_thread9sleep_forIlSt5ratioILl1ELl1EEEEvRKNSt6chrono8durationIT_T0_EE.isra.0
	leaq	.LC3(%rip), %rsi
	movq	%rbx, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	cmpq	$3, 32(%rsp)
	jbe	.L44
	movl	$4, %r14d
.L45:
	movq	8(%rsp), %r12
	movq	-8(%r12), %rdi
	call	cudaFree@PLT
	subq	$8, %r12
	decq	32(%rsp)
	movq	%r12, 8(%rsp)
	decq	%r14
	jne	.L45
.L44:
	movq	%r13, %rsi
	movq	%rbx, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	movl	$5, %edi
	call	_ZNSt11this_thread9sleep_forIlSt5ratioILl1ELl1EEEEvRKNSt6chrono8durationIT_T0_EE.isra.0
	leaq	.LC4(%rip), %rsi
	movq	%rbx, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	movl	$8, %esi
	movq	%rbp, %rdi
	call	_ZN13memory_keeper8allocateEm.isra.0
	movq	%r13, %rsi
	movq	%rbx, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	movl	$5, %edi
	call	_ZNSt11this_thread9sleep_forIlSt5ratioILl1ELl1EEEEvRKNSt6chrono8durationIT_T0_EE.isra.0
	leaq	.LC5(%rip), %rsi
	movq	%rbx, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
.LEHE0:
	movq	%rbp, %rdi
	call	_ZN13memory_keeperD1Ev
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L48
	jmp	.L51
.L49:
	endbr64
	movq	%rax, %rbx
.L46:
	movq	%rbp, %rdi
	call	_ZN13memory_keeperD1Ev
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L51
	movq	%rbx, %rdi
.LEHB1:
	call	_Unwind_Resume@PLT
.LEHE1:
.L51:
	call	__stack_chk_fail@PLT
.L48:
	addq	$48, %rsp
	.cfi_def_cfa_offset 48
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE4230:
	.section	.gcc_except_table,"a",@progbits
.LLSDA4230:
	.byte	0xff
	.byte	0xff
	.byte	0x1
	.uleb128 .LLSDACSE4230-.LLSDACSB4230
.LLSDACSB4230:
	.uleb128 .LEHB0-.LFB4230
	.uleb128 .LEHE0-.LEHB0
	.uleb128 .L49-.LFB4230
	.uleb128 0
	.uleb128 .LEHB1-.LFB4230
	.uleb128 .LEHE1-.LEHB1
	.uleb128 0
	.uleb128 0
.LLSDACSE4230:
	.section	.text.startup
	.size	main, .-main
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.hidden	DW.ref.__gxx_personality_v0
	.weak	DW.ref.__gxx_personality_v0
	.section	.data.rel.local.DW.ref.__gxx_personality_v0,"awG",@progbits,DW.ref.__gxx_personality_v0,comdat
	.align 8
	.type	DW.ref.__gxx_personality_v0, @object
	.size	DW.ref.__gxx_personality_v0, 8
DW.ref.__gxx_personality_v0:
	.quad	__gxx_personality_v0
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


#include <hip/hip_runtime.h>
#include <iostream> 
#include <vector> 
#include <chrono> 
#include <thread> 

class memory_keeper
{
private: 
    std::vector<void*> _memory;
    const size_t _block_size = 128 * 1024 * 1024; //128MB. 
    size_t _blocks;  

    void allocate_block()
    {
        void *block; 
        hipMalloc(&block, _block_size); 
        _memory.push_back(block); 
        ++ _blocks; 
    }

    void free_block()
    {
        void *block = _memory.back();
        hipFree(block); 
        _memory.pop_back();
        -- _blocks;
    }

public: 
    memory_keeper() : _blocks {0}
    {}

    ~memory_keeper()
    {
        if (!_memory.empty())
        {
            void *block = _memory.front();
            hipFree(block); 
        }
        _blocks = 0; 
    }

    size_t get_blocks()
    {
        return _blocks; 
    }

    bool allocate(size_t blocks)
    {
        for (size_t i=0; i<blocks; ++i)
        {
            allocate_block(); 
        }
        return true; 
    }

    bool free(size_t blocks)
    {
        if (blocks > _blocks)
        {
            return false; 
        }
        
        for (size_t i=0; i<blocks; ++i)
        {
            free_block();
        }

        return true; 
    }

    bool reallocate()
    {
        size_t original_blocks = _blocks;

        while (_blocks > 0)
        {
            free_block();
        }

        for (size_t i=0; i<original_blocks; ++i)
        {
            allocate_block(); 
        }
        return true; 
    }
};

int main()
{
    memory_keeper mk; 
    //c++17, no support. using namespace std::chrono_literals; 
    std::cout << "Try to allocate 1G mem: " << std::endl; 
    mk.allocate(8); 
    std::cout << "Done, sleep for 5 seconds. " << std::endl; 
    std::this_thread::sleep_for(std::chrono::seconds(5));
    std::cout << "Release 512M mem: " << std::endl; 
    mk.free(4); 
    std::cout << "Done, sleep for 5 seconds. " << std::endl; 
    std::this_thread::sleep_for(std::chrono::seconds(5));
    std::cout << "Try to allocate another 1G mem: " << std::endl;
    mk.allocate(8);
    std::cout << "Done, sleep for 5 seconds. " << std::endl; 
    std::this_thread::sleep_for(std::chrono::seconds(5));
    std::cout << "Clean them all. " << std::endl; 
}
	.text
	.file	"cuda_memory_keeper_cpp.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
.Lfunc_begin0:
	.cfi_startproc
	.cfi_personality 3, __gxx_personality_v0
	.cfi_lsda 3, .Lexception0
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	subq	$56, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	xorps	%xmm0, %xmm0
	movaps	%xmm0, (%rsp)
	xorl	%eax, %eax
	movq	%rax, 16(%rsp)
	movq	$134217728, 24(%rsp)            # imm = 0x8000000
	movq	%rax, 32(%rsp)
.Ltmp0:
	movl	$_ZSt4cout, %ebx
	movl	$_ZSt4cout, %edi
	movl	$.L.str, %esi
	movl	$24, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
.Ltmp1:
# %bb.1:                                # %_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.exit
	movq	_ZSt4cout(%rip), %rax
	addq	-24(%rax), %rbx
.Ltmp2:
	movq	%rbx, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
.Ltmp3:
# %bb.2:                                # %.noexc42
.Ltmp4:
	movsbl	%al, %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo3putEc
.Ltmp5:
# %bb.3:                                # %.noexc43
.Ltmp6:
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
.Ltmp7:
# %bb.4:                                # %.lr.ph.i.preheader
	movl	$8, %r14d
	movq	%rsp, %rbx
.LBB0_5:                                # %.lr.ph.i
                                        # =>This Inner Loop Header: Depth=1
.Ltmp8:
	movq	%rbx, %rdi
	callq	_ZN13memory_keeper14allocate_blockEv
.Ltmp9:
# %bb.6:                                # %.noexc
                                        #   in Loop: Header=BB0_5 Depth=1
	decq	%r14
	jne	.LBB0_5
# %bb.7:                                # %_ZN13memory_keeper8allocateEm.exit
.Ltmp11:
	movl	$_ZSt4cout, %ebx
	movl	$_ZSt4cout, %edi
	movl	$.L.str.1, %esi
	movl	$27, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
.Ltmp12:
# %bb.8:                                # %_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.exit4
	movq	_ZSt4cout(%rip), %rax
	addq	-24(%rax), %rbx
.Ltmp13:
	movq	%rbx, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
.Ltmp14:
# %bb.9:                                # %.noexc45
.Ltmp15:
	movsbl	%al, %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo3putEc
.Ltmp16:
# %bb.10:                               # %.noexc46
.Ltmp17:
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
.Ltmp18:
# %bb.11:                               # %_ZNSolsEPFRSoS_E.exit6
	leaq	40(%rsp), %rbx
	movq	$5, (%rbx)
	movq	$0, 8(%rbx)
.LBB0_12:                               # =>This Inner Loop Header: Depth=1
.Ltmp19:
	movq	%rbx, %rdi
	movq	%rbx, %rsi
	callq	nanosleep
.Ltmp20:
# %bb.13:                               # %.noexc7
                                        #   in Loop: Header=BB0_12 Depth=1
	cmpl	$-1, %eax
	jne	.LBB0_15
# %bb.14:                               #   in Loop: Header=BB0_12 Depth=1
	callq	__errno_location
	cmpl	$4, (%rax)
	je	.LBB0_12
.LBB0_15:
.Ltmp22:
	movl	$_ZSt4cout, %ebx
	movl	$_ZSt4cout, %edi
	movl	$.L.str.2, %esi
	movl	$18, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
.Ltmp23:
# %bb.16:                               # %_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.exit9
	movq	_ZSt4cout(%rip), %rax
	addq	-24(%rax), %rbx
.Ltmp24:
	movq	%rbx, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
.Ltmp25:
# %bb.17:                               # %.noexc49
.Ltmp26:
	movsbl	%al, %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo3putEc
.Ltmp27:
# %bb.18:                               # %.noexc50
.Ltmp28:
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
.Ltmp29:
# %bb.19:                               # %_ZNSolsEPFRSoS_E.exit11
	cmpq	$4, 32(%rsp)
	jb	.LBB0_23
# %bb.20:                               # %.lr.ph.i12
	movl	$4, %ebx
	movq	8(%rsp), %rax
.LBB0_21:                               # =>This Inner Loop Header: Depth=1
	movq	-8(%rax), %rdi
.Ltmp30:
	callq	hipFree
.Ltmp31:
# %bb.22:                               # %.noexc14
                                        #   in Loop: Header=BB0_21 Depth=1
	movq	8(%rsp), %rax
	addq	$-8, %rax
	movq	%rax, 8(%rsp)
	decq	32(%rsp)
	decq	%rbx
	jne	.LBB0_21
.LBB0_23:                               # %_ZN13memory_keeper4freeEm.exit
.Ltmp33:
	movl	$_ZSt4cout, %ebx
	movl	$_ZSt4cout, %edi
	movl	$.L.str.1, %esi
	movl	$27, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
.Ltmp34:
# %bb.24:                               # %_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.exit16
	movq	_ZSt4cout(%rip), %rax
	addq	-24(%rax), %rbx
.Ltmp35:
	movq	%rbx, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
.Ltmp36:
# %bb.25:                               # %.noexc53
.Ltmp37:
	movsbl	%al, %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo3putEc
.Ltmp38:
# %bb.26:                               # %.noexc54
.Ltmp39:
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
.Ltmp40:
# %bb.27:                               # %_ZNSolsEPFRSoS_E.exit18
	leaq	40(%rsp), %rbx
	movq	$5, (%rbx)
	movq	$0, 8(%rbx)
.LBB0_28:                               # =>This Inner Loop Header: Depth=1
.Ltmp41:
	movq	%rbx, %rdi
	movq	%rbx, %rsi
	callq	nanosleep
.Ltmp42:
# %bb.29:                               # %.noexc20
                                        #   in Loop: Header=BB0_28 Depth=1
	cmpl	$-1, %eax
	jne	.LBB0_31
# %bb.30:                               #   in Loop: Header=BB0_28 Depth=1
	callq	__errno_location
	cmpl	$4, (%rax)
	je	.LBB0_28
.LBB0_31:
.Ltmp44:
	movl	$_ZSt4cout, %ebx
	movl	$_ZSt4cout, %edi
	movl	$.L.str.3, %esi
	movl	$32, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
.Ltmp45:
# %bb.32:                               # %_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.exit23
	movq	_ZSt4cout(%rip), %rax
	addq	-24(%rax), %rbx
.Ltmp46:
	movq	%rbx, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
.Ltmp47:
# %bb.33:                               # %.noexc57
.Ltmp48:
	movsbl	%al, %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo3putEc
.Ltmp49:
# %bb.34:                               # %.noexc58
.Ltmp50:
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
.Ltmp51:
# %bb.35:                               # %.lr.ph.i26.preheader
	movl	$8, %r14d
	movq	%rsp, %rbx
.LBB0_36:                               # %.lr.ph.i26
                                        # =>This Inner Loop Header: Depth=1
.Ltmp52:
	movq	%rbx, %rdi
	callq	_ZN13memory_keeper14allocate_blockEv
.Ltmp53:
# %bb.37:                               # %.noexc29
                                        #   in Loop: Header=BB0_36 Depth=1
	decq	%r14
	jne	.LBB0_36
# %bb.38:                               # %_ZN13memory_keeper8allocateEm.exit30
.Ltmp55:
	movl	$_ZSt4cout, %ebx
	movl	$_ZSt4cout, %edi
	movl	$.L.str.1, %esi
	movl	$27, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
.Ltmp56:
# %bb.39:                               # %_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.exit32
	movq	_ZSt4cout(%rip), %rax
	addq	-24(%rax), %rbx
.Ltmp57:
	movq	%rbx, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
.Ltmp58:
# %bb.40:                               # %.noexc61
.Ltmp59:
	movsbl	%al, %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo3putEc
.Ltmp60:
# %bb.41:                               # %.noexc62
.Ltmp61:
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
.Ltmp62:
# %bb.42:                               # %_ZNSolsEPFRSoS_E.exit34
	leaq	40(%rsp), %rbx
	movq	$5, (%rbx)
	movq	$0, 8(%rbx)
.LBB0_43:                               # =>This Inner Loop Header: Depth=1
.Ltmp63:
	movq	%rbx, %rdi
	movq	%rbx, %rsi
	callq	nanosleep
.Ltmp64:
# %bb.44:                               # %.noexc36
                                        #   in Loop: Header=BB0_43 Depth=1
	cmpl	$-1, %eax
	jne	.LBB0_46
# %bb.45:                               #   in Loop: Header=BB0_43 Depth=1
	callq	__errno_location
	cmpl	$4, (%rax)
	je	.LBB0_43
.LBB0_46:
.Ltmp66:
	movl	$_ZSt4cout, %ebx
	movl	$_ZSt4cout, %edi
	movl	$.L.str.4, %esi
	movl	$16, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
.Ltmp67:
# %bb.47:                               # %_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.exit39
	movq	_ZSt4cout(%rip), %rax
	addq	-24(%rax), %rbx
.Ltmp68:
	movq	%rbx, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
.Ltmp69:
# %bb.48:                               # %.noexc65
.Ltmp70:
	movsbl	%al, %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo3putEc
.Ltmp71:
# %bb.49:                               # %.noexc66
.Ltmp72:
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
.Ltmp73:
# %bb.50:                               # %_ZNSolsEPFRSoS_E.exit41
	movq	%rsp, %rdi
	callq	_ZN13memory_keeperD2Ev
	xorl	%eax, %eax
	addq	$56, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	retq
.LBB0_52:                               # %.loopexit.split-lp.loopexit
	.cfi_def_cfa_offset 80
.Ltmp32:
	jmp	.LBB0_58
.LBB0_57:
.Ltmp65:
	jmp	.LBB0_58
.LBB0_56:
.Ltmp43:
	jmp	.LBB0_58
.LBB0_55:
.Ltmp21:
	jmp	.LBB0_58
.LBB0_54:                               # %.loopexit.split-lp.loopexit.split-lp.loopexit.split-lp
.Ltmp74:
	jmp	.LBB0_58
.LBB0_51:                               # %.loopexit
.Ltmp54:
	jmp	.LBB0_58
.LBB0_53:                               # %.loopexit.split-lp.loopexit.split-lp.loopexit
.Ltmp10:
.LBB0_58:                               # %.loopexit.split-lp
	movq	%rax, %rbx
	movq	%rsp, %rdi
	callq	_ZN13memory_keeperD2Ev
	movq	%rbx, %rdi
	callq	_Unwind_Resume@PLT
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
	.section	.gcc_except_table,"a",@progbits
	.p2align	2, 0x0
GCC_except_table0:
.Lexception0:
	.byte	255                             # @LPStart Encoding = omit
	.byte	255                             # @TType Encoding = omit
	.byte	1                               # Call site Encoding = uleb128
	.uleb128 .Lcst_end0-.Lcst_begin0
.Lcst_begin0:
	.uleb128 .Ltmp0-.Lfunc_begin0           # >> Call Site 1 <<
	.uleb128 .Ltmp7-.Ltmp0                  #   Call between .Ltmp0 and .Ltmp7
	.uleb128 .Ltmp74-.Lfunc_begin0          #     jumps to .Ltmp74
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp8-.Lfunc_begin0           # >> Call Site 2 <<
	.uleb128 .Ltmp9-.Ltmp8                  #   Call between .Ltmp8 and .Ltmp9
	.uleb128 .Ltmp10-.Lfunc_begin0          #     jumps to .Ltmp10
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp11-.Lfunc_begin0          # >> Call Site 3 <<
	.uleb128 .Ltmp18-.Ltmp11                #   Call between .Ltmp11 and .Ltmp18
	.uleb128 .Ltmp74-.Lfunc_begin0          #     jumps to .Ltmp74
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp19-.Lfunc_begin0          # >> Call Site 4 <<
	.uleb128 .Ltmp20-.Ltmp19                #   Call between .Ltmp19 and .Ltmp20
	.uleb128 .Ltmp21-.Lfunc_begin0          #     jumps to .Ltmp21
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp22-.Lfunc_begin0          # >> Call Site 5 <<
	.uleb128 .Ltmp29-.Ltmp22                #   Call between .Ltmp22 and .Ltmp29
	.uleb128 .Ltmp74-.Lfunc_begin0          #     jumps to .Ltmp74
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp30-.Lfunc_begin0          # >> Call Site 6 <<
	.uleb128 .Ltmp31-.Ltmp30                #   Call between .Ltmp30 and .Ltmp31
	.uleb128 .Ltmp32-.Lfunc_begin0          #     jumps to .Ltmp32
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp33-.Lfunc_begin0          # >> Call Site 7 <<
	.uleb128 .Ltmp40-.Ltmp33                #   Call between .Ltmp33 and .Ltmp40
	.uleb128 .Ltmp74-.Lfunc_begin0          #     jumps to .Ltmp74
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp41-.Lfunc_begin0          # >> Call Site 8 <<
	.uleb128 .Ltmp42-.Ltmp41                #   Call between .Ltmp41 and .Ltmp42
	.uleb128 .Ltmp43-.Lfunc_begin0          #     jumps to .Ltmp43
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp44-.Lfunc_begin0          # >> Call Site 9 <<
	.uleb128 .Ltmp51-.Ltmp44                #   Call between .Ltmp44 and .Ltmp51
	.uleb128 .Ltmp74-.Lfunc_begin0          #     jumps to .Ltmp74
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp52-.Lfunc_begin0          # >> Call Site 10 <<
	.uleb128 .Ltmp53-.Ltmp52                #   Call between .Ltmp52 and .Ltmp53
	.uleb128 .Ltmp54-.Lfunc_begin0          #     jumps to .Ltmp54
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp55-.Lfunc_begin0          # >> Call Site 11 <<
	.uleb128 .Ltmp62-.Ltmp55                #   Call between .Ltmp55 and .Ltmp62
	.uleb128 .Ltmp74-.Lfunc_begin0          #     jumps to .Ltmp74
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp63-.Lfunc_begin0          # >> Call Site 12 <<
	.uleb128 .Ltmp64-.Ltmp63                #   Call between .Ltmp63 and .Ltmp64
	.uleb128 .Ltmp65-.Lfunc_begin0          #     jumps to .Ltmp65
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp66-.Lfunc_begin0          # >> Call Site 13 <<
	.uleb128 .Ltmp73-.Ltmp66                #   Call between .Ltmp66 and .Ltmp73
	.uleb128 .Ltmp74-.Lfunc_begin0          #     jumps to .Ltmp74
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp73-.Lfunc_begin0          # >> Call Site 14 <<
	.uleb128 .Lfunc_end0-.Ltmp73            #   Call between .Ltmp73 and .Lfunc_end0
	.byte	0                               #     has no landing pad
	.byte	0                               #   On action: cleanup
.Lcst_end0:
	.p2align	2, 0x0
                                        # -- End function
	.section	.text._ZN13memory_keeperD2Ev,"axG",@progbits,_ZN13memory_keeperD2Ev,comdat
	.weak	_ZN13memory_keeperD2Ev          # -- Begin function _ZN13memory_keeperD2Ev
	.p2align	1, 0x90
	.type	_ZN13memory_keeperD2Ev,@function
_ZN13memory_keeperD2Ev:                 # @_ZN13memory_keeperD2Ev
.Lfunc_begin1:
	.cfi_startproc
	.cfi_personality 3, __gxx_personality_v0
	.cfi_lsda 3, .Lexception1
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movq	(%rdi), %rdi
	cmpq	8(%rbx), %rdi
	je	.LBB1_3
# %bb.1:
	movq	(%rdi), %rdi
.Ltmp75:
	callq	hipFree
.Ltmp76:
# %bb.2:                                # %thread-pre-split
	movq	(%rbx), %rdi
.LBB1_3:
	movq	$0, 32(%rbx)
	testq	%rdi, %rdi
	je	.LBB1_4
# %bb.6:
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	_ZdlPv                          # TAILCALL
.LBB1_4:                                # %_ZNSt6vectorIPvSaIS0_EED2Ev.exit
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.LBB1_5:
	.cfi_def_cfa_offset 16
.Ltmp77:
	movq	%rax, %rdi
	callq	__clang_call_terminate
.Lfunc_end1:
	.size	_ZN13memory_keeperD2Ev, .Lfunc_end1-_ZN13memory_keeperD2Ev
	.cfi_endproc
	.section	.gcc_except_table._ZN13memory_keeperD2Ev,"aG",@progbits,_ZN13memory_keeperD2Ev,comdat
	.p2align	2, 0x0
GCC_except_table1:
.Lexception1:
	.byte	255                             # @LPStart Encoding = omit
	.byte	3                               # @TType Encoding = udata4
	.uleb128 .Lttbase0-.Lttbaseref0
.Lttbaseref0:
	.byte	1                               # Call site Encoding = uleb128
	.uleb128 .Lcst_end1-.Lcst_begin1
.Lcst_begin1:
	.uleb128 .Ltmp75-.Lfunc_begin1          # >> Call Site 1 <<
	.uleb128 .Ltmp76-.Ltmp75                #   Call between .Ltmp75 and .Ltmp76
	.uleb128 .Ltmp77-.Lfunc_begin1          #     jumps to .Ltmp77
	.byte	1                               #   On action: 1
.Lcst_end1:
	.byte	1                               # >> Action Record 1 <<
                                        #   Catch TypeInfo 1
	.byte	0                               #   No further actions
	.p2align	2, 0x0
                                        # >> Catch TypeInfos <<
	.long	0                               # TypeInfo 1
.Lttbase0:
	.p2align	2, 0x0
                                        # -- End function
	.section	.text._ZN13memory_keeper14allocate_blockEv,"axG",@progbits,_ZN13memory_keeper14allocate_blockEv,comdat
	.weak	_ZN13memory_keeper14allocate_blockEv # -- Begin function _ZN13memory_keeper14allocate_blockEv
	.p2align	1, 0x90
	.type	_ZN13memory_keeper14allocate_blockEv,@function
_ZN13memory_keeper14allocate_blockEv:   # @_ZN13memory_keeper14allocate_blockEv
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movq	24(%rdi), %rsi
	leaq	8(%rsp), %rdi
	callq	hipMalloc
	movq	8(%rbx), %rsi
	cmpq	16(%rbx), %rsi
	je	.LBB2_2
# %bb.1:
	movq	8(%rsp), %rax
	movq	%rax, (%rsi)
	addq	$8, 8(%rbx)
	jmp	.LBB2_3
.LBB2_2:
	leaq	8(%rsp), %rdx
	movq	%rbx, %rdi
	callq	_ZNSt6vectorIPvSaIS0_EE17_M_realloc_insertIJRKS0_EEEvN9__gnu_cxx17__normal_iteratorIPS0_S2_EEDpOT_
.LBB2_3:                                # %_ZNSt6vectorIPvSaIS0_EE9push_backERKS0_.exit
	incq	32(%rbx)
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	_ZN13memory_keeper14allocate_blockEv, .Lfunc_end2-_ZN13memory_keeper14allocate_blockEv
	.cfi_endproc
                                        # -- End function
	.section	.text._ZNSt6vectorIPvSaIS0_EE17_M_realloc_insertIJRKS0_EEEvN9__gnu_cxx17__normal_iteratorIPS0_S2_EEDpOT_,"axG",@progbits,_ZNSt6vectorIPvSaIS0_EE17_M_realloc_insertIJRKS0_EEEvN9__gnu_cxx17__normal_iteratorIPS0_S2_EEDpOT_,comdat
	.weak	_ZNSt6vectorIPvSaIS0_EE17_M_realloc_insertIJRKS0_EEEvN9__gnu_cxx17__normal_iteratorIPS0_S2_EEDpOT_ # -- Begin function _ZNSt6vectorIPvSaIS0_EE17_M_realloc_insertIJRKS0_EEEvN9__gnu_cxx17__normal_iteratorIPS0_S2_EEDpOT_
	.p2align	1, 0x90
	.type	_ZNSt6vectorIPvSaIS0_EE17_M_realloc_insertIJRKS0_EEEvN9__gnu_cxx17__normal_iteratorIPS0_S2_EEDpOT_,@function
_ZNSt6vectorIPvSaIS0_EE17_M_realloc_insertIJRKS0_EEEvN9__gnu_cxx17__normal_iteratorIPS0_S2_EEDpOT_: # @_ZNSt6vectorIPvSaIS0_EE17_M_realloc_insertIJRKS0_EEEvN9__gnu_cxx17__normal_iteratorIPS0_S2_EEDpOT_
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rdx, (%rsp)                    # 8-byte Spill
	movq	%rsi, %r12
	movq	%rdi, %rbx
	movl	$1, %esi
	movl	$.L.str.5, %edx
	callq	_ZNKSt6vectorIPvSaIS0_EE12_M_check_lenEmPKc
	movq	(%rbx), %r13
	movq	8(%rbx), %r15
	movq	%r12, 8(%rsp)                   # 8-byte Spill
	subq	%r13, %r12
	movq	%r12, %r14
	sarq	$3, %r14
	testq	%rax, %rax
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	je	.LBB3_1
# %bb.2:
	movq	%rbx, %rdi
	movq	%rax, %rsi
	xorl	%edx, %edx
	callq	_ZNSt15__new_allocatorIPvE8allocateEmPKv
	movq	%rax, %rbp
	jmp	.LBB3_3
.LBB3_1:
	xorl	%ebp, %ebp
.LBB3_3:                                # %_ZNSt12_Vector_baseIPvSaIS0_EE11_M_allocateEm.exit
	movq	(%rsp), %rax                    # 8-byte Reload
	movq	(%rax), %rax
	movq	%rax, (%rbp,%r14,8)
	testq	%r12, %r12
	jle	.LBB3_5
# %bb.4:
	movq	%rbp, %rdi
	movq	%r13, %rsi
	movq	%r12, %rdx
	callq	memmove@PLT
.LBB3_5:                                # %_ZNSt6vectorIPvSaIS0_EE11_S_relocateEPS0_S3_S3_RS1_.exit
	addq	%rbp, %r12
	addq	$8, %r12
	movq	8(%rsp), %rsi                   # 8-byte Reload
	subq	%rsi, %r15
	testq	%r15, %r15
	jle	.LBB3_7
# %bb.6:
	movq	%r12, %rdi
	movq	%r15, %rdx
	callq	memmove@PLT
.LBB3_7:                                # %_ZNSt6vectorIPvSaIS0_EE11_S_relocateEPS0_S3_S3_RS1_.exit16
	testq	%r13, %r13
	je	.LBB3_9
# %bb.8:
	movq	%r13, %rdi
	callq	_ZdlPv
.LBB3_9:                                # %_ZNSt12_Vector_baseIPvSaIS0_EE13_M_deallocateEPS0_m.exit
	addq	%r15, %r12
	movq	%rbp, (%rbx)
	movq	%r12, 8(%rbx)
	movq	16(%rsp), %rax                  # 8-byte Reload
	leaq	(,%rax,8), %rax
	addq	%rbp, %rax
	movq	%rax, 16(%rbx)
	addq	$24, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	_ZNSt6vectorIPvSaIS0_EE17_M_realloc_insertIJRKS0_EEEvN9__gnu_cxx17__normal_iteratorIPS0_S2_EEDpOT_, .Lfunc_end3-_ZNSt6vectorIPvSaIS0_EE17_M_realloc_insertIJRKS0_EEEvN9__gnu_cxx17__normal_iteratorIPS0_S2_EEDpOT_
	.cfi_endproc
                                        # -- End function
	.section	.text._ZNKSt6vectorIPvSaIS0_EE12_M_check_lenEmPKc,"axG",@progbits,_ZNKSt6vectorIPvSaIS0_EE12_M_check_lenEmPKc,comdat
	.weak	_ZNKSt6vectorIPvSaIS0_EE12_M_check_lenEmPKc # -- Begin function _ZNKSt6vectorIPvSaIS0_EE12_M_check_lenEmPKc
	.p2align	1, 0x90
	.type	_ZNKSt6vectorIPvSaIS0_EE12_M_check_lenEmPKc,@function
_ZNKSt6vectorIPvSaIS0_EE12_M_check_lenEmPKc: # @_ZNKSt6vectorIPvSaIS0_EE12_M_check_lenEmPKc
	.cfi_startproc
# %bb.0:
	movabsq	$1152921504606846975, %rcx      # imm = 0xFFFFFFFFFFFFFFF
	movq	8(%rdi), %r8
	subq	(%rdi), %r8
	sarq	$3, %r8
	movq	%rcx, %rax
	subq	%r8, %rax
	cmpq	%rsi, %rax
	jb	.LBB4_2
# %bb.1:
	cmpq	%rsi, %r8
	cmovaq	%r8, %rsi
	leaq	(%rsi,%r8), %rax
	cmpq	%rcx, %rax
	cmovaeq	%rcx, %rax
	addq	%r8, %rsi
	cmovbq	%rcx, %rax
	retq
.LBB4_2:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	%rdx, %rdi
	callq	_ZSt20__throw_length_errorPKc
.Lfunc_end4:
	.size	_ZNKSt6vectorIPvSaIS0_EE12_M_check_lenEmPKc, .Lfunc_end4-_ZNKSt6vectorIPvSaIS0_EE12_M_check_lenEmPKc
	.cfi_endproc
                                        # -- End function
	.section	.text.__clang_call_terminate,"axG",@progbits,__clang_call_terminate,comdat
	.hidden	__clang_call_terminate          # -- Begin function __clang_call_terminate
	.weak	__clang_call_terminate
	.type	__clang_call_terminate,@function
__clang_call_terminate:                 # @__clang_call_terminate
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__cxa_begin_catch
	callq	_ZSt9terminatev
.Lfunc_end5:
	.size	__clang_call_terminate, .Lfunc_end5-__clang_call_terminate
	.cfi_endproc
                                        # -- End function
	.section	.text._ZNSt15__new_allocatorIPvE8allocateEmPKv,"axG",@progbits,_ZNSt15__new_allocatorIPvE8allocateEmPKv,comdat
	.weak	_ZNSt15__new_allocatorIPvE8allocateEmPKv # -- Begin function _ZNSt15__new_allocatorIPvE8allocateEmPKv
	.p2align	1, 0x90
	.type	_ZNSt15__new_allocatorIPvE8allocateEmPKv,@function
_ZNSt15__new_allocatorIPvE8allocateEmPKv: # @_ZNSt15__new_allocatorIPvE8allocateEmPKv
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	%rsi, %rax
	shrq	$60, %rax
	jne	.LBB6_1
# %bb.3:
	shlq	$3, %rsi
	movq	%rsi, %rdi
	popq	%rax
	.cfi_def_cfa_offset 8
	jmp	_Znwm                           # TAILCALL
.LBB6_1:
	.cfi_def_cfa_offset 16
	shrq	$61, %rsi
	je	.LBB6_2
# %bb.4:
	callq	_ZSt28__throw_bad_array_new_lengthv
.LBB6_2:
	callq	_ZSt17__throw_bad_allocv
.Lfunc_end6:
	.size	_ZNSt15__new_allocatorIPvE8allocateEmPKv, .Lfunc_end6-_ZNSt15__new_allocatorIPvE8allocateEmPKv
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Try to allocate 1G mem: "
	.size	.L.str, 25

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Done, sleep for 5 seconds. "
	.size	.L.str.1, 28

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"Release 512M mem: "
	.size	.L.str.2, 19

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"Try to allocate another 1G mem: "
	.size	.L.str.3, 33

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"Clean them all. "
	.size	.L.str.4, 17

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"vector::_M_realloc_insert"
	.size	.L.str.5, 26

	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __gxx_personality_v0
	.addrsig_sym _Unwind_Resume
	.addrsig_sym _ZSt4cout
	.addrsig_sym __hip_cuid_

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
