Re: [gtk-list] Memchunks again



On 17 Aug 1999, Soeren Sandmann wrote:

> Soeren Sandmann <sandmann@daimi.au.dk> writes:
> 
> > The following code segfaults. Do you have any idea why? The seqfault
> > seems to go away if SIZE is less than 65513. It also goes away if
> > G_ALLOC_ONLY is changed into G_ALLOC_AND_FREE.
> > 
> > #include <glib.h>
> > 
> > #define SIZE 65513
> > 
> > int
> > main ()
> > {
> >   GMemChunk *chunk;
> >   guint8 *t1, *t2;
> >   gint i;
> > 
> >   chunk = g_mem_chunk_create (guint8[SIZE], 4, G_ALLOC_ONLY);
> > 
> >   t1 = g_chunk_new (guint8, chunk);
> >   for (i=0; i<SIZE; i++)
> >     t1[i] = 'a'; 
> >   t2 = g_chunk_new (guint8, chunk);
> > }
> 
> I think it is a bug in GLib because GMemChunk->area_size in certain
> cases can be smaller than the atom_size. I think the following patch
> fixes it:
> 
> RCS file: /cvs/gnome/glib/gmem.c,v
> retrieving revision 1.15
> diff -u -r1.15 gmem.c
> --- glib/gmem.c 1999/07/24 18:50:55     1.15
> +++ glib/gmem.c 1999/08/17 09:42:28
> @@ -492,13 +492,13 @@
>    mem_chunk->area_size = area_size;
>    if (mem_chunk->area_size > MAX_MEM_AREA)
>      mem_chunk->area_size = MAX_MEM_AREA;
> -  while (mem_chunk->area_size < mem_chunk->atom_size)
> -    mem_chunk->area_size *= 2;
>    
>    rarea_size = mem_chunk->area_size + sizeof (GMemArea) - MEM_AREA_SIZE;
>    rarea_size = g_mem_chunk_compute_size (rarea_size);
> +  while (rarea_size - (sizeof (GMemArea) - MEM_AREA_SIZE) < mem_chunk->atom_size)
> +    rarea_size *= 2;
>    mem_chunk->area_size = rarea_size - (sizeof (GMemArea) - MEM_AREA_SIZE);
> -  
> +
>    /*
>      mem_chunk->area_size -= (sizeof (GMemArea) - MEM_AREA_SIZE);
>      if (mem_chunk->area_size < mem_chunk->atom_size)

hum, is it really neccessary to double the specified area size in that case?
what about simply enssuring that the area size is always a multitude of the
atom size:

--- gmem.c      Tue Aug 17 12:28:06 1999
+++ gmem-fixed.c        Tue Aug 17 12:29:31 1999
@@ -468,9 +468,16 @@
 {
   GRealMemChunk *mem_chunk;
   gulong rarea_size;
-
+
+  g_return_val_if_fail (atom_size > 0, NULL);
+  g_return_val_if_fail (atom_size < MAX_MEM_AREA / 4, NULL);
+  g_return_val_if_fail (area_size >= atom_size, NULL);
+
   ENTER_MEM_CHUNK_ROUTINE();

+  area_size = (area_size + atom_size - 1) / atom_size;
+  area_size *= atom_size;
+
   mem_chunk = g_new (struct _GRealMemChunk, 1);
   mem_chunk->name = name;
   mem_chunk->type = type;
                   

---
ciaoTJ



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]