| 1 | /* table.h - definitions for tables for keeping track of allocated memory */
|
|---|
| 2 |
|
|---|
| 3 | /* Copyright (C) 2001-2003 Free Software Foundation, Inc.
|
|---|
| 4 |
|
|---|
| 5 | This program is free software; you can redistribute it and/or modify
|
|---|
| 6 | it under the terms of the GNU General Public License as published by
|
|---|
| 7 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 8 | any later version.
|
|---|
| 9 |
|
|---|
| 10 | This program is distributed in the hope that it will be useful,
|
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 13 | GNU General Public License for more details.
|
|---|
| 14 |
|
|---|
| 15 | You should have received a copy of the GNU General Public License
|
|---|
| 16 | along with this program; if not, write to the Free Software
|
|---|
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
|
|---|
| 18 |
|
|---|
| 19 | #ifndef _MTABLE_H
|
|---|
| 20 | #define _MTABLE_H
|
|---|
| 21 |
|
|---|
| 22 | #include "imalloc.h"
|
|---|
| 23 |
|
|---|
| 24 | #ifdef MALLOC_REGISTER
|
|---|
| 25 |
|
|---|
| 26 | /* values for flags byte. */
|
|---|
| 27 | #define MT_ALLOC 0x01
|
|---|
| 28 | #define MT_FREE 0x02
|
|---|
| 29 |
|
|---|
| 30 | /*
|
|---|
| 31 | * Memory table entry.
|
|---|
| 32 | *
|
|---|
| 33 | * MEM is the address of the allocated pointer.
|
|---|
| 34 | * SIZE is the requested allocation size.
|
|---|
| 35 | * FLAGS includes either MT_ALLOC (MEM is allocated) or MT_FREE (MEM is
|
|---|
| 36 | * not allocated). Other flags later.
|
|---|
| 37 | * FUNC is set to the name of the function doing the allocation (from the
|
|---|
| 38 | * `tag' argument to register_alloc().
|
|---|
| 39 | * FILE and LINE are the filename and line number of the last allocation
|
|---|
| 40 | * and free (depending on STATUS) of MEM.
|
|---|
| 41 | * NALLOC and NFREE are incremented on each allocation that returns MEM or
|
|---|
| 42 | * each free of MEM, respectively (way to keep track of memory reuse
|
|---|
| 43 | * and how well the free lists are working).
|
|---|
| 44 | *
|
|---|
| 45 | */
|
|---|
| 46 | typedef struct mr_table {
|
|---|
| 47 | PTR_T mem;
|
|---|
| 48 | size_t size;
|
|---|
| 49 | char flags;
|
|---|
| 50 | const char *func;
|
|---|
| 51 | const char *file;
|
|---|
| 52 | int line;
|
|---|
| 53 | int nalloc, nfree;
|
|---|
| 54 | } mr_table_t;
|
|---|
| 55 |
|
|---|
| 56 | #define REG_TABLE_SIZE 8192
|
|---|
| 57 |
|
|---|
| 58 | extern mr_table_t *mr_table_entry __P((PTR_T));
|
|---|
| 59 | extern void mregister_alloc __P((const char *, PTR_T, size_t, const char *, int));
|
|---|
| 60 | extern void mregister_free __P((PTR_T, int, const char *, int));
|
|---|
| 61 | extern void mregister_describe_mem ();
|
|---|
| 62 | extern void mregister_dump_table __P((void));
|
|---|
| 63 | extern void mregister_table_init __P((void));
|
|---|
| 64 |
|
|---|
| 65 | /* NOTE: HASH_MIX taken from dmalloc (http://dmalloc.com) */
|
|---|
| 66 |
|
|---|
| 67 | /*
|
|---|
| 68 | * void HASH_MIX
|
|---|
| 69 | *
|
|---|
| 70 | * DESCRIPTION:
|
|---|
| 71 | *
|
|---|
| 72 | * Mix 3 32-bit values reversibly. For every delta with one or two
|
|---|
| 73 | * bits set, and the deltas of all three high bits or all three low
|
|---|
| 74 | * bits, whether the original value of a,b,c is almost all zero or is
|
|---|
| 75 | * uniformly distributed.
|
|---|
| 76 | *
|
|---|
| 77 | * If HASH_MIX() is run forward or backward, at least 32 bits in a,b,c
|
|---|
| 78 | * have at least 1/4 probability of changing. If mix() is run
|
|---|
| 79 | * forward, every bit of c will change between 1/3 and 2/3 of the
|
|---|
| 80 | * time. (Well, 22/100 and 78/100 for some 2-bit deltas.)
|
|---|
| 81 | *
|
|---|
| 82 | * HASH_MIX() takes 36 machine instructions, but only 18 cycles on a
|
|---|
| 83 | * superscalar machine (like a Pentium or a Sparc). No faster mixer
|
|---|
| 84 | * seems to work, that's the result of my brute-force search. There
|
|---|
| 85 | * were about 2^68 hashes to choose from. I only tested about a
|
|---|
| 86 | * billion of those.
|
|---|
| 87 | */
|
|---|
| 88 | #define HASH_MIX(a, b, c) \
|
|---|
| 89 | do { \
|
|---|
| 90 | a -= b; a -= c; a ^= (c >> 13); \
|
|---|
| 91 | b -= c; b -= a; b ^= (a << 8); \
|
|---|
| 92 | c -= a; c -= b; c ^= (b >> 13); \
|
|---|
| 93 | a -= b; a -= c; a ^= (c >> 12); \
|
|---|
| 94 | b -= c; b -= a; b ^= (a << 16); \
|
|---|
| 95 | c -= a; c -= b; c ^= (b >> 5); \
|
|---|
| 96 | a -= b; a -= c; a ^= (c >> 3); \
|
|---|
| 97 | b -= c; b -= a; b ^= (a << 10); \
|
|---|
| 98 | c -= a; c -= b; c ^= (b >> 15); \
|
|---|
| 99 | } while(0)
|
|---|
| 100 |
|
|---|
| 101 | #endif /* MALLOC_REGISTER */
|
|---|
| 102 |
|
|---|
| 103 | #endif /* _MTABLE_H */
|
|---|