From 50b49c5360d3201a6ca9180e8eac447f3050b6f4 Mon Sep 17 00:00:00 2001 From: Dmitriy Musatkin Date: Wed, 18 Oct 2023 02:26:46 -0700 Subject: [PATCH] alloc sample --- CMakeLists.txt | 1 + bin/mem_profile/CMakeLists.txt | 30 ++++++++++++++++++++++++++++++ bin/mem_profile/main.c | 30 ++++++++++++++++++++++++++++++ source/allocator.c | 2 +- 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 bin/mem_profile/CMakeLists.txt create mode 100644 bin/mem_profile/main.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 0ff1d9e7eb..493b0aaf9f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -305,6 +305,7 @@ configure_file(${CONFIG_HEADER_TEMPLATE} if (ALLOW_CROSS_COMPILED_TESTS OR NOT CMAKE_CROSSCOMPILING) if (BUILD_TESTING) + add_subdirectory(bin/mem_profile) add_subdirectory(tests) add_subdirectory(bin/system_info) endif() diff --git a/bin/mem_profile/CMakeLists.txt b/bin/mem_profile/CMakeLists.txt new file mode 100644 index 0000000000..79e45ee39e --- /dev/null +++ b/bin/mem_profile/CMakeLists.txt @@ -0,0 +1,30 @@ + +project(mem_profile C) + +list(APPEND CMAKE_MODULE_PATH "${CMAKE_INSTALL_PREFIX}/lib/cmake") + +file(GLOB PROFILE_SRC + "*.c" + ) + +set(PROFILE_PROJECT_NAME mem_profile) +add_executable(${PROFILE_PROJECT_NAME} ${PROFILE_SRC}) +aws_set_common_properties(${PROFILE_PROJECT_NAME}) + + +target_include_directories(${PROFILE_PROJECT_NAME} PUBLIC + $ + $) + +target_link_libraries(${PROFILE_PROJECT_NAME} PRIVATE aws-c-common) + +if (BUILD_SHARED_LIBS AND NOT WIN32) + message(INFO " mem_profile will be built with shared libs, but you may need to set LD_LIBRARY_PATH=${CMAKE_INSTALL_PREFIX}/lib to run the application") +endif() + +install(TARGETS ${PROFILE_PROJECT_NAME} + EXPORT ${PROFILE_PROJECT_NAME}-targets + COMPONENT Runtime + RUNTIME + DESTINATION bin + COMPONENT Runtime) \ No newline at end of file diff --git a/bin/mem_profile/main.c b/bin/mem_profile/main.c new file mode 100644 index 0000000000..64e2179cf4 --- /dev/null +++ b/bin/mem_profile/main.c @@ -0,0 +1,30 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +#include +#include +#include + +#include + +static size_t part_size = 8 * 1024 * 1024; + +int main(void) { + struct aws_allocator *allocator = aws_default_allocator(); + aws_common_library_init(allocator); + + struct aws_byte_buf bufs[100]; + for (size_t i = 0; i < 100; ++i) { + aws_byte_buf_init(&bufs[i], allocator, part_size); + aws_device_random_buffer(&bufs[i]); + } + + for (size_t i = 0; i < 100; ++i) { + aws_byte_buf_clean_up(&bufs[i]); + } + + aws_common_library_clean_up(); + return 0; +} diff --git a/source/allocator.c b/source/allocator.c index e444d282aa..bfad6b8375 100644 --- a/source/allocator.c +++ b/source/allocator.c @@ -48,7 +48,7 @@ static void *s_default_malloc(struct aws_allocator *allocator, size_t size) { * We use PAGE_SIZE as the boundary because we are not aware of any allocations of * this size or greater that are not data buffers */ - const size_t alignment = sizeof(void *) * (size > (size_t)PAGE_SIZE ? 8 : 2); + const size_t alignment = sizeof(void *) * (size > (size_t)PAGE_SIZE ? 1 : 2); #if !defined(_WIN32) void *result = NULL; int err = posix_memalign(&result, alignment, size);