load("@fbsource//tools/build_defs/dirsync:fb_dirsync_cpp_library.bzl", "fb_dirsync_cpp_library")
load("@fbsource//tools/build_defs/dirsync:fb_dirsync_cpp_unittest.bzl", "fb_dirsync_cpp_unittest")

oncall("fbcode_entropy_wardens_folly")

fb_dirsync_cpp_library(
    name = "sanitize_leak",
    srcs = ["SanitizeLeak.cpp"],
    headers = ["SanitizeLeak.h"],
    use_raw_headers = True,
    deps = [
        "//folly/lang:extern",
    ],
    exported_deps = [
        "//folly:portability",
    ],
)

fb_dirsync_cpp_library(
    name = "arena",
    headers = [
        "Arena.h",
        "Arena-inl.h",
    ],
    use_raw_headers = True,
    exported_deps = [
        ":allocator",
        ":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",
    ],
)

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

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

fb_dirsync_cpp_library(
    name = "thread_cached_arena",
    srcs = ["ThreadCachedArena.cpp"],
    headers = ["ThreadCachedArena.h"],
    use_raw_headers = True,
    exported_deps = [
        ":allocator",
        "//folly:likely",
        "//folly:synchronized",
        "//folly:thread_local",
        "//folly/memory:arena",
    ],
)

fb_dirsync_cpp_library(
    name = "memory_resource",
    headers = ["MemoryResource.h"],
    use_raw_headers = True,
    exported_deps = [
        "//folly:portability",
    ],
)

fb_dirsync_cpp_library(
    name = "uninitialized_memory_hacks",
    headers = ["UninitializedMemoryHacks.h"],
    use_raw_headers = True,
    exported_deps = [
        "//folly:traits",
    ],
)

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

fb_dirsync_cpp_library(
    name = "sanitize_address",
    srcs = ["SanitizeAddress.cpp"],
    headers = ["SanitizeAddress.h"],
    use_raw_headers = True,
    deps = [
        "//folly/lang:extern",
    ],
    exported_deps = [
        "//folly:portability",
    ],
)

fb_dirsync_cpp_library(
    name = "malloc",
    srcs = ["Malloc.cpp"],
    headers = ["Malloc.h"],
    compiler_flags = ["-fno-omit-frame-pointer"],
    use_raw_headers = True,
    deps = select({
        "DEFAULT": [],
        "ovr_config//os:linux": [
            "fbsource//third-party/jemalloc:headers",
        ],
    }),
    exported_deps = [
        "//folly:memory",
        "//folly:portability",
        "//folly/lang:checked_math",
        "//folly/lang:exception",
        "//folly/memory/detail:malloc_impl",
        "//folly/portability:malloc",
    ],
)

fb_dirsync_cpp_library(
    name = "allocator",
    headers = ["Allocator.h"],
    use_raw_headers = True,
    exported_deps = [
        ":malloc",
        "//folly:likely",
        "//folly:memory",
        "//folly:traits",
        "//folly/functional:invoke",
        "//folly/lang:exception",
    ],
)

fb_dirsync_cpp_library(
    name = "jemalloc_huge_page_allocator",
    srcs = ["JemallocHugePageAllocator.cpp"],
    headers = ["JemallocHugePageAllocator.h"],
    cxx_exported_deps = [
        "//xplat/third-party/linker_lib:pthread",
    ],
    fbandroid_exported_deps = [
        "//xplat/third-party/linker_lib:pthread",
    ],
    use_raw_headers = True,
    deps = [
        "fbsource//third-party/glog:glog",
        "//folly:c_portability",
        "//folly/memory:malloc",
        "//folly/portability:malloc",
        "//folly/portability:string",
        "//folly/portability:sys_mman",
        "//folly/portability:sys_types",
    ],
)

fb_dirsync_cpp_library(
    name = "jemalloc_nodump_allocator",
    srcs = ["JemallocNodumpAllocator.cpp"],
    headers = ["JemallocNodumpAllocator.h"],
    cxx_exported_deps = [
        "//xplat/third-party/linker_lib:pthread",
    ],
    fbandroid_exported_deps = [
        "//xplat/third-party/linker_lib:pthread",
    ],
    use_raw_headers = True,
    deps = [
        "fbsource//third-party/glog:glog",
        "//folly:conv",
        "//folly:string",
    ],
    exported_deps = [
        "//folly:c_portability",
        "//folly/portability:config",
        "//folly/portability:malloc",
        "//folly/portability:sys_mman",
    ],
)

fb_dirsync_cpp_library(
    name = "io_uring_arena",
    srcs = ["IoUringArena.cpp"],
    headers = ["IoUringArena.h"],
    use_raw_headers = True,
    deps = [
        "fbsource//third-party/glog:glog",
        ":malloc",
        "//folly:c_portability",
        "//folly:conv",
        "//folly/lang:align",
        "//folly/portability:string",
        "//folly/portability:sys_mman",
        "//folly/portability:unistd",
    ],
)

fb_dirsync_cpp_library(
    name = "iobuf_arena_factory",
    headers = ["IOBufArenaFactory.h"],
    use_raw_headers = True,
    exported_deps = [
        "//folly/io:iobuf",
    ],
)

fb_dirsync_cpp_library(
    name = "shared_from_this_ptr",
    headers = ["shared_from_this_ptr.h"],
    use_raw_headers = True,
    exported_deps = [
        "//folly:traits",
    ],
)

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

fb_dirsync_cpp_unittest(
    name = "jemalloc_nodump_allocator_test",
    srcs = ["JemallocNodumpAllocatorTest.cpp"],
    allocator = "jemalloc",
    deps = [
        ":jemalloc_nodump_allocator",
        "//folly/io:iobuf",
        "//folly/portability:gtest",
    ],
)

fb_dirsync_cpp_unittest(
    name = "io_uring_arena_test",
    srcs = ["IoUringArenaTest.cpp"],
    headers = [],
    allocator = "jemalloc",
    deps = [
        ":io_uring_arena",
        "//folly/container:f14_hash",
        "//folly/portability:gtest",
    ],
)
