[Mesa-dev] [RFC PATCH] util/ralloc: make mem_ctx parameter const for all r*alloc functions

Gert Wollny gw.fossdev at gmail.com
Fri Dec 8 16:50:11 UTC 2017


This one got lost ... 

Am Donnerstag, den 02.11.2017, 16:46 +0100 schrieb Nicolai Hähnle:
> On 26.10.2017 13:02, Gert Wollny wrote:
> > r(z)alloc_size and get_header both take a "const void *" for
> > mem_ctx,
> > but most of the other functions take a "void *" and also the "new"
> > operator in DECLARE_ALLOC_CXX_OPERATORS_TEMPLATE. The latter makes
> > it
> > impossible to copy-construct from a constant declared
> > class variable my using its mem_ctx like
> > 
> >     void f(const someclass *p)
> >     {
> >         someclass *q = new(p) someclass(*p);
> >         ...
> >     }
> > 
> > and one has to write
> > 
> >     someclass *q = ralloc(p, someclass);
> >     *q = *p;
> > 
> > With this patch declare the mem_ctx parameters as "const void *"
> > for all relevant functions so that a more C++ like deep copy of
> > pointers to classes can be used.
> 
> Well... the const-ness indicates that you're not going to modify an 
> object, but you *are* modifying it here by adding child objects to
> it.

The key function where the ctx object is actually changed by adding a
child is 

  void *
  ralloc_size(const void *ctx, size_t size); 

so the const-ness is already violated. Actually, most of the r(z)alloc
functions take "const void *ctx", if it wasn't like this I would never
have proposed a patch that works around const-correctness.


> Admittedly there are some weird corner cases with const, but I don't 
> think this is the right call.
In any case the proposed patch would only be convenience that makes
some c++ code more c++-ish ... 

Best, 
Gert 



> 
> Cheers,
> Nicolai
> 
> 
> > 
> > Signed-off-by: Gert Wollny <gw.fossdev at gmail.com>
> > ---
> > Submitter has no write access to mesa-git.
> > 
> >   src/util/ralloc.c | 14 +++++++-------
> >   src/util/ralloc.h | 15 ++++++++-------
> >   2 files changed, 15 insertions(+), 14 deletions(-)
> > 
> > diff --git a/src/util/ralloc.c b/src/util/ralloc.c
> > index 42cfa2e391..cd6bbfa9d4 100644
> > --- a/src/util/ralloc.c
> > +++ b/src/util/ralloc.c
> > @@ -597,7 +597,7 @@ typedef struct linear_size_chunk
> > linear_size_chunk;
> >   
> >   /* Allocate the linear buffer with its header. */
> >   static linear_header *
> > -create_linear_node(void *ralloc_ctx, unsigned min_size)
> > +create_linear_node(const void *ralloc_ctx, unsigned min_size)
> >   {
> >      linear_header *node;
> >   
> > @@ -622,7 +622,7 @@ create_linear_node(void *ralloc_ctx, unsigned
> > min_size)
> >   }
> >   
> >   void *
> > -linear_alloc_child(void *parent, unsigned size)
> > +linear_alloc_child(const void *parent, unsigned size)
> >   {
> >      linear_header *first = LINEAR_PARENT_TO_HEADER(parent);
> >      linear_header *latest = first->latest;
> > @@ -657,7 +657,7 @@ linear_alloc_child(void *parent, unsigned size)
> >   }
> >   
> >   void *
> > -linear_alloc_parent(void *ralloc_ctx, unsigned size)
> > +linear_alloc_parent(const void *ralloc_ctx, unsigned size)
> >   {
> >      linear_header *node;
> >   
> > @@ -676,7 +676,7 @@ linear_alloc_parent(void *ralloc_ctx, unsigned
> > size)
> >   }
> >   
> >   void *
> > -linear_zalloc_child(void *parent, unsigned size)
> > +linear_zalloc_child(const void *parent, unsigned size)
> >   {
> >      void *ptr = linear_alloc_child(parent, size);
> >   
> > @@ -686,7 +686,7 @@ linear_zalloc_child(void *parent, unsigned
> > size)
> >   }
> >   
> >   void *
> > -linear_zalloc_parent(void *parent, unsigned size)
> > +linear_zalloc_parent(const void *parent, unsigned size)
> >   {
> >      void *ptr = linear_alloc_parent(parent, size);
> >   
> > @@ -717,7 +717,7 @@ linear_free_parent(void *ptr)
> >   }
> >   
> >   void
> > -ralloc_steal_linear_parent(void *new_ralloc_ctx, void *ptr)
> > +ralloc_steal_linear_parent(const void *new_ralloc_ctx, void *ptr)
> >   {
> >      linear_header *node;
> >   
> > @@ -737,7 +737,7 @@ ralloc_steal_linear_parent(void
> > *new_ralloc_ctx, void *ptr)
> >   }
> >   
> >   void *
> > -ralloc_parent_of_linear_parent(void *ptr)
> > +ralloc_parent_of_linear_parent(const void *ptr)
> >   {
> >      linear_header *node = LINEAR_PARENT_TO_HEADER(ptr);
> >   #ifdef DEBUG
> > diff --git a/src/util/ralloc.h b/src/util/ralloc.h
> > index 05ae8f8407..e5c7993eb5 100644
> > --- a/src/util/ralloc.h
> > +++ b/src/util/ralloc.h
> > @@ -355,6 +355,7 @@ bool ralloc_asprintf_rewrite_tail(char **str,
> > size_t *start,
> >   				  PRINTFLIKE(3, 4);
> >   
> >   /**
> > +
> >    * Rewrite the tail of an existing string, starting at a given
> > index.
> >    *
> >    * Overwrites the contents of *str starting at \p start with
> > newly formatted
> > @@ -433,7 +434,7 @@
> > private:                                                           
> >       \
> >         reinterpret_cast<TYPE *>(p)-
> > >~TYPE();                              \
> >      }                                                             
> >         \
> >  
> > public:                                                            
> >       \
> > -   static void* operator new(size_t size, void
> > *mem_ctx)                 \
> > +   static void* operator new(size_t size, const void
> > *mem_ctx)           \
> >      {                                                             
> >         \
> >         void *p = ALLOC_FUNC(mem_ctx,
> > size);                               \
> >         assert(p !=
> > NULL);                                                 \
> > @@ -474,7 +475,7 @@
> > public:                                                            
> >       \
> >    * \param parent   parent node of the linear allocator
> >    * \param size     size to allocate (max 32 bits)
> >    */
> > -void *linear_alloc_child(void *parent, unsigned size);
> > +void *linear_alloc_child(const void *parent, unsigned size);
> >   
> >   /**
> >    * Allocate a parent node that will hold linear buffers. The
> > returned
> > @@ -484,17 +485,17 @@ void *linear_alloc_child(void *parent,
> > unsigned size);
> >    * \param ralloc_ctx  ralloc context, must not be NULL
> >    * \param size        size to allocate (max 32 bits)
> >    */
> > -void *linear_alloc_parent(void *ralloc_ctx, unsigned size);
> > +void *linear_alloc_parent(const void *ralloc_ctx, unsigned size);
> >   
> >   /**
> >    * Same as linear_alloc_child, but also clears memory.
> >    */
> > -void *linear_zalloc_child(void *parent, unsigned size);
> > +void *linear_zalloc_child(const void *parent, unsigned size);
> >   
> >   /**
> >    * Same as linear_alloc_parent, but also clears memory.
> >    */
> > -void *linear_zalloc_parent(void *ralloc_ctx, unsigned size);
> > +void *linear_zalloc_parent(const void *ralloc_ctx, unsigned size);
> >   
> >   /**
> >    * Free the linear parent node. This will free all child nodes
> > too.
> > @@ -505,12 +506,12 @@ void linear_free_parent(void *ptr);
> >   /**
> >    * Same as ralloc_steal, but steals the linear parent node.
> >    */
> > -void ralloc_steal_linear_parent(void *new_ralloc_ctx, void *ptr);
> > +void ralloc_steal_linear_parent(const void *new_ralloc_ctx, void
> > *ptr);
> >   
> >   /**
> >    * Return the ralloc parent of the linear parent node.
> >    */
> > -void *ralloc_parent_of_linear_parent(void *ptr);
> > +void *ralloc_parent_of_linear_parent(const void *ptr);
> >   
> >   /**
> >    * Same as realloc except that the linear allocator doesn't free
> > child nodes,
> > 
> 
> 


More information about the mesa-dev mailing list