Add OutOfMemory log helper
Add a special LOG_OOM() function to log all OutOfMemory errors (i.e. allocations returning NULL).
This commit is contained in:
@@ -31,7 +31,7 @@ sc_file_get_local_path(const char *name) {
|
||||
size_t len = dirlen + namelen + 2; // +2: '/' and '\0'
|
||||
char *file_path = malloc(len);
|
||||
if (!file_path) {
|
||||
LOGE("Could not alloc path");
|
||||
LOG_OOM();
|
||||
free(executable_path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ bool
|
||||
sc_intr_init(struct sc_intr *intr) {
|
||||
bool ok = sc_mutex_init(&intr->mutex);
|
||||
if (!ok) {
|
||||
LOGE("Could not init intr mutex");
|
||||
LOG_OOM();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,9 @@
|
||||
|
||||
#include "options.h"
|
||||
|
||||
#define LOG_STR_IMPL_(x) # x
|
||||
#define LOG_STR(x) LOG_STR_IMPL_(x)
|
||||
|
||||
#define LOGV(...) SDL_LogVerbose(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
||||
#define LOGD(...) SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
||||
#define LOGI(...) SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
||||
@@ -14,6 +17,9 @@
|
||||
#define LOGE(...) SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
||||
#define LOGC(...) SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
|
||||
|
||||
#define LOG_OOM() \
|
||||
LOGC("OOM: %s:%d %s()", __FILE__, __LINE__, __func__)
|
||||
|
||||
void
|
||||
sc_set_log_level(enum sc_log_level level);
|
||||
|
||||
|
||||
@@ -52,6 +52,7 @@ wrap(sc_raw_socket sock) {
|
||||
|
||||
struct sc_socket_windows *socket = malloc(sizeof(*socket));
|
||||
if (!socket) {
|
||||
LOG_OOM();
|
||||
closesocket(sock);
|
||||
return SC_SOCKET_NONE;
|
||||
}
|
||||
|
||||
@@ -5,13 +5,15 @@
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "util/strbuf.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
# include <windows.h>
|
||||
# include <tchar.h>
|
||||
#endif
|
||||
|
||||
#include "log.h"
|
||||
#include "strbuf.h"
|
||||
|
||||
size_t
|
||||
sc_strncpy(char *dest, const char *src, size_t n) {
|
||||
size_t i;
|
||||
@@ -51,6 +53,7 @@ sc_str_quote(const char *src) {
|
||||
size_t len = strlen(src);
|
||||
char *quoted = malloc(len + 3);
|
||||
if (!quoted) {
|
||||
LOG_OOM();
|
||||
return NULL;
|
||||
}
|
||||
memcpy("ed[1], src, len);
|
||||
@@ -188,6 +191,7 @@ sc_str_to_wchars(const char *utf8) {
|
||||
|
||||
wchar_t *wide = malloc(len * sizeof(wchar_t));
|
||||
if (!wide) {
|
||||
LOG_OOM();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -204,6 +208,7 @@ sc_str_from_wchars(const wchar_t *ws) {
|
||||
|
||||
char *utf8 = malloc(len);
|
||||
if (!utf8) {
|
||||
LOG_OOM();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
#include "strbuf.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include "log.h"
|
||||
|
||||
bool
|
||||
sc_strbuf_init(struct sc_strbuf *buf, size_t init_cap) {
|
||||
buf->s = malloc(init_cap + 1); // +1 for '\0'
|
||||
if (!buf->s) {
|
||||
LOG_OOM();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -25,6 +27,7 @@ sc_strbuf_reserve(struct sc_strbuf *buf, size_t len) {
|
||||
char *s = realloc(buf->s, new_cap + 1); // +1 for '\0'
|
||||
if (!s) {
|
||||
// Leave the old buf->s
|
||||
LOG_OOM();
|
||||
return false;
|
||||
}
|
||||
buf->s = s;
|
||||
|
||||
@@ -10,6 +10,7 @@ sc_thread_create(sc_thread *thread, sc_thread_fn fn, const char *name,
|
||||
void *userdata) {
|
||||
SDL_Thread *sdl_thread = SDL_CreateThread(fn, name, userdata);
|
||||
if (!sdl_thread) {
|
||||
LOG_OOM();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -26,6 +27,7 @@ bool
|
||||
sc_mutex_init(sc_mutex *mutex) {
|
||||
SDL_mutex *sdl_mutex = SDL_CreateMutex();
|
||||
if (!sdl_mutex) {
|
||||
LOG_OOM();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -94,6 +96,7 @@ bool
|
||||
sc_cond_init(sc_cond *cond) {
|
||||
SDL_cond *sdl_cond = SDL_CreateCond();
|
||||
if (!sdl_cond) {
|
||||
LOG_OOM();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user