forked from pmem/miniasync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_mover_sync.c
181 lines (152 loc) · 3.88 KB
/
data_mover_sync.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2022, Intel Corporation */
/* disable conditional expression is const warning */
#include "core/util.h"
#ifdef _WIN32
#pragma warning(disable : 4127)
#endif
#include "libminiasync/vdm.h"
#include "core/membuf.h"
#include "core/out.h"
#define SUPPORTED_FLAGS 0
struct data_mover_sync {
struct vdm base; /* must be first */
struct membuf *membuf;
};
struct data_mover_sync_data {
int complete;
};
/*
* sync_operation_check -- always returns COMPLETE because sync mover operations
* are complete immediately after starting.
*/
static enum future_state
sync_operation_check(void *data, const struct vdm_operation *operation)
{
SUPPRESS_UNUSED(operation);
struct data_mover_sync_data *sync_data = data;
int complete;
util_atomic_load_explicit32(&sync_data->complete, &complete,
memory_order_acquire);
return complete ? FUTURE_STATE_COMPLETE : FUTURE_STATE_IDLE;
}
/*
* sync_operation_new -- creates a new sync operation
*/
static void *
sync_operation_new(struct vdm *vdm, const enum vdm_operation_type type)
{
SUPPRESS_UNUSED(type);
struct data_mover_sync *vdm_sync = (struct data_mover_sync *)vdm;
struct data_mover_sync_data *sync_data = membuf_alloc(vdm_sync->membuf,
sizeof(struct data_mover_sync_data));
if (sync_data == NULL)
return NULL;
sync_data->complete = 0;
return sync_data;
}
/*
* sync_operation_delete -- deletes sync operation
*/
static void
sync_operation_delete(void *data, const struct vdm_operation *operation,
struct vdm_operation_output *output)
{
output->result = VDM_SUCCESS;
switch (operation->type) {
case VDM_OPERATION_MEMCPY:
output->type = VDM_OPERATION_MEMCPY;
output->output.memcpy.dest =
operation->data.memcpy.dest;
break;
case VDM_OPERATION_MEMMOVE:
output->type = VDM_OPERATION_MEMMOVE;
output->output.memmove.dest =
operation->data.memcpy.dest;
break;
case VDM_OPERATION_MEMSET:
output->type = VDM_OPERATION_MEMSET;
output->output.memset.str =
operation->data.memset.str;
break;
default:
ASSERT(0);
}
membuf_free(data);
}
/*
* sync_operation_start -- start (and perform) a synchronous memory operation
*/
static int
sync_operation_start(void *data, const struct vdm_operation *operation,
struct future_notifier *n)
{
struct data_mover_sync_data *sync_data =
(struct data_mover_sync_data *)data;
if (n)
n->notifier_used = FUTURE_NOTIFIER_NONE;
switch (operation->type) {
case VDM_OPERATION_MEMCPY:
memcpy(operation->data.memcpy.dest,
operation->data.memcpy.src,
operation->data.memcpy.n);
break;
case VDM_OPERATION_MEMMOVE:
memmove(operation->data.memcpy.dest,
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);
}
util_atomic_store_explicit32(&sync_data->complete,
1, memory_order_release);
return 0;
}
static struct vdm data_mover_sync_vdm = {
.op_new = sync_operation_new,
.op_delete = sync_operation_delete,
.op_check = sync_operation_check,
.op_start = sync_operation_start,
.capabilities = SUPPORTED_FLAGS,
};
/*
* data_mover_sync_new -- creates a new synchronous data mover
*/
struct data_mover_sync *
data_mover_sync_new(void)
{
struct data_mover_sync *dms = malloc(sizeof(struct data_mover_sync));
if (dms == NULL)
return NULL;
dms->base = data_mover_sync_vdm;
dms->membuf = membuf_new(dms);
if (dms->membuf == NULL)
goto membuf_failed;
return dms;
membuf_failed:
free(dms);
return NULL;
}
/*
* data_mover_sync_get_vdm -- returns the vdm operations for the sync mover
*/
struct vdm *
data_mover_sync_get_vdm(struct data_mover_sync *dms)
{
return &dms->base;
}
/*
* data_mover_sync_delete -- deletes a synchronous data mover
*/
void
data_mover_sync_delete(struct data_mover_sync *dms)
{
membuf_delete(dms->membuf);
free(dms);
}