]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/sys/osx/macosx_utils.mm
Various Mac OS X tweaks to get this to build. Probably breaking things.
[icculus/iodoom3.git] / neo / sys / osx / macosx_utils.mm
1 /*
2 ===========================================================================
3
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company. 
6
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).  
8
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code.  If not, see <http://www.gnu.org/licenses/>.
21
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code.  If not, please request a copy in writing from id Software at the address below.
23
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25
26 ===========================================================================
27 */
28
29 // -*- mode: objc -*-
30 #import "../../idlib/precompiled.h"
31
32 #import <Foundation/Foundation.h>
33 #import <mach/mach.h>
34 #import <mach/vm_map.h>
35
36
37 #define CD_MOUNT_NAME "DOOM"
38
39 #include "macosx_local.h"
40 #include "macosx_sys.h"
41
42 //extern "C" void *vm_allocate(unsigned, unsigned*, unsigned, int);
43 //extern "C" void vm_deallocate(unsigned, unsigned*, unsigned);
44
45 const char *macosx_scanForLibraryDirectory(void)
46 {
47     return "/Library/DOOM";
48 }
49
50 // EEEK!
51 static long size_save;
52
53 void *osxAllocateMemoryNV(long size, float readFrequency, float writeFrequency, float priority)
54 {
55     kern_return_t kr;    
56     vm_address_t buffer;
57     
58     kr = vm_allocate(mach_task_self(),
59                     (vm_address_t *)&buffer,
60                     size,
61                     VM_FLAGS_ANYWHERE);
62     if(kr == 0) {
63         size_save = size;
64         return buffer;
65     }
66     else
67     {
68         size_save = 0;
69         return NULL;
70     }
71
72 }
73
74 void osxFreeMemoryNV(void *pointer)
75 {
76     if(size_save) {
77         vm_deallocate(mach_task_self(),
78                       (vm_address_t)pointer,
79                       size_save);
80         size_save = 0;
81     }
82 }
83
84 void *osxAllocateMemory(long size)
85 {
86     kern_return_t kr;    
87     vm_address_t buffer;
88     
89     size += sizeof( int );
90     kr = vm_allocate(mach_task_self(),
91                     (vm_address_t *)&buffer,
92                     size,
93                     VM_FLAGS_ANYWHERE);
94     if(kr == 0) {
95         int *ptr = buffer;
96         *ptr = size;
97         ptr = ptr + 1;
98         return ptr;
99     }
100     else
101     {
102         return NULL;
103     }
104
105 }
106
107 void osxFreeMemory(void *pointer)
108 {
109     int size;
110     int *ptr = pointer;
111     ptr = ptr - 1;
112     size = *ptr;
113     vm_deallocate(mach_task_self(), (vm_address_t)ptr, size);
114 }
115
116 #if 0
117 static inline void __eieio(void)
118 {
119         __asm__ ("eieio");
120 }
121
122 static inline void __sync(void)
123 {
124         __asm__ ("sync");
125 }
126
127 static inline void __isync(void)
128 {
129         __asm__ ("isync");
130 }
131
132 static inline void __dcbf(void *base, unsigned long offset)
133 {
134         __asm__ ("dcbf %0, %1"
135                 :
136                 : "r" (base), "r" (offset)
137                 : "r0");
138 }
139
140 static inline void __dcbst(void *base, unsigned long offset)
141 {
142         __asm__ ("dcbst %0, %1"
143                 :
144                 : "r" (base), "r" (offset)
145                 : "r0");
146 }
147
148 static inline void __dcbz(void *base, unsigned long offset)
149 {
150         __asm__ ("dcbz %0, %1"
151                 :
152                 : "r" (base), "r" (offset)
153                 : "r0");
154 }
155
156 void    Sys_FlushCacheMemory( void *base, int bytes ) {
157         unsigned long i;
158         
159         for(i = 0; i <  bytes; i+= 32) {
160             __dcbf(base,i);
161         }
162         __sync();
163         __isync();
164         __dcbf(base, i);
165         __sync(); 
166         __isync();
167         *(volatile unsigned long *)(base + i);
168         __isync(); 
169 }
170 #endif