load("@fbcode_macros//build_defs:build_file_migration.bzl", "fbcode_target", "non_fbcode_target")
load("@fbcode_macros//build_defs:cpp_library.bzl", "cpp_library")
load("@fbcode_macros//build_defs:cpp_unittest.bzl", "cpp_unittest")
load(
    "@fbsource//xplat/folly:defs.bzl",
    "folly_xplat_library",
)
load("@fbsource//xplat/pfh/triage_InfrastructureSupermoduleOptou:DEFS.bzl", "triage_InfrastructureSupermoduleOptou")

oncall("fbcode_entropy_wardens_folly")

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "sanitize_leak",
    srcs = [
        "SanitizeLeak.cpp",
    ],
    feature = triage_InfrastructureSupermoduleOptou,
    raw_headers = [
        "SanitizeLeak.h",
    ],
    deps = [
        "//xplat/folly/lang:extern",
    ],
    exported_deps = [
        "//xplat/folly:portability",
    ],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "arena",
    feature = triage_InfrastructureSupermoduleOptou,
    raw_headers = [
        "Arena.h",
        "Arena-inl.h",
    ],
    deps = ["//xplat/folly/lang:safe_assert"],
    exported_deps = [
        "//third-party/boost:boost",
        "//xplat/folly:conv",
        "//xplat/folly:likely",
        "//xplat/folly:memory",
        "//xplat/folly/lang:align",
        "//xplat/folly/lang:checked_math",
        "//xplat/folly/lang:exception",
        "//xplat/folly/memory:malloc",
    ],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "not_null",
    feature = triage_InfrastructureSupermoduleOptou,
    raw_headers = [
        "not_null.h",
        "not_null-inl.h",
    ],
    deps = [
        "//xplat/folly:memory",
        "//xplat/folly:portability",
        "//xplat/folly:traits",
        "//xplat/folly/lang:exception",
    ],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "reentrant_allocator",
    srcs = [
        "ReentrantAllocator.cpp",
    ],
    feature = triage_InfrastructureSupermoduleOptou,
    raw_headers = [
        "ReentrantAllocator.h",
    ],
    deps = [
        "//xplat/folly:portability",
        "//xplat/folly:portability_sys_mman",
        "//xplat/folly/lang:bits",
        "//xplat/folly/lang:safe_assert",
    ],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "thread_cached_arena",
    srcs = [
        "ThreadCachedArena.cpp",
    ],
    feature = triage_InfrastructureSupermoduleOptou,
    raw_headers = [
        "ThreadCachedArena.h",
    ],
    deps = [
        "//xplat/folly:likely",
        "//xplat/folly:synchronized",
        "//xplat/folly:thread_local",
        "//xplat/folly/memory:arena",
    ],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "memory_resource",
    feature = triage_InfrastructureSupermoduleOptou,
    raw_headers = [
        "MemoryResource.h",
    ],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "uninitialized_memory_hacks",
    feature = triage_InfrastructureSupermoduleOptou,
    raw_headers = [
        "UninitializedMemoryHacks.h",
    ],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "mallctl_helper",
    srcs = [
        "MallctlHelper.cpp",
    ],
    feature = triage_InfrastructureSupermoduleOptou,
    raw_headers = [
        "MallctlHelper.h",
    ],
    deps = [
        "//xplat/folly:format",
        "//xplat/folly:string",
    ],
    exported_deps = [
        "//xplat/folly/memory:malloc",
    ],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "sanitize_address",
    srcs = [
        "SanitizeAddress.cpp",
    ],
    feature = triage_InfrastructureSupermoduleOptou,
    raw_headers = [
        "SanitizeAddress.h",
    ],
    deps = [
        "//xplat/folly/lang:extern",
    ],
    exported_deps = [
        "//xplat/folly:portability",
    ],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "malloc",
    compiler_flags = [
        "-fno-omit-frame-pointer",
    ],
    feature = triage_InfrastructureSupermoduleOptou,
    raw_headers = [
        "Malloc.h",
    ],
    exported_deps = [
        "//xplat/folly:c_portability",
        "//xplat/folly:portability_config",
        "//xplat/folly:portability_malloc",
        "//xplat/folly/lang:bits",
        "//xplat/folly/lang:exception",
        "//xplat/folly/memory/detail:malloc_impl",
    ],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "jemalloc_huge_page_allocator",
    srcs = [
        "JemallocHugePageAllocator.cpp",
    ],
    cxx_exported_deps = [
        "//xplat/third-party/linker_lib:pthread",
    ],
    fbandroid_exported_deps = [
        "//xplat/third-party/linker_lib:pthread",
    ],
    feature = triage_InfrastructureSupermoduleOptou,
    raw_headers = [
        "JemallocHugePageAllocator.h",
    ],
    deps = [
        "//third-party/glog:glog",
        "//xplat/folly:c_portability",
        "//xplat/folly:portability_config",
        "//xplat/folly:portability_malloc",
        "//xplat/folly:portability_memory",
        "//xplat/folly:portability_string",
        "//xplat/folly:portability_sys_mman",
        "//xplat/folly:portability_sys_types",
    ],
    exported_deps = [
        "//xplat/folly/memory:malloc",
    ],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "jemalloc_nodump_allocator",
    srcs = [
        "JemallocNodumpAllocator.cpp",
    ],
    cxx_exported_deps = [
        "//xplat/third-party/linker_lib:pthread",
    ],
    fbandroid_exported_deps = [
        "//xplat/third-party/linker_lib:pthread",
    ],
    feature = triage_InfrastructureSupermoduleOptou,
    raw_headers = [
        "JemallocNodumpAllocator.h",
    ],
    deps = [
        "//third-party/glog:glog",
        "//xplat/folly:c_portability",
        "//xplat/folly:conv",
        "//xplat/folly:portability_config",
        "//xplat/folly:portability_malloc",
        "//xplat/folly:portability_sys_mman",
        "//xplat/folly:string",
    ],
    exported_deps = [
        "//xplat/folly/memory:malloc",
    ],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "shared_from_this_ptr",
    feature = triage_InfrastructureSupermoduleOptou,
    raw_headers = ["shared_from_this_ptr.h"],
    exported_deps = [
        "//xplat/folly:traits",
    ],
)

# !!!! fbcode/folly/memory/TARGETS was merged into this file, see https://fburl.com/workplace/xl8l9yuo for more info !!!!

fbcode_target(
    _kind = cpp_library,
    name = "arena",
    headers = [
        "Arena.h",
        "Arena-inl.h",
    ],
    exported_deps = [
        ":malloc",
        "//folly:conv",
        "//folly:likely",
        "//folly:memory",
        "//folly/lang:align",
        "//folly/lang:checked_math",
        "//folly/lang:exception",
        "//folly/lang:safe_assert",
    ],
    exported_external_deps = [
        "boost",
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "mallctl_helper",
    srcs = ["MallctlHelper.cpp"],
    headers = ["MallctlHelper.h"],
    deps = [
        "//folly:format",
        "//folly:string",
        "//folly/lang:exception",
    ],
    exported_deps = [
        ":malloc",
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "malloc",
    headers = ["Malloc.h"],
    os_deps = [(
        "linux",
        ["third-party//jemalloc:headers"],
    )],
    exported_deps = [
        "//folly:portability",
        "//folly/lang:bits",
        "//folly/lang:exception",
        "//folly/memory/detail:malloc_impl",
        "//folly/portability:malloc",
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "memory_resource",
    headers = ["MemoryResource.h"],
)

fbcode_target(
    _kind = cpp_library,
    name = "not_null",
    srcs = [],
    headers = [
        "not_null.h",
        "not_null-inl.h",
    ],
    exported_deps = [
        "//folly:memory",
        "//folly:portability",
        "//folly:traits",
        "//folly/lang:exception",
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "reentrant_allocator",
    srcs = ["ReentrantAllocator.cpp"],
    headers = ["ReentrantAllocator.h"],
    deps = [
        "//folly/lang:bits",
        "//folly/lang:safe_assert",
        "//folly/portability:sys_mman",
    ],
    exported_deps = [
        "//folly:portability",
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "sanitize_address",
    srcs = ["SanitizeAddress.cpp"],
    headers = ["SanitizeAddress.h"],
    deps = [
        "//folly/lang:extern",
    ],
    exported_deps = [
        "//folly:portability",
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "sanitize_leak",
    srcs = ["SanitizeLeak.cpp"],
    headers = ["SanitizeLeak.h"],
    deps = [
        "//folly/lang:extern",
    ],
    exported_deps = [
        "//folly:portability",
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "shared_from_this_ptr",
    headers = ["shared_from_this_ptr.h"],
    exported_deps = [
        "//folly:traits",
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "thread_cached_arena",
    srcs = ["ThreadCachedArena.cpp"],
    headers = ["ThreadCachedArena.h"],
    exported_deps = [
        "//folly:likely",
        "//folly:synchronized",
        "//folly:thread_local",
        "//folly/memory:arena",
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "uninitialized_memory_hacks",
    headers = ["UninitializedMemoryHacks.h"],
)

fbcode_target(
    _kind = cpp_library,
    name = "jemalloc_nodump_allocator",
    srcs = ["JemallocNodumpAllocator.cpp"],
    headers = ["JemallocNodumpAllocator.h"],
    deps = [
        "//folly:conv",
        "//folly:string",
        "//folly/memory:malloc",
    ],
    exported_deps = [
        "//folly:c_portability",
        "//folly/portability:config",
        "//folly/portability:malloc",
        "//folly/portability:sys_mman",
    ],
    external_deps = [
        "glog",
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "jemalloc_huge_page_allocator",
    srcs = ["JemallocHugePageAllocator.cpp"],
    headers = ["JemallocHugePageAllocator.h"],
    deps = [
        "//folly:c_portability",
        "//folly/memory:malloc",
        "//folly/portability:malloc",
        "//folly/portability:string",
        "//folly/portability:sys_mman",
        "//folly/portability:sys_types",
    ],
    external_deps = [
        "glog",
    ],
)

fbcode_target(
    _kind = cpp_unittest,
    name = "jemalloc_huge_page_allocator_test",
    srcs = ["JemallocHugePageAllocatorTest.cpp"],
    headers = [],
    allocator = "jemalloc",
    labels = ["oss-broken"],
    deps = [
        "//folly/container:f14_hash",
        "//folly/memory:jemalloc_huge_page_allocator",
        "//folly/memory:malloc",
        "//folly/portability:gtest",
    ],
)

fbcode_target(
    _kind = cpp_unittest,
    name = "jemalloc_nodump_allocator_test",
    srcs = ["JemallocNodumpAllocatorTest.cpp"],
    headers = [],
    allocator = "jemalloc",
    deps = [
        "//folly/io:iobuf",
        "//folly/memory:jemalloc_nodump_allocator",
        "//folly/memory:malloc",
        "//folly/portability:gtest",
    ],
)
