summaryrefslogtreecommitdiffhomepage
path: root/circbuffer.c
diff options
context:
space:
mode:
authorMatt Johnston <matt@ucc.asn.au>2015-11-19 23:52:11 +0800
committerMatt Johnston <matt@ucc.asn.au>2015-11-19 23:52:11 +0800
commit87373be960025580a5443daf00875984c52c278f (patch)
tree81d0cfdaf26cbcc3bcb4cf22c340abdc1ba38460 /circbuffer.c
parent85d9672e476d805821a0391ce5130a3d2f6275fc (diff)
lazy allocation of circbuffer
Diffstat (limited to 'circbuffer.c')
-rw-r--r--circbuffer.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/circbuffer.c b/circbuffer.c
index c0c8641..1b8304c 100644
--- a/circbuffer.c
+++ b/circbuffer.c
@@ -37,9 +37,8 @@ circbuffer * cbuf_new(unsigned int size) {
}
cbuf = (circbuffer*)m_malloc(sizeof(circbuffer));
- if (size > 0) {
- cbuf->data = (unsigned char*)m_malloc(size);
- }
+ /* data is malloced on first write */
+ cbuf->data = NULL;
cbuf->used = 0;
cbuf->readpos = 0;
cbuf->writepos = 0;
@@ -50,8 +49,10 @@ circbuffer * cbuf_new(unsigned int size) {
void cbuf_free(circbuffer * cbuf) {
- m_burn(cbuf->data, cbuf->size);
- m_free(cbuf->data);
+ if (cbuf->data) {
+ m_burn(cbuf->data, cbuf->size);
+ m_free(cbuf->data);
+ }
m_free(cbuf);
}
@@ -106,6 +107,11 @@ unsigned char* cbuf_writeptr(circbuffer *cbuf, unsigned int len) {
dropbear_exit("Bad cbuf write");
}
+ if (!cbuf->data) {
+ /* lazy allocation */
+ cbuf->data = (unsigned char*)m_malloc(cbuf->size);
+ }
+
return &cbuf->data[cbuf->writepos];
}