Fix memory leak from #12664
[ghc.git] / rts / RtsSymbolInfo.c
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 2000-2015
4 *
5 * RTS Symbols
6 *
7 * ---------------------------------------------------------------------------*/
8
9 #include "ghcplatform.h"
10 #include "RtsSymbolInfo.h"
11
12 #include "Rts.h"
13 #include "HsFFI.h"
14
15 #include "Hash.h"
16 #include "RtsUtils.h"
17
18 typedef struct _SymbolInfo {
19 /* Determines if the
20 symbol is weak */
21 HsBool isWeak;
22
23 } SymbolInfo;
24
25 /* -----------------------------------------------------------------------------
26 * Performs a check to see if the symbol at the given address
27 * is a weak symbol or not.
28 *
29 * Returns: HS_BOOL_TRUE on symbol being weak, else HS_BOOL_FALSE
30 */
31 HsBool isSymbolWeak(ObjectCode *owner, void *label)
32 {
33 SymbolInfo *info;
34 if (owner
35 && label
36 && owner->extraInfos
37 && (info = lookupStrHashTable(owner->extraInfos, label)) != NULL)
38 {
39 return info->isWeak;
40 }
41
42 return HS_BOOL_FALSE;
43 }
44
45 /* -----------------------------------------------------------------------------
46 * Marks the symbol at the given address as weak or not.
47 * If the extra symbol infos table has not been initialized
48 * yet this will create and allocate a new Hashtable
49 */
50 void setWeakSymbol(ObjectCode *owner, void *label)
51 {
52 SymbolInfo *info;
53 if (owner && label)
54 {
55 info = NULL;
56 if (!owner->extraInfos)
57 {
58 owner->extraInfos = allocStrHashTable();
59 }
60 else {
61 info = lookupStrHashTable(owner->extraInfos, label);
62 }
63
64 if (!info){
65 info = stgMallocBytes(sizeof(SymbolInfo), "setWeakSymbol");
66 }
67
68 info->isWeak = HS_BOOL_TRUE;
69
70 insertStrHashTable(owner->extraInfos, label, info);
71 }
72 }