git ssb

0+

cel / foostudio



Tree: 17960117c91fac592902939ab1835bbd28b27189

Files: 17960117c91fac592902939ab1835bbd28b27189 / foobuf.c

1087 bytesRaw
1#include <malloc.h>
2#include <string.h>
3#include <stdio.h>
4#include "foobuf.h"
5
6int foobuf_init(struct foobuf **bufp, size_t len)
7{
8 struct foobuf *buf = *bufp;
9 if (!buf) {
10 buf = calloc(sizeof *buf, 1);
11 if (!buf) return 1;
12 *bufp = buf;
13 buf->data = calloc(len, sizeof(float));
14 if (!buf->data) return 1;
15 buf->len = len;
16 } else if (buf->len != len) {
17 buf->data = realloc(buf->data, len * sizeof(float));
18 if (!buf->data) return 1;
19 if (len > buf->len) {
20 memset(&buf->data[buf->len], 0, len - buf->len);
21 } else {
22 if (buf->offset >= len) buf->offset = 0;
23 }
24 buf->len = len;
25 }
26 return 0;
27}
28
29float foobuf_read(struct foobuf *buf, size_t offset)
30{
31 size_t len = buf->len;
32 return buf->data[((buf->offset - offset) + len) % len];
33}
34
35void foobuf_write(struct foobuf *buf, float val)
36{
37 if (!buf || !buf->data) return;
38 size_t offset = buf->offset;
39 buf->data[offset] = val;
40 offset++;
41 if (offset == buf->len) offset = 0;
42 buf->offset = offset;
43}
44
45int _struct_init(void **ptr, size_t size)
46{
47 if (*ptr) return 0;
48 if ((*ptr = calloc(1, size))) return 0;
49 return 1;
50}
51

Built with git-ssb-web