feature (utils): implemented streams

This commit is contained in:
antifallobst 2023-04-20 22:10:02 +02:00
parent 2544dc2346
commit eaf189b545
2 changed files with 84 additions and 0 deletions

20
kernel/inc/utils/stream.h Normal file
View File

@ -0,0 +1,20 @@
// This file is part of noxos and licensed under the MIT open source license
#ifndef NOX_STREAM_H
#define NOX_STREAM_H
#include "utils/stdtypes.h"
typedef struct {
uint8_t* buffer;
uint32_t size;
uint32_t pos_sender;
uint32_t pos_receiver;
} stream_T;
stream_T* stream_alloc (uint32_t size);
void stream_destruct (stream_T* stream);
uint32_t stream_write (stream_T* stream, void* buffer_in, uint32_t n);
uint32_t stream_read (stream_T* stream, void* buffer_out, uint32_t n);
#endif //NOX_STREAM_H

64
kernel/src/utils/stream.c Normal file
View File

@ -0,0 +1,64 @@
// This file is part of noxos and licensed under the MIT open source license
#include "utils/stream.h"
#include "utils/memory.h"
#include "utils/math.h"
stream_T* stream_alloc(uint32_t size) {
stream_T* stream = memory_allocate(sizeof(stream_T));
stream->buffer = memory_allocate(size);
stream->size = size;
stream->pos_sender = 0;
stream->pos_receiver = 0;
return stream;
}
void stream_destruct(stream_T* stream) {
memory_free(stream->buffer);
memory_free(stream);
}
#include "utils/logger.h"
uint32_t stream_write(stream_T* stream, void* buffer_in, uint32_t n) {
uint32_t num_top;
uint32_t num_bottom;
if (stream->pos_receiver > stream->pos_sender) {
num_top = MIN(n, stream->pos_receiver - stream->pos_sender - 1);
num_bottom = 0;
} else {
num_top = MIN(n, stream->size - stream->pos_sender);
num_bottom = MIN(n - num_top, stream->pos_receiver - 1);
}
memory_copy(buffer_in, &stream->buffer[stream->pos_sender], num_top);
memory_copy(&buffer_in[num_top], stream->buffer, num_bottom);
stream->pos_sender += num_top + num_bottom;
stream->pos_sender %= stream->size;
return num_top + num_bottom;
}
uint32_t stream_read(stream_T* stream, void* buffer_out, uint32_t n) {
uint32_t num_top;
uint32_t num_bottom;
if (stream->pos_sender > stream->pos_receiver) {
num_top = MIN(n, stream->pos_sender - stream->pos_receiver - 1);
num_bottom = 0;
} else {
num_top = MIN(n, stream->size - stream->pos_receiver);
num_bottom = MIN(n - num_top, stream->pos_sender - 1);
}
memory_copy(&stream->buffer[stream->pos_receiver], buffer_out, num_top);
memory_copy(stream->buffer, &buffer_out[num_top], num_bottom);
stream->pos_receiver += num_top + num_bottom;
stream->pos_receiver %= stream->size;
return num_top + num_bottom;
}