0.8.9.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 #include <string.h>
15
16 #include "sbcl.h"
17 #include "os.h"
18 #include "interr.h"
19
20 /* Except for os_zero, these routines are only called by Lisp code.
21  * These routines may also be replaced by os-dependent versions
22  * instead. See hpux-os.c for some useful restrictions on actual
23  * usage. */
24
25 /* FIXME: this should be turned into a pure inline memset where it is used. */
26 void
27 os_zero(os_vm_address_t addr, os_vm_size_t length)
28 {
29     memset(addr, 0, length);
30 }
31
32 os_vm_address_t
33 os_allocate(os_vm_size_t len)
34 {
35     return os_validate((os_vm_address_t)NULL, len);
36 }
37
38 os_vm_address_t
39 os_allocate_at(os_vm_address_t addr, os_vm_size_t len)
40 {
41     return os_validate(addr, len);
42 }
43
44 void
45 os_deallocate(os_vm_address_t addr, os_vm_size_t len)
46 {
47     os_invalidate(addr,len);
48 }
49
50 /* (This function once tried to grow the chunk by asking os_validate
51  * whether the space was available, but that really only works under
52  * Mach.) */
53 os_vm_address_t
54 os_reallocate(os_vm_address_t addr, os_vm_size_t old_len, os_vm_size_t len)
55 {
56     addr=os_trunc_to_page(addr);
57     len=os_round_up_size_to_page(len);
58     old_len=os_round_up_size_to_page(old_len);
59
60     if (addr==NULL)
61         return os_allocate(len);
62     else{
63         long len_diff=len-old_len;
64
65         if (len_diff<0)
66             os_invalidate(addr+len,-len_diff);
67         else{
68             if (len_diff!=0) {
69               os_vm_address_t new=os_allocate(len);
70
71               if(new!=NULL){
72                 bcopy(addr,new,old_len);
73                 os_invalidate(addr,old_len);
74                 }
75                 
76               addr=new;
77             }
78         }
79         return addr;
80     }
81 }
82
83 int
84 os_get_errno(void)
85 {
86     return errno;
87 }