]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
abuf: Add a helper for initing and allocating a buffer
authorSimon Glass <sjg@chromium.org>
Thu, 1 May 2025 13:37:01 +0000 (07:37 -0600)
committerSimon Glass <sjg@chromium.org>
Fri, 30 May 2025 08:49:31 +0000 (09:49 +0100)
This construct appears in various places. Reduce code size by adding a
function for it.

It inits the abuf, then allocates it to the requested size.

Signed-off-by: Simon Glass <sjg@chromium.org>
boot/cedit.c
boot/scene.c
boot/scene_textline.c
include/abuf.h
lib/abuf.c
lib/of_live.c
test/lib/abuf.c

index d69290c172e7697fbd744b9380fa33c3b0b12213..4e80875828b915eb6aa1ecf2a2a444982545096c 100644 (file)
@@ -449,8 +449,7 @@ int cedit_write_settings(struct expo *exp, struct abuf *buf)
        void *fdt;
        int ret;
 
-       abuf_init(buf);
-       if (!abuf_realloc(buf, CEDIT_SIZE_INC))
+       if (!abuf_init_size(buf, CEDIT_SIZE_INC))
                return log_msg_ret("buf", -ENOMEM);
 
        fdt = abuf_data(buf);
index fb82ffe768ca83c9949bbe2b03fa0662480edbc0..90b4ccf4766d6b2a7b6e59cec8ea0d1be3993fb0 100644 (file)
@@ -31,8 +31,7 @@ int scene_new(struct expo *exp, const char *name, uint id, struct scene **scnp)
                return log_msg_ret("name", -ENOMEM);
        }
 
-       abuf_init(&scn->buf);
-       if (!abuf_realloc(&scn->buf, EXPO_MAX_CHARS + 1)) {
+       if (!abuf_init_size(&scn->buf, EXPO_MAX_CHARS + 1)) {
                free(scn->name);
                free(scn);
                return log_msg_ret("buf", -ENOMEM);
index 6adef7cc173af3bab3a2a32e2815f2243e6f48f3..90642a3f03d6cc4675c3091fe0756a8cc175e5a2 100644 (file)
@@ -31,8 +31,7 @@ int scene_textline(struct scene *scn, const char *name, uint id, uint max_chars,
                            (struct scene_obj **)&tline);
        if (ret < 0)
                return log_msg_ret("obj", -ENOMEM);
-       abuf_init(&tline->buf);
-       if (!abuf_realloc(&tline->buf, max_chars + 1))
+       if (!abuf_init_size(&tline->buf, max_chars + 1))
                return log_msg_ret("buf", -ENOMEM);
        buf = abuf_data(&tline->buf);
        *buf = '\0';
index 62ff6499a0ca2570a815f893a57704e9151f3427..749bb188b0c617b0bee3c661b9365dbb33d1be16 100644 (file)
@@ -170,6 +170,17 @@ void abuf_init_set(struct abuf *abuf, void *data, size_t size);
  */
 void abuf_init_const(struct abuf *abuf, const void *data, size_t size);
 
+/**
+ * abuf_init_size() - Set up an allocated abuf
+ *
+ * Init a new abuf and allocate its size.
+ *
+ * @abuf: abuf to set up
+ * @data: New contents of abuf
+ * @size: New size of abuf
+ */
+bool abuf_init_size(struct abuf *buf, size_t size);
+
 /**
  * abuf_uninit() - Free any memory used by an abuf
  *
index 61adf7fc6b1024664581d2c444d20ee6905ef146..3cbe320fb089b75787d9b4247d956300600d71c3 100644 (file)
@@ -119,6 +119,15 @@ void abuf_init_set(struct abuf *abuf, void *data, size_t size)
        abuf_set(abuf, data, size);
 }
 
+bool abuf_init_size(struct abuf *buf, size_t size)
+{
+       abuf_init(buf);
+       if (!abuf_realloc(buf, size))
+               return false;
+
+       return true;
+}
+
 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 */
index c1620616513c2e32448b4a6d156a9162d97c76b7..24200b948a66a0529c5680ce7376bbed1289d62a 100644 (file)
@@ -448,8 +448,7 @@ int of_live_flatten(const struct device_node *root, struct abuf *buf)
 {
        int ret;
 
-       abuf_init(buf);
-       if (!abuf_realloc(buf, BUF_STEP))
+       if (!abuf_init_size(buf, BUF_STEP))
                return log_msg_ret("ini", -ENOMEM);
 
        ret = fdt_create(abuf_data(buf), abuf_size(buf));
index b38690fe1a994ea5de75c6772d0afcc8e580c53b..cdc86aad9888d48cb428a2a5e8373de9712836ae 100644 (file)
@@ -419,3 +419,24 @@ static int lib_test_abuf_init(struct unit_test_state *uts)
        return 0;
 }
 LIB_TEST(lib_test_abuf_init, 0);
+
+/* Test abuf_init_size() */
+static int lib_test_abuf_init_size(struct unit_test_state *uts)
+{
+       struct abuf buf;
+       ulong start;
+
+       start = ut_check_free();
+
+       ut_assert(abuf_init_size(&buf, TEST_DATA_LEN));
+       ut_assertnonnull(buf.data);
+       ut_asserteq(TEST_DATA_LEN, buf.size);
+       ut_asserteq(true, buf.alloced);
+       abuf_uninit(&buf);
+
+       /* Check for memory leaks */
+       ut_assertok(ut_check_delta(start));
+
+       return 0;
+}
+LIB_TEST(lib_test_abuf_init_size, 0);