summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lib.c24
-rw-r--r--src/log.c34
2 files changed, 36 insertions, 22 deletions
diff --git a/src/lib.c b/src/lib.c
index 0033463..8f67be3 100644
--- a/src/lib.c
+++ b/src/lib.c
@@ -12,13 +12,13 @@ static void *(*_al_malloc)(size_t) = malloc;
static void *(*_al_calloc)(size_t, size_t) = calloc;
static void *(*_al_realloc)(void *, size_t) = realloc;
static void (*_al_free)(void *) = free;
-#ifdef AL_HAVE_POSIX_MEMALIGN
+#if defined AL_HAVE_POSIX_MEMALIGN
s32 (*_al_posix_memalign)(void **, size_t, size_t) = posix_memalign;
#endif
// Testing allocation tracking stuff (Very scuffed, not thread-safe!).
-#define MEMORY_TRACKING 0
-#if MEMORY_TRACKING
+#define AL_MEMORY_TRACKING 0
+#if AL_MEMORY_TRACKING
struct alloc_t {
void *ptr;
size_t size;
@@ -41,14 +41,14 @@ static u32 total_allocated = 0;
void al_malloc_init(void)
{
-#if MEMORY_TRACKING
+#if AL_MEMORY_TRACKING
al_array_init(allocations);
#endif
}
void al_malloc_close(void)
{
-#if MEMORY_TRACKING
+#if AL_MEMORY_TRACKING
al_array_free(allocations);
#endif
}
@@ -56,7 +56,7 @@ void al_malloc_close(void)
void *al_malloc(size_t n)
{
void *ptr = _al_malloc(n);
-#if MEMORY_TRACKING
+#if AL_MEMORY_TRACKING
printf("**al_malloc(%zu)\n", n);
struct alloc_t a = {
.ptr = ptr,
@@ -75,7 +75,7 @@ void *al_malloc(size_t n)
void *al_calloc(size_t n, size_t size)
{
void *ptr = _al_calloc(n, size);
-#if MEMORY_TRACKING
+#if AL_MEMORY_TRACKING
printf("**al_calloc(%zu, %zu)\n", n, size);
struct alloc_t a = {
.ptr = ptr,
@@ -94,7 +94,7 @@ void *al_calloc(size_t n, size_t size)
void *al_realloc(void *ptr, size_t n)
{
void *nptr = _al_realloc(ptr, n);
-#if MEMORY_TRACKING
+#if AL_MEMORY_TRACKING
printf("**al_realloc(%p, %zu)\n", ptr, n);
for (u32 i = 0; i < allocations.size; i++) {
struct alloc_t *a = &al_array_at(allocations, i);
@@ -125,7 +125,7 @@ void *al_realloc(void *ptr, size_t n)
void al_free(void *ptr)
{
-#if MEMORY_TRACKING
+#if AL_MEMORY_TRACKING
printf("**al_free(%p)\n", ptr);
for (u32 i = 0; i < allocations.size; i++) {
struct alloc_t *a = &al_array_at(allocations, i);
@@ -139,11 +139,11 @@ void al_free(void *ptr)
_al_free(ptr);
}
-#ifdef AL_HAVE_POSIX_MEMALIGN
+#if defined AL_HAVE_POSIX_MEMALIGN
s32 al_posix_memalign(void **ptr, size_t alignment, size_t n)
{
s32 res = _al_posix_memalign(ptr, alignment, n);
-#if MEMORY_TRACKING
+#if AL_MEMORY_TRACKING
size_t actual_size = (n + alignment) - ((n + alignment) % alignment);
printf("**al_posix_memalign(%zu, %zu(%zu))\n", alignment, n, actual_size);
struct alloc_t a = {
@@ -163,7 +163,7 @@ s32 al_posix_memalign(void **ptr, size_t alignment, size_t n)
void al_malloc_stats(u32 *current, u32 *peak, u32 *total)
{
-#if MEMORY_TRACKING
+#if AL_MEMORY_TRACKING
*current = currently_allocated;
*peak = peak_allocated;
*total = total_allocated;
diff --git a/src/log.c b/src/log.c
index 4ae2184..3466320 100644
--- a/src/log.c
+++ b/src/log.c
@@ -1,15 +1,16 @@
#include "../include/al/log.h"
-#define AL_LOG_SKIP
-#define AL_LOG_MESSAGE_SIZE 511
-#define AL_LOG_USE_SECTION
-#ifdef AL_LOG_USE_SECTION
+#define AL_LOG_SKIP 0
+#define AL_LOG_USE_SECTION 1
+#if AL_LOG_USE_SECTION
#define AL_LOG_TEMPLATE "%s:%d %s(): %s -> (%s) "
#else
#define AL_LOG_TEMPLATE "%s:%d %s(): %s -> "
#endif
-#ifndef AL_LOG_SKIP
+#define AL_LOG_MESSAGE_SIZE 511
+
+#if !AL_LOG_SKIP
static s32 al_print_default(void *userdata, char *s)
{
(void)userdata;
@@ -30,7 +31,7 @@ void al_set_print(s32 (*print_func)(void *, char *), void *userdata)
static char *get_buffer(const char *level, const char *section, const char *fmt, const char *name, const s32 line, const char *func, va_list args)
{
char *buffer = al_calloc(1, AL_LOG_MESSAGE_SIZE + 1);
-#ifdef AL_LOG_USE_SECTION
+#if AL_LOG_USE_SECTION
s32 prefix = snprintf(buffer, AL_LOG_MESSAGE_SIZE, AL_LOG_TEMPLATE, name, line, func, level, section);
#else
(void)section;
@@ -47,12 +48,18 @@ void al_set_print(s32 (*print_func)(void *, char *), void *userdata) { (void)pri
s32 _al_log(const char *level, const char *section, const char *name, const s32 line, const char *func, const char *fmt, ...)
{
#if AL_FORCE_DISABLE_OUTPUT
+ (void)level;
+ (void)section;
+ (void)name;
+ (void)line;
+ (void)func;
+ (void)fmt;
return 0;
#else
va_list args;
va_start(args, fmt);
if (!section) section = "";
-#ifdef AL_LOG_SKIP
+#if AL_LOG_SKIP
s32 ret = al_printf(AL_LOG_TEMPLATE, name, line, func, level, section);
ret += al_vprintf(fmt, args);
if (strcspn(fmt, "\r\n") == al_strlen(fmt)) ret += al_printf("\n");
@@ -60,7 +67,7 @@ s32 _al_log(const char *level, const char *section, const char *name, const s32
char *buffer = get_buffer(level, section, fmt, name, line, func, args);
#endif
va_end(args);
-#ifdef AL_LOG_SKIP
+#if AL_LOG_SKIP
return ret;
#else
return _al_print(_al_log_userdata, buffer);
@@ -71,17 +78,24 @@ s32 _al_log(const char *level, const char *section, const char *name, const s32
s32 _al_logv(const char *level, const char *section, const char *name, const s32 line, const char *func, const char *fmt, va_list args)
{
#if AL_FORCE_DISABLE_OUTPUT
+ (void)level;
+ (void)section;
+ (void)name;
+ (void)line;
+ (void)func;
+ (void)fmt;
+ (void)args;
return 0;
#else
if (!section) section = "";
-#ifdef AL_LOG_SKIP
+#if AL_LOG_SKIP
s32 ret = al_printf(AL_LOG_TEMPLATE, name, line, func, level, section);
ret += al_vprintf(fmt, args);
if (strcspn(fmt, "\r\n") == al_strlen(fmt)) ret += al_printf("\n");
#else
char *buffer = get_buffer(level, section, fmt, name, line, func, args);
#endif
-#ifdef AL_LOG_SKIP
+#if AL_LOG_SKIP
return ret;
#else
return _al_print(_al_log_userdata, buffer);