Skip to content

Commit

Permalink
alloc sample
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyMusatkin committed Oct 18, 2023
1 parent b61c12e commit 50b49c5
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
30 changes: 30 additions & 0 deletions bin/mem_profile/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)

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)
30 changes: 30 additions & 0 deletions bin/mem_profile/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

#include <aws/common/byte_buf.h>
#include <aws/common/clock.h>
#include <aws/common/device_random.h>

#include <inttypes.h>

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;
}
2 changes: 1 addition & 1 deletion source/allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 50b49c5

Please sign in to comment.