From 4662e5286aa259e158f314157d0d8f2ad213a321 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Tue, 18 Mar 2025 16:20:42 +0100 Subject: membuff: Rename functions to have membuf_ prefix The double 'f' is not necessary and is a bit annoying as elsewhere in U-Boot we use 'buf'. Rename all the functions before it is used more widely. Signed-off-by: Simon Glass --- lib/membuff.c | 66 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 33 insertions(+), 33 deletions(-) (limited to 'lib') diff --git a/lib/membuff.c b/lib/membuff.c index b242a38ff1c..435d12b8a3c 100644 --- a/lib/membuff.c +++ b/lib/membuff.c @@ -11,15 +11,15 @@ #include #include "membuff.h" -void membuff_purge(struct membuff *mb) +void membuf_purge(struct membuff *mb) { /* set mb->head and mb->tail so the buffers look empty */ mb->head = mb->start; mb->tail = mb->start; } -static int membuff_putrawflex(struct membuff *mb, int maxlen, bool update, - char ***data, int *offsetp) +static int membuf_putrawflex(struct membuff *mb, int maxlen, bool update, + char ***data, int *offsetp) { int len; @@ -72,30 +72,30 @@ static int membuff_putrawflex(struct membuff *mb, int maxlen, bool update, return len; } -int membuff_putraw(struct membuff *mb, int maxlen, bool update, char **data) +int membuf_putraw(struct membuff *mb, int maxlen, bool update, char **data) { char **datap; int offset; int size; - size = membuff_putrawflex(mb, maxlen, update, &datap, &offset); + size = membuf_putrawflex(mb, maxlen, update, &datap, &offset); *data = *datap + offset; return size; } -bool membuff_putbyte(struct membuff *mb, int ch) +bool membuf_putbyte(struct membuff *mb, int ch) { char *data; - if (membuff_putraw(mb, 1, true, &data) != 1) + if (membuf_putraw(mb, 1, true, &data) != 1) return false; *data = ch; return true; } -int membuff_getraw(struct membuff *mb, int maxlen, bool update, char **data) +int membuf_getraw(struct membuff *mb, int maxlen, bool update, char **data) { int len; @@ -146,21 +146,21 @@ int membuff_getraw(struct membuff *mb, int maxlen, bool update, char **data) return len; } -int membuff_getbyte(struct membuff *mb) +int membuf_getbyte(struct membuff *mb) { char *data = 0; - return membuff_getraw(mb, 1, true, &data) != 1 ? -1 : *(uint8_t *)data; + return membuf_getraw(mb, 1, true, &data) != 1 ? -1 : *(uint8_t *)data; } -int membuff_peekbyte(struct membuff *mb) +int membuf_peekbyte(struct membuff *mb) { char *data = 0; - return membuff_getraw(mb, 1, false, &data) != 1 ? -1 : *(uint8_t *)data; + return membuf_getraw(mb, 1, false, &data) != 1 ? -1 : *(uint8_t *)data; } -int membuff_get(struct membuff *mb, char *buff, int maxlen) +int membuf_get(struct membuff *mb, char *buff, int maxlen) { char *data = 0, *buffptr = buff; int len = 1, i; @@ -171,7 +171,7 @@ int membuff_get(struct membuff *mb, char *buff, int maxlen) */ for (i = 0; len && i < 2; i++) { /* get a pointer to the data available */ - len = membuff_getraw(mb, maxlen, true, &data); + len = membuf_getraw(mb, maxlen, true, &data); /* copy it into the buffer */ memcpy(buffptr, data, len); @@ -183,14 +183,14 @@ int membuff_get(struct membuff *mb, char *buff, int maxlen) return buffptr - buff; } -int membuff_put(struct membuff *mb, const char *buff, int length) +int membuf_put(struct membuff *mb, const char *buff, int length) { char *data; int towrite, i, written; for (i = written = 0; i < 2; i++) { /* ask where some data can be written */ - towrite = membuff_putraw(mb, length, true, &data); + towrite = membuf_putraw(mb, length, true, &data); /* and write it, updating the bytes length */ memcpy(data, buff, towrite); @@ -203,12 +203,12 @@ int membuff_put(struct membuff *mb, const char *buff, int length) return written; } -bool membuff_isempty(struct membuff *mb) +bool membuf_isempty(struct membuff *mb) { return mb->head == mb->tail; } -int membuff_avail(struct membuff *mb) +int membuf_avail(struct membuff *mb) { struct membuff copy; int i, avail; @@ -219,18 +219,18 @@ int membuff_avail(struct membuff *mb) /* now read everything out of the copied buffer */ for (i = avail = 0; i < 2; i++) - avail += membuff_getraw(©, -1, true, &data); + avail += membuf_getraw(©, -1, true, &data); /* and return how much we read */ return avail; } -int membuff_size(struct membuff *mb) +int membuf_size(struct membuff *mb) { return mb->end - mb->start; } -bool membuff_makecontig(struct membuff *mb) +bool membuf_makecontig(struct membuff *mb) { int topsize, botsize; @@ -281,13 +281,13 @@ bool membuff_makecontig(struct membuff *mb) return true; } -int membuff_free(struct membuff *mb) +int membuf_free(struct membuff *mb) { return mb->end == mb->start ? 0 : - (mb->end - mb->start) - 1 - membuff_avail(mb); + (mb->end - mb->start) - 1 - membuf_avail(mb); } -int membuff_readline(struct membuff *mb, char *str, int maxlen, int minch, bool must_fit) +int membuf_readline(struct membuff *mb, char *str, int maxlen, int minch, bool must_fit) { int len; /* number of bytes read (!= string length) */ char *s, *end; @@ -322,7 +322,7 @@ int membuff_readline(struct membuff *mb, char *str, int maxlen, int minch, bool return len; } -int membuff_extend_by(struct membuff *mb, int by, int max) +int membuf_extend_by(struct membuff *mb, int by, int max) { int oldhead, oldtail; int size, orig; @@ -358,32 +358,32 @@ int membuff_extend_by(struct membuff *mb, int by, int max) return 0; } -void membuff_init(struct membuff *mb, char *buff, int size) +void membuf_init(struct membuff *mb, char *buff, int size) { mb->start = buff; mb->end = mb->start + size; - membuff_purge(mb); + membuf_purge(mb); } -int membuff_new(struct membuff *mb, int size) +int membuf_new(struct membuff *mb, int size) { mb->start = malloc(size); if (!mb->start) return -ENOMEM; - membuff_init(mb, mb->start, size); + membuf_init(mb, mb->start, size); return 0; } -void membuff_uninit(struct membuff *mb) +void membuf_uninit(struct membuff *mb) { mb->end = NULL; mb->start = NULL; - membuff_purge(mb); + membuf_purge(mb); } -void membuff_dispose(struct membuff *mb) +void membuf_dispose(struct membuff *mb) { free(&mb->start); - membuff_uninit(mb); + membuf_uninit(mb); } -- cgit v1.2.3 From 9ca1789ff099a364c6e16dcb6f6ad2e8677f32f1 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Tue, 18 Mar 2025 16:20:43 +0100 Subject: membuff: Rename the files to membuf Rename the C and header files to use the membuf basename, to match the functions. Add a MAINTAINERS entry while we are here. Signed-off-by: Simon Glass --- lib/Makefile | 2 +- lib/membuf.c | 389 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/membuff.c | 389 ---------------------------------------------------------- 3 files changed, 390 insertions(+), 390 deletions(-) create mode 100644 lib/membuf.c delete mode 100644 lib/membuff.c (limited to 'lib') diff --git a/lib/Makefile b/lib/Makefile index a30ce1595d5..41de2671cc6 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -125,7 +125,7 @@ obj-y += hang.o obj-y += linux_compat.o obj-y += linux_string.o obj-$(CONFIG_$(PHASE_)LMB) += lmb.o -obj-y += membuff.o +obj-y += membuf.o obj-$(CONFIG_REGEX) += slre.o obj-y += string.o obj-y += tables_csum.o diff --git a/lib/membuf.c b/lib/membuf.c new file mode 100644 index 00000000000..5473efa98ca --- /dev/null +++ b/lib/membuf.c @@ -0,0 +1,389 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (c) 2015 Google, Inc + * Written by Simon Glass + * + * Copyright (c) 1992 Simon Glass + */ + +#include +#include +#include +#include "membuf.h" + +void membuf_purge(struct membuff *mb) +{ + /* set mb->head and mb->tail so the buffers look empty */ + mb->head = mb->start; + mb->tail = mb->start; +} + +static int membuf_putrawflex(struct membuff *mb, int maxlen, bool update, + char ***data, int *offsetp) +{ + int len; + + /* always write to 'mb->head' */ + assert(data && offsetp); + *data = &mb->start; + *offsetp = mb->head - mb->start; + + /* if there is no buffer, we can do nothing */ + if (!mb->start) + return 0; + + /* + * if head is ahead of tail, we can write from head until the end of + * the buffer + */ + if (mb->head >= mb->tail) { + /* work out how many bytes can fit here */ + len = mb->end - mb->head - 1; + if (maxlen >= 0 && len > maxlen) + len = maxlen; + + /* update the head pointer to mark these bytes as written */ + if (update) + mb->head += len; + + /* + * if the tail isn't at start of the buffer, then we can + * write one more byte right at the end + */ + if ((maxlen < 0 || len < maxlen) && mb->tail != mb->start) { + len++; + if (update) + mb->head = mb->start; + } + + /* otherwise now we can write until head almost reaches tail */ + } else { + /* work out how many bytes can fit here */ + len = mb->tail - mb->head - 1; + if (maxlen >= 0 && len > maxlen) + len = maxlen; + + /* update the head pointer to mark these bytes as written */ + if (update) + mb->head += len; + } + + /* return the number of bytes which can be/must be written */ + return len; +} + +int membuf_putraw(struct membuff *mb, int maxlen, bool update, char **data) +{ + char **datap; + int offset; + int size; + + size = membuf_putrawflex(mb, maxlen, update, &datap, &offset); + *data = *datap + offset; + + return size; +} + +bool membuf_putbyte(struct membuff *mb, int ch) +{ + char *data; + + if (membuf_putraw(mb, 1, true, &data) != 1) + return false; + *data = ch; + + return true; +} + +int membuf_getraw(struct membuff *mb, int maxlen, bool update, char **data) +{ + int len; + + /* assume for now there is no data to get */ + len = 0; + + /* + * in this case head is ahead of tail, so we must return data between + *'tail' and 'head' + */ + if (mb->head > mb->tail) { + /* work out the amount of data */ + *data = mb->tail; + len = mb->head - mb->tail; + + /* check it isn't too much */ + if (maxlen >= 0 && len > maxlen) + len = maxlen; + + /* & mark it as read from the buffer */ + if (update) + mb->tail += len; + } + + /* + * if head is before tail, then we have data between 'tail' and 'end' + * and some more data between 'start' and 'head'(which we can't + * return this time + */ + else if (mb->head < mb->tail) { + /* work out the amount of data */ + *data = mb->tail; + len = mb->end - mb->tail; + if (maxlen >= 0 && len > maxlen) + len = maxlen; + if (update) { + mb->tail += len; + if (mb->tail == mb->end) + mb->tail = mb->start; + } + } + + debug("getraw: maxlen=%d, update=%d, head=%d, tail=%d, data=%d, len=%d", + maxlen, update, (int)(mb->head - mb->start), + (int)(mb->tail - mb->start), (int)(*data - mb->start), len); + + /* return the number of bytes we found */ + return len; +} + +int membuf_getbyte(struct membuff *mb) +{ + char *data = 0; + + return membuf_getraw(mb, 1, true, &data) != 1 ? -1 : *(uint8_t *)data; +} + +int membuf_peekbyte(struct membuff *mb) +{ + char *data = 0; + + return membuf_getraw(mb, 1, false, &data) != 1 ? -1 : *(uint8_t *)data; +} + +int membuf_get(struct membuff *mb, char *buff, int maxlen) +{ + char *data = 0, *buffptr = buff; + int len = 1, i; + + /* + * do this in up to two lots(see GetRaw for why) stopping when there + * is no more data + */ + for (i = 0; len && i < 2; i++) { + /* get a pointer to the data available */ + len = membuf_getraw(mb, maxlen, true, &data); + + /* copy it into the buffer */ + memcpy(buffptr, data, len); + buffptr += len; + maxlen -= len; + } + + /* return the number of bytes read */ + return buffptr - buff; +} + +int membuf_put(struct membuff *mb, const char *buff, int length) +{ + char *data; + int towrite, i, written; + + for (i = written = 0; i < 2; i++) { + /* ask where some data can be written */ + towrite = membuf_putraw(mb, length, true, &data); + + /* and write it, updating the bytes length */ + memcpy(data, buff, towrite); + written += towrite; + buff += towrite; + length -= towrite; + } + + /* return the number of bytes written */ + return written; +} + +bool membuf_isempty(struct membuff *mb) +{ + return mb->head == mb->tail; +} + +int membuf_avail(struct membuff *mb) +{ + struct membuff copy; + int i, avail; + char *data = 0; + + /* make a copy of this buffer's control data */ + copy = *mb; + + /* now read everything out of the copied buffer */ + for (i = avail = 0; i < 2; i++) + avail += membuf_getraw(©, -1, true, &data); + + /* and return how much we read */ + return avail; +} + +int membuf_size(struct membuff *mb) +{ + return mb->end - mb->start; +} + +bool membuf_makecontig(struct membuff *mb) +{ + int topsize, botsize; + + debug("makecontig: head=%d, tail=%d, size=%d", + (int)(mb->head - mb->start), (int)(mb->tail - mb->start), + (int)(mb->end - mb->start)); + + /* + * first we move anything at the start of the buffer into the correct + * place some way along + */ + if (mb->tail > mb->head) { + /* + * the data is split into two parts, from 0 to ->head and + * from ->tail to ->end. We move the stuff from 0 to ->head + * up to make space for the other data before it + */ + topsize = mb->end - mb->tail; + botsize = mb->head - mb->start; + + /* + * must move data at bottom up by 'topsize' bytes - check if + * there's room + */ + if (mb->head + topsize >= mb->tail) + return false; + memmove(mb->start + topsize, mb->start, botsize); + debug(" - memmove(%d, %d, %d)", topsize, 0, botsize); + + /* nothing at the start, so skip that step */ + } else { + topsize = mb->head - mb->tail; + botsize = 0; + } + + /* now move data at top down to the bottom */ + memcpy(mb->start, mb->tail, topsize); + debug(" - memcpy(%d, %d, %d)", 0, (int)(mb->tail - mb->start), topsize); + + /* adjust pointers */ + mb->tail = mb->start; + mb->head = mb->start + topsize + botsize; + + debug(" - head=%d, tail=%d", (int)(mb->head - mb->start), + (int)(mb->tail - mb->start)); + + /* all ok */ + return true; +} + +int membuf_free(struct membuff *mb) +{ + return mb->end == mb->start ? 0 : + (mb->end - mb->start) - 1 - membuf_avail(mb); +} + +int membuf_readline(struct membuff *mb, char *str, int maxlen, int minch, bool must_fit) +{ + int len; /* number of bytes read (!= string length) */ + char *s, *end; + bool ok = false; + char *orig = str; + + end = mb->head >= mb->tail ? mb->head : mb->end; + for (len = 0, s = mb->tail; s < end && len < maxlen - 1; str++) { + *str = *s++; + len++; + if (*str == '\n' || *str < minch) { + ok = true; + break; + } + if (s == end && mb->tail > mb->head) { + s = mb->start; + end = mb->head; + } + } + + /* couldn't get the whole string */ + if (!ok && must_fit) { + if (maxlen) + *orig = '\0'; + return 0; + } + + /* terminate the string, update the membuff and return success */ + *str = '\0'; + mb->tail = s == mb->end ? mb->start : s; + + return len; +} + +int membuf_extend_by(struct membuff *mb, int by, int max) +{ + int oldhead, oldtail; + int size, orig; + char *ptr; + + /* double the buffer size until it is big enough */ + assert(by >= 0); + for (orig = mb->end - mb->start, size = orig; size < orig + by;) + size *= 2; + if (max != -1) + size = min(size, max); + by = size - orig; + + /* if we're already at maximum, give up */ + if (by <= 0) + return -E2BIG; + + oldhead = mb->head - mb->start; + oldtail = mb->tail - mb->start; + ptr = realloc(mb->start, size); + if (!ptr) + return -ENOMEM; + mb->start = ptr; + mb->head = mb->start + oldhead; + mb->tail = mb->start + oldtail; + + if (mb->head < mb->tail) { + memmove(mb->tail + by, mb->tail, orig - oldtail); + mb->tail += by; + } + mb->end = mb->start + size; + + return 0; +} + +void membuf_init(struct membuff *mb, char *buff, int size) +{ + mb->start = buff; + mb->end = mb->start + size; + membuf_purge(mb); +} + +int membuf_new(struct membuff *mb, int size) +{ + mb->start = malloc(size); + if (!mb->start) + return -ENOMEM; + + membuf_init(mb, mb->start, size); + return 0; +} + +void membuf_uninit(struct membuff *mb) +{ + mb->end = NULL; + mb->start = NULL; + membuf_purge(mb); +} + +void membuf_dispose(struct membuff *mb) +{ + free(&mb->start); + membuf_uninit(mb); +} diff --git a/lib/membuff.c b/lib/membuff.c deleted file mode 100644 index 435d12b8a3c..00000000000 --- a/lib/membuff.c +++ /dev/null @@ -1,389 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0+ -/* - * Copyright (c) 2015 Google, Inc - * Written by Simon Glass - * - * Copyright (c) 1992 Simon Glass - */ - -#include -#include -#include -#include "membuff.h" - -void membuf_purge(struct membuff *mb) -{ - /* set mb->head and mb->tail so the buffers look empty */ - mb->head = mb->start; - mb->tail = mb->start; -} - -static int membuf_putrawflex(struct membuff *mb, int maxlen, bool update, - char ***data, int *offsetp) -{ - int len; - - /* always write to 'mb->head' */ - assert(data && offsetp); - *data = &mb->start; - *offsetp = mb->head - mb->start; - - /* if there is no buffer, we can do nothing */ - if (!mb->start) - return 0; - - /* - * if head is ahead of tail, we can write from head until the end of - * the buffer - */ - if (mb->head >= mb->tail) { - /* work out how many bytes can fit here */ - len = mb->end - mb->head - 1; - if (maxlen >= 0 && len > maxlen) - len = maxlen; - - /* update the head pointer to mark these bytes as written */ - if (update) - mb->head += len; - - /* - * if the tail isn't at start of the buffer, then we can - * write one more byte right at the end - */ - if ((maxlen < 0 || len < maxlen) && mb->tail != mb->start) { - len++; - if (update) - mb->head = mb->start; - } - - /* otherwise now we can write until head almost reaches tail */ - } else { - /* work out how many bytes can fit here */ - len = mb->tail - mb->head - 1; - if (maxlen >= 0 && len > maxlen) - len = maxlen; - - /* update the head pointer to mark these bytes as written */ - if (update) - mb->head += len; - } - - /* return the number of bytes which can be/must be written */ - return len; -} - -int membuf_putraw(struct membuff *mb, int maxlen, bool update, char **data) -{ - char **datap; - int offset; - int size; - - size = membuf_putrawflex(mb, maxlen, update, &datap, &offset); - *data = *datap + offset; - - return size; -} - -bool membuf_putbyte(struct membuff *mb, int ch) -{ - char *data; - - if (membuf_putraw(mb, 1, true, &data) != 1) - return false; - *data = ch; - - return true; -} - -int membuf_getraw(struct membuff *mb, int maxlen, bool update, char **data) -{ - int len; - - /* assume for now there is no data to get */ - len = 0; - - /* - * in this case head is ahead of tail, so we must return data between - *'tail' and 'head' - */ - if (mb->head > mb->tail) { - /* work out the amount of data */ - *data = mb->tail; - len = mb->head - mb->tail; - - /* check it isn't too much */ - if (maxlen >= 0 && len > maxlen) - len = maxlen; - - /* & mark it as read from the buffer */ - if (update) - mb->tail += len; - } - - /* - * if head is before tail, then we have data between 'tail' and 'end' - * and some more data between 'start' and 'head'(which we can't - * return this time - */ - else if (mb->head < mb->tail) { - /* work out the amount of data */ - *data = mb->tail; - len = mb->end - mb->tail; - if (maxlen >= 0 && len > maxlen) - len = maxlen; - if (update) { - mb->tail += len; - if (mb->tail == mb->end) - mb->tail = mb->start; - } - } - - debug("getraw: maxlen=%d, update=%d, head=%d, tail=%d, data=%d, len=%d", - maxlen, update, (int)(mb->head - mb->start), - (int)(mb->tail - mb->start), (int)(*data - mb->start), len); - - /* return the number of bytes we found */ - return len; -} - -int membuf_getbyte(struct membuff *mb) -{ - char *data = 0; - - return membuf_getraw(mb, 1, true, &data) != 1 ? -1 : *(uint8_t *)data; -} - -int membuf_peekbyte(struct membuff *mb) -{ - char *data = 0; - - return membuf_getraw(mb, 1, false, &data) != 1 ? -1 : *(uint8_t *)data; -} - -int membuf_get(struct membuff *mb, char *buff, int maxlen) -{ - char *data = 0, *buffptr = buff; - int len = 1, i; - - /* - * do this in up to two lots(see GetRaw for why) stopping when there - * is no more data - */ - for (i = 0; len && i < 2; i++) { - /* get a pointer to the data available */ - len = membuf_getraw(mb, maxlen, true, &data); - - /* copy it into the buffer */ - memcpy(buffptr, data, len); - buffptr += len; - maxlen -= len; - } - - /* return the number of bytes read */ - return buffptr - buff; -} - -int membuf_put(struct membuff *mb, const char *buff, int length) -{ - char *data; - int towrite, i, written; - - for (i = written = 0; i < 2; i++) { - /* ask where some data can be written */ - towrite = membuf_putraw(mb, length, true, &data); - - /* and write it, updating the bytes length */ - memcpy(data, buff, towrite); - written += towrite; - buff += towrite; - length -= towrite; - } - - /* return the number of bytes written */ - return written; -} - -bool membuf_isempty(struct membuff *mb) -{ - return mb->head == mb->tail; -} - -int membuf_avail(struct membuff *mb) -{ - struct membuff copy; - int i, avail; - char *data = 0; - - /* make a copy of this buffer's control data */ - copy = *mb; - - /* now read everything out of the copied buffer */ - for (i = avail = 0; i < 2; i++) - avail += membuf_getraw(©, -1, true, &data); - - /* and return how much we read */ - return avail; -} - -int membuf_size(struct membuff *mb) -{ - return mb->end - mb->start; -} - -bool membuf_makecontig(struct membuff *mb) -{ - int topsize, botsize; - - debug("makecontig: head=%d, tail=%d, size=%d", - (int)(mb->head - mb->start), (int)(mb->tail - mb->start), - (int)(mb->end - mb->start)); - - /* - * first we move anything at the start of the buffer into the correct - * place some way along - */ - if (mb->tail > mb->head) { - /* - * the data is split into two parts, from 0 to ->head and - * from ->tail to ->end. We move the stuff from 0 to ->head - * up to make space for the other data before it - */ - topsize = mb->end - mb->tail; - botsize = mb->head - mb->start; - - /* - * must move data at bottom up by 'topsize' bytes - check if - * there's room - */ - if (mb->head + topsize >= mb->tail) - return false; - memmove(mb->start + topsize, mb->start, botsize); - debug(" - memmove(%d, %d, %d)", topsize, 0, botsize); - - /* nothing at the start, so skip that step */ - } else { - topsize = mb->head - mb->tail; - botsize = 0; - } - - /* now move data at top down to the bottom */ - memcpy(mb->start, mb->tail, topsize); - debug(" - memcpy(%d, %d, %d)", 0, (int)(mb->tail - mb->start), topsize); - - /* adjust pointers */ - mb->tail = mb->start; - mb->head = mb->start + topsize + botsize; - - debug(" - head=%d, tail=%d", (int)(mb->head - mb->start), - (int)(mb->tail - mb->start)); - - /* all ok */ - return true; -} - -int membuf_free(struct membuff *mb) -{ - return mb->end == mb->start ? 0 : - (mb->end - mb->start) - 1 - membuf_avail(mb); -} - -int membuf_readline(struct membuff *mb, char *str, int maxlen, int minch, bool must_fit) -{ - int len; /* number of bytes read (!= string length) */ - char *s, *end; - bool ok = false; - char *orig = str; - - end = mb->head >= mb->tail ? mb->head : mb->end; - for (len = 0, s = mb->tail; s < end && len < maxlen - 1; str++) { - *str = *s++; - len++; - if (*str == '\n' || *str < minch) { - ok = true; - break; - } - if (s == end && mb->tail > mb->head) { - s = mb->start; - end = mb->head; - } - } - - /* couldn't get the whole string */ - if (!ok && must_fit) { - if (maxlen) - *orig = '\0'; - return 0; - } - - /* terminate the string, update the membuff and return success */ - *str = '\0'; - mb->tail = s == mb->end ? mb->start : s; - - return len; -} - -int membuf_extend_by(struct membuff *mb, int by, int max) -{ - int oldhead, oldtail; - int size, orig; - char *ptr; - - /* double the buffer size until it is big enough */ - assert(by >= 0); - for (orig = mb->end - mb->start, size = orig; size < orig + by;) - size *= 2; - if (max != -1) - size = min(size, max); - by = size - orig; - - /* if we're already at maximum, give up */ - if (by <= 0) - return -E2BIG; - - oldhead = mb->head - mb->start; - oldtail = mb->tail - mb->start; - ptr = realloc(mb->start, size); - if (!ptr) - return -ENOMEM; - mb->start = ptr; - mb->head = mb->start + oldhead; - mb->tail = mb->start + oldtail; - - if (mb->head < mb->tail) { - memmove(mb->tail + by, mb->tail, orig - oldtail); - mb->tail += by; - } - mb->end = mb->start + size; - - return 0; -} - -void membuf_init(struct membuff *mb, char *buff, int size) -{ - mb->start = buff; - mb->end = mb->start + size; - membuf_purge(mb); -} - -int membuf_new(struct membuff *mb, int size) -{ - mb->start = malloc(size); - if (!mb->start) - return -ENOMEM; - - membuf_init(mb, mb->start, size); - return 0; -} - -void membuf_uninit(struct membuff *mb) -{ - mb->end = NULL; - mb->start = NULL; - membuf_purge(mb); -} - -void membuf_dispose(struct membuff *mb) -{ - free(&mb->start); - membuf_uninit(mb); -} -- cgit v1.2.3 From 68b0af2127dbd7b304f897f4796c6eb86fd74402 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Tue, 18 Mar 2025 16:20:44 +0100 Subject: membuf: Rename struct Rename the struct to match the function prefix and filenames. Signed-off-by: Simon Glass --- lib/membuf.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'lib') diff --git a/lib/membuf.c b/lib/membuf.c index 5473efa98ca..b13998ccdbd 100644 --- a/lib/membuf.c +++ b/lib/membuf.c @@ -11,14 +11,14 @@ #include #include "membuf.h" -void membuf_purge(struct membuff *mb) +void membuf_purge(struct membuf *mb) { /* set mb->head and mb->tail so the buffers look empty */ mb->head = mb->start; mb->tail = mb->start; } -static int membuf_putrawflex(struct membuff *mb, int maxlen, bool update, +static int membuf_putrawflex(struct membuf *mb, int maxlen, bool update, char ***data, int *offsetp) { int len; @@ -72,7 +72,7 @@ static int membuf_putrawflex(struct membuff *mb, int maxlen, bool update, return len; } -int membuf_putraw(struct membuff *mb, int maxlen, bool update, char **data) +int membuf_putraw(struct membuf *mb, int maxlen, bool update, char **data) { char **datap; int offset; @@ -84,7 +84,7 @@ int membuf_putraw(struct membuff *mb, int maxlen, bool update, char **data) return size; } -bool membuf_putbyte(struct membuff *mb, int ch) +bool membuf_putbyte(struct membuf *mb, int ch) { char *data; @@ -95,7 +95,7 @@ bool membuf_putbyte(struct membuff *mb, int ch) return true; } -int membuf_getraw(struct membuff *mb, int maxlen, bool update, char **data) +int membuf_getraw(struct membuf *mb, int maxlen, bool update, char **data) { int len; @@ -146,21 +146,21 @@ int membuf_getraw(struct membuff *mb, int maxlen, bool update, char **data) return len; } -int membuf_getbyte(struct membuff *mb) +int membuf_getbyte(struct membuf *mb) { char *data = 0; return membuf_getraw(mb, 1, true, &data) != 1 ? -1 : *(uint8_t *)data; } -int membuf_peekbyte(struct membuff *mb) +int membuf_peekbyte(struct membuf *mb) { char *data = 0; return membuf_getraw(mb, 1, false, &data) != 1 ? -1 : *(uint8_t *)data; } -int membuf_get(struct membuff *mb, char *buff, int maxlen) +int membuf_get(struct membuf *mb, char *buff, int maxlen) { char *data = 0, *buffptr = buff; int len = 1, i; @@ -183,7 +183,7 @@ int membuf_get(struct membuff *mb, char *buff, int maxlen) return buffptr - buff; } -int membuf_put(struct membuff *mb, const char *buff, int length) +int membuf_put(struct membuf *mb, const char *buff, int length) { char *data; int towrite, i, written; @@ -203,14 +203,14 @@ int membuf_put(struct membuff *mb, const char *buff, int length) return written; } -bool membuf_isempty(struct membuff *mb) +bool membuf_isempty(struct membuf *mb) { return mb->head == mb->tail; } -int membuf_avail(struct membuff *mb) +int membuf_avail(struct membuf *mb) { - struct membuff copy; + struct membuf copy; int i, avail; char *data = 0; @@ -225,12 +225,12 @@ int membuf_avail(struct membuff *mb) return avail; } -int membuf_size(struct membuff *mb) +int membuf_size(struct membuf *mb) { return mb->end - mb->start; } -bool membuf_makecontig(struct membuff *mb) +bool membuf_makecontig(struct membuf *mb) { int topsize, botsize; @@ -281,13 +281,13 @@ bool membuf_makecontig(struct membuff *mb) return true; } -int membuf_free(struct membuff *mb) +int membuf_free(struct membuf *mb) { return mb->end == mb->start ? 0 : (mb->end - mb->start) - 1 - membuf_avail(mb); } -int membuf_readline(struct membuff *mb, char *str, int maxlen, int minch, bool must_fit) +int membuf_readline(struct membuf *mb, char *str, int maxlen, int minch, bool must_fit) { int len; /* number of bytes read (!= string length) */ char *s, *end; @@ -322,7 +322,7 @@ int membuf_readline(struct membuff *mb, char *str, int maxlen, int minch, bool m return len; } -int membuf_extend_by(struct membuff *mb, int by, int max) +int membuf_extend_by(struct membuf *mb, int by, int max) { int oldhead, oldtail; int size, orig; @@ -358,14 +358,14 @@ int membuf_extend_by(struct membuff *mb, int by, int max) return 0; } -void membuf_init(struct membuff *mb, char *buff, int size) +void membuf_init(struct membuf *mb, char *buff, int size) { mb->start = buff; mb->end = mb->start + size; membuf_purge(mb); } -int membuf_new(struct membuff *mb, int size) +int membuf_new(struct membuf *mb, int size) { mb->start = malloc(size); if (!mb->start) @@ -375,14 +375,14 @@ int membuf_new(struct membuff *mb, int size) return 0; } -void membuf_uninit(struct membuff *mb) +void membuf_uninit(struct membuf *mb) { mb->end = NULL; mb->start = NULL; membuf_purge(mb); } -void membuf_dispose(struct membuff *mb) +void membuf_dispose(struct membuf *mb) { free(&mb->start); membuf_uninit(mb); -- cgit v1.2.3 From 44e763bd6624f23fe6d94340efb43c627b5ce4a7 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Tue, 18 Mar 2025 16:20:46 +0100 Subject: membuf: Correct implementation of membuf_dispose() This should free the pointer, not the address of the pointer. Fix it. Signed-off-by: Simon Glass --- lib/membuf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/membuf.c b/lib/membuf.c index b13998ccdbd..695d16d051e 100644 --- a/lib/membuf.c +++ b/lib/membuf.c @@ -384,6 +384,6 @@ void membuf_uninit(struct membuf *mb) void membuf_dispose(struct membuf *mb) { - free(&mb->start); + free(mb->start); membuf_uninit(mb); } -- cgit v1.2.3 From da8694a7d2cc9fdfd67e8a039ba1e70bdd543d0b Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Tue, 18 Mar 2025 16:20:48 +0100 Subject: membuf: Minor code-style improvements Show the start in end in the comment. Comment a missing variable in membuf_readline() and fix its line length. Signed-off-by: Simon Glass --- lib/membuf.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/membuf.c b/lib/membuf.c index 695d16d051e..f38ff36cb0b 100644 --- a/lib/membuf.c +++ b/lib/membuf.c @@ -287,7 +287,8 @@ int membuf_free(struct membuf *mb) (mb->end - mb->start) - 1 - membuf_avail(mb); } -int membuf_readline(struct membuf *mb, char *str, int maxlen, int minch, bool must_fit) +int membuf_readline(struct membuf *mb, char *str, int maxlen, int minch, + bool must_fit) { int len; /* number of bytes read (!= string length) */ char *s, *end; -- cgit v1.2.3