| | 207 | } |
| | 208 | |
| | 209 | |
| | 210 | /** |
| | 211 | Size is the number of bytes to allocate. |
| | 212 | |
| | 213 | If retp is non-zero it will be set to the new buffer. |
| | 214 | */ |
| | 215 | UIndex ur_makeBuffer( int dataType, int size, UBuffer** retp ) |
| | 216 | { |
| | 217 | UBuffer* buf; |
| | 218 | UGCArray* gc = &ur_env->buffers; |
| | 219 | UArray* ca = &gc->arr; |
| | 220 | |
| | 221 | #ifdef GC_STRESS_TEST |
| | 222 | if( ur_env->flags & UR_ENV_GC ) |
| | 223 | ur_recycle( ur_env ); |
| | 224 | #endif |
| | 225 | |
| | 226 | if( ur_env->buffers.freeCount ) |
| | 227 | { |
| | 228 | #ifndef GC_STRESS_TEST |
| | 229 | getFree: |
| | 230 | #endif |
| | 231 | buf = ((UBuffer*) ca->ptr.v) + gc->freeList; |
| | 232 | |
| | 233 | assert( gc->freeList > -1 ); |
| | 234 | assert( buf->dataType == UT_UNSET ); |
| | 235 | |
| | 236 | gc->freeList = buf->link; |
| | 237 | gc->freeCount--; |
| | 238 | |
| | 239 | goto init; |
| | 240 | } |
| | 241 | |
| | 242 | #ifndef GC_STRESS_TEST |
| | 243 | if( (ca->used == ca->avail) && (ur_env->flags & UR_ENV_GC) ) |
| | 244 | { |
| | 245 | ur_recycle( ur_env ); |
| | 246 | if( ur_env->buffers.freeCount ) |
| | 247 | goto getFree; |
| | 248 | } |
| | 249 | #endif |
| | 250 | |
| | 251 | UR_EXPAND_1( UBuffer, ca, buf ); |
| | 252 | |
| | 253 | init: |
| | 254 | |
| | 255 | buf->dataType = dataType; |
| | 256 | buf->ptr = size ? memAlloc( size ) : 0; |
| | 257 | |
| | 258 | if( retp ) |
| | 259 | *retp = buf; |
| | 260 | |
| | 261 | return buf - ((UBuffer*) ca->ptr.v); |