-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.c
164 lines (129 loc) · 4.51 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**********************************************************************
* File: util.c
* Author: Kevin Howe
* Copyright (C) Genome Research Limited, 2002-
*-------------------------------------------------------------------
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------
* NOTES:
* This file contains general utiliy functions used throughout the
* application, such as those for memory management and error
* messaging. I have used it as a place to put other general stuff
* until I have somewhere better to put it.
**********************************************************************/
#include "njheaders/util.h"
/*********************************************************************
FUNCTION: calloc_util
DESCRIPTION:
A wrapper for the stdlib.h function calloc; exits if memory cannot
be allocated
RETURNS: A generic pointer to the reallocated memory
ARGS:
The number of bytes to be allocated
NOTES:
*********************************************************************/
void *calloc_util( size_t numobjs, size_t size) {
void *ret;
if ((ret = calloc( numobjs, size )) == NULL)
fatal_util("calloc_util: Out of memory");
return ret;
}
/*********************************************************************
FUNCTION: malloc_util
DESCRIPTION:
A wrapper for the stdlib.h function malloc; exits if memory cannot
be allocated
RETURNS: A generic pointer to the reallocated memory
ARGS:
The number of bytes to be allocated
NOTES:
*********************************************************************/
void *malloc_util(size_t numbytes) {
void *ret;
if ((ret = malloc( numbytes )) == NULL)
fatal_util("malloc_util: out of memory when requesting %d bytes", numbytes);
return ret;
}
/*********************************************************************
FUNCTION: realloc_util
DESCRIPTION:
A wrapper for the stdlib.h function realloc; exits if memory cannot
be allocated
RETURNS: A generic pointer to the reallocated memory
ARGS:
A pointer to the memory to be reallocated
The number of bytes to be allocated
NOTES:
*********************************************************************/
void *realloc_util(void *ptr, size_t bytes) {
void *ret = NULL;
if (ptr == NULL)
fatal_util("Call to realloc_util with a null pointer");
else {
if ((ret = realloc(ptr, bytes)) == NULL)
fatal_util("realloc_util: out of memory when requesting %d bytes", bytes);
}
return ret;
}
/*********************************************************************
FUNCTION: free_util
DESCRIPTION:
A wrapper for the stdlib.h function free; warns if the free could
not be performed
RETURNS: A (hopefully null) pointer
ARGS:
A pointer to the memory to be deallocated
NOTES:
*********************************************************************/
void *free_util( void *ptr ) {
if (ptr == NULL)
warning_util("Call to free_util with null pointer");
else {
free(ptr);
ptr = NULL;
}
return ptr;
}
/*********************************************************************
FUNCTION: fatal_util
DESCRIPTION:
Prints the given formatted error message and exits
RETURNS:
ARGS:
A format string + args, c.f. printf
NOTES:
*********************************************************************/
void fatal_util( char *fmt, ... ) {
va_list args;
va_start( args, fmt );
fprintf( stderr, "\nA Fatal Error occurred: ");
vfprintf( stderr, fmt, args);
fprintf( stderr,"\n");
va_end( args );
exit(1);
}
/*********************************************************************
FUNCTION: warning_util
DESCRIPTION:
Prints the given formatted warning to stderr
RETURNS:
ARGS:
A format string + args, c.f. printf
NOTES:
*********************************************************************/
void warning_util( char *fmt, ...) {
va_list args;
va_start( args, fmt );
fprintf( stderr, "\nWARNING: " );
vfprintf( stderr, fmt, args );
fprintf( stderr, "\n" );
va_end( args );
}