Skip to content

Commit

Permalink
masync: add memset operation for synchronous data mover
Browse files Browse the repository at this point in the history
  • Loading branch information
damian committed Apr 6, 2022
1 parent 6c21f8a commit 189177a
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/data_mover_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ sync_operation_delete(void *data, const struct vdm_operation *operation,
output->output.memmove.dest =
operation->data.memcpy.dest;
break;
case VDM_OPERATION_MEMSET:
output->type = VDM_OPERATION_MEMMOVE;
output->output.memset.str =
operation->data.memset.str;
break;
default:
ASSERT(0);
}
Expand Down Expand Up @@ -109,6 +114,11 @@ sync_operation_start(void *data, const struct vdm_operation *operation,
operation->data.memcpy.src,
operation->data.memcpy.n);
break;
case VDM_OPERATION_MEMSET:
memset(operation->data.memset.str,
operation->data.memset.c,
operation->data.memset.n);
break;
default:
ASSERT(0);
}
Expand Down
38 changes: 38 additions & 0 deletions src/include/libminiasync/vdm.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct vdm;
enum vdm_operation_type {
VDM_OPERATION_MEMCPY,
VDM_OPERATION_MEMMOVE,
VDM_OPERATION_MEMSET,
};

enum vdm_operation_result {
Expand All @@ -55,13 +56,21 @@ struct vdm_operation_data_memmove {
uint64_t flags;
};

struct vdm_operation_data_memset {
void *str;
int c;
size_t n;
uint64_t flags;
};

/* sized so that sizeof(vdm_operation_data) is 64 */
#define VDM_OPERATION_DATA_MAX_SIZE (40)

struct vdm_operation {
union {
struct vdm_operation_data_memcpy memcpy;
struct vdm_operation_data_memmove memmove;
struct vdm_operation_data_memset memset;
uint8_t data[VDM_OPERATION_DATA_MAX_SIZE];
} data;
enum vdm_operation_type type;
Expand All @@ -82,12 +91,17 @@ struct vdm_operation_output_memmove {
void *dest;
};

struct vdm_operation_output_memset {
void *str;
};

struct vdm_operation_output {
enum vdm_operation_type type;
enum vdm_operation_result result;
union {
struct vdm_operation_output_memcpy memcpy;
struct vdm_operation_output_memmove memmove;
struct vdm_operation_output_memset memset;
} output;
};

Expand Down Expand Up @@ -207,6 +221,30 @@ vdm_memmove(struct vdm *vdm, void *dest, void *src, size_t n, uint64_t flags)
return future;
}

/*
* vdm_memset -- instantiates a new memset vdm operation and returns a new
* future to represent that operation
*/
static inline struct vdm_operation_future
vdm_memset(struct vdm *vdm, void *str, int c, size_t n, uint64_t flags)
{
struct vdm_operation_future future = {.data.operation = {
.type = VDM_OPERATION_MEMSET,
.data = {
.memset.str = str,
.memset.flags = flags,
.memset.n = n,
.memset.c = c,
}
}};

future.data.data = vdm->op_new(vdm, VDM_OPERATION_MEMSET);
future.data.vdm = vdm;
FUTURE_INIT(&future, vdm_operation_impl);

return future;
}

#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 5 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ set(SOURCES_MEMMOVE_SYNC_TEST

set(SOURCES_VDM_TEST
vdm/test_vdm.c)
set(SOURCES_MEMSET_SYNC_TEST
memset_sync/memset_sync.c)

set(SOURCES_VDM_OPERATION_FUTURE_POLL
vdm_operation_future_poll/vdm_operation_future_poll.c
Expand Down Expand Up @@ -89,6 +91,8 @@ add_link_executable(memmove_sync

add_link_executable(vdm
"${SOURCES_VDM_TEST}"
add_link_executable(memset_sync
"${SOURCES_MEMSET_SYNC_TEST}"
"${LIBS_BASIC}")

# add test using test function defined in the ctest_helpers.cmake file
Expand All @@ -103,6 +107,7 @@ test("memcpy_threads" "memcpy_threads" test_memcpy_threads none)
test("membuf" "membuf" test_membuf none)
test("memmove_sync" "memmove_sync" test_memmove_sync none)
test("vdm" "vdm" test_vdm none)
test("memset_sync" "memset_sync" test_memset_sync none)

# add tests running examples only if they are built
if(BUILD_EXAMPLES)
Expand Down
63 changes: 63 additions & 0 deletions tests/memset_sync/memset_sync.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2022, Intel Corporation */

#include <stdlib.h>
#include <string.h>
#include "libminiasync.h"
#include "test_helpers.h"

int
main(void)
{
int ret = 0;
char c = '!';

size_t buffer_size = strlen("teststring");

char *buffer = malloc(buffer_size + 1);
if (buffer == NULL) {
fprintf(stderr,
"memory for the first buffer could not be allocated");
return 1;
}

memcpy(buffer, "teststring", buffer_size + 1);

struct data_mover_sync *dms = data_mover_sync_new();
if (dms == NULL) {
ret = 1;
fprintf(stderr,
"error while creating synchronous data mover");
goto cleanup_1;
}

struct vdm *sync_mover = data_mover_sync_get_vdm(dms);
if (sync_mover == NULL) {
ret = 1;
fprintf(stderr,
"error while extracting synchronous vdm");
goto cleanup_2;
}

struct vdm_operation_future test_memset_fut =
vdm_memset(sync_mover, buffer, c, buffer_size / 2, 0);

FUTURE_BUSY_POLL(&test_memset_fut);

for (size_t i = 0; i < buffer_size / 2; i++) {
if (buffer[i] != '!')
ret = 1;
}

if (ret)
fprintf(stderr,
"incorrect value in buffer after memset");

cleanup_2:
data_mover_sync_delete(dms);

cleanup_1:
free(buffer);

return ret;
}
11 changes: 11 additions & 0 deletions tests/memset_sync/test_memset_sync.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2022, Intel Corporation

include(${SRC_DIR}/cmake/test_helpers.cmake)

setup()

execute(0 ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${BUILD}/memset_sync)
execute_assert_pass(${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${BUILD}/memset_sync)

cleanup()

0 comments on commit 189177a

Please sign in to comment.