1
/* -*- mode: C; c-basic-offset: 3; indent-tabs-mode: nil; -*- */
3
This file is part of drd, a thread error detector.
5
Copyright (C) 2006-2011 Bart Van Assche <bvanassche@acm.org>.
7
This program is free software; you can redistribute it and/or
8
modify it under the terms of the GNU General Public License as
9
published by the Free Software Foundation; either version 2 of the
10
License, or (at your option) any later version.
12
This program is distributed in the hope that it will be useful, but
13
WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22
The GNU General Public License is contained in the file COPYING.
26
* Block allocator for second-level bitmap nodes. Each node consists of
27
* an OSetGen node and a struct bitmap2. The code below allocates
28
* NODES_PER_CHUNK nodes at a time.
32
#include "drd_basics.h" /* DRD_() */
33
#include "drd_bitmap.h" /* struct bitmap2 */
34
#include "pub_drd_bitmap.h"
35
#include "pub_tool_basics.h" /* Addr, SizeT */
36
#include "pub_tool_libcassert.h" /* tl_assert() */
37
#include "pub_tool_libcbase.h" /* VG_ROUNDUP() */
38
#include "pub_tool_libcprint.h" /* VG_(message)() */
39
#include "pub_tool_mallocfree.h" /* VG_(malloc), VG_(free) */
42
#define NODES_PER_CHUNCK 512
45
/* Local type definitions. */
47
struct block_allocator_chunk {
48
struct block_allocator_chunk* next;
49
struct block_allocator_chunk* prev;
57
/* Local variables. */
59
static SizeT s_root_node_size;
60
static SizeT s_bm2_node_size;
61
static struct block_allocator_chunk* s_first;
64
/* Function definitions. */
67
* Allocate a new chunk and insert it at the start of the doubly-linked list
70
static struct block_allocator_chunk* allocate_new_chunk(void)
72
struct block_allocator_chunk* p;
75
tl_assert(s_bm2_node_size > 0);
77
p = VG_(malloc)("drd.bitmap.bac",
78
sizeof(*p) + NODES_PER_CHUNCK * s_bm2_node_size);
86
p->data = (char*)p + sizeof(*p);
88
p->data_end = (char*)(p->data) + NODES_PER_CHUNCK * s_bm2_node_size;
89
p->first_free = p->data;
90
for (i = 0; i < NODES_PER_CHUNCK - 1; i++)
92
*(void**)((char*)(p->data) + i * s_bm2_node_size)
93
= (char*)(p->data) + (i + 1) * s_bm2_node_size;
95
tl_assert(i == NODES_PER_CHUNCK - 1);
96
*(void**)((char*)(p->data) + i * s_bm2_node_size) = NULL;
101
/** Free a chunk and remove it from the list of chunks. */
102
static void free_chunk(struct block_allocator_chunk* const p)
105
tl_assert(p->nallocated == 0);
110
p->prev->next = p->next;
112
p->next->prev = p->prev;
116
/** Allocate a node. */
117
void* DRD_(bm2_alloc_node)(HChar* const ec, const SizeT szB)
120
* If szB < sizeof(struct bitmap2) then this function has been called to
121
* allocate an AVL tree root node. Otherwise it has been called to allocate
122
* an AVL tree branch or leaf node.
124
if (s_root_node_size == 0)
125
s_root_node_size = szB;
126
if (szB == s_root_node_size)
127
return VG_(malloc)(ec, szB);
131
struct block_allocator_chunk* p;
133
if (s_bm2_node_size == 0)
134
s_bm2_node_size = szB;
136
tl_assert(s_bm2_node_size == szB);
138
for (p = s_first; p; p = p->next)
145
result = p->first_free;
146
p->first_free = *(void**)(p->first_free);
151
allocate_new_chunk();
156
void DRD_(bm2_free_node)(void* const bm2)
158
struct block_allocator_chunk* p;
162
if (s_bm2_node_size > 0) {
163
for (p = s_first; p; p = p->next) {
164
if (p->data <= bm2 && bm2 < p->data_end) {
165
/* Free a non-root AVL tree node. */
166
tl_assert(((char*)bm2 - (char*)(p->data)) % s_bm2_node_size == 0);
167
*(void**)bm2 = p->first_free;
169
tl_assert(p->nallocated >= 1);
170
if (--(p->nallocated) == 0)
176
/* Free the memory that was allocated for an AVL tree root node. */