GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/alloc.c Lines: 37 39 94.9 %
Date: 2020-12-10 21:44:00 Branches: 19 24 79.2 %

Line Branch Exec Source
1
#include <stdlib.h>             /* atoi, malloc, free, realloc */
2
#include <string.h>             /* strerror, memset, strncpy, memcpy, strlen */
3
4
#include "alloc.h"
5
#include "predict.h"
6
7
/**
8
 * Function that allocates memory.
9
 * @param 	ptr		[void *] 	"The memory that needs to be copies"
10
 * @param 	void	[size_t] 	"The size of the memory that should be copied"
11
 * @return 	      	[void *] 	"Returns pointer to allocated memory if successful, otherwise NULL"
12
 */
13
13
void *WSS_copy(void *ptr, size_t size) {
14
13
	void *buffer;
15
16
13
    if ( unlikely(ptr == NULL) ) {
17
        return NULL;
18
    }
19
20
12
    if ( unlikely(NULL == (buffer = WSS_malloc(size))) ) {
21
        return NULL;
22
    }
23
24
12
    return memcpy(buffer, ptr, size);
25
}
26
27
/**
28
 * Function that allocates memory.  *
29
 * @param 	size	[size_t] 	"The size of the memory that should be allocated"
30
 * @return 	      	[void *] 	"Returns pointer to allocated memory if successful, otherwise NULL"
31
 */
32
3991
void *WSS_malloc(size_t size) {
33
3991
	void *buffer;
34
35
3991
    if ( unlikely(size == 0) ) {
36
        return NULL;
37
    }
38
39
#ifdef USE_RPMALLOC
40
3988
	if ( unlikely(NULL == (buffer = rpmalloc( size ))) ) {
41
		return NULL;
42
	}
43
#else
44
	if ( unlikely(NULL == (buffer = malloc( size ))) ) {
45
		return NULL;
46
	}
47
#endif
48
49
50
3988
	memset(buffer, '\0', size);
51
52
3988
	return buffer;
53
}
54
55
/**
56
 * Function that allocates an array of memory.
57
 *
58
 * @param 	nmemb	[size_t] 	"The size of the array i.e. indexes"
59
 * @param 	size	[size_t] 	"The size of the memory that should be allocated"
60
 * @return 	      	[void *] 	"Returns pointer to allocated memory if successful, otherwise NULL"
61
 */
62
4993
void *WSS_calloc(size_t memb, size_t size) {
63
4993
	void *buffer;
64
65
4993
    if ( unlikely(size == 0 || memb == 0) ) {
66
        return NULL;
67
    }
68
69
#ifdef USE_RPMALLOC
70
4991
	if ( unlikely(NULL == (buffer = rpcalloc(memb, size))) ) {
71
		return NULL;
72
	}
73
#else
74
	if ( unlikely(NULL == (buffer = calloc(memb, size))) ) {
75
		return NULL;
76
	}
77
#endif
78
79
80
4991
	memset(buffer, '\0', size*memb);
81
82
4991
	return buffer;
83
}
84
85
/**
86
 * Function that re-allocates some memory.
87
 *
88
 * @param 	ptr		[void **] 	"The memory that needs to be rearranged"
89
 * @param 	oldSize	[size_t] 	"The size of the memory that is already allocated"
90
 * @param 	newSize	[size_t] 	"The size of the memory that should be allocated"
91
 * @return 	      	[void *] 	"Returns pointer to allocated memory if successful, otherwise NULL"
92
 */
93
150
void *WSS_realloc(void **ptr, size_t oldSize, size_t newSize) {
94
150
	void *buffer;
95
96
150
    if ( unlikely(newSize == 0) ) {
97
2
        WSS_free(ptr);
98
2
        return NULL;
99
    }
100
101
148
    if ( unlikely(ptr == NULL) ) {
102
#ifdef USE_RPMALLOC
103
1
        if ( unlikely(NULL == (buffer = rprealloc(NULL, newSize))) ) {
104
            return NULL;
105
        }
106
#else
107
        if ( unlikely(NULL == (buffer = realloc(NULL, newSize))) ) {
108
            return NULL;
109
        }
110
#endif
111
    } else {
112
#ifdef USE_RPMALLOC
113
147
        if ( unlikely(NULL == (buffer = rprealloc(*ptr, newSize))) ) {
114
            WSS_free(ptr);
115
            return NULL;
116
        }
117
#else
118
        if ( unlikely(NULL == (buffer = realloc(*ptr, newSize))) ) {
119
            WSS_free(ptr);
120
            return NULL;
121
        }
122
#endif
123
    }
124
125
126
148
    if ( likely(oldSize < newSize) ) {
127
86
        memset(((char *) buffer)+oldSize, '\0', (newSize-oldSize));
128
    }
129
130
	return buffer;
131
}
132
133
/**
134
 * Function that re-allocates some memory. The interface is of this function is
135
 * similar to the realloc(3) function.
136
 *
137
 * @param 	ptr		[void *] 	"The memory that needs to be rearranged"
138
 * @param 	newSize	[size_t] 	"The size of the memory that should be allocated"
139
 * @return 		    [void *] 	"Returns pointer to allocated memory if successful, otherwise NULL"
140
 */
141
1
void *WSS_realloc_normal(void *ptr, size_t newSize) {
142
1
    return WSS_realloc(&ptr, newSize, newSize);
143
}
144
145
#ifndef NDEBUG
146
/**
147
 * Function that re-allocates some memory.
148
 *
149
 * @param 	ptr		[void **] 	"The memory that needs to be freed"
150
 * @return 		    [void]
151
 */
152
9114
void WSS_free(void **ptr) {
153
9114
    if ( likely(NULL != *ptr) ) {
154
#ifdef USE_RPMALLOC
155
8852
        rpfree(*ptr);
156
#else
157
        free(*ptr);
158
#endif
159
8852
        *ptr = NULL;
160
    }
161
9114
}
162
#endif
163
164
/**
165
 * Function that re-allocates some memory. The interface is of this function is
166
 * similar to the free(3) function.
167
 *
168
 * @param 	ptr		[void *] 	"The memory that needs to be freed"
169
 * @return 		    [void]
170
 */
171
15
void WSS_free_normal(void *ptr) {
172
15
    WSS_free(&ptr);
173
15
}