source: roaraudio/libroar/libroar.c @ 4916:cc2890ef2f64

Last change on this file since 4916:cc2890ef2f64 was 4916:cc2890ef2f64, checked in by phi, 13 years ago

added a ckport ignore for sleep()

File size: 3.7 KB
Line 
1//libroar.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36/* ckport options:
37 * ckport: ignore-symbol: sleep      -- checked by configure
38 */
39
40#include "libroar.h"
41
42#ifndef roar_mm_mlock
43int roar_mm_mlock(const void *addr, size_t len) {
44#if defined(ROAR_TARGET_WIN32)
45 return GlobalLock(addr) == addr ? 0 : -1;
46#elif defined(ROAR_TARGET_MICROCONTROLLER)
47 return 0;
48#elif defined(_SC_PAGESIZE)
49 long sz = sysconf(_SC_PAGESIZE);
50 unsigned long int pos = (unsigned long int) addr;
51
52 len += sz - (len % sz);
53
54 pos -= pos % sz;
55
56 return mlock((void*)pos, len);
57#else
58 return -1;
59#endif
60}
61#endif
62
63#ifndef roar_mm_munlock
64int roar_mm_munlock(const void *addr, size_t len) {
65#if defined(ROAR_TARGET_WIN32)
66 // TODO: find out what do do here. GlobalUnLock()? does such a function exist?
67// return GlobalLock(addr) == addr ? 0 : -1;
68 return -1;
69#elif defined(ROAR_TARGET_MICROCONTROLLER)
70 return 0;
71#elif defined(_SC_PAGESIZE)
72 long sz = sysconf(_SC_PAGESIZE);
73 unsigned long int pos = (unsigned long int) addr;
74
75 len += sz - (len % sz);
76
77 pos -= pos % sz;
78
79 return munlock((void*)pos, len);
80#else
81 return -1;
82#endif
83}
84#endif
85
86// for compatibility with old versions:
87int _ROAR_MLOCK(const void *addr, size_t len) {
88 roar_debug_warn_obsolete("_ROAR_MLOCK", "roar_mm_mlock", NULL);
89 return roar_mm_mlock(addr, len);
90}
91
92void * roar_mm_memdup(const void * s, size_t len) {
93 void * ret = roar_mm_malloc(len);
94
95 if ( ret == NULL )
96  return NULL;
97
98 memcpy(ret, s, len);
99
100 return ret;
101}
102
103int roar_usleep(uint_least32_t t) {
104#ifdef ROAR_TARGET_WIN32
105 Sleep(t/(uint_least32_t)1000);
106 return 0;
107#elif defined(ROAR_HAVE_NANOSLEEP)
108 struct timespec tv;
109 struct timespec left;
110
111 if ( t > (uint_least32_t)1000000 ) {
112  tv.tv_sec  = t/(uint_least32_t)1000000;
113  t         -= tv.tv_sec*(uint_least32_t)1000000;
114 } else {
115  tv.tv_sec  = 0;
116 }
117
118 tv.tv_nsec = t*(uint_least32_t)1000;
119
120 while (nanosleep(&tv, &left) == -1)
121  memcpy(&tv, &left, sizeof(tv));
122 return 0;
123#elif defined(ROAR_HAVE_USLEEP)
124 usleep(t);
125 return 0;
126#else
127 ROAR_ERR("roar_usleep(t=%llu): can not sleep: not implemented", (long long unsigned int)t);
128 roar_strap(ROAR_TRAP_GROUP_LIBROAR, "usleep.not-implemented");
129 roar_err_set(ROAR_ERROR_NOSYS);
130 return -1;
131#endif
132}
133
134int roar_sleep(int t) {
135 if ( t < 0 ) {
136  roar_err_set(ROAR_ERROR_CAUSALITY);
137  return -1;
138 }
139
140#ifdef ROAR_TARGET_WIN32
141 Sleep(1000L*(long)t);
142#else
143 while (t)
144  t = sleep(t);
145#endif
146
147 return 0;
148}
149
150//ll
Note: See TracBrowser for help on using the repository browser.