1 /* table.c - bookkeeping functions for allocated memory */
3 /* Copyright (C) 2001-2003 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with Bash; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
29 extern int malloc_register;
31 #ifdef MALLOC_REGISTER
33 #define FIND_ALLOC 0x01 /* allocate new entry or find existing */
34 #define FIND_EXIST 0x02 /* find existing entry */
36 static int table_count = 0;
37 static int table_allocated = 0;
38 static mr_table_t mem_table[REG_TABLE_SIZE];
39 static mr_table_t mem_overflow;
42 * NOTE: taken from dmalloc (http://dmalloc.com) and modified.
51 /* set up the internal state */
52 a = 0x9e3779b9; /* the golden ratio; an arbitrary value */
53 x = (unsigned long)key; /* truncation is OK */
55 c = x >> 3; /* XXX - was >> 4 */
66 return (mt_hash ((unsigned char *)mem) & (REG_TABLE_SIZE-1));
69 #define which_bucket(mem) (mt_hash ((unsigned char *)(mem)) & (REG_TABLE_SIZE-1));
73 find_entry (mem, flags)
78 register mr_table_t *tp;
79 mr_table_t *endp, *lastp;
81 if (mem_overflow.mem == mem)
82 return (&mem_overflow);
84 bucket = which_bucket (mem); /* get initial hash */
85 tp = endp = mem_table + bucket;
86 lastp = mem_table + REG_TABLE_SIZE;
92 if (tp->mem == 0 && (flags & FIND_ALLOC))
100 if (tp == lastp) /* wrap around */
103 if (tp == endp && (flags & FIND_EXIST))
104 return ((mr_table_t *)NULL);
106 if (tp == endp && (flags & FIND_ALLOC))
110 /* oops. table is full. replace an existing free entry. */
113 /* If there are no free entries, punt right away without searching. */
114 if (table_allocated == REG_TABLE_SIZE)
117 if (tp->flags & MT_FREE)
119 memset(tp, 0, sizeof (mr_table_t));
129 /* wow. entirely full. return mem_overflow dummy entry. */
131 memset (tp, 0, sizeof (mr_table_t));
139 return (find_entry (mem, FIND_EXIST));
143 mregister_describe_mem (mem, fp)
149 entry = find_entry (mem, FIND_EXIST);
152 fprintf (fp, "malloc: %p: %s: last %s from %s:%d\n",
154 (entry->flags & MT_ALLOC) ? "allocated" : "free",
155 (entry->flags & MT_ALLOC) ? "allocated" : "freed",
156 entry->file ? entry->file : "unknown",
161 mregister_alloc (tag, mem, size, file, line)
170 tentry = find_entry (mem, FIND_ALLOC);
174 /* oops. table is full. punt. */
175 fprintf (stderr, _("register_alloc: alloc table is full with FIND_ALLOC?\n"));
179 if (tentry->flags & MT_ALLOC)
181 /* oops. bad bookkeeping. ignore for now */
182 fprintf (stderr, _("register_alloc: %p already in table as allocated?\n"), mem);
188 tentry->flags = MT_ALLOC;
193 if (tentry != &mem_overflow)
198 mregister_free (mem, size, file, line)
206 tentry = find_entry (mem, FIND_EXIST);
209 /* oops. not found. */
211 fprintf (stderr, "register_free: %p not in allocation table?\n", mem);
215 if (tentry->flags & MT_FREE)
217 /* oops. bad bookkeeping. ignore for now */
218 fprintf (stderr, _("register_free: %p already in table as free?\n"), mem);
221 tentry->flags = MT_FREE;
222 tentry->func = "free";
227 if (tentry != &mem_overflow)
231 /* If we ever add more flags, this will require changes. */
238 else if (x & MT_ALLOC)
241 return "undetermined?";
245 _register_dump_table(fp)
251 for (i = 0; i < REG_TABLE_SIZE; i++)
253 entry = mem_table[i];
255 fprintf (fp, "[%d] %p:%d:%s:%s:%s:%d:%d:%d\n", i,
256 entry.mem, entry.size,
257 _entry_flags(entry.flags),
258 entry.func ? entry.func : "unknown",
259 entry.file ? entry.file : "unknown",
261 entry.nalloc, entry.nfree);
266 mregister_dump_table()
268 _register_dump_table (stderr);
272 mregister_table_init ()
274 memset (mem_table, 0, sizeof(mr_table_t) * REG_TABLE_SIZE);
275 memset (&mem_overflow, 0, sizeof (mr_table_t));
279 #endif /* MALLOC_REGISTER */
282 malloc_set_register(n)
287 old = malloc_register;