summaryrefslogtreecommitdiff
path: root/src/allocators.c
blob: c0107cc00f39a4e3c76eaf664b7c0676f197e470 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
#include <gtk/gtk.h>
#include <math.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

#include "memfault.h"

#define _(x) x

struct _sum_allocator {
    guint index;
    const gchar *frame;
    guint64 size;
    guint count;
    guint n_pages;
    guint min, max;
    Block **blocks;
    gboolean sorted;
    struct _sum_allocator *next, *ht_next;
};

typedef struct _allocators_store {
    GObject object;

    GPtrArray *allocators;
    struct {
	guint size;
	struct _sum_allocator **nodes;
    } sums;

    Chunk *sum_allocator_chunks;
} AllocatorsStore;

typedef struct _allocators_store_class {
    GObjectClass parent_class;
} AllocatorsStoreClass;


struct _allocators {
    GtkTreeView tv;

    Block *blocks;
    Block **blocks_mem;
    AllocatorsStore *store;
    guint reload;
};
typedef struct _allocators_class {
    GtkTreeViewClass parent_class;
} AllocatorsClass;

static GType
allocators_get_type (void);

G_DEFINE_TYPE (Allocators, allocators, GTK_TYPE_TREE_VIEW)

static void
allocators_store_tree_model_init (GtkTreeModelIface *iface);

static GType
allocators_store_get_type (void);

G_DEFINE_TYPE_WITH_CODE (AllocatorsStore, allocators_store, G_TYPE_OBJECT,
	G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
	                       allocators_store_tree_model_init))
enum {
    FRAME,
    COUNT,
    NPAGES,
    SIZE,
    N_COLUMNS,
};

enum {
    PROP_0 = 0,
    PROP_BLOCKS,
};

static struct _sum_allocator *
_sum_allocator_alloc (AllocatorsStore *store)
{
    gpointer mem;
    gsize size = sizeof (struct _sum_allocator);

#ifndef G_ENABLE_DEBUG
    Chunk *c = store->sum_allocator_chunks;
    if (c == NULL || c->used + size > c->size) {
	guint len = size * (32<<10);
	c = g_malloc (sizeof (Chunk) + len);
	c->size = len;
	c->used = 0;
	c->next = store->sum_allocator_chunks;
	store->sum_allocator_chunks = c;
    }

    mem = c->mem + c->used;
    c->used += size;
#else
    mem = g_malloc (size);
#endif

    return mem;
}

static struct _sum_allocator *
sum_get (AllocatorsStore *store, const gchar *fn)
{
    guint index = (GPOINTER_TO_UINT (fn) ^ 6017773) % store->sums.size;
    struct _sum_allocator *sum = store->sums.nodes[index];
    while (sum != NULL && sum->frame != fn)
	sum = sum->ht_next;
    return sum;
}

static gint
block_cmp (gconstpointer A, gconstpointer B)
{
    const Block * const *aa = A, * const *bb = B;
    const Block *a = *aa, *b = *bb;
    const Allocator *Aa = a->allocator, *Ab = b->allocator;
    gint cmp;

    cmp = Ab->time_tail->n_allocs - Aa->time_tail->n_allocs;
    if (cmp)
	return cmp;

    cmp = b->size - a->size;
    if (cmp)
	return cmp;

    return a->addr - b->addr;
}

static gint
_sum_allocators_cmp (gconstpointer A, gconstpointer B)
{
    const struct _sum_allocator * const *aa = A, * const *bb = B;
    const struct _sum_allocator *a = *aa, *b = *bb;
    gint cmp;

    if (b->size == 0)
	return 1;
    if (a->size == 0)
	return -1;

    cmp = b->n_pages * 4096 / b->size * b->count - a->n_pages * 4096 / a->size * a->count;
    if (cmp)
	return cmp;

    cmp = b->count - a->count;
    if (cmp)
	return cmp;

    cmp = b->n_pages - a->n_pages;
    if (cmp)
	return cmp;

    return b->size - a->size;
}

static void
allocators_store_finalize (GObject *obj)
{
    AllocatorsStore *self = (AllocatorsStore *) obj;
    Chunk *c;

    c = self->sum_allocator_chunks;
    self->sum_allocator_chunks = NULL;
    while (c != NULL) {
	Chunk *next = c->next;
	g_free (c);
	c = next;
    }

    g_ptr_array_free (self->allocators, TRUE);
    g_free (self->sums.nodes);

    G_OBJECT_CLASS (allocators_store_parent_class)->finalize (obj);
}

static void
allocators_store_class_init (AllocatorsStoreClass *klass)
{
    GObjectClass *object_class = (GObjectClass *) klass;

    object_class->finalize = allocators_store_finalize;
}


/* GtkTreeModelIface */

static GtkTreeModelFlags
allocators_store_get_flags (GtkTreeModel *tree_model)
{
    return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
}

static gint
allocators_store_get_n_columns (GtkTreeModel *tree_model)
{
    return N_COLUMNS;
}

static GType
allocators_store_get_column_type (GtkTreeModel *tree_model,
				  gint          index)
{
    switch (index) {
    case FRAME: return G_TYPE_STRING;
    case SIZE: return G_TYPE_UINT64;
    case COUNT: return G_TYPE_UINT;
    case NPAGES: return G_TYPE_UINT;
    default: return G_TYPE_INVALID;
    }
}

static gboolean
allocators_store_get_iter (GtkTreeModel *tree_model,
			   GtkTreeIter  *iter,
			   GtkTreePath  *path)
{
    AllocatorsStore *self = (AllocatorsStore *) tree_model;
    guint n;

    n = gtk_tree_path_get_depth (path);
    g_return_val_if_fail (n == 1, FALSE);

    n = gtk_tree_path_get_indices (path)[0];
    if (n >= self->allocators->len)
	return FALSE;

    iter->user_data = g_ptr_array_index (self->allocators, n);
    iter->stamp = 1;
    return TRUE;
}

static GtkTreePath *
allocators_store_get_path (GtkTreeModel *tree_model,
			   GtkTreeIter  *iter)
{
    GtkTreePath *path = gtk_tree_path_new ();
    struct _sum_allocator *sum = iter->user_data;
    gtk_tree_path_append_index (path, sum->index);
    return path;
}

static void
allocators_store_get_value (GtkTreeModel *tree_model,
			    GtkTreeIter  *iter,
			    gint          column,
			    GValue       *value)
{
    struct _sum_allocator *sum = iter->user_data;
    switch (column) {
    case FRAME:
	g_value_init (value, G_TYPE_STRING);
	g_value_set_string (value, sum->frame);
	break;

    case SIZE:
	g_value_init (value, G_TYPE_UINT64);
	g_value_set_uint64 (value, sum->size);
	break;

    case COUNT:
	g_value_init (value, G_TYPE_UINT);
	g_value_set_uint (value, sum->count);
	break;

    case NPAGES:
	g_value_init (value, G_TYPE_UINT);
	g_value_set_uint (value, sum->n_pages);
	break;
    }
}

static gboolean
allocators_store_iter_next (GtkTreeModel  *tree_model,
			    GtkTreeIter   *iter)
{
    AllocatorsStore *self = (AllocatorsStore *) tree_model;
    struct _sum_allocator *sum = iter->user_data;
    GPtrArray *array = self->allocators;
    guint index = sum->index;

    if (index >= array->len - 1)
	return FALSE;

    iter->user_data = g_ptr_array_index (array, index + 1);
    return TRUE;
}

static gboolean
allocators_store_iter_children (GtkTreeModel *tree_model,
			        GtkTreeIter  *iter,
			        GtkTreeIter  *parent)
{
    return parent == NULL;
}

static gboolean
allocators_store_iter_has_child (GtkTreeModel *tree_model,
			         GtkTreeIter  *iter)
{
    return FALSE;
}

static gint
allocators_store_iter_n_children (GtkTreeModel *tree_model,
				  GtkTreeIter  *iter)
{
    return 0;
}

static gboolean
allocators_store_iter_nth_child (GtkTreeModel *tree_model,
			         GtkTreeIter  *iter,
				 GtkTreeIter  *parent,
				 gint          n)
{
    AllocatorsStore *self = (AllocatorsStore *) tree_model;
    GPtrArray *array;

    if (parent != NULL)
	return FALSE;

    array = self->allocators;
    if ((guint) n >= array->len)
	return FALSE;

    iter->stamp = 1;
    iter->user_data = g_ptr_array_index (array, n);
    return TRUE;
}

static gboolean
allocators_store_iter_parent (GtkTreeModel *tree_model,
			      GtkTreeIter  *iter,
			      GtkTreeIter  *child)
{
    return FALSE;
}

static void
allocators_store_tree_model_init (GtkTreeModelIface *iface)
{
    iface->get_flags = allocators_store_get_flags;
    iface->get_n_columns = allocators_store_get_n_columns;
    iface->get_column_type = allocators_store_get_column_type;
    iface->get_iter = allocators_store_get_iter;
    iface->get_path = allocators_store_get_path;
    iface->get_value = allocators_store_get_value;
    iface->iter_next = allocators_store_iter_next;
    iface->iter_children = allocators_store_iter_children;
    iface->iter_has_child = allocators_store_iter_has_child;
    iface->iter_n_children = allocators_store_iter_n_children;
    iface->iter_nth_child = allocators_store_iter_nth_child;
    iface->iter_parent = allocators_store_iter_parent;
}

static void
allocators_store_init (AllocatorsStore *self)
{
    self->allocators = g_ptr_array_new ();
}

static void
_sum_allocators_compute_pages_hierachically (struct _sum_allocator *sum,
	                                     guint pagesize,
					     guint count)
{
    struct pages {
	guint pages[1024];
    } **pdt, *pde;
    guint n, per_level;

    per_level = 32 * 1024;
    count = count / per_level + 1;
    pdt = g_new0 (struct pages *, count);

    for (n = 0; n < sum->count; n++) {
	Block *block = sum->blocks[n];
	guint p = (block->addr - sum->min) / pagesize;
	guint last_page = (block->addr + block->size - sum->min) / pagesize;
	guint *pages, offset;

	pde = pdt[p / per_level];
	if (pde == NULL) {
	    pde = g_new0 (struct pages, 1);
	    pdt[p / per_level] = pde;
	}
	pages = pde->pages;
	offset = p &~ (per_level - 1);

	do {
	    guint po = p - offset;
	    if ((pages[po / 32] & (1 << (po & 31 ))) == 0) {
		pages [po / 32] |= 1 << (po & 31);
		sum->n_pages++;
	    }
	    if (++p >= last_page)
		break;

	    if ((p & (per_level - 1)) == 0) {
		pde = pdt[p / per_level];
		if (pde == NULL) {
		    pde = g_new0 (struct pages, 1);
		    pdt[p / per_level] = pde;
		}
		pages = pde->pages;
		offset = p;
	    }
	} while (TRUE);
    }

    for (n = 0; n < count; n++) {
	if (pdt[n])
	    g_free (pdt[n]);
    }
    g_free (pdt);
}

static void
_sum_allocators_add_to_array (struct _sum_allocator *sum, GPtrArray *array)
{
    guint pagesize = sysconf (_SC_PAGESIZE);
    guint pages[1024];
    guint n = (sum->max - sum->min + 32) / 32;

    g_ptr_array_add (array, sum);

    if (n > G_N_ELEMENTS (pages))
	return _sum_allocators_compute_pages_hierachically (sum, pagesize, n);

    memset (pages, 0, n);
    for (n = 0; n < sum->count; n++) {
	Block *block = sum->blocks[n];
	guint p = (block->addr - sum->min) / pagesize;
	guint last_page = (block->addr + block->size - sum->min) / pagesize;
	do {
	    if ((pages[p / 32] & (1 << (p & 31 ))) == 0) {
		pages [p / 32] |= 1 << (p & 31);
		sum->n_pages++;
	    }
	} while (++p < last_page);
    }
}

static void
allocators_set_blocks (Allocators *self, Block *blocks)
{
    App *app = app_get ((GtkWidget *) self);
    Block *b, **block_mem;
    AllocatorsStore *store;
    struct _sum_allocator *sum, *sums = NULL;
    guint n, count;

    if (self->store != NULL)
	g_object_unref (self->store);

    self->blocks = blocks;

    count = 0;
    store = g_object_new (allocators_store_get_type (), NULL);
    store->sums.size = g_spaced_primes_closest (
	    app_get_number_of_allocators (app));
    store->sums.nodes = g_new0 (struct _sum_allocator *, store->sums.size);

    for (b = blocks; b != NULL; b = b->next) {
	Allocator *A = b->allocator;
	sum = sum_get (store, A->alloc_fn);

	if (sum == NULL) {
	    guint index;

	    sum = _sum_allocator_alloc (store);
	    sum->size = 0;
	    sum->count = 0;
	    sum->n_pages = 0;
	    sum->min = (guint) -1;
	    sum->max = 0;
	    sum->sorted = FALSE;
	    sum->next = sums;
	    sums = sum;

	    sum->frame = A->alloc_fn;

	    index = (GPOINTER_TO_UINT (A->alloc_fn) ^ 6017773) % store->sums.size;
	    sum->ht_next = store->sums.nodes[index];
	    store->sums.nodes[index] = sum;
	}

	if (b->addr < sum->min)
	    sum->min = b->addr;
	if (b->addr + b->size > sum->max)
	    sum->max = b->addr + b->size;

	sum->size += b->size;
	sum->count++;
	count++;
    }

    self->blocks_mem = g_renew (Block *, self->blocks_mem, count);
    block_mem = self->blocks_mem;
    for (sum = sums; sum != NULL; sum = sum->next) {
	sum->blocks = block_mem;
	block_mem += sum->count;
	sum->count = 0;
    }

    for (b = blocks; b != NULL; b = b->next) {
	Allocator *A = b->allocator;
	sum = sum_get (store, A->alloc_fn);
	sum->blocks[sum->count++] = b;
    }

    for (sum = sums; sum != NULL; sum = sum->next)
	_sum_allocators_add_to_array (sum, store->allocators);

    g_ptr_array_sort (store->allocators, _sum_allocators_cmp);
    for (n = 0; n < store->allocators->len; n++) {
	struct _sum_allocator *sum = g_ptr_array_index (store->allocators, n);
	sum->index = n;
    }
    self->store = store;

    gtk_tree_view_set_model (&self->tv, GTK_TREE_MODEL (self->store));
}


static void
allocators_set_property (GObject *obj, guint id, const GValue *v, GParamSpec *spec)
{
    Allocators *self = (Allocators *) obj;
    switch (id) {
    case PROP_BLOCKS:
	allocators_set_blocks (self, g_value_get_pointer (v));
	break;
    default:
	G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, spec);
	break;
    }
}

static void
allocators_get_property (GObject *obj, guint id, GValue *v, GParamSpec *spec)
{
    Allocators *self = (Allocators *) obj;
    switch (id) {
    case PROP_BLOCKS:
	g_value_set_pointer (v, self->blocks);
	break;
    default:
	G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, spec);
	break;
    }
}

static gboolean
allocators_query_tooltip (GtkWidget  *widget,
			  gint        x,
			  gint        y,
			  gboolean    keyboard_tip,
			  GtkTooltip *tooltip)
{
    Allocators *self = (Allocators *) widget;
    struct _sum_allocator *sum;
    Allocator *A;
    Block *block;
    GtkTreePath *path;
    GtkTreeViewColumn *column;
    GtkTreeIter iter;
    gint cell_x, cell_y;
    GString *string;
    gchar *text;
    guint n, m;
    gchar calls[80];
    gint len;

    gtk_tree_view_convert_widget_to_bin_window_coords (&self->tv, x, y, &x, &y);
    if (! gtk_tree_view_get_path_at_pos (&self->tv, x, y,
	                                 &path, &column, &cell_x, &cell_y))
	return FALSE;

    gtk_tree_view_set_tooltip_row (&self->tv, tooltip, path);

    gtk_tree_model_get_iter ((GtkTreeModel *) self->store, &iter, path);
    gtk_tree_path_free (path);

    sum = iter.user_data;
    if (! sum->sorted) {
	qsort (sum->blocks, sum->count, sizeof (Block *), block_cmp);
	sum->sorted = TRUE;
    }

    block = sum->blocks[0];
    A = block->allocator;
    string = g_string_new ("Most frequent allocation callsite, ");
    len = g_snprintf (calls + 40, 40, "%d", A->time_tail->n_allocs);
    pretty_print_number (calls + 40, len, calls);
    g_string_append (string, calls);
    g_string_append (string, A->time_tail->n_allocs > 1 ? " calls:" : " call:");

    for (n = 0; n < A->n_frames; n++)
	if (A->alloc_fn == A->functions[n])
	    break;

    for (m = n; m < MIN (n + 8, A->n_frames); m++) {
	if (strcmp (A->functions_srcloc[m], A->functions_srcloc[m-1])) {
	    g_string_append_c (string, '\n');
	    g_string_append_c (string, '\t');
	    g_string_append (string, A->functions_srcloc[m]);
	} else
	    n++;
    }
    text = g_string_free (string, FALSE);
    gtk_tooltip_set_text (tooltip, text);
    g_free (text);
    return TRUE;
}

static gboolean
reload_blocks (gpointer data)
{
    Allocators *self = data;
    GtkWidget *toplevel = gtk_widget_get_toplevel (data);

    if (toplevel->window != NULL) {
	GdkCursor *cursor = gdk_cursor_new (GDK_WATCH);
	gdk_window_set_cursor (toplevel->window, cursor);
	gdk_cursor_unref (cursor);
	gdk_flush ();
    }

    allocators_set_blocks (self, self->blocks);

    if (toplevel->window != NULL)
	gdk_window_set_cursor (toplevel->window, NULL);

    self->reload = 0;
    return FALSE;
}

static gboolean
allocators_button_press (GtkWidget *widget, GdkEventButton *ev)
{
    Allocators *self = (Allocators *) widget;
    if (ev->button == 3) {
	GtkTreePath *path;
	GtkTreeViewColumn *column;
	GtkTreeIter iter;
	gint cell_x, cell_y;

	if (gtk_tree_view_get_path_at_pos (&self->tv, ev->x, ev->y,
		    &path, &column, &cell_x, &cell_y)) {
	    struct _sum_allocator *sum;
	    GtkWidget *dialog, *entry;

	    gtk_tree_model_get_iter ((GtkTreeModel *) self->store, &iter, path);
	    gtk_tree_path_free (path);

	   sum	= iter.user_data;
	   dialog = gtk_dialog_new_with_buttons (
		    "Add an allocation function",
		    GTK_WINDOW (gtk_widget_get_toplevel (widget)),
		    GTK_DIALOG_DESTROY_WITH_PARENT |
		    GTK_DIALOG_NO_SEPARATOR,
		    GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
		    GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
		    NULL);
	    entry = gtk_entry_new ();

	    gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
	    gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);

	    gtk_entry_set_text (GTK_ENTRY (entry), sum->frame);
	    gtk_container_add (
		    GTK_CONTAINER (GTK_DIALOG (dialog)->vbox),
		    entry);
	    gtk_widget_show (entry);

	    if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
		const char *pattern = gtk_entry_get_text (GTK_ENTRY (entry));
		GError *error = NULL;
		if (! app_add_alloc_fn (app_get (widget), pattern, &error)) {
		    GtkWidget *msg = gtk_message_dialog_new (
			    GTK_WINDOW (gtk_widget_get_toplevel (widget)),
			    GTK_DIALOG_DESTROY_WITH_PARENT |
			    GTK_DIALOG_NO_SEPARATOR,
			    GTK_MESSAGE_ERROR,
			    GTK_BUTTONS_OK,
			    "Failed to compile '%s' into a regex: %s",
			    pattern, error->message);
		    g_error_free (error);
		    gtk_dialog_run (GTK_DIALOG (msg));
		    gtk_widget_destroy (msg);
		} else if (self->reload == 0)
		    self->reload = gdk_threads_add_idle (reload_blocks, self);
	    }
	    gtk_widget_destroy (dialog);
	}

	return TRUE;
    }

    if (GTK_WIDGET_CLASS (allocators_parent_class)->button_press_event)
	return GTK_WIDGET_CLASS (allocators_parent_class)->button_press_event (widget, ev);

    return FALSE;
}

static void
allocators_class_init (AllocatorsClass *klass)
{
    GObjectClass *object_class = (GObjectClass *) klass;
    GtkWidgetClass *widget_class = (GtkWidgetClass *) klass;

    object_class->set_property = allocators_set_property;
    object_class->get_property = allocators_get_property;

    widget_class->query_tooltip = allocators_query_tooltip;
    widget_class->button_press_event = allocators_button_press;

    g_object_class_install_property (object_class,
	    PROP_BLOCKS,
	    g_param_spec_pointer ("blocks",
		_("blocks"),
		_("Allocated blocks"),
		G_PARAM_STATIC_NICK  |
		G_PARAM_STATIC_BLURB |
		G_PARAM_STATIC_NAME  |
		G_PARAM_READWRITE));
}

static void
allocators_init (Allocators *self)
{
    GtkTreeStore *store;
    GtkTreeViewColumn *column;
    GtkCellRenderer *renderer;

    renderer = gtk_cell_renderer_text_new ();
    g_object_set (G_OBJECT (renderer),
	    "ellipsize", PANGO_ELLIPSIZE_END,
	    "ellipsize-set", TRUE,
	    "width-chars", 50,
	    NULL);
    column = gtk_tree_view_column_new_with_attributes ("Allocator",
	    renderer, "text", FRAME, NULL);
    gtk_tree_view_append_column (&self->tv, column);

    renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes ("Count",
	    renderer, NULL);
    gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (column), renderer,
	    cell_layout_pretty_print_uint, GUINT_TO_POINTER (COUNT), NULL);
    gtk_tree_view_append_column (&self->tv, column);

    renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes ("Size",
	    renderer, NULL);
    gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (column), renderer,
	    cell_layout_pretty_print_uint64, GUINT_TO_POINTER (SIZE), NULL);
    gtk_tree_view_append_column (&self->tv, column);

    renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes ("Pages",
	    renderer, NULL);
    gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (column), renderer,
	    cell_layout_pretty_print_uint, GUINT_TO_POINTER (NPAGES), NULL);
    gtk_tree_view_append_column (&self->tv, column);

    gtk_tree_view_set_grid_lines (&self->tv, GTK_TREE_VIEW_GRID_LINES_VERTICAL);

    gtk_tree_selection_set_mode (gtk_tree_view_get_selection (&self->tv),
	                         GTK_SELECTION_MULTIPLE);

    gtk_widget_set_has_tooltip (GTK_WIDGET (self), TRUE);

    /* insert a dummy model */
    store = gtk_tree_store_new (N_COLUMNS,
	    G_TYPE_STRING, G_TYPE_UINT, G_TYPE_UINT64, G_TYPE_UINT);
    gtk_tree_view_set_model (&self->tv, GTK_TREE_MODEL (store));
    g_object_unref (store);
}

GtkWidget *
allocators_new (void)
{
    return g_object_new (allocators_get_type (), NULL);
}

GSList *
allocators_get_blocks_for_iter (Allocators *self,
	                        GtkTreeIter *iter,
				GSList *blocks)
{
    struct _sum_allocator *sum = iter->user_data;
    guint n;
    for (n = 0; n < sum->count; n++)
	blocks = g_slist_prepend (blocks, sum->blocks[n]);

    return blocks;
}

void
allocators_select_blocks (Allocators *self, GSList *blocks)
{
    GtkTreeModel *model = (GtkTreeModel *) self->store;
    GtkTreeSelection *selection = gtk_tree_view_get_selection (&self->tv);
    GtkTreePath *first = NULL;

    gtk_tree_selection_unselect_all (selection);

    if (blocks == NULL)
	return;

    do {
	Block *b = blocks->data;
	GtkTreeIter iter;
	GtkTreePath *path;

	iter.user_data = sum_get (self->store, b->allocator->alloc_fn);
	iter.stamp = 1;
	path = gtk_tree_model_get_path (model, &iter);
	gtk_tree_view_expand_to_path (&self->tv, path);

	if (first == NULL) {
	    first = path;
	} else if (gtk_tree_path_compare (path, first) < 0){
	    gtk_tree_path_free (first);
	    first = path;
	} else
	    gtk_tree_path_free (path);

	gtk_tree_selection_select_iter (selection, &iter);
    } while ((blocks = blocks->next) != NULL);

    gtk_tree_view_scroll_to_cell (&self->tv, first, NULL, FALSE, 0., 0.);
    gtk_tree_path_free (first);
}