source: roaraudio/roard/memlock.c @ 4247:1097fae58767

Last change on this file since 4247:1097fae58767 was 4247:1097fae58767, checked in by phi, 14 years ago

get the locking working

File size: 3.5 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 inline int memlock_lock(struct memlock * seg) {
70 return roar_mm_mlock(seg->addr, seg->len);
71}
72
73static inline int memlock_unlock(struct memlock * seg) {
74 return roar_mm_munlock(seg->addr, seg->len);
75}
76
77int memlock_str2level(const char * str) {
78 if ( str == NULL )
79  return -1;
80
81 if ( !strcasecmp(str, "none") ) {
82  return MEMLOCK_NONE;
83 } else if ( !strcasecmp(str, "low") ) {
84  return MEMLOCK_LOW;
85 } else if ( !strcasecmp(str, "medium") ) {
86  return MEMLOCK_MEDIUM;
87 } else if ( !strcasecmp(str, "all") ) {
88  return MEMLOCK_ALL;
89 } else if ( !strcasecmp(str, "default") ) {
90  return MEMLOCK_DEFAULT;
91 } else {
92  return -1;
93 }
94}
95
96int memlock_set_level(int level) {
97 static int old_level = MEMLOCK_NONE;
98 int i;
99 int ret = 0;
100
101 ROAR_DBG("memlock_set_level(level=%i) = ?", level);
102
103 ROAR_DBG("memlock_set_level(level=%i): ask for locking level change: %i->%i", level, old_level, level);
104
105 if ( !memlock_table_inited )
106  memlock_table_init();
107
108 if ( level == old_level ) {
109  ROAR_DBG("memlock_set_level(level=%i) = 0 // old and new level are the same, nothing to do", level);
110  return 0;
111 }
112
113 for (i = 0; i < MAX_SEGMENTS; i++) {
114  if ( memlock_table[i].addr != NULL ) {
115   ROAR_DBG("memlock_set_level(level=%i): found registerd segment %i at %p with %llu Byte length", level, i, memlock_table[i].addr, (unsigned long long int)memlock_table[i].len);
116   if ( level > old_level ) {
117    if ( memlock_table[i].level > old_level && memlock_table[i].level <= level )
118     if ( memlock_lock(&(memlock_table[i])) == -1 )
119      ret = -1;
120   } else {
121    if ( memlock_table[i].level <= old_level && memlock_table[i].level > level )
122     if ( memlock_unlock(&(memlock_table[i])) == -1 )
123      ret = -1;
124   }
125  }
126 }
127
128 old_level = level;
129
130 ROAR_DBG("memlock_set_level(level=%i) = %i", level, ret);
131 return ret;
132}
133
134void memlock_unload() {
135}
136
137void memlock_reload() {
138}
139
140//ll
Note: See TracBrowser for help on using the repository browser.