mirror of
https://source.denx.de/u-boot/u-boot.git
synced 2025-12-20 17:01:50 +01:00
abuf: Add a function to copy a buffer
It is useful to be able to copy an abuf, to allow changes while preserving the original. Add a function for this. Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
parent
97b586695c
commit
d58cebbbc7
@ -111,6 +111,17 @@ bool abuf_realloc(struct abuf *abuf, size_t new_size);
|
|||||||
*/
|
*/
|
||||||
bool abuf_realloc_inc(struct abuf *abuf, size_t inc);
|
bool abuf_realloc_inc(struct abuf *abuf, size_t inc);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* abuf_copy() - Make a copy of an abuf
|
||||||
|
*
|
||||||
|
* Creates an allocated copy of @old in @new
|
||||||
|
*
|
||||||
|
* @old: abuf to copy
|
||||||
|
* @new: new abuf to hold the copy (inited by this function)
|
||||||
|
* Return: true if OK, false if out of memory
|
||||||
|
*/
|
||||||
|
bool abuf_copy(const struct abuf *old, struct abuf *new);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* abuf_uninit_move() - Return the allocated contents and uninit the abuf
|
* abuf_uninit_move() - Return the allocated contents and uninit the abuf
|
||||||
*
|
*
|
||||||
|
|||||||
14
lib/abuf.c
14
lib/abuf.c
@ -128,6 +128,20 @@ bool abuf_init_size(struct abuf *buf, size_t size)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool abuf_copy(const struct abuf *old, struct abuf *copy)
|
||||||
|
{
|
||||||
|
char *data;
|
||||||
|
|
||||||
|
data = malloc(old->size);
|
||||||
|
if (!data)
|
||||||
|
return false;
|
||||||
|
memcpy(data, old->data, old->size);
|
||||||
|
abuf_init_set(copy, data, old->size);
|
||||||
|
copy->alloced = true;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void abuf_init_const(struct abuf *abuf, const void *data, size_t size)
|
void abuf_init_const(struct abuf *abuf, const void *data, size_t size)
|
||||||
{
|
{
|
||||||
/* for now there is no flag indicating that the abuf data is constant */
|
/* for now there is no flag indicating that the abuf data is constant */
|
||||||
|
|||||||
@ -420,6 +420,29 @@ static int lib_test_abuf_init(struct unit_test_state *uts)
|
|||||||
}
|
}
|
||||||
LIB_TEST(lib_test_abuf_init, 0);
|
LIB_TEST(lib_test_abuf_init, 0);
|
||||||
|
|
||||||
|
/* Test abuf_copy() */
|
||||||
|
static int lib_test_abuf_copy(struct unit_test_state *uts)
|
||||||
|
{
|
||||||
|
struct abuf buf, copy;
|
||||||
|
ulong start;
|
||||||
|
|
||||||
|
start = ut_check_free();
|
||||||
|
|
||||||
|
abuf_init_set(&buf, test_data, TEST_DATA_LEN);
|
||||||
|
ut_assert(abuf_copy(&buf, ©));
|
||||||
|
ut_asserteq(buf.size, copy.size);
|
||||||
|
ut_assert(buf.data != copy.data);
|
||||||
|
ut_assert(copy.alloced);
|
||||||
|
abuf_uninit(©);
|
||||||
|
abuf_uninit(&buf);
|
||||||
|
|
||||||
|
/* Check for memory leaks */
|
||||||
|
ut_assertok(ut_check_delta(start));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
LIB_TEST(lib_test_abuf_copy, 0);
|
||||||
|
|
||||||
/* Test abuf_init_size() */
|
/* Test abuf_init_size() */
|
||||||
static int lib_test_abuf_init_size(struct unit_test_state *uts)
|
static int lib_test_abuf_init_size(struct unit_test_state *uts)
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user