b6978178b1
Introduces wrappers around memory allocation functions in `memory.h` that should be used instead of the standard C ones. These never return NULL and, with the exception of `mem_realloc()`, zero-initialize the allocated memory like `calloc()` does. All allocations made with the memory.h API must be deallocated with `mem_free()`. Although standard `free()` will work on some platforms, it's not portable (currently it won't work on Windows). Likewise, `mem_free()` must not be used to free foreign allocations. The standard C allocation functions are now diagnosed as deprecated. They are, however, available with the `libc_` prefix in case interfacing with foreign APIs is required. So far they are only used to implement `memory.h`. Perhaps the most important change is the introduction of the `ALLOC()`, `ALLOC_ARRAY()`, and `ALLOC_FLEX()` macros. They take a type as a parameter, and allocate enough memory with the correct alignment for that type. That includes overaligned types as well. In most circumstances you should prefer to use these macros. See the `memory.h` header for some usage examples.
69 lines
2 KiB
C
69 lines
2 KiB
C
/*
|
|
* This software is licensed under the terms of the MIT License.
|
|
* See COPYING for further information.
|
|
* ---
|
|
* Copyright (c) 2011-2019, Lukas Weber <laochailan@web.de>.
|
|
* Copyright (c) 2012-2019, Andrei Alexeyev <akari@taisei-project.org>.
|
|
*/
|
|
|
|
#include "taisei.h"
|
|
|
|
#include "pngcruft.h"
|
|
#include "log.h"
|
|
|
|
static void pngutil_rwops_write_data(png_structp png_ptr, png_bytep data, png_size_t length) {
|
|
SDL_RWops *out = png_get_io_ptr(png_ptr);
|
|
SDL_RWwrite(out, data, length, 1);
|
|
}
|
|
|
|
static void pngutil_rwops_flush_data(png_structp png_ptr) {
|
|
// no flush operation in SDL_RWops
|
|
}
|
|
|
|
static void pngutil_rwops_read_data(png_structp png_ptr, png_bytep data, png_size_t length) {
|
|
SDL_RWops *out = png_get_io_ptr(png_ptr);
|
|
SDL_RWread(out, data, length, 1);
|
|
}
|
|
|
|
void pngutil_init_rwops_read(png_structp png, SDL_RWops *rwops) {
|
|
png_set_read_fn(png, rwops, pngutil_rwops_read_data);
|
|
}
|
|
|
|
void pngutil_init_rwops_write(png_structp png, SDL_RWops *rwops) {
|
|
png_set_write_fn(png, rwops, pngutil_rwops_write_data, pngutil_rwops_flush_data);
|
|
}
|
|
|
|
noreturn static void pngutil_error_handler(png_structp png_ptr, png_const_charp error_msg) {
|
|
log_error("PNG error: %s", error_msg);
|
|
png_longjmp(png_ptr, 1);
|
|
}
|
|
|
|
static void pngutil_warning_handler(png_structp png_ptr, png_const_charp warning_msg) {
|
|
log_warn("PNG warning: %s", warning_msg);
|
|
}
|
|
|
|
void pngutil_setup_error_handlers(png_structp png) {
|
|
png_set_error_fn(png, NULL, pngutil_error_handler, pngutil_warning_handler);
|
|
}
|
|
|
|
static PNG_CALLBACK(png_voidp, pngutil_malloc, (png_structp png, png_alloc_size_t size)) {
|
|
return mem_alloc(size);
|
|
}
|
|
|
|
static PNG_CALLBACK(void, pngutil_free, (png_structp png, png_voidp ptr)) {
|
|
mem_free(ptr);
|
|
}
|
|
|
|
png_structp pngutil_create_read_struct(void) {
|
|
return png_create_read_struct_2(
|
|
PNG_LIBPNG_VER_STRING,
|
|
NULL, pngutil_error_handler, pngutil_warning_handler,
|
|
NULL, pngutil_malloc, pngutil_free);
|
|
}
|
|
|
|
png_structp pngutil_create_write_struct(void) {
|
|
return png_create_write_struct_2(
|
|
PNG_LIBPNG_VER_STRING,
|
|
NULL, pngutil_error_handler, pngutil_warning_handler,
|
|
NULL, pngutil_malloc, pngutil_free);
|
|
}
|