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 void RelimitUIntMapNoLock(UIntMap
*map
, ALsizei limit
)
44 ALenum
InsertUIntMapEntry(UIntMap
*map
, ALuint key
, ALvoid
*value
)
48 WriteLock(&map
->lock
);
51 ALsizei count
= map
->size
;
53 ALsizei step
= count
>>1;
55 if(!(map
->keys
[i
] < key
))
65 if(pos
== map
->size
|| map
->keys
[pos
] != key
)
67 if(map
->size
>= map
->limit
)
69 WriteUnlock(&map
->lock
);
70 return AL_OUT_OF_MEMORY
;
73 if(map
->size
== map
->capacity
)
77 ALsizei newcap
, keylen
;
79 newcap
= (map
->capacity
? (map
->capacity
<<1) : 4);
80 if(map
->limit
> 0 && newcap
> map
->limit
)
82 if(newcap
> map
->capacity
)
84 /* Round the memory size for keys up to a multiple of the
87 keylen
= newcap
* sizeof(map
->keys
[0]);
88 keylen
+= sizeof(map
->values
[0]) - 1;
89 keylen
-= keylen
%sizeof(map
->values
[0]);
91 keys
= al_malloc(16, keylen
+ newcap
*sizeof(map
->values
[0]));
95 WriteUnlock(&map
->lock
);
96 return AL_OUT_OF_MEMORY
;
98 values
= (ALvoid
**)((ALbyte
*)keys
+ keylen
);
102 memcpy(keys
, map
->keys
, map
->size
*sizeof(map
->keys
[0]));
103 memcpy(values
, map
->values
, map
->size
*sizeof(map
->values
[0]));
107 map
->values
= values
;
108 map
->capacity
= newcap
;
113 memmove(&map
->keys
[pos
+1], &map
->keys
[pos
],
114 (map
->size
-pos
)*sizeof(map
->keys
[0]));
115 memmove(&map
->values
[pos
+1], &map
->values
[pos
],
116 (map
->size
-pos
)*sizeof(map
->values
[0]));
120 map
->keys
[pos
] = key
;
121 map
->values
[pos
] = value
;
122 WriteUnlock(&map
->lock
);
127 ALenum
InsertUIntMapEntryNoLock(UIntMap
*map
, ALuint key
, ALvoid
*value
)
133 ALsizei count
= map
->size
;
135 ALsizei step
= count
>>1;
136 ALsizei i
= pos
+step
;
137 if(!(map
->keys
[i
] < key
))
147 if(pos
== map
->size
|| map
->keys
[pos
] != key
)
149 if(map
->size
>= map
->limit
)
150 return AL_OUT_OF_MEMORY
;
152 if(map
->size
== map
->capacity
)
156 ALsizei newcap
, keylen
;
158 newcap
= (map
->capacity
? (map
->capacity
<<1) : 4);
159 if(map
->limit
> 0 && newcap
> map
->limit
)
161 if(newcap
> map
->capacity
)
163 /* Round the memory size for keys up to a multiple of the
166 keylen
= newcap
* sizeof(map
->keys
[0]);
167 keylen
+= sizeof(map
->values
[0]) - 1;
168 keylen
-= keylen
%sizeof(map
->values
[0]);
170 keys
= al_malloc(16, keylen
+ newcap
*sizeof(map
->values
[0]));
173 return AL_OUT_OF_MEMORY
;
174 values
= (ALvoid
**)((ALbyte
*)keys
+ keylen
);
178 memcpy(keys
, map
->keys
, map
->size
*sizeof(map
->keys
[0]));
179 memcpy(values
, map
->values
, map
->size
*sizeof(map
->values
[0]));
183 map
->values
= values
;
184 map
->capacity
= newcap
;
189 memmove(&map
->keys
[pos
+1], &map
->keys
[pos
],
190 (map
->size
-pos
)*sizeof(map
->keys
[0]));
191 memmove(&map
->values
[pos
+1], &map
->values
[pos
],
192 (map
->size
-pos
)*sizeof(map
->values
[0]));
196 map
->keys
[pos
] = key
;
197 map
->values
[pos
] = value
;
202 ALvoid
*RemoveUIntMapKey(UIntMap
*map
, ALuint key
)
205 WriteLock(&map
->lock
);
209 ALsizei count
= map
->size
;
211 ALsizei step
= count
>>1;
212 ALsizei i
= pos
+step
;
213 if(!(map
->keys
[i
] < key
))
221 if(pos
< map
->size
&& map
->keys
[pos
] == key
)
223 ptr
= map
->values
[pos
];
224 if(pos
< map
->size
-1)
226 memmove(&map
->keys
[pos
], &map
->keys
[pos
+1],
227 (map
->size
-1-pos
)*sizeof(map
->keys
[0]));
228 memmove(&map
->values
[pos
], &map
->values
[pos
+1],
229 (map
->size
-1-pos
)*sizeof(map
->values
[0]));
234 WriteUnlock(&map
->lock
);
238 ALvoid
*RemoveUIntMapKeyNoLock(UIntMap
*map
, ALuint key
)
244 ALsizei count
= map
->size
;
246 ALsizei step
= count
>>1;
247 ALsizei i
= pos
+step
;
248 if(!(map
->keys
[i
] < key
))
256 if(pos
< map
->size
&& map
->keys
[pos
] == key
)
258 ptr
= map
->values
[pos
];
259 if(pos
< map
->size
-1)
261 memmove(&map
->keys
[pos
], &map
->keys
[pos
+1],
262 (map
->size
-1-pos
)*sizeof(map
->keys
[0]));
263 memmove(&map
->values
[pos
], &map
->values
[pos
+1],
264 (map
->size
-1-pos
)*sizeof(map
->values
[0]));
272 ALvoid
*LookupUIntMapKey(UIntMap
*map
, ALuint key
)
275 ReadLock(&map
->lock
);
279 ALsizei count
= map
->size
;
281 ALsizei step
= count
>>1;
282 ALsizei i
= pos
+step
;
283 if(!(map
->keys
[i
] < key
))
291 if(pos
< map
->size
&& map
->keys
[pos
] == key
)
292 ptr
= map
->values
[pos
];
294 ReadUnlock(&map
->lock
);
298 ALvoid
*LookupUIntMapKeyNoLock(UIntMap
*map
, ALuint key
)
303 ALsizei count
= map
->size
;
305 ALsizei step
= count
>>1;
306 ALsizei i
= pos
+step
;
307 if(!(map
->keys
[i
] < key
))
315 if(pos
< map
->size
&& map
->keys
[pos
] == key
)
316 return map
->values
[pos
];