diff mbox series

[v3,14/25] tools/xenstore: change talloc_free() to take a const pointer

Message ID 20230724110247.10520-15-jgross@suse.com (mailing list archive)
State Superseded
Headers show
Series tools/xenstore: drop TDB | expand

Commit Message

Jürgen Groß July 24, 2023, 11:02 a.m. UTC
With talloc_free() and related functions not taking a ponter to const
it is tedious to use the const attribute for talloc()-ed memory in
many cases.

Change the related prototypes to use "const void *" instead of
"void *".

Signed-off-by: Juergen Gross <jgross@suse.com>
---
V3:
- new patch
---
 tools/xenstore/talloc.c | 8 ++++----
 tools/xenstore/talloc.h | 4 ++--
 2 files changed, 6 insertions(+), 6 deletions(-)

Comments

Julien Grall July 27, 2023, 9:21 p.m. UTC | #1
Hi Juergen,

On 24/07/2023 12:02, Juergen Gross wrote:
> With talloc_free() and related functions not taking a ponter to const

typo: s/ponter/pointer/

> it is tedious to use the const attribute for talloc()-ed memory in
> many cases.
> 
> Change the related prototypes to use "const void *" instead of
> "void *".
> 
> Signed-off-by: Juergen Gross <jgross@suse.com>
> ---
> V3:
> - new patch
> ---
>   tools/xenstore/talloc.c | 8 ++++----
>   tools/xenstore/talloc.h | 4 ++--
>   2 files changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/tools/xenstore/talloc.c b/tools/xenstore/talloc.c
> index 23c3a23b19..4f08dbec59 100644
> --- a/tools/xenstore/talloc.c
> +++ b/tools/xenstore/talloc.c
> @@ -319,7 +319,7 @@ static int talloc_unreference(const void *context, const void *ptr)
>     remove a specific parent context from a pointer. This is a more
>     controlled varient of talloc_free()
>   */
> -int talloc_unlink(const void *context, void *ptr)
> +int talloc_unlink(const void *context, const void *ptr)
>   {
>   	struct talloc_chunk *tc_p, *new_p;
>   	void *new_parent;
> @@ -499,7 +499,7 @@ void *talloc_init(const char *fmt, ...)
>     should probably not be used in new code. It's in here to keep the talloc
>     code consistent across Samba 3 and 4.
>   */
> -static void talloc_free_children(void *ptr)
> +static void talloc_free_children(const void *ptr)
>   {
>   	struct talloc_chunk *tc;
>   
> @@ -539,7 +539,7 @@ static void talloc_free_children(void *ptr)
>      will not be freed if the ref_count is > 1 or the destructor (if
>      any) returns non-zero
>   */
> -int talloc_free(void *ptr)
> +int talloc_free(const void *ptr)
>   {
>   	int saved_errno = errno;
>   	struct talloc_chunk *tc;
> @@ -571,7 +571,7 @@ int talloc_free(void *ptr)
>   			goto err;
>   		}
>   		tc->destructor = (talloc_destructor_t)-1;
> -		if (d(ptr) == -1) {
> +		if (d((void *)ptr) == -1) {

AFAICT, you can't propagate the const because the destructor may need to 
modify the content. I guess this is a necessary evil here but it would 
be good to document it.

Cheers,
Jürgen Groß July 28, 2023, 6:15 a.m. UTC | #2
On 27.07.23 23:21, Julien Grall wrote:
> Hi Juergen,
> 
> On 24/07/2023 12:02, Juergen Gross wrote:
>> With talloc_free() and related functions not taking a ponter to const
> 
> typo: s/ponter/pointer/
> 
>> it is tedious to use the const attribute for talloc()-ed memory in
>> many cases.
>>
>> Change the related prototypes to use "const void *" instead of
>> "void *".
>>
>> Signed-off-by: Juergen Gross <jgross@suse.com>
>> ---
>> V3:
>> - new patch
>> ---
>>   tools/xenstore/talloc.c | 8 ++++----
>>   tools/xenstore/talloc.h | 4 ++--
>>   2 files changed, 6 insertions(+), 6 deletions(-)
>>
>> diff --git a/tools/xenstore/talloc.c b/tools/xenstore/talloc.c
>> index 23c3a23b19..4f08dbec59 100644
>> --- a/tools/xenstore/talloc.c
>> +++ b/tools/xenstore/talloc.c
>> @@ -319,7 +319,7 @@ static int talloc_unreference(const void *context, const 
>> void *ptr)
>>     remove a specific parent context from a pointer. This is a more
>>     controlled varient of talloc_free()
>>   */
>> -int talloc_unlink(const void *context, void *ptr)
>> +int talloc_unlink(const void *context, const void *ptr)
>>   {
>>       struct talloc_chunk *tc_p, *new_p;
>>       void *new_parent;
>> @@ -499,7 +499,7 @@ void *talloc_init(const char *fmt, ...)
>>     should probably not be used in new code. It's in here to keep the talloc
>>     code consistent across Samba 3 and 4.
>>   */
>> -static void talloc_free_children(void *ptr)
>> +static void talloc_free_children(const void *ptr)
>>   {
>>       struct talloc_chunk *tc;
>> @@ -539,7 +539,7 @@ static void talloc_free_children(void *ptr)
>>      will not be freed if the ref_count is > 1 or the destructor (if
>>      any) returns non-zero
>>   */
>> -int talloc_free(void *ptr)
>> +int talloc_free(const void *ptr)
>>   {
>>       int saved_errno = errno;
>>       struct talloc_chunk *tc;
>> @@ -571,7 +571,7 @@ int talloc_free(void *ptr)
>>               goto err;
>>           }
>>           tc->destructor = (talloc_destructor_t)-1;
>> -        if (d(ptr) == -1) {
>> +        if (d((void *)ptr) == -1) {
> 
> AFAICT, you can't propagate the const because the destructor may need to modify 
> the content. I guess this is a necessary evil here but it would be good to 
> document it.

Okay, will add a comment.


Juergen
diff mbox series

Patch

diff --git a/tools/xenstore/talloc.c b/tools/xenstore/talloc.c
index 23c3a23b19..4f08dbec59 100644
--- a/tools/xenstore/talloc.c
+++ b/tools/xenstore/talloc.c
@@ -319,7 +319,7 @@  static int talloc_unreference(const void *context, const void *ptr)
   remove a specific parent context from a pointer. This is a more
   controlled varient of talloc_free()
 */
-int talloc_unlink(const void *context, void *ptr)
+int talloc_unlink(const void *context, const void *ptr)
 {
 	struct talloc_chunk *tc_p, *new_p;
 	void *new_parent;
@@ -499,7 +499,7 @@  void *talloc_init(const char *fmt, ...)
   should probably not be used in new code. It's in here to keep the talloc
   code consistent across Samba 3 and 4.
 */
-static void talloc_free_children(void *ptr)
+static void talloc_free_children(const void *ptr)
 {
 	struct talloc_chunk *tc;
 
@@ -539,7 +539,7 @@  static void talloc_free_children(void *ptr)
    will not be freed if the ref_count is > 1 or the destructor (if
    any) returns non-zero
 */
-int talloc_free(void *ptr)
+int talloc_free(const void *ptr)
 {
 	int saved_errno = errno;
 	struct talloc_chunk *tc;
@@ -571,7 +571,7 @@  int talloc_free(void *ptr)
 			goto err;
 		}
 		tc->destructor = (talloc_destructor_t)-1;
-		if (d(ptr) == -1) {
+		if (d((void *)ptr) == -1) {
 			tc->destructor = d;
 			goto err;
 		}
diff --git a/tools/xenstore/talloc.h b/tools/xenstore/talloc.h
index 518fcac151..32cee63d4d 100644
--- a/tools/xenstore/talloc.h
+++ b/tools/xenstore/talloc.h
@@ -92,7 +92,7 @@  void *_talloc(const void *context, size_t size);
 void talloc_set_destructor(const void *ptr, int (*destructor)(void *));
 void talloc_increase_ref_count(const void *ptr);
 void *talloc_reference(const void *context, const void *ptr);
-int talloc_unlink(const void *context, void *ptr);
+int talloc_unlink(const void *context, const void *ptr);
 void talloc_set_name(const void *ptr, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
 void talloc_set_name_const(const void *ptr, const char *name);
 void *talloc_named(const void *context, size_t size, 
@@ -103,7 +103,7 @@  void *talloc_check_name(const void *ptr, const char *name);
 void talloc_report_depth(const void *ptr, FILE *f, int depth);
 void *talloc_parent(const void *ptr);
 void *talloc_init(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
-int talloc_free(void *ptr);
+int talloc_free(const void *ptr);
 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name);
 void *talloc_steal(const void *new_ctx, const void *ptr);
 off_t talloc_total_size(const void *ptr);