checkwps: harden filename extension checking.
[maemo-rb.git] / lib / unwarminder / unwarmmem.c
blob5991720412590b61eb7bc8359e1e8a9638b225bd
1 /***************************************************************************
2 * ARM Stack Unwinder, Michael.McTernan.2001@cs.bris.ac.uk
4 * This program is PUBLIC DOMAIN.
5 * This means that there is no copyright and anyone is able to take a copy
6 * for free and use it as they wish, with or without modifications, and in
7 * any context, commerically or otherwise. The only limitation is that I
8 * don't guarantee that the software is fit for any purpose or accept any
9 * liablity for it's use or misuse - this software is without warranty.
10 ***************************************************************************
11 * File Description: Implementation of the memory tracking sub-system.
12 **************************************************************************/
14 #define MODULE_NAME "UNWARMMEM"
16 /***************************************************************************
17 * Include Files
18 **************************************************************************/
20 #include "types.h"
21 #include <stdio.h>
22 #include "unwarmmem.h"
23 #include "unwarm.h"
25 /***************************************************************************
26 * Manifest Constants
27 **************************************************************************/
30 /***************************************************************************
31 * Type Definitions
32 **************************************************************************/
35 /***************************************************************************
36 * Variables
37 **************************************************************************/
40 /***************************************************************************
41 * Macros
42 **************************************************************************/
45 #define M_IsIdxUsed(a, v) (((a)[v >> 3] & (1 << (v & 0x7))) ? TRUE : FALSE)
47 #define M_SetIdxUsed(a, v) ((a)[v >> 3] |= (1 << (v & 0x7)))
49 #define M_ClrIdxUsed(a, v) ((a)[v >> 3] &= ~(1 << (v & 0x7)))
51 /***************************************************************************
52 * Local Functions
53 **************************************************************************/
55 /** Search the memory hash to see if an entry is stored in the hash already.
56 * This will search the hash and either return the index where the item is
57 * stored, or -1 if the item was not found.
59 static SignedInt16 memHashIndex(MemData * const memData,
60 const Int32 addr)
62 const Int16 v = addr % MEM_HASH_SIZE;
63 Int16 s = v;
67 /* Check if the element is occupied */
68 if(M_IsIdxUsed(memData->used, s))
70 /* Check if it is occupied with the sought data */
71 if(memData->a[s] == addr)
73 return s;
76 else
78 /* Item is free, this is where the item should be stored */
79 return s;
82 /* Search the next entry */
83 s++;
84 if(s > MEM_HASH_SIZE)
86 s = 0;
89 while(s != v);
91 /* Search failed, hash is full and the address not stored */
92 return -1;
97 /***************************************************************************
98 * Global Functions
99 **************************************************************************/
101 Boolean UnwMemHashRead(MemData * const memData,
102 Int32 addr,
103 Int32 * const data,
104 Boolean * const tracked)
106 SignedInt16 i = memHashIndex(memData, addr);
108 if(i >= 0 && M_IsIdxUsed(memData->used, i) && memData->a[i] == addr)
110 *data = memData->v[i];
111 *tracked = M_IsIdxUsed(memData->tracked, i);
112 return TRUE;
114 else
116 /* Address not found in the hash */
117 return FALSE;
121 Boolean UnwMemHashWrite(MemData * const memData,
122 Int32 addr,
123 Int32 val,
124 Boolean valValid)
126 SignedInt16 i = memHashIndex(memData, addr);
128 if(i < 0)
130 /* Hash full */
131 return FALSE;
133 else
135 /* Store the item */
136 memData->a[i] = addr;
137 M_SetIdxUsed(memData->used, i);
139 if(valValid)
141 memData->v[i] = val;
142 M_SetIdxUsed(memData->tracked, i);
144 else
146 #if defined(UNW_DEBUG)
147 memData->v[i] = 0xdeadbeef;
148 #endif
149 M_ClrIdxUsed(memData->tracked, i);
152 return TRUE;
157 void UnwMemHashGC(UnwState * const state)
159 const Int32 minValidAddr = state->regData[13].v;
160 MemData * const memData = &state->memData;
161 Int16 t;
163 for(t = 0; t < MEM_HASH_SIZE; t++)
165 if(M_IsIdxUsed(memData->used, t) && (memData->a[t] < minValidAddr))
167 UnwPrintd3("MemHashGC: Free elem %d, addr 0x%08x\n",
168 t, memData->a[t]);
170 M_ClrIdxUsed(memData->used, t);
175 /* END OF FILE */