-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalloc.c
72 lines (60 loc) · 1.54 KB
/
alloc.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
/*
* alloc.c
*
* Written by: Rick Ohnemus (rick@sterling.com)
*
* This file contains functions related to dynamic memory allocation.
*
* Global Functions: allocate, reallocate, savestr
*/
#if !defined(lint)
static char rcsid[] = "$Id: alloc.c,v 1.1 2008/12/27 00:56:03 vandys Exp $";
#endif
#include <stdlib.h>
#include <string.h>
#include "errmsg.h"
/*
* allocate:
* Allocate 'n' bytes of memory. If the allocation fails a
* message is displayed containing the name of the source
* file and line number in the file that allocate() was
* called from.
*/
void *allocate(size_t n, const char *file, const int line)
{
void *p = malloc(n);
if (p == NULL) {
error("%s : %d : no space : %m", file, line);
}
return p;
}
/*
* reallocate:
* Reallocate 'n' bytes of memory. If the address to
* reallocate is null then just call allocate() to
* allocate a new block of memory.
* If the allocation fails a message is displayed
* containing the name of the source file and line
* number in the file that reallocate() was called from.
*/
void *reallocate(void *a, size_t n, const char *file, const int line)
{
void *p;
if (a == NULL) {
return allocate(n, file, line);
}
p = realloc(a, n);
if (p == NULL) {
error("%s : %d : no space : %m", file, line);
}
return p;
}
/*
* savestr:
* Allocate space for a string then copy the string
* into the newly allocated memory.
*/
char *savestr(const char *str, const char *file, const int line)
{
return strcpy(allocate(strlen(str) + 1, file, line), str);
}