698d0c97dad9d448373e912cf7b3047d23fde4be
[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 void
26 os_zero(os_vm_address_t addr, os_vm_size_t length)
27 {
28     os_vm_address_t block_start;
29     os_vm_size_t block_size;
30
31 #ifdef DEBUG
32     fprintf(stderr,";;; os_zero: addr: 0x%08x, len: 0x%08x\n",addr,length);
33 #endif
34
35     block_start = os_round_up_to_page(addr);
36
37     length -= block_start-addr;
38     block_size = os_trunc_size_to_page(length);
39
40     if (block_start > addr)
41         bzero((char *)addr, block_start-addr);
42     if (block_size < length)
43         bzero((char *)block_start+block_size, length-block_size);
44
45     if (block_size != 0) {
46         /* Now deallocate and allocate the block so that it faults in
47          * zero-filled. */
48
49         os_invalidate(block_start, block_size);
50         addr = os_validate(block_start, block_size);
51
52         if (addr == NULL || addr != block_start)
53             lose("os_zero: block moved! 0x%08x ==> 0x%08x\n",
54                  block_start,
55                  addr);
56     }
57 }
58
59 os_vm_address_t
60 os_allocate(os_vm_size_t len)
61 {
62     return os_validate((os_vm_address_t)NULL, len);
63 }
64
65 void
66 os_deallocate(os_vm_address_t addr, os_vm_size_t len)
67 {
68     os_invalidate(addr,len);
69 }
70
71 int
72 os_get_errno(void)
73 {
74     return errno;
75 }
76
77
78 #if defined(LISP_FEATURE_SB_THREAD) && (!defined(CANNOT_USE_POSIX_SEM_T) || defined(LISP_FEATURE_WIN32))
79
80 void
81 os_sem_init(os_sem_t *sem, unsigned int value)
82 {
83     if (-1==sem_init(sem, 0, value))
84         lose("os_sem_init(%p, %u): %s", sem, value, strerror(errno));
85     FSHOW((stderr, "os_sem_init(%p, %u)\n", sem, value));
86 }
87
88 void
89 os_sem_wait(os_sem_t *sem, char *what)
90 {
91     FSHOW((stderr, "%s: os_sem_wait(%p) ...\n", what, sem));
92     while (-1 == sem_wait(sem))
93         if (EINTR!=errno)
94             lose("%s: os_sem_wait(%p): %s", what, sem, strerror(errno));
95     FSHOW((stderr, "%s: os_sem_wait(%p) => ok\n", what, sem));
96 }
97
98 void
99 os_sem_post(sem_t *sem, char *what)
100 {
101     if (-1 == sem_post(sem))
102         lose("%s: os_sem_post(%p): %s", what, sem, strerror(errno));
103     FSHOW((stderr, "%s: os_sem_post(%p)\n", what, sem));
104 }
105
106 void
107 os_sem_destroy(os_sem_t *sem)
108 {
109     if (-1==sem_destroy(sem))
110         lose("os_sem_destroy(%p): %s", sem, strerror(errno));
111 }
112
113 #endif