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
);
47 ALsizei high
= map
->size
- 1;
50 ALsizei mid
= low
+ (high
-low
)/2;
51 if(map
->keys
[mid
] < key
)
56 if(map
->keys
[low
] < key
)
61 if(pos
== map
->size
|| map
->keys
[pos
] != key
)
63 if(map
->size
== map
->limit
)
65 WriteUnlock(&map
->lock
);
66 return AL_OUT_OF_MEMORY
;
69 if(map
->size
== map
->capacity
)
73 ALsizei newcap
, keylen
;
75 newcap
= (map
->capacity
? (map
->capacity
<<1) : 4);
76 if(map
->limit
> 0 && newcap
> map
->limit
)
78 if(newcap
> map
->capacity
)
80 /* Round the memory size for keys up to a multiple of the
83 keylen
= newcap
* sizeof(map
->keys
[0]);
84 keylen
+= sizeof(map
->values
[0]) - 1;
85 keylen
-= keylen
%sizeof(map
->values
[0]);
87 keys
= al_malloc(16, keylen
+ newcap
*sizeof(map
->values
[0]));
91 WriteUnlock(&map
->lock
);
92 return AL_OUT_OF_MEMORY
;
94 values
= (ALvoid
**)((ALbyte
*)keys
+ keylen
);
98 memcpy(keys
, map
->keys
, map
->size
*sizeof(map
->keys
[0]));
99 memcpy(values
, map
->values
, map
->size
*sizeof(map
->values
[0]));
103 map
->values
= values
;
104 map
->capacity
= newcap
;
109 memmove(&map
->keys
[pos
+1], &map
->keys
[pos
],
110 (map
->size
-pos
)*sizeof(map
->keys
[0]));
111 memmove(&map
->values
[pos
+1], &map
->values
[pos
],
112 (map
->size
-pos
)*sizeof(map
->values
[0]));
116 map
->keys
[pos
] = key
;
117 map
->values
[pos
] = value
;
118 WriteUnlock(&map
->lock
);
123 ALvoid
*RemoveUIntMapKey(UIntMap
*map
, ALuint key
)
126 WriteLock(&map
->lock
);
130 ALsizei high
= map
->size
- 1;
133 ALsizei mid
= low
+ (high
-low
)/2;
134 if(map
->keys
[mid
] < key
)
139 if(map
->keys
[low
] == key
)
141 ptr
= map
->values
[low
];
142 if(low
< map
->size
-1)
144 memmove(&map
->keys
[low
], &map
->keys
[low
+1],
145 (map
->size
-1-low
)*sizeof(map
->keys
[0]));
146 memmove(&map
->values
[low
], &map
->values
[low
+1],
147 (map
->size
-1-low
)*sizeof(map
->values
[0]));
152 WriteUnlock(&map
->lock
);
156 ALvoid
*RemoveUIntMapKeyNoLock(UIntMap
*map
, ALuint key
)
161 ALsizei high
= map
->size
- 1;
164 ALsizei mid
= low
+ (high
-low
)/2;
165 if(map
->keys
[mid
] < key
)
170 if(map
->keys
[low
] == key
)
172 ALvoid
*ptr
= map
->values
[low
];
173 if(low
< map
->size
-1)
175 memmove(&map
->keys
[low
], &map
->keys
[low
+1],
176 (map
->size
-1-low
)*sizeof(map
->keys
[0]));
177 memmove(&map
->values
[low
], &map
->values
[low
+1],
178 (map
->size
-1-low
)*sizeof(map
->values
[0]));
187 ALvoid
*LookupUIntMapKey(UIntMap
*map
, ALuint key
)
190 ReadLock(&map
->lock
);
194 ALsizei high
= map
->size
- 1;
197 ALsizei mid
= low
+ (high
-low
)/2;
198 if(map
->keys
[mid
] < key
)
203 if(map
->keys
[low
] == key
)
204 ptr
= map
->values
[low
];
206 ReadUnlock(&map
->lock
);
210 ALvoid
*LookupUIntMapKeyNoLock(UIntMap
*map
, ALuint key
)
215 ALsizei high
= map
->size
- 1;
218 ALsizei mid
= low
+ (high
-low
)/2;
219 if(map
->keys
[mid
] < key
)
224 if(map
->keys
[low
] == key
)
225 return map
->values
[low
];