add (copy of 2.0-ongoing) rubberband to 3.0
[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 <cstdio>
24 #include <cstring>
25 #endif /* !__APPLE__, !_WIN32 */
26 #endif /* !_WIN32 */
27
28 #include <cstdlib>
29 #include <iostream>
30
31 namespace RubberBand {
32
33 bool
34 system_is_multiprocessor()
35 {
36     static bool tested = false, mp = false;
37
38     if (tested) return mp;
39     int count = 0;
40
41 #ifdef _WIN32
42
43     SYSTEM_INFO sysinfo;
44     GetSystemInfo(&sysinfo);
45     count = sysinfo.dwNumberOfProcessors;
46
47 #else /* !_WIN32 */
48 #ifdef __APPLE__
49     
50     size_t sz = sizeof(count);
51     if (sysctlbyname("hw.ncpu", &count, &sz, NULL, 0)) {
52         mp = false;
53     } else {
54         mp = (count > 1);
55     }
56
57 #else /* !__APPLE__, !_WIN32 */
58     
59     //...
60
61     FILE *cpuinfo = fopen("/proc/cpuinfo", "r");
62     if (!cpuinfo) return false;
63
64     char buf[256];
65     while (!feof(cpuinfo)) {
66         fgets(buf, 256, cpuinfo);
67         if (!strncmp(buf, "processor", 9)) {
68             ++count;
69         }
70         if (count > 1) break;
71     }
72
73     fclose(cpuinfo);
74
75 #endif /* !__APPLE__, !_WIN32 */
76 #endif /* !_WIN32 */
77
78     mp = (count > 1);
79     tested = true;
80     return mp;
81 }
82
83 #ifdef _WIN32
84
85 int gettimeofday(struct timeval *tv, void *tz)
86 {
87     union { 
88         long long ns100;  
89         FILETIME ft; 
90     } now; 
91     
92     ::GetSystemTimeAsFileTime(&now.ft); 
93     tv->tv_usec = (long)((now.ns100 / 10LL) % 1000000LL); 
94     tv->tv_sec = (long)((now.ns100 - 116444736000000000LL) / 10000000LL); 
95     return 0;
96 }
97
98 void usleep(unsigned long usec)
99 {
100     ::Sleep(usec == 0 ? 0 : usec < 1000 ? 1 : usec / 1000);
101 }
102
103 #endif
104
105
106 float *allocFloat(float *ptr, int count)
107 {
108     if (ptr) free((void *)ptr);
109     void *allocated;
110 #ifndef _WIN32
111 #ifndef __APPLE__
112     if (!posix_memalign(&allocated, 16, count * sizeof(float)))
113 #endif
114 #endif
115         allocated = malloc(count * sizeof(float));
116     for (int i = 0; i < count; ++i) ((float *)allocated)[i] = 0.f;
117     return (float *)allocated;
118 }
119
120 float *allocFloat(int count)
121 {
122     return allocFloat(0, count);
123 }
124
125 void freeFloat(float *ptr)
126 {
127     if (ptr) free(ptr);
128 }
129       
130 double *allocDouble(double *ptr, int count)
131 {
132     if (ptr) free((void *)ptr);
133     void *allocated;
134 #ifndef _WIN32
135 #ifndef __APPLE__
136     if (!posix_memalign(&allocated, 16, count * sizeof(double)))
137 #endif
138 #endif
139         allocated = malloc(count * sizeof(double));
140     for (int i = 0; i < count; ++i) ((double *)allocated)[i] = 0.f;
141     return (double *)allocated;
142 }
143
144 double *allocDouble(int count)
145 {
146     return allocDouble(0, count);
147 }
148
149 void freeDouble(double *ptr)
150 {
151     if (ptr) free(ptr);
152 }
153  
154
155 }
156
157
158