0.6.7.22: removed CVS dollar-Header-dollar tags from sources
[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 #include <stdio.h>
13 #include <errno.h>
14
15 #include "os.h"
16
17 /* Except for os_zero, these routines are only called by Lisp code.
18  * These routines may also be replaced by os-dependent versions
19  * instead. See hpux-os.c for some useful restrictions on actual
20  * usage. */
21
22 void
23 os_zero(os_vm_address_t addr, os_vm_size_t length)
24 {
25     os_vm_address_t block_start;
26     os_vm_size_t block_size;
27
28 #ifdef DEBUG
29     fprintf(stderr,";;; os_zero: addr: 0x%08x, len: 0x%08x\n",addr,length);
30 #endif
31
32     block_start = os_round_up_to_page(addr);
33
34     length -= block_start-addr;
35     block_size = os_trunc_size_to_page(length);
36
37     if (block_start > addr)
38         bzero((char *)addr, block_start-addr);
39     if (block_size < length)
40         bzero((char *)block_start+block_size, length-block_size);
41
42     if (block_size != 0) {
43         /* Now deallocate and allocate the block so that it faults in
44          * zero-filled. */
45
46         os_invalidate(block_start, block_size);
47         addr = os_validate(block_start, block_size);
48
49         if(addr == NULL || addr != block_start)
50             lose("os_zero: block moved! 0x%08x ==> 0x%08x",
51                  block_start,
52                  addr);
53     }
54 }
55
56 os_vm_address_t
57 os_allocate(os_vm_size_t len)
58 {
59     return os_validate((os_vm_address_t)NULL, len);
60 }
61
62 os_vm_address_t
63 os_allocate_at(os_vm_address_t addr, os_vm_size_t len)
64 {
65     return os_validate(addr, len);
66 }
67
68 void
69 os_deallocate(os_vm_address_t addr, os_vm_size_t len)
70 {
71     os_invalidate(addr,len);
72 }
73
74 /* (This function once tried to grow the chunk by asking os_validate
75  * whether the space was available, but that really only works under
76  * Mach.) */
77 os_vm_address_t
78 os_reallocate(os_vm_address_t addr, os_vm_size_t old_len, os_vm_size_t len)
79 {
80     addr=os_trunc_to_page(addr);
81     len=os_round_up_size_to_page(len);
82     old_len=os_round_up_size_to_page(old_len);
83
84     if(addr==NULL)
85         return os_allocate(len);
86     else{
87         long len_diff=len-old_len;
88
89         if(len_diff<0)
90             os_invalidate(addr+len,-len_diff);
91         else{
92             if(len_diff!=0){
93               os_vm_address_t new=os_allocate(len);
94
95               if(new!=NULL){
96                 bcopy(addr,new,old_len);
97                 os_invalidate(addr,old_len);
98                 }
99                 
100               addr=new;
101             }
102         }
103         return addr;
104     }
105 }
106
107 int
108 os_get_errno(void)
109 {
110     return errno;
111 }