Free various things we allocate
[ghc.git] / rts / ThreadLabels.c
1 /* -----------------------------------------------------------------------------
2 * ThreadLabels.c
3 *
4 * (c) The GHC Team 2002-2003
5 *
6 * Table of thread labels.
7 *
8 * ---------------------------------------------------------------------------*/
9
10 #include "PosixSource.h"
11 #include "Rts.h"
12 #include "ThreadLabels.h"
13 #include "RtsUtils.h"
14 #include "Hash.h"
15
16 #include <stdlib.h>
17 #include <string.h>
18
19 #if defined(DEBUG)
20 /* to the end */
21 static HashTable * threadLabels = NULL;
22
23 void
24 initThreadLabelTable(void)
25 {
26 if (threadLabels == NULL) {
27 threadLabels = allocHashTable();
28 }
29 }
30
31 void
32 freeThreadLabelTable(void)
33 {
34 if (threadLabels != NULL) {
35 freeHashTable(threadLabels, NULL);
36 threadLabels = NULL;
37 }
38 }
39
40 void
41 updateThreadLabel(StgWord key, void *data)
42 {
43 removeThreadLabel(key);
44 insertHashTable(threadLabels,key,data);
45 }
46
47 void *
48 lookupThreadLabel(StgWord key)
49 {
50 return lookupHashTable(threadLabels,key);
51 }
52
53 void
54 removeThreadLabel(StgWord key)
55 {
56 void * old = NULL;
57 if ((old = lookupHashTable(threadLabels,key))) {
58 removeHashTable(threadLabels,key,old);
59 stgFree(old);
60 }
61 }
62
63 void
64 labelThread(StgPtr tso, char *label)
65 {
66 int len;
67 void *buf;
68
69 /* Caveat: Once set, you can only set the thread name to "" */
70 len = strlen(label)+1;
71 buf = stgMallocBytes(len * sizeof(char), "Schedule.c:labelThread()");
72 strncpy(buf,label,len);
73 /* Update will free the old memory for us */
74 updateThreadLabel(((StgTSO *)tso)->id,buf);
75 }
76
77 #endif /* DEBUG */