posix_memalign fixes from RB svn
[ardour.git] / libs / rubberband / src / sysutils.cpp
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2
3 /*
4     Rubber Band
5     An audio time-stretching and pitch-shifting library.
6     Copyright 2007-2008 Chris Cannam.
7     
8     This program is free software; you can redistribute it and/or
9     modify it under the terms of the GNU General Public License as
10     published by the Free Software Foundation; either version 2 of the
11     License, or (at your option) any later version.  See the file
12     COPYING included with this distribution for more information.
13 */
14
15 #include "sysutils.h"
16
17 #ifdef _WIN32
18 #include <windows.h>
19 #else /* !_WIN32 */
20 #ifdef __APPLE__
21 #include <sys/sysctl.h>
22 #else /* !__APPLE__, !_WIN32 */
23 #include <stdio.h>
24 #include <string.h>
25 #endif /* !__APPLE__, !_WIN32 */
26 #endif /* !_WIN32 */
27
28 #include <cstdlib>
29 #include <iostream>
30 #include <stdlib.h>
31
32 namespace RubberBand {
33
34 bool
35 system_is_multiprocessor()
36 {
37     static bool tested = false, mp = false;
38
39     if (tested) return mp;
40     int count = 0;
41
42 #ifdef _WIN32
43
44     SYSTEM_INFO sysinfo;
45     GetSystemInfo(&sysinfo);
46     count = sysinfo.dwNumberOfProcessors;
47
48 #else /* !_WIN32 */
49 #ifdef __APPLE__
50     
51     size_t sz = sizeof(count);
52     if (sysctlbyname("hw.ncpu", &count, &sz, NULL, 0)) {
53         mp = false;
54     } else {
55         mp = (count > 1);
56     }
57
58 #else /* !__APPLE__, !_WIN32 */
59     
60     //...
61
62     FILE *cpuinfo = fopen("/proc/cpuinfo", "r");
63     if (!cpuinfo) return false;
64
65     char buf[256];
66     while (!feof(cpuinfo)) {
67         fgets(buf, 256, cpuinfo);
68         if (!strncmp(buf, "processor", 9)) {
69             ++count;
70         }
71         if (count > 1) break;
72     }
73
74     fclose(cpuinfo);
75
76 #endif /* !__APPLE__, !_WIN32 */
77 #endif /* !_WIN32 */
78
79     mp = (count > 1);
80     tested = true;
81     return mp;
82 }
83
84 #ifdef _WIN32
85
86 int gettimeofday(struct timeval *tv, void *tz)
87 {
88     union { 
89         long long ns100;  
90         FILETIME ft; 
91     } now; 
92     
93     ::GetSystemTimeAsFileTime(&now.ft); 
94     tv->tv_usec = (long)((now.ns100 / 10LL) % 1000000LL); 
95     tv->tv_sec = (long)((now.ns100 - 116444736000000000LL) / 10000000LL); 
96     return 0;
97 }
98
99 void usleep(unsigned long usec)
100 {
101     ::Sleep(usec == 0 ? 0 : usec < 1000 ? 1 : usec / 1000);
102 }
103
104 #endif
105
106
107 float *allocFloat(float *ptr, int count)
108 {
109     if (ptr) free((void *)ptr);
110     void *allocated;
111 #ifndef _WIN32
112 #ifndef __APPLE__
113     if (!posix_memalign(&allocated, 16, count * sizeof(float)))
114 #endif
115 #endif
116         allocated = malloc(count * sizeof(float));
117     for (int i = 0; i < count; ++i) ((float *)allocated)[i] = 0.f;
118     return (float *)allocated;
119 }
120
121 float *allocFloat(int count)
122 {
123     return allocFloat(0, count);
124 }
125
126 void freeFloat(float *ptr)
127 {
128     if (ptr) free(ptr);
129 }
130       
131 double *allocDouble(double *ptr, int count)
132 {
133     if (ptr) free((void *)ptr);
134     void *allocated;
135 #ifndef _WIN32
136 #ifndef __APPLE__
137     if (!posix_memalign(&allocated, 16, count * sizeof(double)))
138 #endif
139 #endif
140         allocated = malloc(count * sizeof(double));
141     for (int i = 0; i < count; ++i) ((double *)allocated)[i] = 0.f;
142     return (double *)allocated;
143 }
144
145 double *allocDouble(int count)
146 {
147     return allocDouble(0, count);
148 }
149
150 void freeDouble(double *ptr)
151 {
152     if (ptr) free(ptr);
153 }
154  
155
156 }
157
158
159