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