1 /* -----------------------------------------------------------------------------
2 * $Id: RtsUtils.c,v 1.7 1999/03/02 20:05:41 sof 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 -------------------------------------------------------------------------- */
115 stackOverflow(nat max_stack_size)
118 StackOverflowHook(max_stack_size * sizeof(W_)); /*msg*/
120 #if defined(TICKY_TICKY)
121 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
124 stg_exit(EXIT_FAILURE);
131 OutOfHeapHook(0/*unknown request size*/,
132 RtsFlags.GcFlags.maxHeapSize * BLOCK_SIZE);
134 #if defined(TICKY_TICKY)
135 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
138 stg_exit(EXIT_FAILURE);
141 /* -----------------------------------------------------------------------------
144 Used in addr2Integer because the C compiler on x86 chokes on
145 strlen, trying to inline it with not enough registers available.
146 -------------------------------------------------------------------------- */
148 nat stg_strlen(char *s)
157 /* -----------------------------------------------------------------------------
158 genSym stuff, used by GHC itself for its splitting unique supply.
160 ToDo: put this somewhere sensible.
161 ------------------------------------------------------------------------- */
163 I_ __GenSymCounter = 0;
168 return(__GenSymCounter++);
171 resetGenSymZh(void) /* it's your funeral */
174 return(__GenSymCounter);
177 /* -----------------------------------------------------------------------------
178 Get the current time as a string. Used in profiling reports.
179 -------------------------------------------------------------------------- */
181 #if defined(PROFILING) || defined(DEBUG)
185 static time_t now = 0;
186 static char nowstr[26];
190 strcpy(nowstr, ctime(&now));
191 strcpy(nowstr+16,nowstr+19);
198 /* -----------------------------------------------------------------------------
199 Print large numbers, with punctuation.
200 -------------------------------------------------------------------------- */
203 ullong_format_string(ullong x, char *s, rtsBool with_commas)
205 if (x < (ullong)1000)
206 sprintf(s, "%d", (nat)x);
207 else if (x < (ullong)1000000)
208 sprintf(s, (with_commas) ? "%ld,%3.3ld" : "%ld%3.3ld",
209 (nat)((x)/(ullong)1000),
210 (nat)((x)%(ullong)1000));
211 else if (x < (ullong)1000000000)
212 sprintf(s, (with_commas) ? "%ld,%3.3ld,%3.3ld" : "%ld%3.3ld%3.3ld",
213 (nat)((x)/(ullong)1000000),
214 (nat)((x)/(ullong)1000%(ullong)1000),
215 (nat)((x)%(ullong)1000));
217 sprintf(s, (with_commas) ? "%ld,%3.3ld,%3.3ld,%3.3ld" : "%ld%3.3ld%3.3ld%3.3ld",
218 (nat)((x)/(ullong)1000000000),
219 (nat)((x)/(ullong)1000000%(ullong)1000),
220 (nat)((x)/(ullong)1000%(ullong)1000),
221 (nat)((x)%(ullong)1000));