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 | |
---|
30 | struct memlock { |
---|
31 | int level; |
---|
32 | void * addr; |
---|
33 | size_t len; |
---|
34 | }; |
---|
35 | |
---|
36 | static struct memlock memlock_table[MAX_SEGMENTS]; |
---|
37 | |
---|
38 | static volatile int memlock_table_inited = 0; |
---|
39 | |
---|
40 | static 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 | |
---|
51 | int 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 | |
---|
69 | static inline int memlock_lock(struct memlock * seg) { |
---|
70 | return roar_mm_mlock(seg->addr, seg->len); |
---|
71 | } |
---|
72 | |
---|
73 | static inline int memlock_unlock(struct memlock * seg) { |
---|
74 | return roar_mm_munlock(seg->addr, seg->len); |
---|
75 | } |
---|
76 | |
---|
77 | int 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 | |
---|
96 | int memlock_set_level(int level) { |
---|
97 | static int old_level = MEMLOCK_NONE; |
---|
98 | int i; |
---|
99 | int ret = 0; |
---|
100 | |
---|
101 | if ( !memlock_table_inited ) |
---|
102 | memlock_table_init(); |
---|
103 | |
---|
104 | if ( level == old_level ) |
---|
105 | return 0; |
---|
106 | |
---|
107 | for (i = 0; i < MAX_SEGMENTS; i++) { |
---|
108 | if ( memlock_table[i].addr == NULL ) { |
---|
109 | if ( level > old_level ) { |
---|
110 | if ( memlock_table[i].level > old_level && memlock_table[i].level <= level ) |
---|
111 | if ( memlock_lock(&(memlock_table[i])) == -1 ) |
---|
112 | ret = -1; |
---|
113 | } else { |
---|
114 | if ( memlock_table[i].level <= old_level && memlock_table[i].level > level ) |
---|
115 | if ( memlock_unlock(&(memlock_table[i])) == -1 ) |
---|
116 | ret = -1; |
---|
117 | } |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | old_level = level; |
---|
122 | |
---|
123 | return ret; |
---|
124 | } |
---|
125 | |
---|
126 | void memlock_unload() { |
---|
127 | } |
---|
128 | |
---|
129 | void memlock_reload() { |
---|
130 | } |
---|
131 | |
---|
132 | //ll |
---|