1 | /************************************************************************* |
---|
2 | * * |
---|
3 | * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. * |
---|
4 | * All rights reserved. Email: russ@q12.org Web: www.q12.org * |
---|
5 | * * |
---|
6 | * This library is free software; you can redistribute it and/or * |
---|
7 | * modify it under the terms of EITHER: * |
---|
8 | * (1) The GNU Lesser General Public License as published by the Free * |
---|
9 | * Software Foundation; either version 2.1 of the License, or (at * |
---|
10 | * your option) any later version. The text of the GNU Lesser * |
---|
11 | * General Public License is included with this library in the * |
---|
12 | * file LICENSE.TXT. * |
---|
13 | * (2) The BSD-style license that is included with this library in * |
---|
14 | * the file LICENSE-BSD.TXT. * |
---|
15 | * * |
---|
16 | * This library is distributed in the hope that it will be useful, * |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files * |
---|
19 | * LICENSE.TXT and LICENSE-BSD.TXT for more details. * |
---|
20 | * * |
---|
21 | *************************************************************************/ |
---|
22 | |
---|
23 | @@@ this file should not be compiled any more @@@ |
---|
24 | |
---|
25 | #include <string.h> |
---|
26 | #include <errno.h> |
---|
27 | #include "stack.h" |
---|
28 | #include "ode/error.h" |
---|
29 | #include "ode/config.h" |
---|
30 | |
---|
31 | //**************************************************************************** |
---|
32 | // unix version that uses mmap(). some systems have anonymous mmaps and some |
---|
33 | // need to mmap /dev/zero. |
---|
34 | |
---|
35 | #ifndef WIN32 |
---|
36 | |
---|
37 | #include <unistd.h> |
---|
38 | #include <sys/mman.h> |
---|
39 | #include <sys/types.h> |
---|
40 | #include <sys/stat.h> |
---|
41 | #include <fcntl.h> |
---|
42 | |
---|
43 | |
---|
44 | void dStack::init (int max_size) |
---|
45 | { |
---|
46 | if (sizeof(long int) != sizeof(char*)) dDebug (0,"internal"); |
---|
47 | if (max_size <= 0) dDebug (0,"Stack::init() given size <= 0"); |
---|
48 | |
---|
49 | #ifndef MMAP_ANONYMOUS |
---|
50 | static int dev_zero_fd = -1; // cached file descriptor for /dev/zero |
---|
51 | if (dev_zero_fd < 0) dev_zero_fd = open ("/dev/zero", O_RDWR); |
---|
52 | if (dev_zero_fd < 0) dError (0,"can't open /dev/zero (%s)",strerror(errno)); |
---|
53 | base = (char*) mmap (0,max_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, |
---|
54 | dev_zero_fd,0); |
---|
55 | #else |
---|
56 | base = (char*) mmap (0,max_size, PROT_READ | PROT_WRITE, |
---|
57 | MAP_PRIVATE | MAP_ANON,0,0); |
---|
58 | #endif |
---|
59 | |
---|
60 | if (int(base) == -1) dError (0,"Stack::init(), mmap() failed, " |
---|
61 | "max_size=%d (%s)",max_size,strerror(errno)); |
---|
62 | size = max_size; |
---|
63 | pointer = base; |
---|
64 | frame = 0; |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | void dStack::destroy() |
---|
69 | { |
---|
70 | munmap (base,size); |
---|
71 | base = 0; |
---|
72 | size = 0; |
---|
73 | pointer = 0; |
---|
74 | frame = 0; |
---|
75 | } |
---|
76 | |
---|
77 | #endif |
---|
78 | |
---|
79 | //**************************************************************************** |
---|
80 | |
---|
81 | #ifdef WIN32 |
---|
82 | |
---|
83 | #include "windows.h" |
---|
84 | |
---|
85 | |
---|
86 | void dStack::init (int max_size) |
---|
87 | { |
---|
88 | if (sizeof(LPVOID) != sizeof(char*)) dDebug (0,"internal"); |
---|
89 | if (max_size <= 0) dDebug (0,"Stack::init() given size <= 0"); |
---|
90 | base = (char*) VirtualAlloc (NULL,max_size,MEM_RESERVE,PAGE_READWRITE); |
---|
91 | if (base == 0) dError (0,"Stack::init(), VirtualAlloc() failed, " |
---|
92 | "max_size=%d",max_size); |
---|
93 | size = max_size; |
---|
94 | pointer = base; |
---|
95 | frame = 0; |
---|
96 | committed = 0; |
---|
97 | |
---|
98 | // get page size |
---|
99 | SYSTEM_INFO info; |
---|
100 | GetSystemInfo (&info); |
---|
101 | pagesize = info.dwPageSize; |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | void dStack::destroy() |
---|
106 | { |
---|
107 | VirtualFree (base,0,MEM_RELEASE); |
---|
108 | base = 0; |
---|
109 | size = 0; |
---|
110 | pointer = 0; |
---|
111 | frame = 0; |
---|
112 | } |
---|
113 | |
---|
114 | #endif |
---|