1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
|
/**
@file System.cpp
@maintainer Morgan McGuire, matrix@graphics3d.com
Note: every routine must call init() first.
There are two kinds of detection used in this file. At compile
time, the _MSC_VER #define is used to determine whether x86 assembly
can be used at all. At runtime, processor detection is used to
determine if we can safely call the routines that use that assembly.
@cite Rob Wyatt http://www.gamasutra.com/features/wyatts_world/19990709/processor_detection_01.htm
@cite Benjamin Jurke http://www.flipcode.com/cgi-bin/msg.cgi?showThread=COTD-ProcessorDetectionClass&forum=cotd&id=-1
@cite Michael Herf http://www.stereopsis.com/memcpy.html
@created 2003-01-25
@edited 2008-09-02
*/
#include "G3D/platform.h"
#include "G3D/System.h"
#include "G3D/debug.h"
#include "G3D/fileutils.h"
#include "G3D/TextOutput.h"
#include "G3D/G3DGameUnits.h"
#include "G3D/Crypto.h"
#include "G3D/prompt.h"
#include "G3D/Log.h"
#include <time.h>
#include <cstring>
#include <cstdio>
// Uncomment the following line to turn off G3D::System memory
// allocation and use the operating system's malloc.
//#define NO_BUFFERPOOL
#if defined(__i386__) || defined(__x86_64__) || defined(G3D_WIN32)
# define G3D_NOT_OSX_PPC
#endif
#ifdef G3D_WIN32
# include <conio.h>
# include <sys/timeb.h>
# include "G3D/RegistryUtil.h"
#elif defined(G3D_LINUX)
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/select.h>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <pthread.h>
#elif defined(G3D_OSX)
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <termios.h>
#include <unistd.h>
#include <pthread.h>
#include <mach-o/arch.h>
#include <sstream>
#include <CoreServices/CoreServices.h>
#endif
#if defined(SSE)
#include <xmmintrin.h>
#endif
namespace G3D {
struct CpuInfo
{
public:
int m_cpuSpeed;
bool m_hasCPUID;
bool m_hasRDTSC;
bool m_hasMMX;
bool m_hasSSE;
bool m_hasSSE2;
bool m_hasSSE3;
bool m_has3DNOW;
char m_cpuVendorStr[1024];
};
// helper macro to call cpuid functions and return all values
#ifdef _MSC_VER
// VC on Intel
# define CALL_CPUID(func, areg, breg, creg, dreg) \
__asm mov eax, func \
__asm cpuid \
__asm mov areg, eax \
__asm mov breg, ebx \
__asm mov creg, ecx \
__asm mov dreg, edx
#elif defined(__GNUC__) && defined(G3D_OSX_INTEL)
// GCC on OS X intel
# define CALL_CPUID(func, areg, breg, creg, dreg) \
areg = 0; \
breg = 0; \
creg = 0; \
dreg = 0;
#else
// Any other compiler/platform, likely GCC
# define CALL_CPUID(func, areg, breg, creg, dreg) \
__asm__ ( \
"cpuid \n": \
"=a" (areg), \
"=b" (breg), \
"=c" (creg), \
"=d" (dreg): \
"a" (func) \
);
#endif
// this holds the data directory set by the application (currently GApp) for use by findDataFile
static char g_appDataDir[FILENAME_MAX] = "";
static CpuInfo g_cpuInfo = {
0, false, false, false, false, false, false, false, {'U', 'n', 'k', 'n', 'o', 'w', 'n', '\0'}};
static G3DEndian _machineEndian = G3D_LITTLE_ENDIAN;
static char _cpuArchCstr[1024];
static char _operatingSystemCstr[1024];
#ifdef G3D_WIN32
/** Used by getTick() for timing */
static LARGE_INTEGER _start;
static LARGE_INTEGER _counterFrequency;
#else
static struct timeval _start;
#endif
static char versionCstr[1024];
System::OutOfMemoryCallback System::outOfMemoryCallback = NULL;
#ifdef G3D_OSX
long System::m_OSXCPUSpeed;
double System::m_secondsPerNS;
#endif
/** The Real-World time of System::getTick() time 0. Set by initTime */
static RealTime realWorldGetTickTime0;
static unsigned int maxSupportedCPUIDLevel = 0;
static unsigned int maxSupportedExtendedLevel = 0;
/** Checks if the CPUID command is available on the processor (called from init) */
static void checkForCPUID();
/** ReadRead the standard processor extensions. Called from init(). */
static void getStandardProcessorExtensions();
/** Called from init */
static void initTime();
std::string System::findDataFile
(const std::string& full,
bool errorIfNotFound) {
if (fileExists(full)) {
return full;
}
std::string initialAppDataDir(g_appDataDir);
std::string name = filenameBaseExt(full);
std::string originalPath = filenamePath(full);
// Search several paths
Array<std::string> pathBase;
int backlen = 4;
// add what should be the current working directory
pathBase.append("");
// add application specified data directory to be searched first
pathBase.append(initialAppDataDir);
// try walking back along the directory tree
std::string prev = "";
for (int i = 0; i < backlen; ++i) {
pathBase.append(originalPath + prev);
prev = prev + "../";
}
prev = "../";
for (int i = 0; i < backlen; ++i) {
pathBase.append(prev);
prev = prev + "../";
}
// Hard-code in likely install directories
int ver = G3D_VER;
std::string lname = format("G3D-%d.%02d", ver / 10000, (ver / 100) % 100);
if (G3D_VER % 100 != 0) {
lname = lname + format("-b%02d/", ver % 100);
} else {
lname = lname + "/";
}
// Look in some other likely places
# ifdef G3D_WIN32
std::string lpath = "libraries/G3D/";
pathBase.append(std::string("c:/") + lpath);
pathBase.append(std::string("d:/") + lpath);
pathBase.append(std::string("e:/") + lpath);
pathBase.append(std::string("f:/") + lpath);
pathBase.append(std::string("g:/") + lpath);
pathBase.append(std::string("x:/") + lpath);
# endif
# if defined(G3D_LINUX)
pathBase.append("/usr/local/");
pathBase.append("/course/cs224/");
pathBase.append("/map/gfx0/common/games/");
# endif
# if defined(G3D_FREEBSD)
pathBase.append("/usr/local/");
pathBase.append("/usr/local/371/");
pathBase.append("/usr/cs-local/371/");
# endif
# if defined(G3D_OSX)
pathBase.append("/usr/local/" + lname);
pathBase.append("/Volumes/McGuire/Projects/");
# endif
// Add the library name to all variations
int N = pathBase.size();
for (int i = 0; i < N; ++i) {
pathBase.append(pathBase[i] + lname);
pathBase.append(pathBase[i] + "G3D/");
}
Array<std::string> subDir;
subDir.append("", "font/", "sky/", "gui/");
subDir.append("image/", "quake2/", "quake2/players/");
subDir.append("quake3/", "SuperShader/", "ifs/", "3ds/");
subDir.append("quake2/speedway/");
Array<std::string> path;
for (int p = 0; p < pathBase.size(); ++p) {
for (int s = 0; s < subDir.size(); ++s) {
path.append(pathBase[p] + subDir[s]);
path.append(pathBase[p] + "data/" + subDir[s]);
path.append(pathBase[p] + "data-files/" + subDir[s]);
}
}
for (int i = 0; i < path.length(); ++i) {
std::string filename = path[i] + name;
if (fileExists(filename)) {
logPrintf("\nWARNING: Could not find '%s' so '%s' "
"was substituted.\n", full.c_str(),
filename.c_str());
return filename;
}
}
if (errorIfNotFound) {
// Generate an error message
std::string locations;
for (int i = 0; i < path.size(); ++i) {
locations += path[i] + name + "\n";
}
alwaysAssertM(false, "Could not find '" + full + "' in:\n" + locations);
}
// Not found
return "";
}
void System::setAppDataDir(const std::string& path) {
// just copy the path, it needs to be valid
strncpy(g_appDataDir, path.c_str(), sizeof(g_appDataDir));
}
std::string demoFindData(bool errorIfNotFound) {
// Directories that might contain the data
Array<std::string> potential;
// Look back up the directory tree
std::string x = "../";
std::string f = "";
for (int i = 0; i < 6; ++i) {
potential.append(f);
f = f + x;
}
// Hard-code in likely install directories
int ver = G3D_VER;
std::string lname = format("G3D-%d.%02d", ver / 10000, (ver / 100) % 100);
if (G3D_VER % 100 != 0) {
lname = lname + format("-b%02d/", ver % 100);
} else {
lname = lname + "/";
}
std::string lpath = "libraries/" + lname;
#ifdef G3D_WIN32
potential.append(std::string("c:/") + lpath);
potential.append(std::string("d:/") + lpath);
potential.append(std::string("e:/") + lpath);
potential.append(std::string("f:/") + lpath);
potential.append(std::string("g:/") + lpath);
potential.append(std::string("x:/") + lpath);
#elif defined(G3D_LINUX)
potential.append("/usr/local/" + lname);
potential.append("/course/cs224/");
potential.append("/map/gfx0/common/games/");
#elif defined(G3D_FREEBSD)
potential.append("/usr/local/" + lname);
potential.append("/usr/local/371/")
potential.append("/usr/cs-local/371/")
#elif defined(G3D_OSX)
potential.append("/usr/local/" + lname);
potential.append("/Volumes/McGuire/Projects/");
potential.append("/Volumes/McGuire/Projects/G3D/");
#endif
// Scan all potentials for the font directory
for (int p = 0; p < potential.size(); ++p) {
std::string path = potential[p];
//debugPrintf("Looking at: %sdata\n", path.c_str());
if (fileExists(path + "data") && fileExists(path + "data/font")) {
return path + "data/";
}
if (fileExists(path + "data-files") && fileExists(path + "data-files/font")) {
return path + "data-files/";
}
}
if (errorIfNotFound) {
const char* choice[] = {"Exit"};
prompt("Demo Error", "The demo could not locate the data directory. "
"The data is required to run this demo. If you have not downloaded "
"the data zipfile, get it from http://g3d-cpp.sf.net. If you have "
"downloaded it, it needs to be no more than 4 directories above the "
"demo directory.", choice, 1, true);
}
return "";
}
bool System::hasCPUID() {
init();
return g_cpuInfo.m_hasCPUID;
}
bool System::hasRDTSC() {
init();
return g_cpuInfo.m_hasRDTSC;
}
bool System::hasSSE() {
init();
return g_cpuInfo.m_hasSSE;
}
bool System::hasSSE2() {
init();
return g_cpuInfo.m_hasSSE2;
}
bool System::hasSSE3() {
init();
return g_cpuInfo.m_hasSSE3;
}
bool System::hasMMX() {
init();
return g_cpuInfo.m_hasMMX;
}
bool System::has3DNow() {
init();
return g_cpuInfo.m_has3DNOW;
}
const std::string& System::cpuVendor() {
init();
static const std::string _cpuVendor = g_cpuInfo.m_cpuVendorStr;
return _cpuVendor;
}
G3DEndian System::machineEndian() {
init();
return _machineEndian;
}
const std::string& System::operatingSystem() {
init();
static const std::string _operatingSystem =_operatingSystemCstr;
return _operatingSystem;
}
const std::string& System::cpuArchitecture() {
init();
static const std::string _cpuArch = _cpuArchCstr;
return _cpuArch;
}
const std::string& System::build() {
const static std::string b =
# ifdef _DEBUG
"Debug";
# else
"Release";
# endif
return b;
}
const std::string& System::version() {
init();
static const std::string _version = versionCstr;
return _version;
}
void System::init() {
// Cannot use most G3D data structures or utility functions in here because
// they are not initialized.
static bool initialized = false;
if (initialized) {
return;
}
initialized = true;
if ((G3D_VER % 100) != 0) {
sprintf(versionCstr, "G3D %d.%02d beta %d",
G3D_VER / 10000,
(G3D_VER / 100) % 100,
G3D_VER % 100);
} else {
sprintf(versionCstr, "G3D %d.%02d",
G3D_VER / 10000,
(G3D_VER / 100) % 100);
}
// First of all we check if the CPUID command is available
checkForCPUID();
// Figure out if this machine is little or big endian.
{
int32 a = 1;
if (*(uint8*)&a == 1) {
_machineEndian = G3D_LITTLE_ENDIAN;
} else {
_machineEndian = G3D_BIG_ENDIAN;
}
}
# ifdef G3D_NOT_OSX_PPC
// Process the CPUID information
if (g_cpuInfo.m_hasCPUID) {
// We read the standard CPUID level 0x00000000 which should
// be available on every x86 processor. This fills out
// a string with the processor vendor tag.
unsigned int eaxreg = 0, ebxreg = 0, ecxreg = 0, edxreg = 0;
CALL_CPUID(0x00, eaxreg, ebxreg, ecxreg, edxreg);
// Then we connect the single register values to the vendor string
*((unsigned int*) g_cpuInfo.m_cpuVendorStr) = ebxreg;
*((unsigned int*) (g_cpuInfo.m_cpuVendorStr + 4)) = edxreg;
*((unsigned int*) (g_cpuInfo.m_cpuVendorStr + 8)) = ecxreg;
g_cpuInfo.m_cpuVendorStr[12] = '\0';
// We can also read the max. supported standard CPUID level
maxSupportedCPUIDLevel = eaxreg & 0xFFFF;
// Then we read the ext. CPUID level 0x80000000
CALL_CPUID(0x80000000, eaxreg, ebxreg, ecxreg, edxreg);
// ...to check the max. supported extended CPUID level
maxSupportedExtendedLevel = eaxreg;
// Then we switch to the specific processor vendors.
// Fill out _cpuArch based on this information. It will
// be overwritten by the next block of code on Windows,
// but on Linux will stand.
switch (ebxreg) {
case 0x756E6547: // GenuineIntel
strcpy(_cpuArchCstr, "Intel Processor");
break;
case 0x68747541: // AuthenticAMD
strcpy(_cpuArchCstr, "AMD Processor");
break;
case 0x69727943: // CyrixInstead
strcpy(_cpuArchCstr, "Cyrix Processor");
break;
default:
strcpy(_cpuArchCstr, "Unknown Processor Vendor");
break;
}
}
#endif // G3D_NOT_OSX_PPC
#ifdef G3D_WIN32
bool success = RegistryUtil::readInt32
("HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", "~MHz", g_cpuInfo.m_cpuSpeed);
SYSTEM_INFO systemInfo;
GetSystemInfo(&systemInfo);
char* arch;
switch (systemInfo.wProcessorArchitecture) {
case PROCESSOR_ARCHITECTURE_INTEL:
arch = "Intel";
break;
case PROCESSOR_ARCHITECTURE_MIPS:
arch = "MIPS";
break;
case PROCESSOR_ARCHITECTURE_ALPHA:
arch = "Alpha";
break;
case PROCESSOR_ARCHITECTURE_PPC:
arch = "Power PC";
break;
default:
arch = "Unknown";
}
uint32 maxAddr = (uint32)systemInfo.lpMaximumApplicationAddress;
sprintf(_cpuArchCstr, "%d x %d-bit %s processor",
systemInfo.dwNumberOfProcessors,
(int)(::log((double)maxAddr) / ::log(2.0) + 2.0),
arch);
// _CPUSpeed / (1024.0 * 1024));
OSVERSIONINFO osVersionInfo;
osVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
success = GetVersionEx(&osVersionInfo) != 0;
if (success) {
sprintf(_operatingSystemCstr, "Windows %d.%d build %d Platform %d %s",
osVersionInfo.dwMajorVersion,
osVersionInfo.dwMinorVersion,
osVersionInfo.dwBuildNumber,
osVersionInfo.dwPlatformId,
osVersionInfo.szCSDVersion);
} else {
strcpy(_operatingSystemCstr, "Windows");
}
#elif defined(G3D_LINUX)
{
// Shell out to the 'uname' command
FILE* f = popen("uname -a", "r");
int len = 100;
char* r = (char*)::malloc(len * sizeof(char));
fgets(r, len, f);
// Remove trailing newline
if (r[strlen(r) - 1] == '\n') {
r[strlen(r) - 1] = '\0';
}
fclose(f);
strcpy(_operatingSystemCstr, r);
::free(r);
}
#elif defined(G3D_OSX)
// Operating System:
SInt32 macVersion;
Gestalt(gestaltSystemVersion, &macVersion);
int major = (macVersion >> 8) & 0xFF;
int minor = (macVersion >> 4) & 0xF;
int revision = macVersion & 0xF;
sprintf(_operatingSystemCstr, "OS X %x.%x.%x", major, minor, revision);
// Clock Cycle Timing Information:
Gestalt('pclk', &System::m_OSXCPUSpeed);
g_cpuInfo.m_cpuSpeed = iRound((double)m_OSXCPUSpeed / (1024 * 1024));
m_secondsPerNS = 1.0 / 1.0e9;
// System Architecture:
const NXArchInfo* pInfo = NXGetLocalArchInfo();
if (pInfo) {
strcpy(_cpuArchCstr, pInfo->description);
switch (pInfo->cputype) {
case CPU_TYPE_POWERPC:
switch(pInfo->cpusubtype){
case CPU_SUBTYPE_POWERPC_750:
case CPU_SUBTYPE_POWERPC_7400:
case CPU_SUBTYPE_POWERPC_7450:
strcpy(g_cpuInfo.m_cpuVendorStr, "Motorola");
break;
case CPU_SUBTYPE_POWERPC_970:
strcpy(g_cpuInfo.m_cpuVendorStr, "IBM");
break;
}
break;
case CPU_TYPE_I386:
strcpy(g_cpuInfo.m_cpuVendorStr, "Intel");
break;
}
}
#endif
initTime();
getStandardProcessorExtensions();
}
static void checkForCPUID() {
unsigned int bitChanged = 0;
// We've to check if we can toggle the flag register bit 21.
// If we can't the processor does not support the CPUID command.
#if defined(_MSC_VER)
__asm {
push eax
push ebx
pushfd
pushfd
pop eax
mov ebx, eax
xor eax, 0x00200000
push eax
popfd
pushfd
pop eax
popfd
xor eax, ebx
mov bitChanged, eax
pop ebx
pop eax
}
#elif defined(__GNUC__) && defined(i386) && !defined(G3D_OSX_INTEL)
// 32-bit g++
__asm__ (
"pushfl # Get original EFLAGS \n"
"pushfl \n"
"popl %%eax \n"
"movl %%eax, %%ecx \n"
"xorl $0x200000, %%eax # Flip ID bit in EFLAGS \n"
"pushl %%eax # Save new EFLAGS value on stack \n"
"popfl # Replace current EFLAGS value \n"
"pushfl # Get new EFLAGS \n"
"popl %%eax # Store new EFLAGS in EAX \n"
"popfl \n"
"xorl %%ecx, %%eax # Can not toggle ID bit, \n"
"movl %%eax, %0 # We have CPUID support \n"
: "=m" (bitChanged)
: // No inputs
: "%eax", "%ecx"
);
#elif defined(__GNUC__) && defined(__x86_64__) && !defined(G3D_OSX_INTEL)
// x86_64 has SSE and CPUID
bitChanged = 1;
#else
// Unknown architecture
bitChanged = 0;
#endif
g_cpuInfo.m_hasCPUID = ((bitChanged == 0) ? false : true);
}
void getStandardProcessorExtensions() {
#if !defined(G3D_OSX) || defined(G3D_OSX_INTEL)
if (! g_cpuInfo.m_hasCPUID) {
return;
}
unsigned int eaxreg = 0, ebxreg = 0, ecxreg = 0;
unsigned int features = 0;
// http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/25481.pdf
// call cpuid with function 0x01 in EAX
// Invoking CPUID with '1' in EAX fills out edx with a bit string.
// The bits of this value indicate the presence or absence of
// useful processor features.
CALL_CPUID(0x01, eaxreg, ebxreg, ecxreg, features);
#define checkBit(var, bit) ((var & (1 << bit)) ? true : false)
g_cpuInfo.m_hasRDTSC = checkBit(features, 16);
g_cpuInfo.m_hasMMX = checkBit(features, 23);
g_cpuInfo.m_hasSSE = checkBit(features, 25);
g_cpuInfo.m_hasSSE2 = checkBit(features, 26);
g_cpuInfo.m_hasSSE3 = checkBit(ecxreg, 0);
if (maxSupportedExtendedLevel >= 0x80000001) {
// function 0x80000001 changes bit 31 of edx to 3dnow support flag
CALL_CPUID(0x80000001, eaxreg, ebxreg, ecxreg, features);
g_cpuInfo.m_has3DNOW = checkBit(features, 31);
} else {
g_cpuInfo.m_has3DNOW = false;
}
#undef checkBit
#endif
}
#if defined(SSE)
// Copy in 128 bytes chunks, where each chunk contains 8*float32x4 = 8 * 4 * 4 bytes = 128 bytes
//
//
void memcpySSE2(void* dst, const void* src, int nbytes) {
int remainingBytes = nbytes;
if (nbytes > 128) {
// Number of chunks
int N = nbytes / 128;
float* restrict d = (float*)dst;
const float* restrict s = (const float*)src;
// Finish when the destination pointer has moved 8N elements
float* stop = d + (N * 8 * 4);
while (d < stop) {
// Inner loop unrolled 8 times
const __m128 r0 = _mm_loadu_ps(s);
const __m128 r1 = _mm_loadu_ps(s + 4);
const __m128 r2 = _mm_loadu_ps(s + 8);
const __m128 r3 = _mm_loadu_ps(s + 12);
const __m128 r4 = _mm_loadu_ps(s + 16);
const __m128 r5 = _mm_loadu_ps(s + 20);
const __m128 r6 = _mm_loadu_ps(s + 24);
const __m128 r7 = _mm_loadu_ps(s + 28);
_mm_storeu_ps(d, r0);
_mm_storeu_ps(d + 4, r1);
_mm_storeu_ps(d + 8, r2);
_mm_storeu_ps(d + 12, r3);
_mm_storeu_ps(d + 16, r4);
_mm_storeu_ps(d + 20, r5);
_mm_storeu_ps(d + 24, r6);
_mm_storeu_ps(d + 28, r7);
s += 32;
d += 32;
}
remainingBytes -= N * 8 * 4 * 4;
}
if (remainingBytes > 0) {
// Memcpy the rest
memcpy((uint8*)dst + (nbytes - remainingBytes), (const uint8*)src + (nbytes - remainingBytes), remainingBytes);
}
}
#else
// Fall back to memcpy
void memcpySSE2(void *dst, const void *src, int nbytes) {
memcpy(dst, src, nbytes);
}
#endif
#if defined(G3D_WIN32) && defined(SSE)
/** Michael Herf's fast memcpy */
void memcpyMMX(void* dst, const void* src, int nbytes) {
int remainingBytes = nbytes;
if (nbytes > 64) {
_asm {
mov esi, src
mov edi, dst
mov ecx, nbytes
shr ecx, 6 // 64 bytes per iteration
loop1:
movq mm1, 0[ESI] // Read in source data
movq mm2, 8[ESI]
movq mm3, 16[ESI]
movq mm4, 24[ESI]
movq mm5, 32[ESI]
movq mm6, 40[ESI]
movq mm7, 48[ESI]
movq mm0, 56[ESI]
movntq 0[EDI], mm1 // Non-temporal stores
movntq 8[EDI], mm2
movntq 16[EDI], mm3
movntq 24[EDI], mm4
movntq 32[EDI], mm5
movntq 40[EDI], mm6
movntq 48[EDI], mm7
movntq 56[EDI], mm0
add esi, 64
add edi, 64
dec ecx
jnz loop1
emms
}
remainingBytes -= ((nbytes >> 6) << 6);
}
if (remainingBytes > 0) {
// Memcpy the rest
memcpy((uint8*)dst + (nbytes - remainingBytes), (const uint8*)src + (nbytes - remainingBytes), remainingBytes);
}
}
#else
// Fall back to memcpy
void memcpyMMX(void *dst, const void *src, int nbytes) {
memcpy(dst, src, nbytes);
}
#endif
void System::memcpy(void* dst, const void* src, size_t numBytes) {
if (System::hasSSE2() && System::hasMMX()) {
G3D::memcpyMMX(dst, src, numBytes);
} else if (System::hasSSE() && System::hasMMX()) {
G3D::memcpyMMX(dst, src, numBytes);
} else {
::memcpy(dst, src, numBytes);
}
}
/** Michael Herf's fastest memset. n32 must be filled with the same
character repeated. */
#if defined(G3D_WIN32) && defined(SSE)
// On x86 processors, use MMX
void memfill(void *dst, int n32, unsigned long i) {
int originalSize = i;
int bytesRemaining = i;
if (i > 16) {
bytesRemaining = i % 16;
i -= bytesRemaining;
__asm {
movq mm0, n32
punpckldq mm0, mm0
mov edi, dst
loopwrite:
movntq 0[edi], mm0
movntq 8[edi], mm0
add edi, 16
sub i, 16
jg loopwrite
emms
}
}
if (bytesRemaining > 0) {
::memset((uint8*)dst + (originalSize - bytesRemaining), n32, bytesRemaining);
}
}
#else
// For non x86 processors, we fall back to the standard memset
void memfill(void *dst, int n32, unsigned long i) {
::memset(dst, n32, i);
}
#endif
void System::memset(void* dst, uint8 value, size_t numBytes) {
if (System::hasSSE() && System::hasMMX()) {
uint32 v = value;
v = v + (v << 8) + (v << 16) + (v << 24);
G3D::memfill(dst, v, numBytes);
} else {
::memset(dst, value, numBytes);
}
}
std::string& System::appName() {
static std::string n = filenameBase(currentProgramFilename());
return n;
}
std::string System::currentProgramFilename() {
char filename[2048];
#ifdef G3D_WIN32
{
GetModuleFileNameA(NULL, filename, sizeof(filename));
}
#else
{
int ret = readlink("/proc/self/exe", filename, sizeof(filename));
// In case of an error, leave the handling up to the caller
if (ret == -1) {
return "";
}
debugAssert((int)sizeof(filename) > ret);
// Ensure proper NULL termination
filename[ret] = 0;
}
#endif
return filename;
}
void System::sleep(RealTime t) {
// Overhead of calling this function.
static const RealTime OVERHEAD = .000006;
RealTime now = time();
RealTime wakeupTime = now + t - OVERHEAD;
RealTime remainingTime = wakeupTime - now;
RealTime sleepTime = 0;
while (remainingTime > 0) {
if (remainingTime > 0.001) {
// Safe to use Sleep with a time... sleep for half the remaining time
sleepTime = max(remainingTime * .5, 0.0005);
} else if (remainingTime > 0.0001) {
// Safe to use Sleep with a zero time;
// causes the program to yield only
// the current time slice, and then return.
sleepTime = 0;
} else {
// Not safe to use Sleep; busy wait
sleepTime = -1;
}
if (sleepTime >= 0) {
#ifdef G3D_WIN32
// Translate to milliseconds
Sleep((int)(sleepTime * 1e3));
#else
// Translate to microseconds
usleep((int)(sleepTime * 1e6));
#endif
}
now = time();
remainingTime = wakeupTime - now;
}
}
void System::consoleClearScreen() {
#ifdef G3D_WIN32
system("cls");
#else
system("clear");
#endif
}
bool System::consoleKeyPressed() {
#ifdef G3D_WIN32
return _kbhit() != 0;
#else
static const int STDIN = 0;
static bool initialized = false;
if (! initialized) {
// Use termios to turn off line buffering
termios term;
tcgetattr(STDIN, &term);
term.c_lflag &= ~ICANON;
tcsetattr(STDIN, TCSANOW, &term);
setbuf(stdin, NULL);
initialized = true;
}
#ifdef G3D_LINUX
int bytesWaiting;
ioctl(STDIN, FIONREAD, &bytesWaiting);
return bytesWaiting;
#else
timeval timeout;
fd_set rdset;
FD_ZERO(&rdset);
FD_SET(STDIN, &rdset);
timeout.tv_sec = 0;
timeout.tv_usec = 0;
return select(STDIN + 1, &rdset, NULL, NULL, &timeout);
#endif
#endif
}
int System::consoleReadKey() {
#ifdef G3D_WIN32
return _getch();
#else
char c;
read(0, &c, 1);
return c;
#endif
}
void initTime() {
#ifdef G3D_WIN32
if (QueryPerformanceFrequency(&_counterFrequency)) {
QueryPerformanceCounter(&_start);
}
struct _timeb t;
_ftime(&t);
realWorldGetTickTime0 = (RealTime)t.time - t.timezone * G3D::MINUTE + (t.dstflag ? G3D::HOUR : 0);
#else
gettimeofday(&_start, NULL);
// "sse" = "seconds since epoch". The time
// function returns the seconds since the epoch
// GMT (perhaps more correctly called UTC).
time_t gmt = time(NULL);
// No call to free or delete is needed, but subsequent
// calls to asctime, ctime, mktime, etc. might overwrite
// local_time_vals.
tm* localTimeVals = localtime(&gmt);
time_t local = gmt;
if (localTimeVals) {
// tm_gmtoff is already corrected for daylight savings.
local = local + localTimeVals->tm_gmtoff;
}
realWorldGetTickTime0 = local;
#endif
}
RealTime System::time() {
init();
#ifdef G3D_WIN32
LARGE_INTEGER now;
QueryPerformanceCounter(&now);
return ((RealTime)(now.QuadPart - _start.QuadPart) /
_counterFrequency.QuadPart) + realWorldGetTickTime0;
#else
// Linux resolution defaults to 100Hz.
// There is no need to do a separate RDTSC call as gettimeofday
// actually uses RDTSC when on systems that support it, otherwise
// it uses the system clock.
struct timeval now;
gettimeofday(&now, NULL);
return (now.tv_sec - _start.tv_sec) +
(now.tv_usec - _start.tv_usec) / 1e6
+ realWorldGetTickTime0;
#endif
}
////////////////////////////////////////////////////////////////
#define REALPTR_TO_USERPTR(x) ((uint8*)(x) + sizeof (void *))
#define USERPTR_TO_REALPTR(x) ((uint8*)(x) - sizeof (void *))
#define REALBLOCK_SIZE(x) ((x) + sizeof (void *))
class BufferPool {
public:
/** Only store buffers up to these sizes (in bytes) in each pool->
Different pools have different management strategies.
A large block is preallocated for tiny buffers; they are used with
tremendous frequency. Other buffers are allocated as demanded.
Tiny buffers are 128 bytes long because that seems to align well with
cache sizes on many machines.
*/
enum {tinyBufferSize = 128, smallBufferSize = 1024, medBufferSize = 4096};
/**
Most buffers we're allowed to store.
128000 * 128 = 16 MB (preallocated)
2048 * 1024 = 2 MB (allocated on demand)
1024 * 4096 = 4 MB (allocated on demand)
*/
enum {maxTinyBuffers = 128000, maxSmallBuffers = 2048, maxMedBuffers = 1024};
private:
class MemBlock {
public:
void* ptr;
size_t bytes;
inline MemBlock() : ptr(NULL), bytes(0) {}
inline MemBlock(void* p, size_t b) : ptr(p), bytes(b) {}
};
MemBlock smallPool[maxSmallBuffers];
int smallPoolSize;
MemBlock medPool[maxMedBuffers];
int medPoolSize;
/** The tiny pool is a single block of storage into which all tiny
objects are allocated. This provides better locality for
small objects and avoids the search time, since all tiny
blocks are exactly the same size. */
void* tinyPool[maxTinyBuffers];
int tinyPoolSize;
/** Pointer to the data in the tiny pool */
void* tinyHeap;
# ifdef G3D_WIN32
CRITICAL_SECTION mutex;
# else
pthread_mutex_t mutex;
# endif
/** Provide synchronization between threads */
void lock() {
# ifdef G3D_WIN32
EnterCriticalSection(&mutex);
# else
pthread_mutex_lock(&mutex);
# endif
}
void unlock() {
# ifdef G3D_WIN32
LeaveCriticalSection(&mutex);
# else
pthread_mutex_unlock(&mutex);
# endif
}
/**
Malloc out of the tiny heap. Returns NULL if allocation failed.
*/
inline void* tinyMalloc(size_t bytes) {
// Note that we ignore the actual byte size
// and create a constant size block.
(void)bytes;
assert(tinyBufferSize >= bytes);
void* ptr = NULL;
if (tinyPoolSize > 0) {
--tinyPoolSize;
// Return the old last pointer from the freelist
ptr = tinyPool[tinyPoolSize];
# ifdef G3D_DEBUG
if (tinyPoolSize > 0) {
assert(tinyPool[tinyPoolSize - 1] != ptr);
// "System::malloc heap corruption detected: "
// "the last two pointers on the freelist are identical (during tinyMalloc).");
}
# endif
// NULL out the entry to help detect corruption
tinyPool[tinyPoolSize] = NULL;
}
return ptr;
}
/** Returns true if this is a pointer into the tiny heap. */
bool inTinyHeap(void* ptr) {
return
(ptr >= tinyHeap) &&
(ptr < (uint8*)tinyHeap + maxTinyBuffers * tinyBufferSize);
}
void tinyFree(void* ptr) {
assert(ptr);
assert(tinyPoolSize < maxTinyBuffers);
// "Tried to free a tiny pool buffer when the tiny pool freelist is full.");
# ifdef G3D_DEBUG
if (tinyPoolSize > 0) {
void* prevOnHeap = tinyPool[tinyPoolSize - 1];
assert(prevOnHeap != ptr);
// "System::malloc heap corruption detected: "
// "the last two pointers on the freelist are identical (during tinyFree).");
}
# endif
assert(tinyPool[tinyPoolSize] == NULL);
// Put the pointer back into the free list
tinyPool[tinyPoolSize] = ptr;
++tinyPoolSize;
}
void flushPool(MemBlock* pool, int& poolSize) {
for (int i = 0; i < poolSize; ++i) {
::free(pool[i].ptr);
pool[i].ptr = NULL;
pool[i].bytes = 0;
}
poolSize = 0;
}
/** Allocate out of a specific pool-> Return NULL if no suitable
memory was found.
*/
void* malloc(MemBlock* pool, int& poolSize, size_t bytes) {
// OPT: find the smallest block that satisfies the request.
// See if there's something we can use in the buffer pool->
// Search backwards since usually we'll re-use the last one.
for (int i = (int)poolSize - 1; i >= 0; --i) {
if (pool[i].bytes >= bytes) {
// We found a suitable entry in the pool->
// No need to offset the pointer; it is already offset
void* ptr = pool[i].ptr;
// Remove this element from the pool
--poolSize;
pool[i] = pool[poolSize];
return ptr;
}
}
return NULL;
}
public:
/** Count of memory allocations that have occurred. */
int totalMallocs;
int mallocsFromTinyPool;
int mallocsFromSmallPool;
int mallocsFromMedPool;
/** Amount of memory currently allocated (according to the application).
This does not count the memory still remaining in the buffer pool,
but does count extra memory required for rounding off to the size
of a buffer.
Primarily useful for detecting leaks.*/
// TODO: make me an atomic int!
volatile int bytesAllocated;
BufferPool() {
totalMallocs = 0;
mallocsFromTinyPool = 0;
mallocsFromSmallPool = 0;
mallocsFromMedPool = 0;
bytesAllocated = true;
tinyPoolSize = 0;
tinyHeap = NULL;
smallPoolSize = 0;
medPoolSize = 0;
// Initialize the tiny heap as a bunch of pointers into one
// pre-allocated buffer.
tinyHeap = ::malloc(maxTinyBuffers * tinyBufferSize);
for (int i = 0; i < maxTinyBuffers; ++i) {
tinyPool[i] = (uint8*)tinyHeap + (tinyBufferSize * i);
}
tinyPoolSize = maxTinyBuffers;
# ifdef G3D_WIN32
InitializeCriticalSection(&mutex);
# else
pthread_mutex_init(&mutex, NULL);
# endif
}
~BufferPool() {
::free(tinyHeap);
# ifdef G3D_WIN32
DeleteCriticalSection(&mutex);
# else
// No destruction on pthreads
# endif
}
void* realloc(void* ptr, size_t bytes) {
if (ptr == NULL) {
return malloc(bytes);
}
if (inTinyHeap(ptr)) {
if (bytes <= tinyBufferSize) {
// The old pointer actually had enough space.
return ptr;
} else {
// Free the old pointer and malloc
void* newPtr = malloc(bytes);
System::memcpy(newPtr, ptr, tinyBufferSize);
tinyFree(ptr);
return newPtr;
}
} else {
// In one of our heaps.
// See how big the block really was
size_t realSize = *(uint32*)USERPTR_TO_REALPTR(ptr);
if (bytes <= realSize) {
// The old block was big enough.
return ptr;
}
// Need to reallocate
void* newPtr = malloc(bytes);
System::memcpy(newPtr, ptr, realSize);
free(ptr);
return newPtr;
}
}
void* malloc(size_t bytes) {
lock();
++totalMallocs;
if (bytes <= tinyBufferSize) {
void* ptr = tinyMalloc(bytes);
if (ptr) {
++mallocsFromTinyPool;
unlock();
return ptr;
}
}
// Failure to allocate a tiny buffer is allowed to flow
// through to a small buffer
if (bytes <= smallBufferSize) {
void* ptr = malloc(smallPool, smallPoolSize, bytes);
if (ptr) {
++mallocsFromSmallPool;
unlock();
return ptr;
}
} else if (bytes <= medBufferSize) {
// Note that a small allocation failure does *not* fall
// through into a medium allocation because that would
// waste the medium buffer's resources.
void* ptr = malloc(medPool, medPoolSize, bytes);
if (ptr) {
++mallocsFromMedPool;
unlock();
debugAssertM(ptr != NULL, "BufferPool::malloc returned NULL");
return ptr;
}
}
bytesAllocated += REALBLOCK_SIZE(bytes);
unlock();
// Heap allocate
// Allocate 4 extra bytes for our size header (unfortunate,
// since malloc already added its own header).
void* ptr = ::malloc(REALBLOCK_SIZE(bytes));
if (ptr == NULL) {
// Flush memory pools to try and recover space
flushPool(smallPool, smallPoolSize);
flushPool(medPool, medPoolSize);
ptr = ::malloc(REALBLOCK_SIZE(bytes));
}
if (ptr == NULL) {
if ((System::outOfMemoryCallback != NULL) &&
(System::outOfMemoryCallback(REALBLOCK_SIZE(bytes), true) == true)) {
// Re-attempt the malloc
ptr = ::malloc(REALBLOCK_SIZE(bytes));
}
}
if (ptr == NULL) {
if (System::outOfMemoryCallback != NULL) {
// Notify the application
System::outOfMemoryCallback(REALBLOCK_SIZE(bytes), false);
}
# ifdef G3D_DEBUG
debugPrintf("::malloc(%d) returned NULL\n", REALBLOCK_SIZE(bytes));
# endif
debugAssertM(ptr != NULL,
"::malloc returned NULL. Either the "
"operating system is out of memory or the "
"heap is corrupt.");
return NULL;
}
*(uint32*)ptr = bytes;
return REALPTR_TO_USERPTR(ptr);
}
void free(void* ptr) {
if (ptr == NULL) {
// Free does nothing on null pointers
return;
}
assert(isValidPointer(ptr));
if (inTinyHeap(ptr)) {
lock();
tinyFree(ptr);
unlock();
return;
}
uint32 bytes = *(uint32*)USERPTR_TO_REALPTR(ptr);
lock();
if (bytes <= smallBufferSize) {
if (smallPoolSize < maxSmallBuffers) {
smallPool[smallPoolSize] = MemBlock(ptr, bytes);
++smallPoolSize;
unlock();
return;
}
} else if (bytes <= medBufferSize) {
if (medPoolSize < maxMedBuffers) {
medPool[medPoolSize] = MemBlock(ptr, bytes);
++medPoolSize;
unlock();
return;
}
}
bytesAllocated -= REALBLOCK_SIZE(bytes);
unlock();
// Free; the buffer pools are full or this is too big to store.
::free(USERPTR_TO_REALPTR(ptr));
}
std::string performance() const {
if (totalMallocs > 0) {
int pooled = mallocsFromTinyPool +
mallocsFromSmallPool +
mallocsFromMedPool;
int total = totalMallocs;
return format("malloc performance: %5.1f%% <= %db, %5.1f%% <= %db, "
"%5.1f%% <= %db, %5.1f%% > %db",
100.0 * mallocsFromTinyPool / total,
BufferPool::tinyBufferSize,
100.0 * mallocsFromSmallPool / total,
BufferPool::smallBufferSize,
100.0 * mallocsFromMedPool / total,
BufferPool::medBufferSize,
100.0 * (1.0 - (double)pooled / total),
BufferPool::medBufferSize);
} else {
return "No System::malloc calls made yet.";
}
}
std::string status() const {
return format("preallocated shared buffers: %5d/%d x %db",
maxTinyBuffers - tinyPoolSize, maxTinyBuffers, tinyBufferSize);
}
};
// Dynamically allocated because we need to ensure that
// the buffer pool is still around when the last global variable
// is deallocated.
static BufferPool* bufferpool = NULL;
std::string System::mallocPerformance() {
#ifndef NO_BUFFERPOOL
return bufferpool->performance();
#else
return "NO_BUFFERPOOL";
#endif
}
std::string System::mallocStatus() {
#ifndef NO_BUFFERPOOL
return bufferpool->status();
#else
return "NO_BUFFERPOOL";
#endif
}
void System::resetMallocPerformanceCounters() {
#ifndef NO_BUFFERPOOL
bufferpool->totalMallocs = 0;
bufferpool->mallocsFromMedPool = 0;
bufferpool->mallocsFromSmallPool = 0;
bufferpool->mallocsFromTinyPool = 0;
#endif
}
#ifndef NO_BUFFERPOOL
inline void initMem() {
// Putting the test here ensures that the system is always
// initialized, even when globals are being allocated.
static bool initialized = false;
if (! initialized) {
bufferpool = new BufferPool();
initialized = true;
}
}
#endif
void* System::malloc(size_t bytes) {
#ifndef NO_BUFFERPOOL
initMem();
return bufferpool->malloc(bytes);
#else
return ::malloc(bytes);
#endif
}
void* System::calloc(size_t n, size_t x) {
#ifndef NO_BUFFERPOOL
void* b = System::malloc(n * x);
debugAssertM(b != NULL, "System::malloc returned NULL");
debugAssertM(isValidHeapPointer(b), "System::malloc returned an invalid pointer");
System::memset(b, 0, n * x);
return b;
#else
return ::calloc(n, x);
#endif
}
void* System::realloc(void* block, size_t bytes) {
#ifndef NO_BUFFERPOOL
initMem();
return bufferpool->realloc(block, bytes);
#else
return ::realloc(block, bytes);
#endif
}
void System::free(void* p) {
#ifndef NO_BUFFERPOOL
bufferpool->free(p);
#else
return ::free(p);
#endif
}
void* System::alignedMalloc(size_t bytes, size_t alignment) {
alwaysAssertM(isPow2(alignment), "alignment must be a power of 2");
// We must align to at least a word boundary.
alignment = iMax(alignment, sizeof(void *));
// Pad the allocation size with the alignment size and the
// size of the redirect pointer.
size_t totalBytes = bytes + alignment + sizeof(void*);
size_t truePtr = (size_t)System::malloc(totalBytes);
if (truePtr == 0) {
// malloc returned NULL
return NULL;
}
debugAssert(isValidHeapPointer((void*)truePtr));
#ifdef G3D_WIN32
// The blocks we return will not be valid Win32 debug heap
// pointers because they are offset
// debugAssert(_CrtIsValidPointer((void*)truePtr, totalBytes, TRUE) );
#endif
// The return pointer will be the next aligned location (we must at least
// leave space for the redirect pointer, however).
size_t alignedPtr = truePtr + sizeof(void*);
// 2^n - 1 has the form 1111... in binary.
uint32 bitMask = (alignment - 1);
// Advance forward until we reach an aligned location.
while ((alignedPtr & bitMask) != 0) {
alignedPtr += sizeof(void*);
}
debugAssert(alignedPtr - truePtr + bytes <= totalBytes);
// Immediately before the aligned location, write the true array location
// so that we can free it correctly.
size_t* redirectPtr = (size_t *)(alignedPtr - sizeof(void *));
redirectPtr[0] = truePtr;
debugAssert(isValidHeapPointer((void*)truePtr));
#ifdef G3D_WIN32
debugAssert( _CrtIsValidPointer((void*)alignedPtr, bytes, TRUE) );
#endif
return (void *)alignedPtr;
}
void System::alignedFree(void* _ptr) {
if (_ptr == NULL) {
return;
}
size_t alignedPtr = (size_t)_ptr;
// Back up one word from the pointer the user passed in.
// We now have a pointer to a pointer to the true start
// of the memory block.
size_t* redirectPtr = (size_t*)(alignedPtr - sizeof(void *));
// Dereference that pointer so that ptr = true start
void* truePtr = (void*)redirectPtr[0];
debugAssert(isValidHeapPointer((void*)truePtr));
System::free(truePtr);
}
void System::setEnv(const std::string& name, const std::string& value) {
std::string cmd = name + "=" + value;
# ifdef G3D_WIN32
_putenv(cmd.c_str());
# else
// Many linux implementations of putenv expect char*
putenv(const_cast<char*>(cmd.c_str()));
# endif
}
const char* System::getEnv(const std::string& name) {
return getenv(name.c_str());
}
static void var(TextOutput& t, const std::string& name, const std::string& val) {
t.writeSymbols(name,"=");
t.writeString(val);
t.writeNewline();
}
static void var(TextOutput& t, const std::string& name, const bool val) {
t.writeSymbols(name, "=", val ? "Yes" : "No");
t.writeNewline();
}
static void var(TextOutput& t, const std::string& name, const int val) {
t.writeSymbols(name,"=");
t.writeNumber(val);
t.writeNewline();
}
void System::describeSystem(
std::string& s) {
TextOutput t;
describeSystem(t);
t.commitString(s);
}
void System::describeSystem(
TextOutput& t) {
t.writeSymbols("App", "{");
t.writeNewline();
t.pushIndent();
var(t, "Name", System::currentProgramFilename());
char cwd[1024];
getcwd(cwd, 1024);
var(t, "cwd", std::string(cwd));
t.popIndent();
t.writeSymbols("}");
t.writeNewline();
t.writeNewline();
t.writeSymbols("OS", "{");
t.writeNewline();
t.pushIndent();
var(t, "Name", System::operatingSystem());
t.popIndent();
t.writeSymbols("}");
t.writeNewline();
t.writeNewline();
t.writeSymbols("CPU", "{");
t.writeNewline();
t.pushIndent();
var(t, "Vendor", System::cpuVendor());
var(t, "Architecture", System::cpuArchitecture());
var(t, "hasCPUID", System::hasCPUID());
var(t, "hasMMX", System::hasMMX());
var(t, "hasSSE", System::hasSSE());
var(t, "hasSSE2", System::hasSSE2());
var(t, "hasSSE3", System::hasSSE3());
var(t, "has3DNow", System::has3DNow());
var(t, "hasRDTSC", System::hasRDTSC());
t.popIndent();
t.writeSymbols("}");
t.writeNewline();
t.writeNewline();
t.writeSymbols("G3D", "{");
t.writeNewline();
t.pushIndent();
var(t, "Link version", G3D_VER);
var(t, "Compile version", System::version());
t.popIndent();
t.writeSymbols("}");
t.writeNewline();
t.writeNewline();
}
int System::cpuSpeedMHz() {
return g_cpuInfo.m_cpuSpeed;
}
void System::setClipboardText(const std::string& s) {
# ifdef G3D_WIN32
if (OpenClipboard(NULL)) {
HGLOBAL hMem = GlobalAlloc(GHND | GMEM_DDESHARE, s.size() + 1);
if (hMem) {
char *pMem = (char*)GlobalLock(hMem);
strcpy(pMem, s.c_str());
GlobalUnlock(hMem);
EmptyClipboard();
SetClipboardData(CF_TEXT, hMem);
}
CloseClipboard();
GlobalFree(hMem);
}
# endif
}
std::string System::getClipboardText() {
std::string s;
# ifdef G3D_WIN32
if (OpenClipboard(NULL)) {
HANDLE h = GetClipboardData(CF_TEXT);
if (h) {
char* temp = (char*)GlobalLock(h);
if (temp) {
s = temp;
}
temp = NULL;
GlobalUnlock(h);
}
CloseClipboard();
}
# endif
return s;
}
std::string System::currentDateString() {
time_t t1;
::time(&t1);
tm* t = localtime(&t1);
return format("%d-%02d-%02d", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);
}
} // namespace
|