/src/FreeRDP/winpr/libwinpr/crt/alignment.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /**  | 
2  |  |  * WinPR: Windows Portable Runtime  | 
3  |  |  * Data Alignment  | 
4  |  |  *  | 
5  |  |  * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>  | 
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 <stdlib.h>  | 
21  |  | #include <winpr/config.h>  | 
22  |  |  | 
23  |  | #include <winpr/crt.h>  | 
24  |  |  | 
25  |  | /* Data Alignment: http://msdn.microsoft.com/en-us/library/fs9stz4e/ */  | 
26  |  |  | 
27  |  | #if !defined(_WIN32) || (defined(__MINGW32__) && !defined(_UCRT))  | 
28  |  |  | 
29  |  | #include <stdint.h>  | 
30  |  | #include <limits.h>  | 
31  |  |  | 
32  | 13.3M  | #define WINPR_ALIGNED_MEM_SIGNATURE 0x0BA0BAB  | 
33  |  |  | 
34  |  | #define WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(_memptr) \  | 
35  | 16.4M  |   (WINPR_ALIGNED_MEM*)(((size_t)(((BYTE*)(_memptr)) - sizeof(WINPR_ALIGNED_MEM))))  | 
36  |  |  | 
37  |  | #include <stdlib.h>  | 
38  |  |  | 
39  |  | #include "../log.h"  | 
40  |  | #define TAG WINPR_TAG("crt") | 
41  |  |  | 
42  |  | struct winpr_aligned_mem  | 
43  |  | { | 
44  |  |   UINT32 sig;  | 
45  |  |   size_t size;  | 
46  |  |   void* base_addr;  | 
47  |  | };  | 
48  |  | typedef struct winpr_aligned_mem WINPR_ALIGNED_MEM;  | 
49  |  |  | 
50  |  | void* winpr_aligned_malloc(size_t size, size_t alignment)  | 
51  | 3.58M  | { | 
52  | 3.58M  |   return winpr_aligned_offset_malloc(size, alignment, 0);  | 
53  | 3.58M  | }  | 
54  |  |  | 
55  |  | void* winpr_aligned_calloc(size_t count, size_t size, size_t alignment)  | 
56  | 1.28M  | { | 
57  | 1.28M  |   return winpr_aligned_recalloc(NULL, count, size, alignment);  | 
58  | 1.28M  | }  | 
59  |  |  | 
60  |  | void* winpr_aligned_realloc(void* memblock, size_t size, size_t alignment)  | 
61  | 0  | { | 
62  | 0  |   return winpr_aligned_offset_realloc(memblock, size, alignment, 0);  | 
63  | 0  | }  | 
64  |  |  | 
65  |  | void* winpr_aligned_recalloc(void* memblock, size_t num, size_t size, size_t alignment)  | 
66  | 3.09M  | { | 
67  | 3.09M  |   return winpr_aligned_offset_recalloc(memblock, num, size, alignment, 0);  | 
68  | 3.09M  | }  | 
69  |  |  | 
70  |  | void* winpr_aligned_offset_malloc(size_t size, size_t alignment, size_t offset)  | 
71  | 6.67M  | { | 
72  | 6.67M  |   size_t header = 0;  | 
73  | 6.67M  |   size_t alignsize = 0;  | 
74  | 6.67M  |   uintptr_t basesize = 0;  | 
75  | 6.67M  |   void* base = NULL;  | 
76  | 6.67M  |   void* memblock = NULL;  | 
77  | 6.67M  |   WINPR_ALIGNED_MEM* pMem = NULL;  | 
78  |  |  | 
79  |  |   /* alignment must be a power of 2 */  | 
80  | 6.67M  |   if (alignment % 2 == 1)  | 
81  | 0  |     return NULL;  | 
82  |  |  | 
83  |  |   /* offset must be less than size */  | 
84  | 6.67M  |   if (offset >= size)  | 
85  | 0  |     return NULL;  | 
86  |  |  | 
87  |  |   /* minimum alignment is pointer size */  | 
88  | 6.67M  |   if (alignment < sizeof(void*))  | 
89  | 0  |     alignment = sizeof(void*);  | 
90  |  |  | 
91  | 6.67M  |   if (alignment > SIZE_MAX - sizeof(WINPR_ALIGNED_MEM))  | 
92  | 0  |     return NULL;  | 
93  |  |  | 
94  | 6.67M  |   header = sizeof(WINPR_ALIGNED_MEM) + alignment;  | 
95  |  |  | 
96  | 6.67M  |   if (size > SIZE_MAX - header)  | 
97  | 0  |     return NULL;  | 
98  |  |  | 
99  | 6.67M  |   alignsize = size + header;  | 
100  |  |   /* malloc size + alignment to make sure we can align afterwards */  | 
101  |  | #if defined(_ISOC11_SOURCE)  | 
102  |  |   base = aligned_alloc(alignment, alignsize);  | 
103  |  | #elif defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) || (_XOPEN_SOURCE >= 600)  | 
104  | 6.67M  |   if (posix_memalign(&base, alignment, alignsize) != 0)  | 
105  | 0  |     return NULL;  | 
106  |  | #else  | 
107  |  |   base = malloc(alignsize);  | 
108  |  | #endif  | 
109  | 6.67M  |   if (!base)  | 
110  | 0  |     return NULL;  | 
111  |  |  | 
112  | 6.67M  |   basesize = (uintptr_t)base;  | 
113  |  |  | 
114  | 6.67M  |   if ((offset > UINTPTR_MAX) || (header > UINTPTR_MAX - offset) ||  | 
115  | 6.67M  |       (basesize > UINTPTR_MAX - header - offset))  | 
116  | 0  |   { | 
117  | 0  |     free(base);  | 
118  | 0  |     return NULL;  | 
119  | 0  |   }  | 
120  |  |  | 
121  | 6.67M  |   memblock = (void*)(((basesize + header + offset) & ~(alignment - 1)) - offset);  | 
122  | 6.67M  |   pMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(memblock);  | 
123  | 6.67M  |   pMem->sig = WINPR_ALIGNED_MEM_SIGNATURE;  | 
124  | 6.67M  |   pMem->base_addr = base;  | 
125  | 6.67M  |   pMem->size = size;  | 
126  | 6.67M  |   return memblock;  | 
127  | 6.67M  | }  | 
128  |  |  | 
129  |  | void* winpr_aligned_offset_realloc(void* memblock, size_t size, size_t alignment, size_t offset)  | 
130  | 0  | { | 
131  | 0  |   size_t copySize = 0;  | 
132  | 0  |   void* newMemblock = NULL;  | 
133  | 0  |   WINPR_ALIGNED_MEM* pMem = NULL;  | 
134  | 0  |   WINPR_ALIGNED_MEM* pNewMem = NULL;  | 
135  |  | 
  | 
136  | 0  |   if (!memblock)  | 
137  | 0  |     return winpr_aligned_offset_malloc(size, alignment, offset);  | 
138  |  |  | 
139  | 0  |   pMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(memblock);  | 
140  |  | 
  | 
141  | 0  |   if (pMem->sig != WINPR_ALIGNED_MEM_SIGNATURE)  | 
142  | 0  |   { | 
143  | 0  |     WLog_ERR(TAG,  | 
144  | 0  |              "_aligned_offset_realloc: memory block was not allocated by _aligned_malloc!");  | 
145  | 0  |     return NULL;  | 
146  | 0  |   }  | 
147  |  |  | 
148  | 0  |   if (size == 0)  | 
149  | 0  |   { | 
150  | 0  |     winpr_aligned_free(memblock);  | 
151  | 0  |     return NULL;  | 
152  | 0  |   }  | 
153  |  |  | 
154  | 0  |   newMemblock = winpr_aligned_offset_malloc(size, alignment, offset);  | 
155  |  | 
  | 
156  | 0  |   if (!newMemblock)  | 
157  | 0  |     return NULL;  | 
158  |  |  | 
159  | 0  |   pNewMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(newMemblock);  | 
160  | 0  |   copySize = (pNewMem->size < pMem->size) ? pNewMem->size : pMem->size;  | 
161  | 0  |   CopyMemory(newMemblock, memblock, copySize);  | 
162  | 0  |   winpr_aligned_free(memblock);  | 
163  | 0  |   return newMemblock;  | 
164  | 0  | }  | 
165  |  |  | 
166  |  | static INLINE size_t cMIN(size_t a, size_t b)  | 
167  | 199  | { | 
168  | 199  |   if (a > b)  | 
169  | 0  |     return b;  | 
170  | 199  |   return a;  | 
171  | 199  | }  | 
172  |  |  | 
173  |  | void* winpr_aligned_offset_recalloc(void* memblock, size_t num, size_t size, size_t alignment,  | 
174  |  |                                     size_t offset)  | 
175  | 3.09M  | { | 
176  | 3.09M  |   char* newMemblock = NULL;  | 
177  | 3.09M  |   WINPR_ALIGNED_MEM* pMem = NULL;  | 
178  | 3.09M  |   WINPR_ALIGNED_MEM* pNewMem = NULL;  | 
179  |  |  | 
180  | 3.09M  |   if (!memblock)  | 
181  | 3.09M  |   { | 
182  | 3.09M  |     newMemblock = winpr_aligned_offset_malloc(size * num, alignment, offset);  | 
183  |  |  | 
184  | 3.09M  |     if (newMemblock)  | 
185  | 3.09M  |     { | 
186  | 3.09M  |       pNewMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(newMemblock);  | 
187  | 3.09M  |       ZeroMemory(newMemblock, pNewMem->size);  | 
188  | 3.09M  |     }  | 
189  |  |  | 
190  | 3.09M  |     return newMemblock;  | 
191  | 3.09M  |   }  | 
192  |  |  | 
193  | 199  |   pMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(memblock);  | 
194  |  |  | 
195  | 199  |   if (pMem->sig != WINPR_ALIGNED_MEM_SIGNATURE)  | 
196  | 0  |   { | 
197  | 0  |     WLog_ERR(TAG,  | 
198  | 0  |              "_aligned_offset_recalloc: memory block was not allocated by _aligned_malloc!");  | 
199  | 0  |     goto fail;  | 
200  | 0  |   }  | 
201  |  |  | 
202  | 199  |   if ((num == 0) || (size == 0))  | 
203  | 0  |     goto fail;  | 
204  |  |  | 
205  | 199  |   if (pMem->size > (1ull * num * size) + alignment)  | 
206  | 0  |     return memblock;  | 
207  |  |  | 
208  | 199  |   newMemblock = winpr_aligned_offset_malloc(size * num, alignment, offset);  | 
209  |  |  | 
210  | 199  |   if (!newMemblock)  | 
211  | 0  |     goto fail;  | 
212  |  |  | 
213  | 199  |   pNewMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(newMemblock);  | 
214  | 199  |   { | 
215  | 199  |     const size_t csize = cMIN(pMem->size, pNewMem->size);  | 
216  | 199  |     memcpy(newMemblock, memblock, csize);  | 
217  | 199  |     ZeroMemory(newMemblock + csize, pNewMem->size - csize);  | 
218  | 199  |   }  | 
219  | 199  | fail:  | 
220  | 199  |   winpr_aligned_free(memblock);  | 
221  | 199  |   return newMemblock;  | 
222  | 199  | }  | 
223  |  |  | 
224  |  | size_t winpr_aligned_msize(void* memblock, size_t alignment, size_t offset)  | 
225  | 0  | { | 
226  | 0  |   WINPR_ALIGNED_MEM* pMem = NULL;  | 
227  |  | 
  | 
228  | 0  |   if (!memblock)  | 
229  | 0  |     return 0;  | 
230  |  |  | 
231  | 0  |   pMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(memblock);  | 
232  |  | 
  | 
233  | 0  |   if (pMem->sig != WINPR_ALIGNED_MEM_SIGNATURE)  | 
234  | 0  |   { | 
235  | 0  |     WLog_ERR(TAG, "_aligned_msize: memory block was not allocated by _aligned_malloc!");  | 
236  | 0  |     return 0;  | 
237  | 0  |   }  | 
238  |  |  | 
239  | 0  |   return pMem->size;  | 
240  | 0  | }  | 
241  |  |  | 
242  |  | void winpr_aligned_free(void* memblock)  | 
243  | 867M  | { | 
244  | 867M  |   WINPR_ALIGNED_MEM* pMem = NULL;  | 
245  |  |  | 
246  | 867M  |   if (!memblock)  | 
247  | 860M  |     return;  | 
248  |  |  | 
249  | 6.67M  |   pMem = WINPR_ALIGNED_MEM_STRUCT_FROM_PTR(memblock);  | 
250  |  |  | 
251  | 6.67M  |   if (pMem->sig != WINPR_ALIGNED_MEM_SIGNATURE)  | 
252  | 0  |   { | 
253  | 0  |     WLog_ERR(TAG, "_aligned_free: memory block was not allocated by _aligned_malloc!");  | 
254  | 0  |     return;  | 
255  | 0  |   }  | 
256  |  |  | 
257  | 6.67M  |   free(pMem->base_addr);  | 
258  | 6.67M  | }  | 
259  |  |  | 
260  |  | #endif /* _WIN32 */  |