Skip to content

Commit b7aacc6

Browse files
committed
stop over-allocating ios_t buffer by 1 byte
This should make allocations more predictable if the buffer size becomes customizable.
1 parent 482f5ab commit b7aacc6

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

src/flisp/iostream.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,13 @@ value_t fl_ioreaduntil(fl_context_t *fl_ctx, value_t *args, uint32_t nargs)
352352
cv->len = n;
353353
if (dest.buf != data) {
354354
// outgrew initial space
355-
cv->data = dest.buf;
355+
size_t sz;
356+
cv->data = ios_take_buffer(&dest, &sz);
356357
cv_autorelease(fl_ctx, cv);
357358
}
358-
((char*)cv->data)[n] = '\0';
359+
else {
360+
((char*)cv->data)[n] = '\0';
361+
}
359362
if (n == 0 && ios_eof(src))
360363
return fl_ctx->FL_EOF;
361364
return str;

src/support/ios.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,12 @@ static char *_buf_realloc(ios_t *s, size_t sz)
198198

199199
if (s->ownbuf && s->buf != &s->local[0]) {
200200
// if we own the buffer we're free to resize it
201-
// always allocate 1 bigger in case user wants to add a NUL
202-
// terminator after taking over the buffer
203-
temp = (char*)LLT_REALLOC(s->buf, sz+1);
201+
temp = (char*)LLT_REALLOC(s->buf, sz);
204202
if (temp == NULL)
205203
return NULL;
206204
}
207205
else {
208-
temp = (char*)LLT_ALLOC(sz+1);
206+
temp = (char*)LLT_ALLOC(sz);
209207
if (temp == NULL)
210208
return NULL;
211209
s->ownbuf = 1;
@@ -691,22 +689,24 @@ char *ios_take_buffer(ios_t *s, size_t *psize)
691689

692690
ios_flush(s);
693691

694-
if (s->buf == &s->local[0]) {
692+
if (s->buf == &s->local[0] || s->buf == NULL || (!s->ownbuf && s->size == s->maxsize)) {
695693
buf = (char*)LLT_ALLOC((size_t)s->size + 1);
696694
if (buf == NULL)
697695
return NULL;
698696
if (s->size)
699697
memcpy(buf, s->buf, (size_t)s->size);
700698
}
699+
else if (s->size == s->maxsize) {
700+
buf = (char*)LLT_REALLOC(s->buf, (size_t)s->size + 1);
701+
if (buf == NULL)
702+
return NULL;
703+
}
701704
else {
702-
if (s->buf == NULL)
703-
buf = (char*)LLT_ALLOC((size_t)s->size + 1);
704-
else
705-
buf = s->buf;
705+
buf = s->buf;
706706
}
707707
buf[s->size] = '\0';
708708

709-
*psize = s->size+1; // buffer is actually 1 bigger for terminating NUL
709+
*psize = s->size + 1;
710710

711711
/* empty stream and reinitialize */
712712
_buf_init(s, s->bm);

src/support/ios.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ JL_DLLEXPORT int ios_eof_blocking(ios_t *s);
9595
JL_DLLEXPORT int ios_flush(ios_t *s);
9696
JL_DLLEXPORT int ios_close(ios_t *s) JL_NOTSAFEPOINT;
9797
JL_DLLEXPORT int ios_isopen(ios_t *s);
98-
JL_DLLEXPORT char *ios_take_buffer(ios_t *s, size_t *psize); // release buffer to caller
98+
JL_DLLEXPORT char *ios_take_buffer(ios_t *s, size_t *psize); // nul terminate and release buffer to caller
9999
// set buffer space to use
100100
JL_DLLEXPORT int ios_setbuf(ios_t *s, char *buf, size_t size, int own) JL_NOTSAFEPOINT;
101101
JL_DLLEXPORT int ios_bufmode(ios_t *s, bufmode_t mode) JL_NOTSAFEPOINT;

0 commit comments

Comments
 (0)