source: roaraudio/roard/memlock.c @ 4241:2e673249758d

Last change on this file since 4241:2e673249758d was 4241:2e673249758d, checked in by phi, 14 years ago

implement some basic locking logic

File size: 3.0 KB
Line 
1//memlock.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010
5 *
6 *  This file is part of roard 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 *  RoarAudio 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 */
25
26#include "roard.h"
27
28#define MAX_SEGMENTS 16
29
30struct memlock {
31 int level;
32 void * addr;
33 size_t len;
34};
35
36static struct memlock memlock_table[MAX_SEGMENTS];
37
38static volatile int memlock_table_inited = 0;
39
40static void memlock_table_init (void) {
41 if ( memlock_table_inited )
42  return;
43
44 memset(memlock_table, 0, sizeof(memlock_table));
45
46 memlock_table_inited = 1;
47
48 memlock_register(MEMLOCK_MEDIUM, memlock_table, sizeof(memlock_table));
49}
50
51int memlock_register(int level, void * addr, size_t len) {
52 int i;
53
54 if ( !memlock_table_inited )
55  memlock_table_init();
56
57 for (i = 0; i < MAX_SEGMENTS; i++) {
58  if ( memlock_table[i].addr == NULL ) {
59   memlock_table[i].level = level;
60   memlock_table[i].addr  = addr;
61   memlock_table[i].len   = len;
62   return 0;
63  }
64 }
65
66 return -1;
67}
68
69static int memlock_lock(struct memlock * seg) {
70#ifdef ROAR_HAVE_MLOCK
71 return ROAR_MLOCK(seg->addr, seg->len);
72#else
73 return -1;
74#endif
75}
76
77static int memlock_unlock(struct memlock * seg) {
78 return -1;
79}
80
81int memlock_str2level(const char * str) {
82 if ( str == NULL )
83  return -1;
84
85 if ( !strcasecmp(str, "none") ) {
86  return MEMLOCK_NONE;
87 } else if ( !strcasecmp(str, "low") ) {
88  return MEMLOCK_LOW;
89 } else if ( !strcasecmp(str, "medium") ) {
90  return MEMLOCK_MEDIUM;
91 } else if ( !strcasecmp(str, "all") ) {
92  return MEMLOCK_ALL;
93 } else if ( !strcasecmp(str, "default") ) {
94  return MEMLOCK_DEFAULT;
95 } else {
96  return -1;
97 }
98}
99
100int memlock_set_level(int level) {
101 static int old_level = MEMLOCK_NONE;
102 int i;
103 int ret = 0;
104
105 if ( !memlock_table_inited )
106  memlock_table_init();
107
108 if ( level == old_level )
109  return 0;
110
111 for (i = 0; i < MAX_SEGMENTS; i++) {
112  if ( memlock_table[i].addr == NULL ) {
113   if ( level > old_level ) {
114    if ( memlock_table[i].level > old_level && memlock_table[i].level <= level )
115     if ( memlock_lock(&(memlock_table[i])) == -1 )
116      ret = -1;
117   } else {
118    if ( memlock_table[i].level <= old_level && memlock_table[i].level > level )
119     if ( memlock_unlock(&(memlock_table[i])) == -1 )
120      ret = -1;
121   }
122  }
123 }
124
125 old_level = level;
126
127 return ret;
128}
129
130void memlock_unload() {
131}
132
133void memlock_reload() {
134}
135
136//ll
Note: See TracBrowser for help on using the repository browser.