1 /* This file is part of Xpra.
2  * Copyright (C) 2012, 2013 Antoine Martin <antoine@xpra.org>
3  * Xpra is released under the terms of the GNU GPL v2, or, at your option, any
4  * later version. See the file COPYING for details.
5  */
6 
7 #include <stdlib.h>
8 #include "memalign.h"
9 
10 //not honoured on MS Windows:
11 #define MEMALIGN 1
12 
13 #ifdef _WIN32
14 #define _STDINT_H
15 #endif
16 #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__DragonFly__) \
17 		&& !defined(__OpenBSD__)
18 #include <malloc.h>
19 #endif
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
pad(int size)25 int pad(int size) {
26     return (size + MEMALIGN_ALIGNMENT - 1) & ~(MEMALIGN_ALIGNMENT - 1);
27 }
28 
xmemalign(size_t size)29 void *xmemalign(size_t size)
30 {
31 #ifdef MEMALIGN
32 #ifdef _WIN32
33 	//_aligned_malloc and _aligned_free lead to a memleak
34 	//well done Microsoft, I didn't think you could screw up this badly
35 	//and thank you for wasting my time once again
36 	return malloc(size);
37 #elif defined(__APPLE__) || defined(__OSX__)
38 	//Crapple version: "all memory allocations are 16-byte aligned"
39 	//no choice, this is what you get
40 	return malloc(size);
41 #else
42 	//not WIN32 and not APPLE/OSX, assume POSIX:
43 	void *memptr = NULL;
44 	if (posix_memalign(&memptr, MEMALIGN_ALIGNMENT, size))
45 		return NULL;
46 	return memptr;
47 #endif
48 //MEMALIGN not set:
49 #else
50 	return malloc(size);
51 #endif
52 }
53 
54 #ifdef __cplusplus
55 }
56 #endif
57