1 | /* $Id$Revision: */ |
2 | /* vim:set shiftwidth=4 ts=8: */ |
3 | |
4 | /********************************************************** |
5 | * See the LICENSE file for copyright information. * |
6 | **********************************************************/ |
7 | |
8 | #include "config.h" |
9 | |
10 | #include "misc.h" |
11 | #include <stdio.h> |
12 | |
13 | jmp_buf rb_jbuf; |
14 | |
15 | /***********************************************************************/ |
16 | /* FUNCTION: void Assert(int assertion, char* error) */ |
17 | /**/ |
18 | /* INPUTS: assertion should be a predicated that the programmer */ |
19 | /* assumes to be true. If this assumption is not true the message */ |
20 | /* error is printed and the program exits. */ |
21 | /**/ |
22 | /* OUTPUT: None. */ |
23 | /**/ |
24 | /* Modifies input: none */ |
25 | /**/ |
26 | /* Note: If DEBUG_ASSERT is not defined then assertions should not */ |
27 | /* be in use as they will slow down the code. Therefore the */ |
28 | /* compiler will complain if an assertion is used when */ |
29 | /* DEBUG_ASSERT is undefined. */ |
30 | /***********************************************************************/ |
31 | |
32 | |
33 | void Assert(int assertion, char* error) { |
34 | if(!assertion) { |
35 | fprintf(stderr, "Assertion Failed: %s\n" ,error); |
36 | longjmp(rb_jbuf, 1); |
37 | } |
38 | } |
39 | |
40 | |
41 | |
42 | /***********************************************************************/ |
43 | /* FUNCTION: SafeMalloc */ |
44 | /**/ |
45 | /* INPUTS: size is the size to malloc */ |
46 | /**/ |
47 | /* OUTPUT: returns pointer to allocated memory if successful */ |
48 | /**/ |
49 | /* EFFECT: mallocs new memory. If malloc fails, prints error message */ |
50 | /* and terminates program. */ |
51 | /**/ |
52 | /* Modifies Input: none */ |
53 | /**/ |
54 | /***********************************************************************/ |
55 | |
56 | void * SafeMalloc(size_t size) { |
57 | void * result; |
58 | |
59 | if ( (result = malloc(size)) ) { /* assignment intentional */ |
60 | return(result); |
61 | } else { |
62 | fprintf(stderr, "memory overflow: malloc failed in SafeMalloc." ); |
63 | /* printf(" Exiting Program.\n"); */ |
64 | longjmp(rb_jbuf, 2); |
65 | return(0); |
66 | } |
67 | } |
68 | /* NullFunction does nothing it is included so that it can be passed */ |
69 | /* as a function to RBTreeCreate when no other suitable function has */ |
70 | /* been defined */ |
71 | |
72 | void NullFunction(void * junk) { ; } |
73 | |