Skip to content

Commit 504b2e5

Browse files
author
di
committed
[FS] added buffer type
1 parent 1f210b5 commit 504b2e5

3 files changed

Lines changed: 49 additions & 1 deletion

File tree

shared/files/buffer.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "buffer.h"
2+
#include "syscalls/syscalls.h"
3+
4+
buffer buffer_create(size_t size, bool can_grow, bool circular){
5+
return (buffer){
6+
.buffer = zalloc(size),
7+
.buffer_size = 0,
8+
.limit = size,
9+
.can_grow = can_grow,
10+
.circular = circular,
11+
.cursor = 0,
12+
};
13+
}
14+
15+
void buffer_write(buffer *buf, char* fmt, ...){
16+
__attribute__((aligned(16))) va_list args;
17+
va_start(args, fmt);
18+
size_t n = string_format_va_buf(fmt, buf->buffer+buf->cursor, buf->limit-buf->cursor, args);
19+
buf->cursor += n;
20+
buf->buffer_size += n;
21+
if (buf->can_grow && buf->buffer_size > buf->limit-256){
22+
size_t new_size = buf->limit;
23+
buf->buffer = realloc_sized(buf->buffer, buf->limit, new_size);
24+
buf->limit = new_size;
25+
}
26+
//TODO: circular
27+
va_end(args);
28+
}
29+
30+
void buffer_write_space(buffer *buf){
31+
buffer_write(buf, " ");
32+
}

shared/files/buffer.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include "types.h"
4+
5+
typedef struct {
6+
char* buffer;
7+
size_t buffer_size;
8+
size_t limit;
9+
bool can_grow;
10+
bool circular;
11+
uintptr_t cursor;
12+
} buffer;
13+
14+
buffer buffer_create(size_t size, bool can_grow, bool circular);
15+
void buffer_write(buffer *buf, char* fmt, ...);
16+
void buffer_write_space(buffer *buf);

user/default_process.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ int audio_example(){
8989
if (wav_load_as_int16("/resources/scale.wav", audio)){
9090
mixin[0] = audio_play_sync(&audio[0], 0, AUDIO_ONESHOT, AUDIO_LEVEL_MAX/4, PAN_CENTRE);
9191
return 0;
92-
}else{
92+
} else {
9393
printf("Could not load wav");
9494
return -1;
9595
}

0 commit comments

Comments
 (0)