Skip to content

Commit 1f8d218

Browse files
committed
fix vtable misuse
1 parent fd2121e commit 1f8d218

1 file changed

Lines changed: 21 additions & 22 deletions

File tree

runners/s3-benchrunner-c/CRunner.cpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ void addHeader(aws_http_message *request, string_view name, string_view value)
291291
// equal to the full upload size, we reuse a small cache-friendly buffer repeatedly.
292292
struct LoopingUploadStream
293293
{
294-
aws_input_stream base; // must be first member
295294
const uint8_t *data;
296295
size_t dataLen;
297296
uint64_t totalSize;
@@ -300,7 +299,7 @@ struct LoopingUploadStream
300299

301300
static int s_looping_stream_seek(aws_input_stream *stream, int64_t offset, enum aws_stream_seek_basis basis)
302301
{
303-
auto *s = reinterpret_cast<LoopingUploadStream *>(stream);
302+
auto *s = reinterpret_cast<LoopingUploadStream *>(stream->impl);
304303
if (basis == AWS_SSB_BEGIN)
305304
s->bytesWritten = (uint64_t)offset;
306305
else if (basis == AWS_SSB_END)
@@ -310,7 +309,7 @@ static int s_looping_stream_seek(aws_input_stream *stream, int64_t offset, enum
310309

311310
static int s_looping_stream_read(aws_input_stream *stream, aws_byte_buf *dest)
312311
{
313-
auto *s = reinterpret_cast<LoopingUploadStream *>(stream);
312+
auto *s = reinterpret_cast<LoopingUploadStream *>(stream->impl);
314313
while (s->bytesWritten < s->totalSize && dest->len < dest->capacity)
315314
{
316315
uint64_t remaining = s->totalSize - s->bytesWritten;
@@ -326,31 +325,24 @@ static int s_looping_stream_read(aws_input_stream *stream, aws_byte_buf *dest)
326325

327326
static int s_looping_stream_get_status(aws_input_stream *stream, aws_stream_status *status)
328327
{
329-
auto *s = reinterpret_cast<LoopingUploadStream *>(stream);
328+
auto *s = reinterpret_cast<LoopingUploadStream *>(stream->impl);
330329
status->is_end_of_stream = (s->bytesWritten >= s->totalSize);
331330
status->is_valid = true;
332331
return AWS_OP_SUCCESS;
333332
}
334333

335334
static int s_looping_stream_get_length(aws_input_stream *stream, int64_t *out_length)
336335
{
337-
auto *s = reinterpret_cast<LoopingUploadStream *>(stream);
336+
auto *s = reinterpret_cast<LoopingUploadStream *>(stream->impl);
338337
*out_length = (int64_t)s->totalSize;
339338
return AWS_OP_SUCCESS;
340339
}
341340

342-
static void s_looping_stream_destroy(aws_input_stream *stream)
343-
{
344-
auto *s = reinterpret_cast<LoopingUploadStream *>(stream);
345-
aws_mem_release(aws_default_allocator(), s);
346-
}
347-
348341
static aws_input_stream_vtable s_looping_stream_vtable = {
349342
.seek = s_looping_stream_seek,
350343
.read = s_looping_stream_read,
351344
.get_status = s_looping_stream_get_status,
352345
.get_length = s_looping_stream_get_length,
353-
.destroy = s_looping_stream_destroy,
354346
};
355347

356348
static aws_input_stream *aws_input_stream_new_looping(
@@ -359,17 +351,24 @@ static aws_input_stream *aws_input_stream_new_looping(
359351
size_t dataLen,
360352
uint64_t totalSize)
361353
{
362-
auto *s = reinterpret_cast<LoopingUploadStream *>(aws_mem_calloc(alloc, 1, sizeof(LoopingUploadStream)));
363-
s->base.vtable = &s_looping_stream_vtable;
354+
auto *stream = reinterpret_cast<aws_input_stream *>(aws_mem_calloc(alloc, 1, sizeof(aws_input_stream)));
355+
auto *impl = reinterpret_cast<LoopingUploadStream *>(aws_mem_calloc(alloc, 1, sizeof(LoopingUploadStream)));
356+
impl->data = data;
357+
impl->dataLen = dataLen;
358+
impl->totalSize = totalSize;
359+
impl->bytesWritten = 0;
360+
stream->impl = impl;
361+
stream->vtable = &s_looping_stream_vtable;
364362
aws_ref_count_init(
365-
&s->base.ref_count,
366-
s,
367-
[](void *user_data) { s_looping_stream_destroy(reinterpret_cast<aws_input_stream *>(user_data)); });
368-
s->data = data;
369-
s->dataLen = dataLen;
370-
s->totalSize = totalSize;
371-
s->bytesWritten = 0;
372-
return &s->base;
363+
&stream->ref_count,
364+
stream,
365+
[](void *user_data)
366+
{
367+
auto *st = reinterpret_cast<aws_input_stream *>(user_data);
368+
aws_mem_release(aws_default_allocator(), st->impl);
369+
aws_mem_release(aws_default_allocator(), st);
370+
});
371+
return stream;
373372
}
374373

375374
Task::Task(CRunner &runner, size_t taskI, FILE *telemetryFile)

0 commit comments

Comments
 (0)