This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH][BZ #11087] Use atomic operations to track memory
- From: OndÅej BÃlka <neleai at seznam dot cz>
- To: libc-alpha at sourceware dot org
- Date: Thu, 17 Oct 2013 13:41:40 +0200
- Subject: [PATCH][BZ #11087] Use atomic operations to track memory
- Authentication-results: sourceware.org; auth=none
Hi,
I fixed this mostly because ulrich was wrong here in several ways.
Calling added locking to update statistics as too expensive is nonsense
as this is needed only after mmap and mmap + associated minor faults
are much more costy.
Also there is no locking needed, atomic add will do job well.
This bug affects also malloc_stats.
Comments?
* malloc/malloc.c: Accurately track mmaped memory.
diff --git a/malloc/malloc.c b/malloc/malloc.c
index 2938234..cdbd6f3 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -2334,10 +2334,11 @@ static void* sysmalloc(INTERNAL_SIZE_T nb, mstate av)
/* update statistics */
- if (++mp_.n_mmaps > mp_.max_n_mmaps)
+ __sync_fetch_and_add (&mp_.n_mmaps, 1);
+ if (mp_.n_mmaps > mp_.max_n_mmaps)
mp_.max_n_mmaps = mp_.n_mmaps;
- sum = mp_.mmapped_mem += size;
+ sum = __sync_fetch_and_add (&mp_.mmapped_mem, size);
if (sum > (unsigned long)(mp_.max_mmapped_mem))
mp_.max_mmapped_mem = sum;
@@ -2789,8 +2790,8 @@ munmap_chunk(mchunkptr p)
return;
}
- mp_.n_mmaps--;
- mp_.mmapped_mem -= total_size;
+ __sync_fetch_and_sub (&mp_.n_mmaps, 1)
+ __sync_fetch_and_sub (&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
@@ -2831,8 +2832,7 @@ 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;
+ __sync_fetch_and_add (&mp_.mmapped_mem, new_size - size - offset);
if ((unsigned long)mp_.mmapped_mem > (unsigned long)mp_.max_mmapped_mem)
mp_.max_mmapped_mem = mp_.mmapped_mem;
return p;