1 | /** |
---|
2 | @file System.h |
---|
3 | |
---|
4 | @maintainer Morgan McGuire, matrix@graphics3d.com |
---|
5 | |
---|
6 | @cite Rob Wyatt http://www.gamasutra.com/features/wyatts_world/19990709/processor_detection_01.htm |
---|
7 | @cite Benjamin Jurke http://www.flipcode.com/cgi-bin/msg.cgi?showThread=COTD-ProcessorDetectionClass&forum=cotd&id=-1 |
---|
8 | @cite Michael Herf http://www.stereopsis.com/memcpy.html |
---|
9 | |
---|
10 | @created 2003-01-25 |
---|
11 | @edited 2006-04-26 |
---|
12 | */ |
---|
13 | |
---|
14 | #ifndef G3D_SYSTEM_H |
---|
15 | #define G3D_SYSTEM_H |
---|
16 | |
---|
17 | #include "G3D/platform.h" |
---|
18 | #include "G3D/g3dmath.h" |
---|
19 | #include <string> |
---|
20 | |
---|
21 | #ifdef G3D_OSX |
---|
22 | # include <CoreServices/CoreServices.h> |
---|
23 | #endif |
---|
24 | |
---|
25 | |
---|
26 | namespace G3D { |
---|
27 | |
---|
28 | typedef double RealTime; |
---|
29 | |
---|
30 | class System { |
---|
31 | public: |
---|
32 | |
---|
33 | /** Called automatically by the other System routines.*/ |
---|
34 | static void init(); |
---|
35 | |
---|
36 | /** |
---|
37 | Guarantees that the start of the array is aligned to the |
---|
38 | specified number of bytes. |
---|
39 | */ |
---|
40 | static void* alignedMalloc(size_t bytes, size_t alignment); |
---|
41 | |
---|
42 | /** |
---|
43 | Uses pooled storage to optimize small allocations (1 byte to 5 kilobytes). |
---|
44 | Can be 10x to 100x faster than calling ::malloc or new. |
---|
45 | |
---|
46 | The result must be freed with free. |
---|
47 | |
---|
48 | Threadsafe on Win32. |
---|
49 | |
---|
50 | @sa calloc realloc OutOfMemoryCallback free |
---|
51 | */ |
---|
52 | static void* malloc(size_t bytes); |
---|
53 | |
---|
54 | static void* calloc(size_t n, size_t x); |
---|
55 | |
---|
56 | /** |
---|
57 | @param size Size of memory that the system was trying to allocate |
---|
58 | @param recoverable If true, the system will attempt to allocate again |
---|
59 | if the callback returns true. If false, malloc is going to return |
---|
60 | NULL and this invocation is just to notify the application. |
---|
61 | @return Return true to force malloc to attempt allocation again if the |
---|
62 | error was recoverable. |
---|
63 | */ |
---|
64 | typedef bool (*OutOfMemoryCallback)(size_t size, bool recoverable); |
---|
65 | |
---|
66 | /** |
---|
67 | When System::malloc fails to allocate memory because the system is |
---|
68 | out of memory, it invokes this handler (if it is not NULL). |
---|
69 | The argument to the callback is the amount of memory that malloc |
---|
70 | was trying to allocate when it ran out. If the callback returns |
---|
71 | true, System::malloc will attempt to allocate the memory again. |
---|
72 | If the callback returns false, then System::malloc will return NULL. |
---|
73 | |
---|
74 | You can use outOfMemoryCallback to free data structures or to |
---|
75 | register the failure. |
---|
76 | */ |
---|
77 | static OutOfMemoryCallback outOfMemoryCallback; |
---|
78 | |
---|
79 | /** |
---|
80 | Version of realloc that works with System::malloc. |
---|
81 | */ |
---|
82 | static void* realloc(void* block, size_t bytes); |
---|
83 | |
---|
84 | /** Returns a string describing how well System::malloc is using its internal pooled storage. |
---|
85 | "heap" memory was slow to allocate; the other data sizes are comparatively fast.*/ |
---|
86 | static std::string mallocPerformance(); |
---|
87 | static void resetMallocPerformanceCounters(); |
---|
88 | |
---|
89 | /** |
---|
90 | Returns a string describing the current usage of the buffer pools used for |
---|
91 | optimizing System::malloc. |
---|
92 | */ |
---|
93 | static std::string mallocStatus(); |
---|
94 | |
---|
95 | /** |
---|
96 | Free data allocated with System::malloc. |
---|
97 | |
---|
98 | Threadsafe on Win32. |
---|
99 | */ |
---|
100 | static void free(void* p); |
---|
101 | |
---|
102 | /** |
---|
103 | Frees memory allocated with alignedMalloc. |
---|
104 | */ |
---|
105 | static void alignedFree(void* ptr); |
---|
106 | |
---|
107 | /** An implementation of memcpy that may be up to 2x as fast as the C library |
---|
108 | one on some processors. Guaranteed to have the same behavior as memcpy |
---|
109 | in all cases. */ |
---|
110 | static void memcpy(void* dst, const void* src, size_t numBytes); |
---|
111 | |
---|
112 | /** An implementation of memset that may be up to 2x as fast as the C library |
---|
113 | one on some processors. Guaranteed to have the same behavior as memset |
---|
114 | in all cases. */ |
---|
115 | static void memset(void* dst, uint8 value, size_t numBytes); |
---|
116 | |
---|
117 | }; |
---|
118 | |
---|
119 | |
---|
120 | } // namespace |
---|
121 | |
---|
122 | #endif |
---|