Review and cleanup the raop.c a little bit
[deb_shairplay.git] / src / lib / memalign.h
1 /**
2 * Copyright (C) 2011-2012 Juho Vähä-Herttua
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 */
14
15 #ifndef MEMALIGN_H
16 #define MEMALIGN_H
17
18 #if defined(WIN32)
19
20 #define SYSTEM_GET_PAGESIZE(ret) do {\
21 SYSTEM_INFO si;\
22 GetSystemInfo(&si);\
23 ret = si.dwPageSize;\
24 } while(0)
25 #define SYSTEM_GET_TIME(ret) ret = timeGetTime()
26
27 #define ALIGNED_MALLOC(memptr, alignment, size) do {\
28 char *ptr = malloc(sizeof(void*) + (size) + (alignment)-1);\
29 memptr = NULL;\
30 if (ptr) {\
31 size_t ptrval = (size_t)ptr + sizeof(void*) + (alignment)-1;\
32 ptrval = ptrval / (alignment) * (alignment);\
33 memptr = (void *)ptrval;\
34 *(((void **)memptr)-1) = ptr;\
35 }\
36 } while(0)
37 #define ALIGNED_FREE(memptr) free(*(((void **)memptr)-1))
38
39 #else
40
41 #define SYSTEM_GET_PAGESIZE(ret) ret = sysconf(_SC_PAGESIZE)
42 #define SYSTEM_GET_TIME(ret) do {\
43 struct timeval tv;\
44 gettimeofday(&tv, NULL);\
45 ret = (unsigned int)(tv.tv_sec*1000 + tv.tv_usec/1000);\
46 } while(0)
47
48 #define ALIGNED_MALLOC(memptr, alignment, size) if (posix_memalign((void **)&memptr, alignment, size)) memptr = NULL
49 #define ALIGNED_FREE(memptr) free(memptr)
50
51 #endif
52
53 #endif