diff mbox series

[OpenWrt-Devel] libubox: use const *attr in getters

Message ID 20200129082950.239999-1-peter.stadler@student.uibk.ac.at
State Under Review
Delegated to: Petr Štetiar
Headers show
Series [OpenWrt-Devel] libubox: use const *attr in getters | expand

Commit Message

Peter Stadler Jan. 29, 2020, 8:29 a.m. UTC
Change the signature of the following get-functions to accept
the `struct blob_attr *attr` as `const`:
  - blobmsg_get_u8
  - blobmsg_get_bool
  - blobmsg_get_u16
  - blobmsg_get_u32
  - blobmsg_get_u64
  - blobmsg_get_double
  - blobmsg_get_string
This allows to get the values instead of calling blobmsg_data.

Signed-off-by: Peter Stadler <peter.stadler@student.uibk.ac.at>
---
 blobmsg.h | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

Comments

Petr Štetiar Jan. 29, 2020, 8:48 a.m. UTC | #1
Peter Stadler <peter.stadler@student.uibk.ac.at> [2020-01-29 09:29:50]:

Hi,

> This allows to get the values instead of calling blobmsg_data.

ok, as you didn't provided this information in the commit message and I'm
wondering, does this changes make libubox faster (by how much?), smaller (by
how much?) or was there perhaps something else behind the need for this
changes?

No need to send v2, just give me the details over email and I'll add it to the
commit description, thanks.

-- ynezz
Peter Stadler Jan. 29, 2020, 9:15 a.m. UTC | #2
Hi,

thank you for your fast reply.


It is not faster/smaller as the getters are indirections to blobmsg_data 
(optimized they should have the same performance).


The problem arises, when you have a `const struct blob_attr *` and want 
to get the values: In C++ you cannot pass them to the get-functions 
without removing the constness. You could pass them to `blobmsg_data`, 
but the getters would be the cleaner way as their name is more verbose 
(and they handle the format).


So, it is (should be) more a cosmetic change that reflects the usage.


Peter


P.S.: I am also happy if the patch is not included. I just think, it is 
better to have them const at this place, too (I am not seeing any 
reasons, why it would we better to have them non-const).
diff mbox series

Patch

diff --git a/blobmsg.h b/blobmsg.h
index be3c7ee..a76bc87 100644
--- a/blobmsg.h
+++ b/blobmsg.h
@@ -260,27 +260,27 @@  static inline int blobmsg_buf_init(struct blob_buf *buf)
 	return blob_buf_init(buf, BLOBMSG_TYPE_TABLE);
 }
 
-static inline uint8_t blobmsg_get_u8(struct blob_attr *attr)
+static inline uint8_t blobmsg_get_u8(const struct blob_attr *attr)
 {
 	return *(uint8_t *) blobmsg_data(attr);
 }
 
-static inline bool blobmsg_get_bool(struct blob_attr *attr)
+static inline bool blobmsg_get_bool(const struct blob_attr *attr)
 {
 	return *(uint8_t *) blobmsg_data(attr);
 }
 
-static inline uint16_t blobmsg_get_u16(struct blob_attr *attr)
+static inline uint16_t blobmsg_get_u16(const struct blob_attr *attr)
 {
 	return be16_to_cpu(*(uint16_t *) blobmsg_data(attr));
 }
 
-static inline uint32_t blobmsg_get_u32(struct blob_attr *attr)
+static inline uint32_t blobmsg_get_u32(const struct blob_attr *attr)
 {
 	return be32_to_cpu(*(uint32_t *) blobmsg_data(attr));
 }
 
-static inline uint64_t blobmsg_get_u64(struct blob_attr *attr)
+static inline uint64_t blobmsg_get_u64(const struct blob_attr *attr)
 {
 	uint32_t *ptr = (uint32_t *) blobmsg_data(attr);
 	uint64_t tmp = ((uint64_t) be32_to_cpu(ptr[0])) << 32;
@@ -288,7 +288,7 @@  static inline uint64_t blobmsg_get_u64(struct blob_attr *attr)
 	return tmp;
 }
 
-static inline double blobmsg_get_double(struct blob_attr *attr)
+static inline double blobmsg_get_double(const struct blob_attr *attr)
 {
 	union {
 		double d;
@@ -298,7 +298,7 @@  static inline double blobmsg_get_double(struct blob_attr *attr)
 	return v.d;
 }
 
-static inline char *blobmsg_get_string(struct blob_attr *attr)
+static inline char *blobmsg_get_string(const struct blob_attr *attr)
 {
 	if (!attr)
 		return NULL;