BRL-CAD
bu_heap.c
Go to the documentation of this file.
1 /* B U _ H E A P . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2013-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @file bu_heap.c
21  *
22  * Brief description
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <time.h>
32 
33 #include "bu.h"
34 
35 /* this should match what is in heap.c */
36 #define HEAP_BINS 512
37 
38 
39 /*
40  * FIXME: this routine should compare heap with malloc and make sure
41  * it's always faster.
42  */
43 int
44 main (int ac, char *av[])
45 {
46  int i;
47  void *ptr;
48  size_t allocalls = 0;
49  size_t freecalls = 0;
50 
51  if (ac > 1) {
52  fprintf(stderr,"Usage: %s\n", av[0]);
53  return 1;
54  }
55 
56  srand(time(0));
57 
58  for (i=0; i<1024*1024*10; i++) {
59  size_t sz = ((rand() / (double)(RAND_MAX-1)) * (double)HEAP_BINS) + 1;
60  /* bu_log("allocating %d: %zd\n", i, sz); */
61 #ifdef USE_MALLOC
62  ptr = malloc(sz);
63 #else
64  ptr = bu_heap_get(sz);
65 #endif
66  allocalls++;
67 
68 #ifdef USE_MALLOC
69  free(ptr);
70 #else
71  bu_heap_put(ptr, sz);
72 #endif
73  freecalls++;
74  }
75 
76 #ifdef USE_MALLOC
77  free(NULL);
78 #else
79  bu_heap_put(NULL, 1);
80 #endif
81  freecalls++;
82 
83  /* bu_log("calls: %zd, free: %zd\n", allocalls, freecalls); */
84 
85  return 0;
86 }
87 
88 
89 /*
90  * Local Variables:
91  * mode: C
92  * tab-width: 8
93  * indent-tabs-mode: t
94  * c-file-style: "stroustrup"
95  * End:
96  * ex: shiftwidth=4 tabstop=8
97  */
Header file for the BRL-CAD common definitions.
long time(time_t *)
int main(int ac, char *av[])
Definition: bu_heap.c:44
void bu_heap_put(void *ptr, size_t sz)
Definition: heap.c:232
#define HEAP_BINS
Definition: bu_heap.c:36
void * bu_heap_get(size_t sz)
Definition: heap.c:171