replace memory functions with SDL equivalent

This commit is contained in:
Jan200101
2025-05-18 23:16:09 +08:00
committed by takase1121
parent 9408dddb37
commit affb56e817
12 changed files with 53 additions and 49 deletions
+1 -1
View File
@@ -90,7 +90,7 @@ static int f_dirmonitor_gc(lua_State* L) {
deinit_dirmonitor(monitor->internal);
SDL_UnlockMutex(monitor->mutex);
SDL_WaitThread(monitor->thread, NULL);
free(monitor->internal);
SDL_free(monitor->internal);
SDL_DestroyMutex(monitor->mutex);
return 0;
}
+8 -8
View File
@@ -19,7 +19,7 @@ struct dirmonitor_internal* init_dirmonitor() {
mainloop_registered = true;
}
struct dirmonitor_internal* monitor = malloc(sizeof(struct dirmonitor_internal));
struct dirmonitor_internal* monitor = SDL_calloc(1, sizeof(struct dirmonitor_internal));
monitor->stream = NULL;
monitor->changes = NULL;
monitor->count = 0;
@@ -45,9 +45,9 @@ static void stop_monitor_stream(struct dirmonitor_internal* monitor) {
close(monitor->fds[1]);
if (monitor->count > 0) {
for (size_t i = 0; i<monitor->count; i++) {
free(monitor->changes[i]);
SDL_free(monitor->changes[i]);
}
free(monitor->changes);
SDL_free(monitor->changes);
monitor->changes = NULL;
monitor->count = 0;
}
@@ -82,17 +82,17 @@ static void stream_callback(
size_t total = 0;
if (monitor->count == 0) {
total = numEvents;
monitor->changes = calloc(numEvents, sizeof(char*));
monitor->changes = SDL_calloc(numEvents, sizeof(char*));
} else {
total = monitor->count + numEvents;
monitor->changes = realloc(
monitor->changes = SDL_realloc(
monitor->changes,
sizeof(char*) * total
);
}
for (size_t idx = monitor->count; idx < total; idx++) {
size_t pidx = idx - monitor->count;
monitor->changes[idx] = malloc(strlen(path_list[pidx])+1);
monitor->changes[idx] = SDL_malloc(strlen(path_list[pidx])+1);
strcpy(monitor->changes[idx], path_list[pidx]);
}
monitor->count = total;
@@ -131,9 +131,9 @@ int translate_changes_dirmonitor(
if (monitor->count > 0) {
for (size_t i = 0; i<monitor->count; i++) {
change_callback(strlen(monitor->changes[i]), monitor->changes[i], L);
free(monitor->changes[i]);
SDL_free(monitor->changes[i]);
}
free(monitor->changes);
SDL_free(monitor->changes);
monitor->changes = NULL;
monitor->count = 0;
}
+2 -1
View File
@@ -1,3 +1,4 @@
#include <SDL3/SDL.h>
#include <sys/inotify.h>
#include <stdlib.h>
#include <unistd.h>
@@ -13,7 +14,7 @@ struct dirmonitor_internal {
struct dirmonitor_internal* init_dirmonitor() {
struct dirmonitor_internal* monitor = calloc(1, sizeof(struct dirmonitor_internal));
struct dirmonitor_internal* monitor = SDL_calloc(1, sizeof(struct dirmonitor_internal));
monitor->fd = inotify_init();
pipe(monitor->sig);
fcntl(monitor->sig[0], F_SETFD, FD_CLOEXEC);
+2 -1
View File
@@ -1,3 +1,4 @@
#include <SDL3/SDL.h>
#include <sys/event.h>
#include <sys/stat.h>
#include <stdlib.h>
@@ -12,7 +13,7 @@ struct dirmonitor_internal {
struct dirmonitor_internal* init_dirmonitor() {
struct dirmonitor_internal* monitor = calloc(1, sizeof(struct dirmonitor_internal));
struct dirmonitor_internal* monitor = SDL_calloc(1, sizeof(struct dirmonitor_internal));
monitor->fd = kqueue();
return monitor;
}
+2 -1
View File
@@ -1,3 +1,4 @@
#include <SDL3/SDL.h>
#include <windows.h>
@@ -19,7 +20,7 @@ int get_changes_dirmonitor(struct dirmonitor_internal* monitor, char* buffer, in
struct dirmonitor* init_dirmonitor() {
return calloc(1, sizeof(struct dirmonitor_internal));
return SDL_calloc(1, sizeof(struct dirmonitor_internal));
}
+3 -3
View File
@@ -139,7 +139,7 @@ static void kill_list_free(process_kill_list_t *list) {
while (node) {
temp = node;
node = node->next;
free(temp);
SDL_free(temp);
}
memset(list, 0, sizeof(process_kill_list_t));
}
@@ -281,7 +281,7 @@ static int kill_list_worker(void *ud) {
free_task:
SDL_SignalCondition(list->work_done);
process_handle_close(&current_task->handle);
free(current_task);
SDL_free(current_task);
}
}
delay = list->head ? (list->head->start_time + PROCESS_TERM_DELAY) - SDL_GetTicks() : 0;
@@ -746,7 +746,7 @@ static int f_gc(lua_State* L) {
if (poll_process(self, 0) && !self->detached) {
// attempt to kill the process if still running and not detached
signal_process(self, SIGNAL_TERM);
if (!list || !list->worker_thread || !(p = malloc(sizeof(process_kill_t)))) {
if (!list || !list->worker_thread || !(p = SDL_malloc(sizeof(process_kill_t)))) {
// use synchronous waiting
if (poll_process(self, PROCESS_TERM_DELAY)) {
signal_process(self, SIGNAL_KILL);
+6 -5
View File
@@ -2,6 +2,7 @@
#define PCRE2_CODE_UNIT_WIDTH 8
#include <SDL3/SDL.h>
#include <string.h>
#include <pcre2.h>
#include <stdbool.h>
@@ -269,7 +270,7 @@ static int f_pcre_gsub(lua_State *L) {
pcre2_match_data* match_data = pcre2_match_data_create_from_pattern(re, NULL);
size_t buffer_size = 1024;
char *output = (char *)malloc(buffer_size);
char *output = (char *)SDL_malloc(buffer_size);
int options = PCRE2_SUBSTITUTE_OVERFLOW_LENGTH | PCRE2_SUBSTITUTE_EXTENDED;
if (limit == 0) options |= PCRE2_SUBSTITUTE_GLOBAL;
@@ -304,19 +305,19 @@ static int f_pcre_gsub(lua_State *L) {
} else {
offset = ovector[1] - (subject_len - outlen);
}
if (limit_count > 1) free(subject);
if (limit_count > 1) SDL_free(subject);
if (limit_count == limit || offset-1 == outlen) {
done = true;
results_count = limit_count;
} else {
subject = output;
subject_len = outlen;
output = (char *)malloc(buffer_size);
output = (char *)SDL_malloc(buffer_size);
outlen = buffer_size;
}
} else {
if (limit_count > 1) {
free(subject);
SDL_free(subject);
}
done = true;
results_count = limit_count;
@@ -340,7 +341,7 @@ static int f_pcre_gsub(lua_State *L) {
return_count = 2;
}
free(output);
SDL_free(output);
pcre2_match_data_free(match_data);
if (regex_compiled)
pcre2_code_free(re);
+8 -8
View File
@@ -572,7 +572,7 @@ static int f_chdir(lua_State *L) {
LPWSTR wpath = utfconv_utf8towc(path);
if (wpath == NULL) { return luaL_error(L, UTFCONV_ERROR_INVALID_CONVERSION ); }
int err = _wchdir(wpath);
free(wpath);
SDL_free(wpath);
#else
int err = chdir(path);
#endif
@@ -613,17 +613,17 @@ static int f_absolute_path(lua_State *L) {
if (!wpath) { return 0; }
LPWSTR wfullpath = realpath(wpath, NULL);
free(wpath);
SDL_free(wpath);
if (!wfullpath) { return 0; }
char *res = utfconv_wctoutf8(wfullpath);
free(wfullpath);
SDL_free(wfullpath);
#else
char *res = realpath(path, NULL);
#endif
if (!res) { return 0; }
lua_pushstring(L, res);
free(res);
SDL_free(res);
return 1;
}
@@ -640,7 +640,7 @@ static int f_get_file_info(lua_State *L) {
return 2;
}
int err = _wstat(wpath, &s);
free(wpath);
SDL_free(wpath);
#else
struct stat s;
int err = stat(path, &s);
@@ -733,7 +733,7 @@ static int f_mkdir(lua_State *L) {
}
int err = _wmkdir(wpath);
free(wpath);
SDL_free(wpath);
#else
int err = mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
#endif
@@ -799,7 +799,7 @@ static int f_sleep(lua_State *L) {
static int f_exec(lua_State *L) {
size_t len;
const char *cmd = luaL_checklstring(L, 1, &len);
char *buf = malloc(len + 32);
char *buf = SDL_malloc(len + 32);
if (!buf) { luaL_error(L, "buffer allocation failed"); }
#if _WIN32
sprintf(buf, "cmd /c \"%s\"", cmd);
@@ -809,7 +809,7 @@ static int f_exec(lua_State *L) {
int res = system(buf);
(void) res;
#endif
free(buf);
SDL_free(buf);
return 0;
}
+2 -2
View File
@@ -24,14 +24,14 @@ static SDL_Window *window;
static void get_exe_filename(char *buf, int sz) {
#if _WIN32
int len;
wchar_t *buf_w = malloc(sizeof(wchar_t) * sz);
wchar_t *buf_w = SDL_malloc(sizeof(wchar_t) * sz);
if (buf_w) {
len = GetModuleFileNameW(NULL, buf_w, sz - 1);
buf_w[len] = L'\0';
// if the conversion failed we'll empty the string
if (!WideCharToMultiByte(CP_UTF8, 0, buf_w, -1, buf, sz, NULL, NULL))
buf[0] = '\0';
free(buf_w);
SDL_free(buf_w);
} else {
buf[0] = '\0';
}
+1 -1
View File
@@ -120,7 +120,7 @@ static bool expand_command_buffer(RenWindow *window_renderer) {
if (new_size == 0) {
new_size = CMD_BUF_INIT_SIZE;
}
uint8_t *new_command_buf = realloc(window_renderer->command_buf, new_size);
uint8_t *new_command_buf = SDL_realloc(window_renderer->command_buf, new_size);
if (!new_command_buf) {
return false;
}
+14 -14
View File
@@ -200,7 +200,7 @@ static unsigned int font_get_glyph_id(RenFont *font, unsigned int codepoint) {
if (codepoint > MAX_UNICODE) return 0;
size_t row = codepoint / CHARMAP_COL;
size_t col = codepoint - (row * CHARMAP_COL);
if (!font->charmap.rows[row]) font->charmap.rows[row] = check_alloc(calloc(sizeof(unsigned int), CHARMAP_COL));
if (!font->charmap.rows[row]) font->charmap.rows[row] = check_alloc(SDL_calloc(sizeof(unsigned int), CHARMAP_COL));
if (font->charmap.rows[row][col] == 0) {
unsigned int glyph_id = FT_Get_Char_Index(font->face, codepoint);
// use -1 as a sentinel value for "glyph not available", a bit risky, but OpenType
@@ -234,7 +234,7 @@ static SDL_Surface *font_allocate_glyph_surface(RenFont *font, FT_GlyphSlot slot
}
if (atlas_idx < 0) {
font->glyphs.atlas[glyph_format] = check_alloc(
realloc(font->glyphs.atlas[glyph_format], sizeof(GlyphAtlas) * (font->glyphs.natlas[glyph_format] + 1))
SDL_realloc(font->glyphs.atlas[glyph_format], sizeof(GlyphAtlas) * (font->glyphs.natlas[glyph_format] + 1))
);
font->glyphs.atlas[glyph_format][font->glyphs.natlas[glyph_format]] = (GlyphAtlas) {
.width = metric->x1 + FONT_WIDTH_OVERFLOW_PX, .nsurface = 0,
@@ -266,7 +266,7 @@ static SDL_Surface *font_allocate_glyph_surface(RenFont *font, FT_GlyphSlot slot
if (h <= FONT_HEIGHT_OVERFLOW_PX) h += font->size;
int depth = 0;
SDL_PixelFormat format = glyphformat_to_pixelformat(glyph_format, &depth);
atlas->surfaces = check_alloc(realloc(atlas->surfaces, sizeof(SDL_Surface *) * (atlas->nsurface + 1)));
atlas->surfaces = check_alloc(SDL_realloc(atlas->surfaces, sizeof(SDL_Surface *) * (atlas->nsurface + 1)));
atlas->surfaces[atlas->nsurface] = check_alloc(SDL_CreateSurface(atlas->width, GLYPHS_PER_ATLAS * h, format));
userdata = SDL_GetSurfaceProperties(atlas->surfaces[atlas->nsurface]);
SDL_SetPointerProperty(userdata, "metric", NULL);
@@ -298,7 +298,7 @@ static GlyphMetric *font_load_glyph_metric(RenFont *font, unsigned int glyph_id,
for (int i = 0; i < bitmaps; i++) {
// save the metrics for all subpixel indexes
if (!font->glyphs.metrics[i][row]) {
font->glyphs.metrics[i][row] = check_alloc(calloc(sizeof(GlyphMetric), GLYPHMAP_COL));
font->glyphs.metrics[i][row] = check_alloc(SDL_calloc(sizeof(GlyphMetric), GLYPHMAP_COL));
font->glyphs.bytesize += sizeof(GlyphMetric) * GLYPHMAP_COL;
}
GlyphMetric *metric = &font->glyphs.metrics[i][row][col];
@@ -394,16 +394,16 @@ static void font_clear_glyph_cache(RenFont* font) {
for (int surface_idx = 0; surface_idx < atlas->nsurface; surface_idx++) {
SDL_DestroySurface(atlas->surfaces[surface_idx]);
}
free(atlas->surfaces);
SDL_free(atlas->surfaces);
}
free(font->glyphs.atlas[glyph_format_idx]);
SDL_free(font->glyphs.atlas[glyph_format_idx]);
font->glyphs.atlas[glyph_format_idx] = NULL;
font->glyphs.natlas[glyph_format_idx] = 0;
}
// clear glyph metric
for (int subpixel_idx = 0; subpixel_idx < FONT_BITMAP_COUNT(font); subpixel_idx++) {
for (int glyphmap_row = 0; glyphmap_row < GLYPHMAP_ROW; glyphmap_row++) {
free(font->glyphs.metrics[subpixel_idx][glyphmap_row]);
SDL_free(font->glyphs.metrics[subpixel_idx][glyphmap_row]);
font->glyphs.metrics[subpixel_idx][glyphmap_row] = NULL;
}
}
@@ -426,7 +426,7 @@ static unsigned long font_file_read(FT_Stream stream, unsigned long offset, unsi
static void font_file_close(FT_Stream stream) {
if (stream && stream->descriptor.pointer)
SDL_CloseIO((SDL_IOStream *) stream->descriptor.pointer);
free(stream);
SDL_free(stream);
}
static int font_set_face_metrics(RenFont *font, FT_Face face) {
@@ -465,7 +465,7 @@ RenFont* ren_font_load(RenWindow *window_renderer, const char* path, float size,
if (!file) return NULL; // error set by SDL_IOFromFile
int len = strlen(path);
font = check_alloc(calloc(1, sizeof(RenFont) + len + 1));
font = check_alloc(SDL_calloc(1, sizeof(RenFont) + len + 1));
strcpy(font->path, path);
font->size = size;
font->antialiasing = antialiasing;
@@ -476,7 +476,7 @@ RenFont* ren_font_load(RenWindow *window_renderer, const char* path, float size,
font->scale = 1;
#endif
stream = check_alloc(calloc(1, sizeof(FT_StreamRec)));
stream = check_alloc(SDL_calloc(1, sizeof(FT_StreamRec)));
if (!stream) goto stream_failure;
stream->read = &font_file_read;
stream->close = &font_file_close;
@@ -495,7 +495,7 @@ stream_failure:
failure:
if (err != FT_Err_Ok) SDL_SetError("%s", get_ft_error(err));
if (face) FT_Done_Face(face);
if (font) free(font);
if (font) SDL_free(font);
return NULL;
}
@@ -515,10 +515,10 @@ void ren_font_free(RenFont* font) {
font_clear_glyph_cache(font);
// free codepoint cache as well
for (int i = 0; i < CHARMAP_ROW; i++) {
free(font->charmap.rows[i]);
SDL_free(font->charmap.rows[i]);
}
FT_Done_Face(font->face);
free(font);
SDL_free(font);
}
void ren_font_group_set_tab_size(RenFont **fonts, int n) {
@@ -734,7 +734,7 @@ void ren_draw_rect(RenSurface *rs, RenRect rect, RenColor color) {
void ren_free_window_resources(RenWindow *window_renderer) {
renwin_free(window_renderer);
SDL_DestroySurface(draw_rect_surface);
free(window_renderer->command_buf);
SDL_free(window_renderer->command_buf);
window_renderer->command_buf = NULL;
window_renderer->command_buf_size = 0;
}
+4 -4
View File
@@ -49,13 +49,13 @@ static UNUSED LPWSTR utfconv_utf8towc(const char *str) {
if (len == 0)
return NULL;
output = (LPWSTR) malloc(sizeof(WCHAR) * len);
output = (LPWSTR) SDL_malloc(sizeof(WCHAR) * len);
if (output == NULL)
return NULL;
len = MultiByteToWideChar(CP_UTF8, 0, str, -1, output, len);
if (len == 0) {
free(output);
SDL_free(output);
return NULL;
}
@@ -71,13 +71,13 @@ static UNUSED char *utfconv_wctoutf8(LPCWSTR str) {
if (len == 0)
return NULL;
output = (char *) malloc(sizeof(char) * len);
output = (char *) SDL_malloc(sizeof(char) * len);
if (output == NULL)
return NULL;
len = WideCharToMultiByte(CP_UTF8, 0, str, -1, output, len, NULL, NULL);
if (len == 0) {
free(output);
SDL_free(output);
return NULL;
}