-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_platform.c
235 lines (192 loc) · 4.55 KB
/
test_platform.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* This file is part of tframetest.
*
* Copyright (c) 2023 Tuxera Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#if __STDC_VERSION__ >= 199901L
#define _XOPEN_SOURCE 600
#else
#define _XOPEN_SOURCE 500
#endif
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "platform.h"
#define NAME_MAX 512
typedef struct test_platform_file_t
{
int fd;
int open;
char name[NAME_MAX];
size_t pos;
size_t size;
char *data;
} test_platform_file_t;
static test_platform_file_t *files = NULL;
static size_t file_cnt = 0;
static inline size_t test_platform_find_file(const char *fname,
test_platform_file_t **file)
{
size_t i;
for (i = 0; i < file_cnt; i++) {
if (!strcmp(files[i].name, fname)) {
if (file)
*file = &files[i];
return files[i].fd;
}
}
return 0;
}
static inline platform_handle_t test_platform_open(const char *fname,
platform_open_flags_t flags, int mode)
{
size_t idx = 0;
idx = test_platform_find_file(fname, NULL);
/* In case it's not CREATE and we didn't find file, return err */
if (!idx && !(mode & (PLATFORM_OPEN_CREATE)))
return -1;
if (!idx) {
test_platform_file_t *tmp = NULL;
++file_cnt;
tmp = realloc(files, sizeof(*tmp) * file_cnt);
if (!tmp) {
--file_cnt;
return -1;
}
idx = file_cnt;
files = tmp;
files[idx - 1].size = 0;
files[idx - 1].data = NULL;
}
files[idx - 1].fd = idx;
memcpy(files[idx - 1].name, fname, NAME_MAX);
files[idx - 1].name[NAME_MAX - 1] = 0;
files[idx - 1].pos = 0;
files[idx - 1].open = 1;
return idx;
}
static inline int test_platform_close(platform_handle_t f)
{
if (!f || f > file_cnt)
return 1;
files[f - 1].open = 0;
return 0;
}
static inline size_t test_platform_write(platform_handle_t handle,
const char *buf, size_t size)
{
test_platform_file_t *f;
size_t new_size;
char *tmp;
if (!handle || handle > file_cnt)
return 0;
f = &files[handle - 1];
new_size = f->size + size;
tmp = realloc(f->data, new_size);
if (!tmp)
return 0;
memmove(tmp + f->size, buf, size);
f->data = tmp;
f->size = new_size;
f->pos = new_size;
return size;
}
static inline size_t test_platform_read(platform_handle_t handle, char *buf,
size_t size)
{
test_platform_file_t *f;
size_t cnt;
if (!handle || handle > file_cnt)
return 0;
f = &files[handle - 1];
if (!f->size || !f->data)
return 0;
cnt = size;
if (f->pos + cnt >= f->size)
cnt = f->size - f->pos;
if (!cnt)
return 0;
memmove(buf, f->data + f->pos, cnt);
f->pos += cnt;
return cnt;
}
static inline int test_platform_usleep(uint64_t us)
{
return usleep((useconds_t)us);
}
static inline int test_platform_stat(const char *fname, platform_stat_t *st)
{
test_platform_file_t *f = NULL;
size_t idx;
idx = test_platform_find_file(fname, &f);
if (!idx)
return -1;
memset(st, 0, sizeof(*st));
st->size = f->size;
st->ino = f->fd;
st->nlink = 1;
st->blksize = 512;
st->blksize = (f->size + 511) & ~511;
return 0;
}
int test_platform_thread_create(uint64_t *thread_id, void*(*start)(void*),
void *arg)
{
return -1;
}
int test_platform_thread_cancel(uint64_t thread_id)
{
return -1;
}
int test_platform_thread_join(uint64_t thread_id, void **retval)
{
return -1;
}
static platform_t test_platform = {
.open = test_platform_open,
.close = test_platform_close,
.write = test_platform_write,
.read = test_platform_read,
.usleep = test_platform_usleep,
.stat = test_platform_stat,
.calloc = calloc,
.malloc = malloc,
.aligned_alloc = posix_memalign,
.free = free,
.thread_create = test_platform_thread_create,
.thread_cancel = test_platform_thread_cancel,
.thread_join = test_platform_thread_join,
};
const platform_t * test_platform_get(void)
{
return &test_platform;
}
void test_platform_finalize(void)
{
size_t i;
if (files) {
for (i = 0; i < file_cnt; i++) {
if (files[i].data)
free(files[i].data);
}
free(files);
files = NULL;
file_cnt = 0;
}
}