diff mbox series

[06/13] libpdbg: Restrict set_property to updating existing property

Message ID 20200115051901.17514-7-amitay@ozlabs.org
State Accepted
Headers show
Series Use fdt properties directly | expand

Checks

Context Check Description
snowpatch_ozlabs/apply_patch warning Failed to apply on branch master (8b4611b5d8e7e2279fe4aa80c892fcfe10aa398d)
snowpatch_ozlabs/apply_patch fail Failed to apply to any branch

Commit Message

Amitay Isaacs Jan. 15, 2020, 5:18 a.m. UTC
Device tree properties are going to be always accessed from dtb, so unless
the property already exists and has the exact size, it cannot be updated.

Signed-off-by: Amitay Isaacs <amitay@ozlabs.org>
---
 libpdbg/device.c  | 22 ++++++----------------
 libpdbg/libpdbg.h |  2 +-
 2 files changed, 7 insertions(+), 17 deletions(-)

Comments

Alistair Popple Jan. 16, 2020, 1:32 a.m. UTC | #1
Nice, should even save on a little bit of memory usage.

Reviewed-by: Alistair Popple <alistair@popple.id.au>

On Wednesday, 15 January 2020 4:18:54 PM AEDT Amitay Isaacs wrote:
> Device tree properties are going to be always accessed from dtb, so unless
> the property already exists and has the exact size, it cannot be updated.
> 
> Signed-off-by: Amitay Isaacs <amitay@ozlabs.org>
> ---
>  libpdbg/device.c  | 22 ++++++----------------
>  libpdbg/libpdbg.h |  2 +-
>  2 files changed, 7 insertions(+), 17 deletions(-)
> 
> diff --git a/libpdbg/device.c b/libpdbg/device.c
> index 6599171..91ad258 100644
> --- a/libpdbg/device.c
> +++ b/libpdbg/device.c
> @@ -384,31 +384,21 @@ static struct dt_property *dt_add_property(struct
> pdbg_target *node, return p;
>  }
> 
> -static void dt_resize_property(struct dt_property **prop, size_t len)
> -{
> -	size_t new_len = sizeof(**prop) + len;
> -
> -	*prop = realloc(*prop, new_len);
> -
> -	/* Fix up linked lists in case we moved. (note: not an empty list). */
> -	(*prop)->list.next->prev = &(*prop)->list;
> -	(*prop)->list.prev->next = &(*prop)->list;
> -}
> -
> -void pdbg_target_set_property(struct pdbg_target *target, const char *name,
> const void *val, size_t size) +bool pdbg_target_set_property(struct
> pdbg_target *target, const char *name, const void *val, size_t size) {
>  	struct dt_property *p;
> 
>  	if ((p = dt_find_property(target, name))) {
> -		if (size > p->len) {
> -			dt_resize_property(&p, size);
> -			p->len = size;
> +		if (size != p->len) {
> +			return false;
>  		}
> 
>  		memcpy(p->prop, val, size);
>  	} else {
> -		dt_add_property(target, name, val, size);
> +		return false;
>  	}
> +
> +	return true;
>  }
> 
>  void *pdbg_target_property(struct pdbg_target *target, const char *name,
> size_t *size) diff --git a/libpdbg/libpdbg.h b/libpdbg/libpdbg.h
> index be0aa09..d28fd90 100644
> --- a/libpdbg/libpdbg.h
> +++ b/libpdbg/libpdbg.h
> @@ -84,7 +84,7 @@ struct pdbg_target *pdbg_target_parent_virtual(const char
> *klass, struct pdbg_ta struct pdbg_target *pdbg_target_require_parent(const
> char *klass, struct pdbg_target *target);
> 
>  /* Set the given property. Will automatically add one if one doesn't exist
> */ -void pdbg_target_set_property(struct pdbg_target *target, const char
> *name, const void *val, size_t size); +bool pdbg_target_set_property(struct
> pdbg_target *target, const char *name, const void *val, size_t size);
> 
>  /* Get the given property and return the size */
>  void *pdbg_target_property(struct pdbg_target *target, const char *name,
> size_t *size);
diff mbox series

Patch

diff --git a/libpdbg/device.c b/libpdbg/device.c
index 6599171..91ad258 100644
--- a/libpdbg/device.c
+++ b/libpdbg/device.c
@@ -384,31 +384,21 @@  static struct dt_property *dt_add_property(struct pdbg_target *node,
 	return p;
 }
 
-static void dt_resize_property(struct dt_property **prop, size_t len)
-{
-	size_t new_len = sizeof(**prop) + len;
-
-	*prop = realloc(*prop, new_len);
-
-	/* Fix up linked lists in case we moved. (note: not an empty list). */
-	(*prop)->list.next->prev = &(*prop)->list;
-	(*prop)->list.prev->next = &(*prop)->list;
-}
-
-void pdbg_target_set_property(struct pdbg_target *target, const char *name, const void *val, size_t size)
+bool pdbg_target_set_property(struct pdbg_target *target, const char *name, const void *val, size_t size)
 {
 	struct dt_property *p;
 
 	if ((p = dt_find_property(target, name))) {
-		if (size > p->len) {
-			dt_resize_property(&p, size);
-			p->len = size;
+		if (size != p->len) {
+			return false;
 		}
 
 		memcpy(p->prop, val, size);
 	} else {
-		dt_add_property(target, name, val, size);
+		return false;
 	}
+
+	return true;
 }
 
 void *pdbg_target_property(struct pdbg_target *target, const char *name, size_t *size)
diff --git a/libpdbg/libpdbg.h b/libpdbg/libpdbg.h
index be0aa09..d28fd90 100644
--- a/libpdbg/libpdbg.h
+++ b/libpdbg/libpdbg.h
@@ -84,7 +84,7 @@  struct pdbg_target *pdbg_target_parent_virtual(const char *klass, struct pdbg_ta
 struct pdbg_target *pdbg_target_require_parent(const char *klass, struct pdbg_target *target);
 
 /* Set the given property. Will automatically add one if one doesn't exist */
-void pdbg_target_set_property(struct pdbg_target *target, const char *name, const void *val, size_t size);
+bool pdbg_target_set_property(struct pdbg_target *target, const char *name, const void *val, size_t size);
 
 /* Get the given property and return the size */
 void *pdbg_target_property(struct pdbg_target *target, const char *name, size_t *size);