-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include "bytebuffer.h" | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
|
||
static void bytebuffer_reserve(struct bytebuffer *b, int cap) { | ||
if (b->cap >= cap) { | ||
return; | ||
} | ||
|
||
// prefer doubling capacity | ||
if (b->cap * 2 >= cap) { | ||
cap = b->cap * 2; | ||
} | ||
|
||
char *newbuf = malloc(cap); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
nsf
Author
Owner
|
||
if (b->len > 0) { | ||
// copy what was there, b->len > 0 assumes b->buf != null | ||
memcpy(newbuf, b->buf, b->len); | ||
} | ||
if (b->buf) { | ||
// in case there was an allocated buffer, free it | ||
free(b->buf); | ||
} | ||
b->buf = newbuf; | ||
b->cap = cap; | ||
} | ||
|
||
void init_bytebuffer(struct bytebuffer *b, int cap) { | ||
b->cap = 0; | ||
b->len = 0; | ||
b->buf = 0; | ||
|
||
if (cap > 0) { | ||
b->cap = cap; | ||
b->buf = malloc(cap); // just assume malloc works always | ||
} | ||
} | ||
|
||
void free_bytebuffer(struct bytebuffer *b) { | ||
if (b->buf) | ||
free(b->buf); | ||
} | ||
|
||
void clear_bytebuffer(struct bytebuffer *b) { | ||
b->len = 0; | ||
} | ||
|
||
void bytebuffer_puts(struct bytebuffer *b, const char *str) { | ||
bytebuffer_append(b, str, strlen(str)); | ||
} | ||
|
||
void bytebuffer_append(struct bytebuffer *b, const char *data, int len) { | ||
bytebuffer_reserve(b, b->len + len); | ||
memcpy(b->buf + b->len, data, len); | ||
b->len += len; | ||
} | ||
|
||
void resize_bytebuffer(struct bytebuffer *b, int len) { | ||
bytebuffer_reserve(b, len); | ||
b->len = len; | ||
} | ||
|
||
void flush_bytebuffer(struct bytebuffer *b, int fd) { | ||
write(fd, b->buf, b->len); | ||
clear_bytebuffer(b); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
|
||
struct bytebuffer { | ||
char *buf; | ||
int len; | ||
int cap; | ||
}; | ||
|
||
void init_bytebuffer(struct bytebuffer *b, int cap); | ||
void free_bytebuffer(struct bytebuffer *b); | ||
void clear_bytebuffer(struct bytebuffer *b); | ||
void resize_bytebuffer(struct bytebuffer *b, int len); | ||
void bytebuffer_puts(struct bytebuffer *b, const char *str); | ||
void bytebuffer_append(struct bytebuffer *b, const char *data, int len); | ||
|
||
// writes the contents of the buffer to the given fd and then clears the buffer | ||
void flush_bytebuffer(struct bytebuffer *b, int fd); |
This file was deleted.
This file was deleted.
5 comments
on commit 66c3f91
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this change doesnt really seem to make sense, you're swapping a robust 30 line implementation (the passed buffer is static) out for a non-robust permanently malloc'ing and free'ing 70 line implementation of which the header even relies on unportable compiler specific pragmas.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this change doesnt really seem to make sense, you're swapping a robust 30 line implementation (the passed buffer is static) out for a non-robust permanently malloc'ing and free'ing 70 line implementation of which the header even relies on unportable compiler specific pragmas.
See later commits, I replace both memstream and ringbuffer with bytebuffer. That was the reason for rewriting memstream in the first place. If I can use a simple universal concept for both of the needs, why not. You're free to disagree, but it's a simplification of the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
7 changed files with 66 additions and 213 deletions. See?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rip musl support
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
termbox 1 C impl is dead anyway, use termbox2 instead.
you're aware that malloc can fail ?