| 500 | static SDL_SpinLock formats_lock = 0; |
| 501 | |
| 502 | SDL_PixelFormat * |
| 503 | SDL_AllocFormat(Uint32 pixel_format) |
| 504 | { |
| 505 | SDL_PixelFormat *format; |
| 506 | |
| 507 | SDL_AtomicLock(&formats_lock); |
| 508 | |
| 509 | /* Look it up in our list of previously allocated formats */ |
| 510 | for (format = formats; format; format = format->next) { |
| 511 | if (pixel_format == format->format) { |
| 512 | ++format->refcount; |
| 513 | SDL_AtomicUnlock(&formats_lock); |
| 514 | return format; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | /* Allocate an empty pixel format structure, and initialize it */ |
| 519 | format = SDL_malloc(sizeof(*format)); |
| 520 | if (format == NULL) { |
| 521 | SDL_AtomicUnlock(&formats_lock); |
| 522 | SDL_OutOfMemory(); |
| 523 | return NULL; |
| 524 | } |
| 525 | if (SDL_InitFormat(format, pixel_format) < 0) { |
| 526 | SDL_AtomicUnlock(&formats_lock); |
| 527 | SDL_free(format); |
| 528 | SDL_InvalidParamError("format"); |
| 529 | return NULL; |
| 530 | } |
| 531 | |
| 532 | if (!SDL_ISPIXELFORMAT_INDEXED(pixel_format)) { |
| 533 | /* Cache the RGB formats */ |
| 534 | format->next = formats; |
| 535 | formats = format; |
| 536 | } |
| 537 | |
| 538 | SDL_AtomicUnlock(&formats_lock); |
| 539 | |
| 540 | return format; |
| 541 | } |
| 542 | |
| 543 | int |
| 544 | SDL_InitFormat(SDL_PixelFormat * format, Uint32 pixel_format) |