From: Bart Van Assche Date: Wed, 18 Jan 2012 09:46:57 +0000 (+0000) Subject: drd: Switch to new pool allocator X-Git-Tag: svn/VALGRIND_3_8_0~509 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6cfdbe2eabdf8597a7c573c2b7867a0ebaf1c5c5;p=thirdparty%2Fvalgrind.git drd: Switch to new pool allocator git-svn-id: svn://svn.valgrind.org/valgrind/trunk@12344 --- diff --git a/drd/Makefile.am b/drd/Makefile.am index fd635052c3..153b38484c 100644 --- a/drd/Makefile.am +++ b/drd/Makefile.am @@ -54,7 +54,6 @@ endif DRD_SOURCES_COMMON = \ drd_barrier.c \ - drd_bitmap2_node.c \ drd_clientobj.c \ drd_clientreq.c \ drd_cond.c \ diff --git a/drd/drd_bitmap.c b/drd/drd_bitmap.c index afd3654e04..d7e4f91902 100644 --- a/drd/drd_bitmap.c +++ b/drd/drd_bitmap.c @@ -45,6 +45,7 @@ static void bm2_print(const struct bitmap2* const bm2); /* Local variables. */ +static OSet* s_bm2_set_template; static ULong s_bitmap_creation_count; static ULong s_bitmap_merge_count; static ULong s_bitmap2_merge_count; @@ -52,6 +53,21 @@ static ULong s_bitmap2_merge_count; /* Function definitions. */ +void DRD_(bm_module_init)(void) +{ + tl_assert(!s_bm2_set_template); + s_bm2_set_template + = VG_(OSetGen_Create_With_Pool)(0, 0, VG_(malloc), "drd.bitmap.bn.2", + VG_(free), 512, sizeof(struct bitmap2)); +} + +void DRD_(bm_module_cleanup)(void) +{ + tl_assert(s_bm2_set_template); + VG_(OSetGen_Destroy)(s_bm2_set_template); + s_bm2_set_template = NULL; +} + struct bitmap* DRD_(bm_new)() { struct bitmap* bm; @@ -90,8 +106,7 @@ void DRD_(bm_init)(struct bitmap* const bm) bm->cache[i].a1 = ~(UWord)1; bm->cache[i].bm2 = 0; } - bm->oset = VG_(OSetGen_Create)(0, 0, DRD_(bm2_alloc_node), - "drd.bitmap.bn.2", DRD_(bm2_free_node)); + bm->oset = VG_(OSetGen_EmptyClone)(s_bm2_set_template); s_bitmap_creation_count++; } diff --git a/drd/drd_bitmap2_node.c b/drd/drd_bitmap2_node.c deleted file mode 100644 index 3deb644c20..0000000000 --- a/drd/drd_bitmap2_node.c +++ /dev/null @@ -1,177 +0,0 @@ -/* - This file is part of drd, a thread error detector. - - Copyright (C) 2006-2011 Bart Van Assche . - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307, USA. - - The GNU General Public License is contained in the file COPYING. -*/ - -/* - * Block allocator for second-level bitmap nodes. Each node consists of - * an OSetGen node and a struct bitmap2. The code below allocates - * NODES_PER_CHUNK nodes at a time. - */ - - -#include "drd_basics.h" /* DRD_() */ -#include "drd_bitmap.h" /* struct bitmap2 */ -#include "pub_drd_bitmap.h" -#include "pub_tool_basics.h" /* Addr, SizeT */ -#include "pub_tool_libcassert.h" /* tl_assert() */ -#include "pub_tool_libcbase.h" /* VG_ROUNDUP() */ -#include "pub_tool_libcprint.h" /* VG_(message)() */ -#include "pub_tool_mallocfree.h" /* VG_(malloc), VG_(free) */ - - -#define NODES_PER_CHUNCK 512 - - -/* Local type definitions. */ - -struct block_allocator_chunk { - struct block_allocator_chunk* next; - struct block_allocator_chunk* prev; - int nallocated; - void* data; - void* data_end; - void* first_free; -}; - - -/* Local variables. */ - -static SizeT s_root_node_size; -static SizeT s_bm2_node_size; -static struct block_allocator_chunk* s_first; - - -/* Function definitions. */ - -/** - * Allocate a new chunk and insert it at the start of the doubly-linked list - * s_first. - */ -static struct block_allocator_chunk* allocate_new_chunk(void) -{ - struct block_allocator_chunk* p; - int i; - - tl_assert(s_bm2_node_size > 0); - - p = VG_(malloc)("drd.bitmap.bac", - sizeof(*p) + NODES_PER_CHUNCK * s_bm2_node_size); - tl_assert(p); - p->next = s_first; - if (s_first) - p->next->prev = p; - s_first = p; - p->prev = 0; - p->nallocated = 0; - p->data = (char*)p + sizeof(*p); - tl_assert(p->data); - p->data_end = (char*)(p->data) + NODES_PER_CHUNCK * s_bm2_node_size; - p->first_free = p->data; - for (i = 0; i < NODES_PER_CHUNCK - 1; i++) - { - *(void**)((char*)(p->data) + i * s_bm2_node_size) - = (char*)(p->data) + (i + 1) * s_bm2_node_size; - } - tl_assert(i == NODES_PER_CHUNCK - 1); - *(void**)((char*)(p->data) + i * s_bm2_node_size) = NULL; - - return p; -} - -/** Free a chunk and remove it from the list of chunks. */ -static void free_chunk(struct block_allocator_chunk* const p) -{ - tl_assert(p); - tl_assert(p->nallocated == 0); - - if (p == s_first) - s_first = p->next; - else if (p->prev) - p->prev->next = p->next; - if (p->next) - p->next->prev = p->prev; - VG_(free)(p); -} - -/** Allocate a node. */ -void* DRD_(bm2_alloc_node)(HChar* const ec, const SizeT szB) -{ - /* - * If szB < sizeof(struct bitmap2) then this function has been called to - * allocate an AVL tree root node. Otherwise it has been called to allocate - * an AVL tree branch or leaf node. - */ - if (s_root_node_size == 0) - s_root_node_size = szB; - if (szB == s_root_node_size) - return VG_(malloc)(ec, szB); - - while (True) - { - struct block_allocator_chunk* p; - - if (s_bm2_node_size == 0) - s_bm2_node_size = szB; - else - tl_assert(s_bm2_node_size == szB); - - for (p = s_first; p; p = p->next) - { - if (p->first_free) - { - void* result; - - p->nallocated++; - result = p->first_free; - p->first_free = *(void**)(p->first_free); - return result; - } - } - - allocate_new_chunk(); - } -} - -/** Free a node. */ -void DRD_(bm2_free_node)(void* const bm2) -{ - struct block_allocator_chunk* p; - - tl_assert(bm2); - - if (s_bm2_node_size > 0) { - for (p = s_first; p; p = p->next) { - if (p->data <= bm2 && bm2 < p->data_end) { - /* Free a non-root AVL tree node. */ - tl_assert(((char*)bm2 - (char*)(p->data)) % s_bm2_node_size == 0); - *(void**)bm2 = p->first_free; - p->first_free = bm2; - tl_assert(p->nallocated >= 1); - if (--(p->nallocated) == 0) - free_chunk(p); - return; - } - } - } - /* Free the memory that was allocated for an AVL tree root node. */ - VG_(free)(bm2); -} diff --git a/drd/drd_main.c b/drd/drd_main.c index 815b5a276a..de8613c0c0 100644 --- a/drd/drd_main.c +++ b/drd/drd_main.c @@ -758,6 +758,8 @@ static void DRD_(fini)(Int exitcode) DRD_(get_mutex_lock_count)()); DRD_(print_malloc_stats)(); } + + DRD_(bm_module_cleanup)(); } static @@ -810,6 +812,8 @@ void drd_pre_clo_init(void) DRD_(register_malloc_wrappers)(drd_start_using_mem_w_ecu, drd_stop_using_nonstack_mem); + DRD_(bm_module_init)(); + DRD_(clientreq_init)(); DRD_(suppression_init)(); diff --git a/drd/pub_drd_bitmap.h b/drd/pub_drd_bitmap.h index 68b5707fec..b0981ec693 100644 --- a/drd/pub_drd_bitmap.h +++ b/drd/pub_drd_bitmap.h @@ -75,6 +75,8 @@ struct bitmap /* Function declarations. */ +void DRD_(bm_module_init)(void); +void DRD_(bm_module_cleanup)(void); struct bitmap* DRD_(bm_new)(void); void DRD_(bm_delete)(struct bitmap* const bm); void DRD_(bm_init)(struct bitmap* const bm); @@ -148,7 +150,4 @@ ULong DRD_(bm_get_bitmap_creation_count)(void); ULong DRD_(bm_get_bitmap2_creation_count)(void); ULong DRD_(bm_get_bitmap2_merge_count)(void); -void* DRD_(bm2_alloc_node)(HChar* const ec, const SizeT szB); -void DRD_(bm2_free_node)(void* const bm2); - #endif /* __PUB_DRD_BITMAP_H */ diff --git a/drd/tests/unit_bitmap.c b/drd/tests/unit_bitmap.c index 891d4e294d..b64c4964cb 100644 --- a/drd/tests/unit_bitmap.c +++ b/drd/tests/unit_bitmap.c @@ -11,7 +11,6 @@ #include "coregrind/m_poolalloc.c" #include "coregrind/m_oset.c" #include "drd/drd_bitmap.c" -#include "drd/drd_bitmap2_node.c" #include "drd/pub_drd_bitmap.h" @@ -344,9 +343,11 @@ int main(int argc, char** argv) fprintf(stderr, "Start of DRD BM unit test.\n"); + DRD_(bm_module_init)(); bm_test1(); bm_test2(); bm_test3(outer_loop_step, inner_loop_step); + DRD_(bm_module_cleanup)(); fprintf(stderr, "End of DRD BM unit test.\n");