1
0
mirror of https://github.com/clearml/dropbear synced 2025-03-16 08:27:18 +00:00

lazy allocation of circbuffer

This commit is contained in:
Matt Johnston 2015-11-19 23:52:11 +08:00
parent 85d9672e47
commit 87373be960

View File

@ -37,9 +37,8 @@ circbuffer * cbuf_new(unsigned int size) {
} }
cbuf = (circbuffer*)m_malloc(sizeof(circbuffer)); cbuf = (circbuffer*)m_malloc(sizeof(circbuffer));
if (size > 0) { /* data is malloced on first write */
cbuf->data = (unsigned char*)m_malloc(size); cbuf->data = NULL;
}
cbuf->used = 0; cbuf->used = 0;
cbuf->readpos = 0; cbuf->readpos = 0;
cbuf->writepos = 0; cbuf->writepos = 0;
@ -50,8 +49,10 @@ circbuffer * cbuf_new(unsigned int size) {
void cbuf_free(circbuffer * cbuf) { void cbuf_free(circbuffer * cbuf) {
m_burn(cbuf->data, cbuf->size); if (cbuf->data) {
m_free(cbuf->data); m_burn(cbuf->data, cbuf->size);
m_free(cbuf->data);
}
m_free(cbuf); m_free(cbuf);
} }
@ -106,6 +107,11 @@ unsigned char* cbuf_writeptr(circbuffer *cbuf, unsigned int len) {
dropbear_exit("Bad cbuf write"); dropbear_exit("Bad cbuf write");
} }
if (!cbuf->data) {
/* lazy allocation */
cbuf->data = (unsigned char*)m_malloc(cbuf->size);
}
return &cbuf->data[cbuf->writepos]; return &cbuf->data[cbuf->writepos];
} }