aa9f92d4d713f309edcd5eae5408c0998f26114d
[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 <iostream>
29 #include <stdlib.h>
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     if (!posix_memalign(&allocated, 16, count * sizeof(float)))
112 #endif
113         allocated = malloc(count * sizeof(float));
114     for (int i = 0; i < count; ++i) ((float *)allocated)[i] = 0.f;
115     return (float *)allocated;
116 }
117
118 float *allocFloat(int count)
119 {
120     return allocFloat(0, count);
121 }
122
123 void freeFloat(float *ptr)
124 {
125     if (ptr) free(ptr);
126 }
127       
128 double *allocDouble(double *ptr, int count)
129 {
130     if (ptr) free((void *)ptr);
131     void *allocated;
132 #ifndef _WIN32
133     if (!posix_memalign(&allocated, 16, count * sizeof(double)))
134 #endif
135         allocated = malloc(count * sizeof(double));
136     for (int i = 0; i < count; ++i) ((double *)allocated)[i] = 0.f;
137     return (double *)allocated;
138 }
139
140 double *allocDouble(int count)
141 {
142     return allocDouble(0, count);
143 }
144
145 void freeDouble(double *ptr)
146 {
147     if (ptr) free(ptr);
148 }
149  
150
151 }
152
153
154