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. */
30 extern int malloc_register;
32 #ifdef MALLOC_REGISTER
34 #define FIND_ALLOC 0x01 /* allocate new entry or find existing */
35 #define FIND_EXIST 0x02 /* find existing entry */
37 static int table_count = 0;
38 static int table_allocated = 0;
39 static mr_table_t mem_table[REG_TABLE_SIZE];
40 static mr_table_t mem_overflow;
43 * NOTE: taken from dmalloc (http://dmalloc.com) and modified.
52 /* set up the internal state */
53 a = 0x9e3779b9; /* the golden ratio; an arbitrary value */
54 x = (unsigned long)key; /* truncation is OK */
56 c = x >> 3; /* XXX - was >> 4 */
67 return (mt_hash ((unsigned char *)mem) & (REG_TABLE_SIZE-1));
70 #define which_bucket(mem) (mt_hash ((unsigned char *)(mem)) & (REG_TABLE_SIZE-1));
74 find_entry (mem, flags)
79 register mr_table_t *tp;
80 mr_table_t *endp, *lastp;
82 if (mem_overflow.mem == mem)
83 return (&mem_overflow);
85 bucket = which_bucket (mem); /* get initial hash */
86 tp = endp = mem_table + bucket;
87 lastp = mem_table + REG_TABLE_SIZE;
93 if (tp->mem == 0 && (flags & FIND_ALLOC))
101 if (tp == lastp) /* wrap around */
104 if (tp == endp && (flags & FIND_EXIST))
105 return ((mr_table_t *)NULL);
107 if (tp == endp && (flags & FIND_ALLOC))
111 /* oops. table is full. replace an existing free entry. */
114 /* If there are no free entries, punt right away without searching. */
115 if (table_allocated == REG_TABLE_SIZE)
118 if (tp->flags & MT_FREE)
120 memset(tp, 0, sizeof (mr_table_t));
130 /* wow. entirely full. return mem_overflow dummy entry. */
132 memset (tp, 0, sizeof (mr_table_t));
140 return (find_entry (mem, FIND_EXIST));
144 mregister_describe_mem (mem, fp)
150 entry = find_entry (mem, FIND_EXIST);
153 fprintf (fp, "malloc: %p: %s: last %s from %s:%d\n",
155 (entry->flags & MT_ALLOC) ? "allocated" : "free",
156 (entry->flags & MT_ALLOC) ? "allocated" : "freed",
157 entry->file ? entry->file : "unknown",
162 mregister_alloc (tag, mem, size, file, line)
171 tentry = find_entry (mem, FIND_ALLOC);
175 /* oops. table is full. punt. */
176 fprintf (stderr, _("register_alloc: alloc table is full with FIND_ALLOC?\n"));
180 if (tentry->flags & MT_ALLOC)
182 /* oops. bad bookkeeping. ignore for now */
183 fprintf (stderr, _("register_alloc: %p already in table as allocated?\n"), mem);
189 tentry->flags = MT_ALLOC;
194 if (tentry != &mem_overflow)
199 mregister_free (mem, size, file, line)
207 tentry = find_entry (mem, FIND_EXIST);
210 /* oops. not found. */
212 fprintf (stderr, "register_free: %p not in allocation table?\n", mem);
216 if (tentry->flags & MT_FREE)
218 /* oops. bad bookkeeping. ignore for now */
219 fprintf (stderr, _("register_free: %p already in table as free?\n"), mem);
222 tentry->flags = MT_FREE;
223 tentry->func = "free";
228 if (tentry != &mem_overflow)
232 /* If we ever add more flags, this will require changes. */
239 else if (x & MT_ALLOC)
242 return "undetermined?";
246 _register_dump_table(fp)
252 for (i = 0; i < REG_TABLE_SIZE; i++)
254 entry = mem_table[i];
256 fprintf (fp, "[%d] %p:%d:%s:%s:%s:%d:%d:%d\n", i,
257 entry.mem, entry.size,
258 _entry_flags(entry.flags),
259 entry.func ? entry.func : "unknown",
260 entry.file ? entry.file : "unknown",
262 entry.nalloc, entry.nfree);
267 mregister_dump_table()
269 _register_dump_table (stderr);
273 mregister_table_init ()
275 memset (mem_table, 0, sizeof(mr_table_t) * REG_TABLE_SIZE);
276 memset (&mem_overflow, 0, sizeof (mr_table_t));
280 #endif /* MALLOC_REGISTER */
283 malloc_set_register(n)
288 old = malloc_register;