Fix changelog email address
[freerdp-ubuntu-pcb-backport.git] / libfreerdp-utils / memory.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * Memory Utils
4  *
5  * Copyright 2009-2011 Jay Sorg
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <freerdp/utils/memory.h>
25
26 /**
27  * Allocate memory.
28  * @param size
29  */
30
31 void* xmalloc(size_t size)
32 {
33         void* mem;
34
35         if (size < 1)
36                 size = 1;
37
38         mem = malloc(size);
39
40         if (mem == NULL)
41         {
42                 perror("xmalloc");
43                 printf("xmalloc: failed to allocate memory of size: %d\n", (int) size);
44         }
45
46         return mem;
47 }
48
49 /**
50  * Allocate memory initialized to zero.
51  * @param size
52  */
53
54 void* xzalloc(size_t size)
55 {
56         void* mem;
57
58         if (size < 1)
59                 size = 1;
60
61         mem = calloc(1, size);
62
63         if (mem == NULL)
64         {
65                 perror("xzalloc");
66                 printf("xzalloc: failed to allocate memory of size: %d\n", (int) size);
67         }
68
69         return mem;
70 }
71
72 /**
73  * Reallocate memory.
74  * @param ptr
75  * @param size
76  */
77
78 void* xrealloc(void* ptr, size_t size)
79 {
80         void* mem;
81
82         if (size < 1)
83                 size = 1;
84
85         if (ptr == NULL)
86         {
87                 printf("xrealloc: null pointer given\n");
88                 return NULL;
89         }
90
91         mem = realloc(ptr, size);
92
93         if (mem == NULL)
94                 perror("xrealloc");
95
96         return mem;
97 }
98
99 /**
100  * Free memory.
101  * @param mem
102  */
103
104 void xfree(void* ptr)
105 {
106         if (ptr != NULL)
107                 free(ptr);
108 }
109
110 /**
111  * Duplicate a string in memory.
112  * @param str
113  * @return
114  */
115
116 char* xstrdup(const char* str)
117 {
118         char* mem;
119
120         if (str == NULL)
121                 return NULL;
122
123 #ifdef _WIN32
124         mem = _strdup(str);
125 #else
126         mem = strdup(str);
127 #endif
128
129         if (mem == NULL)
130                 perror("strdup");
131
132         return mem;
133 }