SDL_AllocFormat(Uint32 pixel_format)
{
    SDL_PixelFormat *format;

    SDL_AtomicLock(&formats_lock);

    /* Look it up in our list of previously allocated formats */
    for (format = formats; format; format = format->next) {
        if (pixel_format == format->format) {
            ++format->refcount;
            SDL_AtomicUnlock(&formats_lock);
            return format;
        }
    }

    /* Allocate an empty pixel format structure, and initialize it */
    format = SDL_malloc(sizeof(*format));
    if (format == NULL) {
        SDL_AtomicUnlock(&formats_lock);
        SDL_OutOfMemory();
        return NULL;
    }
    if (SDL_InitFormat(format, pixel_format) < 0) {
        SDL_AtomicUnlock(&formats_lock);
        SDL_free(format);
        SDL_InvalidParamError("format");
        return NULL;
    }

    if (!SDL_ISPIXELFORMAT_INDEXED(pixel_format)) {
        /* Cache the RGB formats */
        format->next = formats;
        formats = format;
    }

    SDL_AtomicUnlock(&formats_lock);

    return format;
}
SDL_AllocFormat(Uint32 pixel_format)
{
    SDL_PixelFormat *format;

    SDL_AtomicLock(&formats_lock);

     
    for (format = formats; format; format = format->next) {
        if (pixel_format == format->format) {
            ++format->refcount;
            SDL_AtomicUnlock(&formats_lock);
            return format;
        }
    }

     
    format = SDL_malloc(sizeof(*format));
    if (format == NULL) {
        SDL_AtomicUnlock(&formats_lock);
        SDL_OutOfMemory();
        return NULL;
    }
    if (SDL_InitFormat(format, pixel_format) < 0) {
        SDL_AtomicUnlock(&formats_lock);
        SDL_free(format);
        SDL_InvalidParamError("format");
        return NULL;
    }

    if (!SDL_ISPIXELFORMAT_INDEXED(pixel_format)) {
         
        format->next = formats;
        formats = format;
    }

    SDL_AtomicUnlock(&formats_lock);

    return format;
}