This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH v2.2][BZ #11087] Use atomic operations to track memory
- From: OndÅej BÃlka <neleai at seznam dot cz>
- To: Siddhesh Poyarekar <siddhesh at redhat dot com>
- Cc: libc-alpha at sourceware dot org
- Date: Wed, 30 Oct 2013 15:52:15 +0100
- Subject: [PATCH v2.2][BZ #11087] Use atomic operations to track memory
- Authentication-results: sourceware.org; auth=none
- References: <20131017114140 dot GA24230 at domone dot podge> <20131018081535 dot GA5679 at domone dot podge> <20131025085352 dot GG23099 at spoyarek dot pnq dot redhat dot com> <20131025180728 dot GC21615 at domone dot podge> <20131028063236 dot GB1633 at spoyarek dot pnq dot redhat dot com>
On Mon, Oct 28, 2013 at 12:02:36PM +0530, Siddhesh Poyarekar wrote:
> On Fri, Oct 25, 2013 at 08:07:28PM +0200, OndÅej BÃlka wrote:
> > @@ -2822,10 +2821,10 @@ mremap_chunk(mchunkptr p, size_t new_size)
> > assert((p->prev_size == offset));
> > set_head(p, (new_size - offset)|IS_MMAPPED);
> >
> > - mp_.mmapped_mem -= size + offset;
> > - mp_.mmapped_mem += new_size;
> > - if ((unsigned long)mp_.mmapped_mem > (unsigned long)mp_.max_mmapped_mem)
> > - mp_.max_mmapped_mem = mp_.mmapped_mem;
> > + INTERNAL_SIZE_T new;
> > + new = atomic_exchange_and_add (&mp_.mmapped_mem, new_size - size - offset) +
> > + + new_size - size - offset;
> > + atomic_max (&mp_.max_mmapped_mem, new);
> > return p;
> > }
> >
>
> You have an extra plus sign there. Also, the formatting is wrong.
>
> Siddhesh
here.
[BZ #11087]
* malloc/malloc.c (sysmalloc): Compute statistics atomically.
(munmap_chunk): Likewise.
(mremap_chunk): Likewise.
diff --git a/malloc/malloc.c b/malloc/malloc.c
index 1a18c3f..33c50e7 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -2253,7 +2253,6 @@ static void* sysmalloc(INTERNAL_SIZE_T nb, mstate av)
mchunkptr remainder; /* remainder from allocation */
unsigned long remainder_size; /* its size */
- unsigned long sum; /* for updating stats */
size_t pagemask = GLRO(dl_pagesize) - 1;
bool tried_mmap = false;
@@ -2325,12 +2324,12 @@ static void* sysmalloc(INTERNAL_SIZE_T nb, mstate av)
/* update statistics */
- if (++mp_.n_mmaps > mp_.max_n_mmaps)
- mp_.max_n_mmaps = mp_.n_mmaps;
+ int new = atomic_exchange_and_add (&mp_.n_mmaps, 1) + 1;
+ atomic_max (&mp_.max_n_mmaps, new);
- sum = mp_.mmapped_mem += size;
- if (sum > (unsigned long)(mp_.max_mmapped_mem))
- mp_.max_mmapped_mem = sum;
+ unsigned long sum;
+ sum = atomic_exchange_and_add(&mp_.mmapped_mem, size) + size;
+ atomic_max (&mp_.max_mmapped_mem, sum);
check_chunk(av, p);
@@ -2780,8 +2779,8 @@ munmap_chunk(mchunkptr p)
return;
}
- mp_.n_mmaps--;
- mp_.mmapped_mem -= total_size;
+ atomic_decrement (&mp_.n_mmaps);
+ atomic_add (&mp_.mmapped_mem, -total_size);
/* If munmap failed the process virtual memory address space is in a
bad shape. Just leave the block hanging around, the process will
@@ -2822,10 +2821,10 @@ mremap_chunk(mchunkptr p, size_t new_size)
assert((p->prev_size == offset));
set_head(p, (new_size - offset)|IS_MMAPPED);
- mp_.mmapped_mem -= size + offset;
- mp_.mmapped_mem += new_size;
- if ((unsigned long)mp_.mmapped_mem > (unsigned long)mp_.max_mmapped_mem)
- mp_.max_mmapped_mem = mp_.mmapped_mem;
+ INTERNAL_SIZE_T new;
+ new = atomic_exchange_and_add (&mp_.mmapped_mem, new_size - size - offset) +
+ new_size - size - offset;
+ atomic_max (&mp_.max_mmapped_mem, new);
return p;
}