SDL_FreeFormat(SDL_PixelFormat *format)
{
    SDL_PixelFormat *prev;

    if (!format) {
        SDL_InvalidParamError("format");
        return;
    }

    SDL_AtomicLock(&formats_lock);

    if (--format->refcount > 0) {
        SDL_AtomicUnlock(&formats_lock);
        return;
    }

    /* Remove this format from our list */
    if (format == formats) {
        formats = format->next;
    } else if (formats) {
        for (prev = formats; prev->next; prev = prev->next) {
            if (prev->next == format) {
                prev->next = format->next;
                break;
            }
        }
    }

    SDL_AtomicUnlock(&formats_lock);

    if (format->palette) {
        SDL_FreePalette(format->palette);
    }
    SDL_free(format);
}
SDL_FreeFormat(SDL_PixelFormat *format)
{
    SDL_PixelFormat *prev;

    if (!format) {
        SDL_InvalidParamError("format");
        return;
    }

    SDL_AtomicLock(&formats_lock);

    if (--format->refcount > 0) {
        SDL_AtomicUnlock(&formats_lock);
        return;
    }

     
    if (format == formats) {
        formats = format->next;
    } else if (formats) {
        for (prev = formats; prev->next; prev = prev->next) {
            if (prev->next == format) {
                prev->next = format->next;
                break;
            }
        }
    }

    SDL_AtomicUnlock(&formats_lock);

    if (format->palette) {
        SDL_FreePalette(format->palette);
    }
    SDL_free(format);
}