12 extern inline void LockUIntMapRead(UIntMap
*map
);
13 extern inline void UnlockUIntMapRead(UIntMap
*map
);
14 extern inline void LockUIntMapWrite(UIntMap
*map
);
15 extern inline void UnlockUIntMapWrite(UIntMap
*map
);
18 void InitUIntMap(UIntMap
*map
, ALsizei limit
)
25 RWLockInit(&map
->lock
);
28 void ResetUIntMap(UIntMap
*map
)
30 WriteLock(&map
->lock
);
36 WriteUnlock(&map
->lock
);
39 ALenum
InsertUIntMapEntry(UIntMap
*map
, ALuint key
, ALvoid
*value
)
43 WriteLock(&map
->lock
);
46 ALsizei count
= map
->size
;
48 ALsizei step
= count
>>1;
50 if(!(map
->keys
[i
] < key
))
60 if(pos
== map
->size
|| map
->keys
[pos
] != key
)
62 if(map
->size
== map
->limit
)
64 WriteUnlock(&map
->lock
);
65 return AL_OUT_OF_MEMORY
;
68 if(map
->size
== map
->capacity
)
72 ALsizei newcap
, keylen
;
74 newcap
= (map
->capacity
? (map
->capacity
<<1) : 4);
75 if(map
->limit
> 0 && newcap
> map
->limit
)
77 if(newcap
> map
->capacity
)
79 /* Round the memory size for keys up to a multiple of the
82 keylen
= newcap
* sizeof(map
->keys
[0]);
83 keylen
+= sizeof(map
->values
[0]) - 1;
84 keylen
-= keylen
%sizeof(map
->values
[0]);
86 keys
= al_malloc(16, keylen
+ newcap
*sizeof(map
->values
[0]));
90 WriteUnlock(&map
->lock
);
91 return AL_OUT_OF_MEMORY
;
93 values
= (ALvoid
**)((ALbyte
*)keys
+ keylen
);
97 memcpy(keys
, map
->keys
, map
->size
*sizeof(map
->keys
[0]));
98 memcpy(values
, map
->values
, map
->size
*sizeof(map
->values
[0]));
102 map
->values
= values
;
103 map
->capacity
= newcap
;
108 memmove(&map
->keys
[pos
+1], &map
->keys
[pos
],
109 (map
->size
-pos
)*sizeof(map
->keys
[0]));
110 memmove(&map
->values
[pos
+1], &map
->values
[pos
],
111 (map
->size
-pos
)*sizeof(map
->values
[0]));
115 map
->keys
[pos
] = key
;
116 map
->values
[pos
] = value
;
117 WriteUnlock(&map
->lock
);
122 ALvoid
*RemoveUIntMapKey(UIntMap
*map
, ALuint key
)
125 WriteLock(&map
->lock
);
129 ALsizei count
= map
->size
;
131 ALsizei step
= count
>>1;
132 ALsizei i
= pos
+step
;
133 if(!(map
->keys
[i
] < key
))
141 if(pos
< map
->size
&& map
->keys
[pos
] == key
)
143 ptr
= map
->values
[pos
];
144 if(pos
< map
->size
-1)
146 memmove(&map
->keys
[pos
], &map
->keys
[pos
+1],
147 (map
->size
-1-pos
)*sizeof(map
->keys
[0]));
148 memmove(&map
->values
[pos
], &map
->values
[pos
+1],
149 (map
->size
-1-pos
)*sizeof(map
->values
[0]));
154 WriteUnlock(&map
->lock
);
158 ALvoid
*RemoveUIntMapKeyNoLock(UIntMap
*map
, ALuint key
)
164 ALsizei count
= map
->size
;
166 ALsizei step
= count
>>1;
167 ALsizei i
= pos
+step
;
168 if(!(map
->keys
[i
] < key
))
176 if(pos
< map
->size
&& map
->keys
[pos
] == key
)
178 ptr
= map
->values
[pos
];
179 if(pos
< map
->size
-1)
181 memmove(&map
->keys
[pos
], &map
->keys
[pos
+1],
182 (map
->size
-1-pos
)*sizeof(map
->keys
[0]));
183 memmove(&map
->values
[pos
], &map
->values
[pos
+1],
184 (map
->size
-1-pos
)*sizeof(map
->values
[0]));
192 ALvoid
*LookupUIntMapKey(UIntMap
*map
, ALuint key
)
195 ReadLock(&map
->lock
);
199 ALsizei count
= map
->size
;
201 ALsizei step
= count
>>1;
202 ALsizei i
= pos
+step
;
203 if(!(map
->keys
[i
] < key
))
211 if(pos
< map
->size
&& map
->keys
[pos
] == key
)
212 ptr
= map
->values
[pos
];
214 ReadUnlock(&map
->lock
);
218 ALvoid
*LookupUIntMapKeyNoLock(UIntMap
*map
, ALuint key
)
223 ALsizei count
= map
->size
;
225 ALsizei step
= count
>>1;
226 ALsizei i
= pos
+step
;
227 if(!(map
->keys
[i
] < key
))
235 if(pos
< map
->size
&& map
->keys
[pos
] == key
)
236 return map
->values
[pos
];