VirtualBox

source: kBuild/trunk/src/kmk/alloccache.c@ 2132

Last change on this file since 2132 was 2062, checked in by bird, 16 years ago

kmk: Moved the alloccache code into a separate file because it seems gcc has ends up in some inlining / aliasing trouble in free_dep_chain (alloccache_free is suspected to be inlined) with gcc 4.3.2 on S/390.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 KB
Line 
1/* $Id: alloccache.c 2062 2008-11-09 22:18:54Z bird $ */
2/** @file
3 * alloccache - Fixed sized allocation cache.
4 *
5 * The rational for using an allocation cache, is that it is way faster
6 * than malloc+free on most systems. It may be more efficient as well,
7 * depending on the way the heap implementes small allocations. Also,
8 * with the incdep.c code being threaded, all heaps (except for MSC)
9 * ran into severe lock contention issues since both the main thread
10 * and the incdep worker thread was allocating a crazy amount of tiny
11 * allocations (struct dep, struct nameseq, ++).
12 *
13 * Darwin also showed a significant amount of time spent just executing
14 * free(), which is kind of silly. The alloccache helps a bit here too.
15 */
16
17/*
18 * Copyright (c) 2008 knut st. osmundsen <[email protected]>
19 *
20 * This file is part of kBuild.
21 *
22 * kBuild is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 3 of the License, or
25 * (at your option) any later version.
26 *
27 * kBuild is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with kBuild. If not, see <http://www.gnu.org/licenses/>
34 *
35 */
36
37/*******************************************************************************
38* Header Files *
39*******************************************************************************/
40#include "make.h"
41#include "dep.h"
42#include "debug.h"
43#include <assert.h>
44
45
46#ifdef CONFIG_WITH_ALLOC_CACHES
47
48/* Free am item.
49 This was not inlined because of aliasing issues arrising with GCC.
50 It is also in a separate file for this reason (it used to be in misc.c
51 but since free_dep_chain() was using it there, we ran the risk of it
52 being inlined and gcc screwing up). */
53void
54alloccache_free (struct alloccache *cache, void *item)
55{
56 struct alloccache_free_ent *f = (struct alloccache_free_ent *)item;
57#if 0 /*ndef NDEBUG*/
58 struct alloccache_free_ent *c;
59 unsigned int i = 0;
60 for (c = cache->free_head; c != NULL; c = c->next, i++)
61 MY_ASSERT_MSG (c != f && i < 0x10000000,
62 ("i=%u total_count=%u\n", i, cache->total_count));
63#endif
64
65 f->next = cache->free_head;
66 cache->free_head = f;
67 MAKE_STATS(cache->free_count++;);
68}
69
70/* Default allocator. */
71static void *
72alloccache_default_grow_alloc(void *ignore, unsigned int size)
73{
74 return xmalloc (size);
75}
76
77/* Worker for growing the cache. */
78struct alloccache_free_ent *
79alloccache_alloc_grow (struct alloccache *cache)
80{
81 void *item;
82 unsigned int items = (64*1024 - 32) / cache->size;
83 cache->free_start = cache->grow_alloc (cache->grow_arg, items * cache->size);
84 cache->free_end = cache->free_start + items * cache->size;
85 cache->total_count+= items;
86
87#ifndef NDEBUG /* skip the first item so the heap can detect free(). */
88 cache->total_count--;
89 cache->free_start += cache->size;
90#endif
91
92 item = cache->free_start;
93 cache->free_start += cache->size;
94 /* caller counts */
95 return (struct alloccache_free_ent *)item;
96}
97
98/* List of alloc caches, for printing. */
99static struct alloccache *alloccache_head = NULL;
100
101/* Initializes an alloc cache */
102void
103alloccache_init (struct alloccache *cache, unsigned int size, const char *name,
104 void *(*grow_alloc)(void *grow_arg, unsigned int size), void *grow_arg)
105{
106 unsigned act_size;
107
108 /* ensure OK alignment and min sizeof (struct alloccache_free_ent). */
109 if (size <= sizeof (struct alloccache_free_ent))
110 act_size = sizeof (struct alloccache_free_ent);
111 else if (size <= 32)
112 {
113 act_size = 4;
114 while (act_size < size)
115 act_size <<= 1;
116 }
117 else
118 act_size = (size + 31U) & ~(size_t)31;
119
120 /* align the structure. */
121 cache->free_start = NULL;
122 cache->free_end = NULL;
123 cache->free_head = NULL;
124 cache->size = act_size;
125 cache->total_count = 0;
126 cache->alloc_count = 0;
127 cache->free_count = 0;
128 cache->name = name;
129 cache->grow_arg = grow_arg;
130 cache->grow_alloc = grow_alloc ? grow_alloc : alloccache_default_grow_alloc;
131
132 /* link it. */
133 cache->next = alloccache_head;
134 alloccache_head = cache;
135}
136
137/* Terminate an alloc cache, free all the memory it contains. */
138void
139alloccache_term (struct alloccache *cache,
140 void (*term_free)(void *term_arg, void *ptr, unsigned int size), void *term_arg)
141{
142 /*cache->size = 0;*/
143 (void)cache;
144 (void)term_free;
145 (void)term_arg;
146 /* FIXME: Implement memory segment tracking and cleanup. */
147}
148
149/* Joins to caches, unlinking the 2nd one. */
150void
151alloccache_join (struct alloccache *cache, struct alloccache *eat)
152{
153 assert (cache->size == eat->size);
154
155#if 0 /* probably a waste of time */ /* FIXME: Optimize joining, avoid all list walking. */
156 /* add the free list... */
157 if (eat->free_head)
158 {
159 unsigned int eat_in_use = eat->alloc_count - eat->free_count;
160 unsigned int dst_in_use = cache->alloc_count - cache->free_count;
161 if (!cache->free_head)
162 cache->free_head = eat->free_head;
163 else if (eat->total_count - eat_in_use < cache->total_count - dst_ins_use)
164 {
165 struct alloccache_free_ent *last = eat->free_head;
166 while (last->next)
167 last = last->next;
168 last->next = cache->free_head;
169 cache->free_head = eat->free_head;
170 }
171 else
172 {
173 struct alloccache_free_ent *last = cache->free_head;
174 while (last->next)
175 last = last->next;
176 last->next = eat->free_head;
177 }
178 }
179
180 /* ... and the free space. */
181 while (eat->free_start != eat->free_end)
182 {
183 struct alloccache_free_ent *f = (struct alloccache_free_ent *)eat->free_start;
184 eat->free_start += eat->size;
185 f->next = cache->free_head;
186 cache->free_head = f;
187 }
188
189 /* and statistics */
190 cache->alloc_count += eat->alloc_count;
191 cache->free_count += eat->free_count;
192#else
193 /* and statistics */
194 cache->alloc_count += eat->alloc_count;
195 cache->free_count += eat->free_count;
196#endif
197 cache->total_count += eat->total_count;
198
199 /* unlink and disable the eat cache */
200 if (alloccache_head == eat)
201 alloccache_head = eat->next;
202 else
203 {
204 struct alloccache *cur = alloccache_head;
205 while (cur->next != eat)
206 cur = cur->next;
207 assert (cur && cur->next == eat);
208 cur->next = eat->next;
209 }
210
211 eat->size = 0;
212 eat->free_end = eat->free_start = NULL;
213 eat->free_head = NULL;
214}
215
216/* Print one alloc cache. */
217void
218alloccache_print (struct alloccache *cache)
219{
220 printf (_("\n# Alloc Cache: %s\n"
221 "# Items: size = %-3u total = %-6u"),
222 cache->name, cache->size, cache->total_count);
223 MAKE_STATS(printf (_(" in-use = %-6lu"),
224 cache->alloc_count - cache->free_count););
225 MAKE_STATS(printf (_("\n# alloc calls = %-7lu free calls = %-7lu"),
226 cache->alloc_count, cache->free_count););
227 printf ("\n");
228}
229
230/* Print all alloc caches. */
231void
232alloccache_print_all (void)
233{
234 struct alloccache *cur;
235 puts ("");
236 for (cur = alloccache_head; cur; cur = cur->next)
237 alloccache_print (cur);
238}
239
240#endif /* CONFIG_WITH_ALLOC_CACHES */
241
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette