VirtualBox

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

Last change on this file since 3140 was 3140, checked in by bird, 6 years ago

kmk: Merged in changes from GNU make 4.2.1 (2e55f5e4abdc0e38c1d64be703b446695e70b3b6 / https://git.savannah.gnu.org/git/make.git).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 KB
Line 
1/* $Id: alloccache.c 3140 2018-03-14 21:28:10Z 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-2010 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
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 "makeint.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#ifndef CONFIG_WITH_ALLOCCACHE_DEBUG
57 struct alloccache_free_ent *f = (struct alloccache_free_ent *)item;
58# if 0 /*ndef NDEBUG*/
59 struct alloccache_free_ent *c;
60 unsigned int i = 0;
61 for (c = cache->free_head; c != NULL; c = c->next, i++)
62 MY_ASSERT_MSG (c != f && i < 0x10000000,
63 ("i=%u total_count=%u\n", i, cache->total_count));
64# endif
65
66 f->next = cache->free_head;
67 cache->free_head = f;
68 MAKE_STATS(cache->free_count++;);
69#else /* CONFIG_WITH_ALLOCCACHE_DEBUG */
70
71 struct alloccache **ppcache = (struct alloccache **)item - 1;
72 MY_ASSERT_MSG (*ppcache == cache, ("*ppcache=%p cache=%p item=%p\n", *ppcache, cache, item));
73 *ppcache = NULL;
74 free(ppcache);
75#endif /* CONFIG_WITH_ALLOCCACHE_DEBUG */
76}
77
78/* Default allocator. */
79static void *
80alloccache_default_grow_alloc(void *ignore, unsigned int size)
81{
82 return xmalloc (size);
83}
84
85/* Worker for growing the cache. */
86struct alloccache_free_ent *
87alloccache_alloc_grow (struct alloccache *cache)
88{
89#ifndef CONFIG_WITH_ALLOCCACHE_DEBUG
90 void *item;
91 unsigned int items = (64*1024 - 32) / cache->size;
92 cache->free_start = cache->grow_alloc (cache->grow_arg, items * cache->size);
93 cache->free_end = cache->free_start + items * cache->size;
94 cache->total_count+= items;
95
96# ifndef NDEBUG /* skip the first item so the heap can detect free(). */
97 cache->total_count--;
98 cache->free_start += cache->size;
99# endif
100
101 item = cache->free_start;
102 cache->free_start += cache->size;
103 /* caller counts */
104 return (struct alloccache_free_ent *)item;
105#else /* CONFIG_WITH_ALLOCCACHE_DEBUG */
106
107 /* Prefix the allocation with a cache pointer so alloccahce_free can better
108 catch incorrect calls. */
109 struct alloccache **ppcache = (struct alloccache **)xmalloc(sizeof(*ppcache) + cache->size);
110 *ppcache = cache;
111 return (struct alloccache_free_ent *)(ppcache + 1);
112#endif /* CONFIG_WITH_ALLOCCACHE_DEBUG */
113}
114
115/* List of alloc caches, for printing. */
116static struct alloccache *alloccache_head = NULL;
117
118/* Initializes an alloc cache */
119void
120alloccache_init (struct alloccache *cache, unsigned int size, const char *name,
121 void *(*grow_alloc)(void *grow_arg, unsigned int size), void *grow_arg)
122{
123 unsigned act_size;
124
125 /* ensure OK alignment and min sizeof (struct alloccache_free_ent). */
126 if (size <= sizeof (struct alloccache_free_ent))
127 act_size = sizeof (struct alloccache_free_ent);
128 else if (size <= 32)
129 {
130 act_size = 4;
131 while (act_size < size)
132 act_size <<= 1;
133 }
134 else
135 act_size = (size + 31U) & ~(size_t)31;
136
137 /* align the structure. */
138 cache->free_start = NULL;
139 cache->free_end = NULL;
140 cache->free_head = NULL;
141 cache->size = act_size;
142 cache->total_count = 0;
143 cache->alloc_count = 0;
144 cache->free_count = 0;
145 cache->name = name;
146 cache->grow_arg = grow_arg;
147 cache->grow_alloc = grow_alloc ? grow_alloc : alloccache_default_grow_alloc;
148
149 /* link it. */
150 cache->next = alloccache_head;
151 alloccache_head = cache;
152}
153
154/* Terminate an alloc cache, free all the memory it contains. */
155void
156alloccache_term (struct alloccache *cache,
157 void (*term_free)(void *term_arg, void *ptr, unsigned int size), void *term_arg)
158{
159 /*cache->size = 0;*/
160 (void)cache;
161 (void)term_free;
162 (void)term_arg;
163 /* FIXME: Implement memory segment tracking and cleanup. */
164}
165
166/* Joins to caches, unlinking the 2nd one. */
167void
168alloccache_join (struct alloccache *cache, struct alloccache *eat)
169{
170 assert (cache->size == eat->size);
171
172#if 0 /* probably a waste of time */ /* FIXME: Optimize joining, avoid all list walking. */
173 /* add the free list... */
174 if (eat->free_head)
175 {
176 unsigned int eat_in_use = eat->alloc_count - eat->free_count;
177 unsigned int dst_in_use = cache->alloc_count - cache->free_count;
178 if (!cache->free_head)
179 cache->free_head = eat->free_head;
180 else if (eat->total_count - eat_in_use < cache->total_count - dst_ins_use)
181 {
182 struct alloccache_free_ent *last = eat->free_head;
183 while (last->next)
184 last = last->next;
185 last->next = cache->free_head;
186 cache->free_head = eat->free_head;
187 }
188 else
189 {
190 struct alloccache_free_ent *last = cache->free_head;
191 while (last->next)
192 last = last->next;
193 last->next = eat->free_head;
194 }
195 }
196
197 /* ... and the free space. */
198 while (eat->free_start != eat->free_end)
199 {
200 struct alloccache_free_ent *f = (struct alloccache_free_ent *)eat->free_start;
201 eat->free_start += eat->size;
202 f->next = cache->free_head;
203 cache->free_head = f;
204 }
205
206 /* and statistics */
207 cache->alloc_count += eat->alloc_count;
208 cache->free_count += eat->free_count;
209#else
210 /* and statistics */
211 cache->alloc_count += eat->alloc_count;
212 cache->free_count += eat->free_count;
213#endif
214 cache->total_count += eat->total_count;
215
216 /* unlink and disable the eat cache */
217 if (alloccache_head == eat)
218 alloccache_head = eat->next;
219 else
220 {
221 struct alloccache *cur = alloccache_head;
222 while (cur->next != eat)
223 cur = cur->next;
224 assert (cur && cur->next == eat);
225 cur->next = eat->next;
226 }
227
228 eat->size = 0;
229 eat->free_end = eat->free_start = NULL;
230 eat->free_head = NULL;
231}
232
233/* Print one alloc cache. */
234void
235alloccache_print (struct alloccache *cache)
236{
237 printf (_("\n# Alloc Cache: %s\n"
238 "# Items: size = %-3u total = %-6u"),
239 cache->name, cache->size, cache->total_count);
240 MAKE_STATS(printf (_(" in-use = %-6lu"),
241 cache->alloc_count - cache->free_count););
242 MAKE_STATS(printf (_("\n# alloc calls = %-7lu free calls = %-7lu"),
243 cache->alloc_count, cache->free_count););
244 printf ("\n");
245}
246
247/* Print all alloc caches. */
248void
249alloccache_print_all (void)
250{
251 struct alloccache *cur;
252 puts ("");
253 for (cur = alloccache_head; cur; cur = cur->next)
254 alloccache_print (cur);
255}
256
257#endif /* CONFIG_WITH_ALLOC_CACHES */
258
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use