1 /* -----------------------------------------------------------------------------
2 * $Id: RtsUtils.c,v 1.8 1999/03/17 13:19:23 simonm Exp $
4 * (c) The GHC Team, 1998-1999
6 * General utility functions used in the RTS.
8 * ---------------------------------------------------------------------------*/
24 /* variable-argument error function. */
26 void barf(char *s, ...)
31 if (prog_argv != NULL && prog_argv[0] != NULL) {
32 fprintf(stderr, "%s: fatal error: ", prog_argv[0]);
34 fprintf(stderr, "fatal error: ");
36 vfprintf(stderr, s, ap);
37 fprintf(stderr, "\n");
39 stg_exit(EXIT_FAILURE);
42 void belch(char *s, ...)
47 vfprintf(stderr, s, ap);
48 fprintf(stderr, "\n");
51 /* result-checking malloc wrappers. */
54 stgMallocBytes (int n, char *msg)
58 if ((space = (char *) malloc((size_t) n)) == NULL) {
60 MallocFailHook((W_) n, msg); /*msg*/
61 stg_exit(EXIT_FAILURE);
67 stgReallocBytes (void *p, int n, char *msg)
71 if ((space = (char *) realloc(p, (size_t) n)) == NULL) {
73 MallocFailHook((W_) n, msg); /*msg*/
80 stgMallocWords (int n, char *msg)
82 return(stgMallocBytes(n * sizeof(W_), msg));
86 stgReallocWords (void *p, int n, char *msg)
88 return(stgReallocBytes(p, n * sizeof(W_), msg));
92 _stgAssert (char *filename, nat linenum)
95 fprintf(stderr, "ASSERTION FAILED: file %s, line %u\n", filename, linenum);
99 StgStablePtr errorHandler = -1; /* -1 indicates no handler installed */
102 raiseError( StgStablePtr handler STG_UNUSED )
105 stg_exit(EXIT_FAILURE);
108 /* -----------------------------------------------------------------------------
111 Not sure if this belongs here.
112 -------------------------------------------------------------------------- */
117 StackOverflowHook(RtsFlags.GcFlags.maxStkSize * sizeof(W_));
119 #if defined(TICKY_TICKY)
120 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
128 OutOfHeapHook(0/*unknown request size*/,
129 RtsFlags.GcFlags.maxHeapSize * BLOCK_SIZE);
131 #if defined(TICKY_TICKY)
132 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
135 stg_exit(EXIT_FAILURE);
138 /* -----------------------------------------------------------------------------
141 Used in addr2Integer because the C compiler on x86 chokes on
142 strlen, trying to inline it with not enough registers available.
143 -------------------------------------------------------------------------- */
145 nat stg_strlen(char *s)
154 /* -----------------------------------------------------------------------------
155 genSym stuff, used by GHC itself for its splitting unique supply.
157 ToDo: put this somewhere sensible.
158 ------------------------------------------------------------------------- */
160 I_ __GenSymCounter = 0;
165 return(__GenSymCounter++);
168 resetGenSymZh(void) /* it's your funeral */
171 return(__GenSymCounter);
174 /* -----------------------------------------------------------------------------
175 Get the current time as a string. Used in profiling reports.
176 -------------------------------------------------------------------------- */
178 #if defined(PROFILING) || defined(DEBUG)
182 static time_t now = 0;
183 static char nowstr[26];
187 strcpy(nowstr, ctime(&now));
188 strcpy(nowstr+16,nowstr+19);
195 /* -----------------------------------------------------------------------------
196 Print large numbers, with punctuation.
197 -------------------------------------------------------------------------- */
200 ullong_format_string(ullong x, char *s, rtsBool with_commas)
202 if (x < (ullong)1000)
203 sprintf(s, "%d", (nat)x);
204 else if (x < (ullong)1000000)
205 sprintf(s, (with_commas) ? "%ld,%3.3ld" : "%ld%3.3ld",
206 (nat)((x)/(ullong)1000),
207 (nat)((x)%(ullong)1000));
208 else if (x < (ullong)1000000000)
209 sprintf(s, (with_commas) ? "%ld,%3.3ld,%3.3ld" : "%ld%3.3ld%3.3ld",
210 (nat)((x)/(ullong)1000000),
211 (nat)((x)/(ullong)1000%(ullong)1000),
212 (nat)((x)%(ullong)1000));
214 sprintf(s, (with_commas) ? "%ld,%3.3ld,%3.3ld,%3.3ld" : "%ld%3.3ld%3.3ld%3.3ld",
215 (nat)((x)/(ullong)1000000000),
216 (nat)((x)/(ullong)1000000%(ullong)1000),
217 (nat)((x)/(ullong)1000%(ullong)1000),
218 (nat)((x)%(ullong)1000));