Initial revision
[sbcl.git] / src / runtime / os-common.c
1 /*
2  * This software is part of the SBCL system. See the README file for
3  * more information.
4  *
5  * This software is derived from the CMU CL system, which was
6  * written at Carnegie Mellon University and released into the
7  * public domain. The software is in the public domain and is
8  * provided with absolutely no warranty. See the COPYING and CREDITS
9  * files for more information.
10  */
11
12 /*
13  * $Header$
14  */
15
16 #include <stdio.h>
17 #include <errno.h>
18
19 #include "os.h"
20
21 /* Except for os_zero, these routines are only called by Lisp code.
22  * These routines may also be replaced by os-dependent versions
23  * instead. See hpux-os.c for some useful restrictions on actual
24  * usage. */
25
26 void
27 os_zero(os_vm_address_t addr, os_vm_size_t length)
28 {
29     os_vm_address_t block_start;
30     os_vm_size_t block_size;
31
32 #ifdef DEBUG
33     fprintf(stderr,";;; os_zero: addr: 0x%08x, len: 0x%08x\n",addr,length);
34 #endif
35
36     block_start = os_round_up_to_page(addr);
37
38     length -= block_start-addr;
39     block_size = os_trunc_size_to_page(length);
40
41     if (block_start > addr)
42         bzero((char *)addr, block_start-addr);
43     if (block_size < length)
44         bzero((char *)block_start+block_size, length-block_size);
45
46     if (block_size != 0) {
47         /* Now deallocate and allocate the block so that it faults in
48          * zero-filled. */
49
50         os_invalidate(block_start, block_size);
51         addr = os_validate(block_start, block_size);
52
53         if(addr == NULL || addr != block_start)
54             lose("os_zero: block moved! 0x%08x ==> 0x%08x",
55                  block_start,
56                  addr);
57     }
58 }
59
60 os_vm_address_t
61 os_allocate(os_vm_size_t len)
62 {
63     return os_validate((os_vm_address_t)NULL, len);
64 }
65
66 os_vm_address_t
67 os_allocate_at(os_vm_address_t addr, os_vm_size_t len)
68 {
69     return os_validate(addr, len);
70 }
71
72 void
73 os_deallocate(os_vm_address_t addr, os_vm_size_t len)
74 {
75     os_invalidate(addr,len);
76 }
77
78 /* (This function once tried to grow the chunk by asking os_validate
79  * whether the space was available, but that really only works under
80  * Mach.) */
81 os_vm_address_t
82 os_reallocate(os_vm_address_t addr, os_vm_size_t old_len, os_vm_size_t len)
83 {
84     addr=os_trunc_to_page(addr);
85     len=os_round_up_size_to_page(len);
86     old_len=os_round_up_size_to_page(old_len);
87
88     if(addr==NULL)
89         return os_allocate(len);
90     else{
91         long len_diff=len-old_len;
92
93         if(len_diff<0)
94             os_invalidate(addr+len,-len_diff);
95         else{
96             if(len_diff!=0){
97               os_vm_address_t new=os_allocate(len);
98
99               if(new!=NULL){
100                 bcopy(addr,new,old_len);
101                 os_invalidate(addr,old_len);
102                 }
103                 
104               addr=new;
105             }
106         }
107         return addr;
108     }
109 }
110
111 int
112 os_get_errno(void)
113 {
114     return errno;
115 }