[17] | 1 | #include "al_siteconfig.h" |
---|
| 2 | |
---|
| 3 | #include <stdlib.h> |
---|
| 4 | #include <sys/types.h> |
---|
| 5 | |
---|
| 6 | #include "al_debug.h" |
---|
| 7 | #include "al_source.h" |
---|
| 8 | #include "al_spool.h" |
---|
| 9 | |
---|
| 10 | /* |
---|
| 11 | * Resize spool to at least newsize units |
---|
| 12 | */ |
---|
| 13 | ALboolean spool_resize(spool_t *spool, size_t newsize) { |
---|
| 14 | void *temp; |
---|
| 15 | unsigned int i; |
---|
| 16 | |
---|
| 17 | if(newsize < 1) { |
---|
| 18 | newsize = 1; |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | if(spool->size >= newsize) { |
---|
| 22 | return AL_TRUE; /* no resize needed */ |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | /* |
---|
| 26 | * Resize the AL_source pool. |
---|
| 27 | */ |
---|
| 28 | temp = realloc(spool->pool, newsize * sizeof *spool->pool); |
---|
| 29 | if(temp == NULL) { |
---|
| 30 | return AL_FALSE; /* could not realloc */ |
---|
| 31 | } |
---|
| 32 | spool->pool = temp; |
---|
| 33 | |
---|
| 34 | /* |
---|
| 35 | * Resize the sid -> index map. |
---|
| 36 | */ |
---|
| 37 | temp = realloc(spool->map, newsize * sizeof *spool->map); |
---|
| 38 | if(temp == NULL) { |
---|
| 39 | return AL_FALSE; /* could not resize map */ |
---|
| 40 | } |
---|
| 41 | spool->map = temp; |
---|
| 42 | |
---|
| 43 | /* |
---|
| 44 | * Resize the mutex locks |
---|
| 45 | */ |
---|
| 46 | temp = realloc( spool->smutexen, newsize * sizeof *spool->smutexen ); |
---|
| 47 | if( temp == NULL ) { |
---|
| 48 | return AL_FALSE; |
---|
| 49 | } |
---|
| 50 | spool->smutexen = temp; |
---|
| 51 | |
---|
| 52 | /* |
---|
| 53 | * set defaults for newly allocated stuff |
---|
| 54 | */ |
---|
| 55 | for(i = spool->size; i < newsize; i++) { |
---|
| 56 | spool->pool[i].inuse = AL_FALSE; /* use flag */ |
---|
| 57 | spool->map[i] = 0; /* unset sid */ |
---|
| 58 | spool->smutexen[i] = _alCreateMutex(); /* new mutex */ |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | spool->size = newsize; |
---|
| 62 | |
---|
| 63 | return AL_TRUE; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | /* |
---|
| 67 | * Reserve an AL_source in spool, returning the index of the AL_source, |
---|
| 68 | * and mark it in use. Return -1 if no reservation is possible. |
---|
| 69 | */ |
---|
| 70 | int spool_alloc(spool_t *spool) { |
---|
| 71 | int sindex; |
---|
| 72 | |
---|
| 73 | sindex = spool_first_free_index(spool); |
---|
| 74 | if(sindex == -1) { |
---|
| 75 | if(spool_resize(spool, spool->size * 2) == AL_FALSE) { |
---|
| 76 | return -1; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | sindex = spool_first_free_index(spool); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | spool->pool[sindex].inuse = AL_TRUE; |
---|
| 83 | spool->map[sindex] = spool_next_id(); |
---|
| 84 | |
---|
| 85 | return spool->map[sindex]; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | /* |
---|
| 89 | * Get the AL_source from sourcepool spool at index index, if |
---|
| 90 | * it's in use. Otherwise, return NULL. |
---|
| 91 | */ |
---|
| 92 | AL_source *spool_index(spool_t *spool, ALuint sid) { |
---|
| 93 | int sindex; |
---|
| 94 | |
---|
| 95 | sindex = spool_sid_to_index(spool, sid); |
---|
| 96 | |
---|
| 97 | if(sindex < 0) { |
---|
| 98 | return NULL; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | if(sindex >= (int) spool->size) { |
---|
| 102 | return NULL; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | if(spool->pool[sindex].inuse == AL_FALSE) { |
---|
| 106 | return NULL; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | return &spool->pool[sindex].data; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | int spool_first_free_index(spool_t *spool) { |
---|
| 113 | unsigned int i; |
---|
| 114 | |
---|
| 115 | for(i = 0; i < spool->size; i++) { |
---|
| 116 | if(spool->pool[i].inuse == AL_FALSE) { |
---|
| 117 | return i; |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | return -1; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | ALboolean spool_dealloc(spool_t *spool, ALuint sid, |
---|
| 125 | void (*freer_func)(void *)) { |
---|
| 126 | AL_source *src; |
---|
| 127 | int sindex; |
---|
| 128 | |
---|
| 129 | sindex = spool_sid_to_index(spool, sid); |
---|
| 130 | if(sindex < 0) { |
---|
| 131 | /* sid is invalid */ |
---|
| 132 | return AL_FALSE; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | if(sindex >= (int) spool->size) { |
---|
| 136 | return AL_FALSE; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | src = spool_index(spool, sid); |
---|
| 140 | if(src == NULL) { |
---|
| 141 | _alDebug(ALD_SOURCE, |
---|
| 142 | __FILE__, __LINE__, |
---|
| 143 | "sid %d is a bad index", sid); |
---|
| 144 | return AL_FALSE; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | if(spool->pool[sindex].inuse == AL_FALSE) { |
---|
| 148 | /* already deleted */ |
---|
| 149 | return AL_FALSE; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | freer_func(src); |
---|
| 153 | |
---|
| 154 | spool->pool[sindex].inuse = AL_FALSE; |
---|
| 155 | |
---|
| 156 | return AL_TRUE; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | void spool_free(spool_t *spool, void (*freer_func)(void *)) { |
---|
| 160 | unsigned int i; |
---|
| 161 | ALuint sid; |
---|
| 162 | |
---|
| 163 | for(i = 0; i < spool->size; i++) { |
---|
| 164 | if(spool->pool[i].inuse == AL_TRUE) { |
---|
| 165 | sid = spool->pool[i].data.sid; |
---|
| 166 | |
---|
| 167 | spool_dealloc(spool, sid, freer_func); |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | if(spool->pool != NULL) { |
---|
| 172 | free(spool->pool); |
---|
| 173 | spool->pool = NULL; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | if(spool->map != NULL) { |
---|
| 177 | free(spool->map); |
---|
| 178 | spool->map = NULL; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | spool->size = 0; |
---|
| 182 | |
---|
| 183 | /* let the caller free spool itself */ |
---|
| 184 | return; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | void spool_init(spool_t *spool) { |
---|
| 188 | if(spool == NULL) { |
---|
| 189 | return; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | spool->size = 0; |
---|
| 193 | spool->pool = NULL; |
---|
| 194 | spool->map = NULL; |
---|
| 195 | spool->smutexen = NULL; |
---|
| 196 | |
---|
| 197 | return; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | /* |
---|
| 201 | * Get unique id. |
---|
| 202 | */ |
---|
| 203 | ALuint spool_next_id(void) { |
---|
| 204 | static ALuint id = AL_FIRST_SOURCE_ID; |
---|
| 205 | |
---|
| 206 | return ++id; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | /* |
---|
| 210 | * Convert the sid to an index in spool's pool. This is |
---|
| 211 | * to enable unique sids but reuse the indexes. |
---|
| 212 | * |
---|
| 213 | * FIXME: use binary search. |
---|
| 214 | */ |
---|
| 215 | int spool_sid_to_index(spool_t *spool, ALuint sid) { |
---|
| 216 | unsigned int i; |
---|
| 217 | |
---|
| 218 | for(i = 0; i < spool->size; i++) { |
---|
| 219 | if(spool->map[i] == sid) { |
---|
| 220 | return i; |
---|
| 221 | } |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | return -1; |
---|
| 225 | } |
---|