feat(hmpl): render simple placeholder with memory leak of course
This commit is contained in:
98
package/c/chectic/chectic.c
Normal file
98
package/c/chectic/chectic.c
Normal file
@@ -0,0 +1,98 @@
|
||||
#include "chectic.h"
|
||||
|
||||
void set_output_color_mode(ColorMode mode) {
|
||||
color_mode = mode;
|
||||
}
|
||||
|
||||
// ------------
|
||||
// -- Logger --
|
||||
// ------------
|
||||
|
||||
const char* log_level_to_string(LogLevel level) {
|
||||
switch (level) {
|
||||
case LOG_LEVEL_DEBUG: return "DEBUG";
|
||||
case LOG_LEVEL_LOG: return "LOG";
|
||||
case LOG_LEVEL_INFO: return "INFO";
|
||||
case LOG_LEVEL_NOTICE: return "NOTICE";
|
||||
case LOG_LEVEL_WARN: return "WARN";
|
||||
case LOG_LEVEL_EXCEPTION: return "EXCEPTION";
|
||||
default: return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
LogLevel log_level_from_string(const char *level_str) {
|
||||
if (!level_str) return LOG_LEVEL_INFO;
|
||||
if (strcmp(level_str, "DEBUG") == 0)
|
||||
return LOG_LEVEL_DEBUG;
|
||||
else if (strcmp(level_str, "LOG") == 0)
|
||||
return LOG_LEVEL_LOG;
|
||||
else if (strcmp(level_str, "INFO") == 0)
|
||||
return LOG_LEVEL_INFO;
|
||||
else if (strcmp(level_str, "NOTICE") == 0)
|
||||
return LOG_LEVEL_NOTICE;
|
||||
else if (strcmp(level_str, "WARN") == 0)
|
||||
return LOG_LEVEL_WARN;
|
||||
else if (strcmp(level_str, "EXCEPTION") == 0)
|
||||
return LOG_LEVEL_EXCEPTION;
|
||||
else
|
||||
return LOG_LEVEL_INFO;
|
||||
}
|
||||
|
||||
LogLevel current_log_level = LOG_LEVEL_INFO;
|
||||
|
||||
void logger_level_reset() {
|
||||
current_log_level = LOG_LEVEL_INFO;
|
||||
}
|
||||
|
||||
void logger_level(LogLevel level) {
|
||||
current_log_level = level;
|
||||
}
|
||||
|
||||
void init_logger(void) {
|
||||
current_log_level = log_level_from_string(getenv("LOG_LEVEL"));
|
||||
}
|
||||
|
||||
char* log_message(LogLevel level, char *file, int line, const char *format, ...) {
|
||||
if (level < current_log_level) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
time_t now = time(NULL);
|
||||
struct tm tm_info;
|
||||
localtime_r(&now, &tm_info);
|
||||
static char timeStr[20];
|
||||
strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", &tm_info);
|
||||
|
||||
fprintf(stderr, "%s %s %s:%d ", timeStr, log_level_to_string(level), file, line);
|
||||
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vfprintf(stderr, format, args);
|
||||
va_end(args);
|
||||
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
return timeStr;
|
||||
}
|
||||
|
||||
// -----------
|
||||
// -- arena --
|
||||
// -----------
|
||||
|
||||
Arena arena_init(size_t size) {
|
||||
Arena arena;
|
||||
arena.begin = malloc(size);
|
||||
memset(arena.begin, 0, size);
|
||||
arena.current = arena.begin;
|
||||
arena.capacity = size;
|
||||
|
||||
return arena;
|
||||
}
|
||||
|
||||
void arena_reset(Arena *arena) {
|
||||
arena->current = arena->begin;
|
||||
}
|
||||
|
||||
void arena_free(Arena *arena) {
|
||||
free(arena->begin);
|
||||
}
|
||||
147
package/c/chectic/chectic.h
Normal file
147
package/c/chectic/chectic.h
Normal file
@@ -0,0 +1,147 @@
|
||||
#ifndef EPRINTF_H
|
||||
#define EPRINTF_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
|
||||
// -------------
|
||||
// -- Helpers --
|
||||
// -------------
|
||||
|
||||
// Helper macros for argument counting
|
||||
// NOTE(yukkop): this ugly macroses for avoid all posible warnings
|
||||
#define PP_CAT(a, b) a##b
|
||||
|
||||
// ------------
|
||||
// -- Colors --
|
||||
// ------------
|
||||
|
||||
// Color mode enumeration
|
||||
typedef enum {
|
||||
COLOR_MODE_AUTO,
|
||||
COLOR_MODE_FORCE,
|
||||
COLOR_MODE_DISABLE
|
||||
} ColorMode;
|
||||
|
||||
// Static color mode variable
|
||||
static ColorMode color_mode = COLOR_MODE_AUTO;
|
||||
|
||||
// Function to set color mode
|
||||
void set_output_color_mode(ColorMode mode);
|
||||
|
||||
// Macros for detecting terminal and color usage
|
||||
#define IS_TERMINAL() (isatty(fileno(stderr)))
|
||||
#define USE_COLOR() ((color_mode == COLOR_MODE_FORCE) || (color_mode == COLOR_MODE_AUTO && IS_TERMINAL()))
|
||||
|
||||
#define COLOR_RED (USE_COLOR() ? "\033[1;31m" : "")
|
||||
#define COLOR_RESET (USE_COLOR() ? "\033[0m" : "")
|
||||
|
||||
// ------------
|
||||
// -- Errors --
|
||||
// ------------
|
||||
|
||||
// Define color macros based on output type
|
||||
//#define ERROR_PREFIX PP_CAT(COLOR_RED, "Error: ")
|
||||
//#define ERROR_SUFFIX PP_CAT(COLOR_RESET, "\n")
|
||||
#define ERROR_PREFIX (USE_COLOR() ? "\033[1;31mError: " : "Error: ")
|
||||
#define ERROR_SUFFIX (USE_COLOR() ? "\033[0m\n" : "\n")
|
||||
|
||||
// eprintf handling 1 or more arguments
|
||||
#define eprintf(fmt, ...) "%s" fmt "%s", ERROR_PREFIX, ##__VA_ARGS__, ERROR_SUFFIX
|
||||
|
||||
#define todo fprintf(stderr, "%sNot implimented yet%s", COLOR_RED, COLOR_RESET);exit(1)
|
||||
|
||||
// ------------
|
||||
// -- Logger --
|
||||
// ------------
|
||||
|
||||
typedef enum {
|
||||
LOG_LEVEL_DEBUG,
|
||||
LOG_LEVEL_LOG,
|
||||
LOG_LEVEL_INFO,
|
||||
LOG_LEVEL_NOTICE,
|
||||
LOG_LEVEL_WARN,
|
||||
LOG_LEVEL_EXCEPTION
|
||||
} LogLevel;
|
||||
|
||||
void logger_level_reset();
|
||||
|
||||
void logger_level(LogLevel level);
|
||||
|
||||
LogLevel log_level_from_string(const char *level_str);
|
||||
|
||||
char* log_message(LogLevel level, char *file, int line, const char *format, ...);
|
||||
|
||||
#define raise_debug(fmt, ...) log_message(LOG_LEVEL_DEBUG, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
|
||||
#define raise_log(fmt, ...) log_message(LOG_LEVEL_LOG, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
|
||||
#define raise_info(fmt, ...) log_message(LOG_LEVEL_INFO, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
|
||||
#define raise_notice(fmt, ...) log_message(LOG_LEVEL_NOTICE, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
|
||||
#define raise_warn(fmt, ...) log_message(LOG_LEVEL_WARN, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
|
||||
#define raise_exception(fmt, ...) log_message(LOG_LEVEL_EXCEPTION, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
|
||||
|
||||
#endif // EPRINTF_H
|
||||
|
||||
// -----------
|
||||
// -- arena --
|
||||
// -----------
|
||||
|
||||
#define ARENA_DEFAULT_SIZE 1024
|
||||
|
||||
typedef struct {
|
||||
void *begin;
|
||||
void *current;
|
||||
size_t capacity;
|
||||
} Arena;
|
||||
|
||||
// NOTE(yukkop): This macro is used to define procedures so that `__LINE__` and `__FILE__`
|
||||
// in `raise_debug` reflect the location where the macro is called, not where it's defined.
|
||||
#define arena_alloc_or_null(arena, size) __extension__ ({ \
|
||||
void *mem__ = NULL; \
|
||||
if ((arena)->begin == 0) { \
|
||||
*(arena) = arena_init(ARENA_DEFAULT_SIZE); \
|
||||
} \
|
||||
size_t current__ = (size_t)(arena)->current - (size_t)(arena)->begin; \
|
||||
if ((arena)->capacity <= current__ || (arena)->capacity - current__ < (size)) {\
|
||||
raise_debug("Arena from %d with capacity %d allocated on %d cannot be allocated on %d", \
|
||||
(arena)->begin, (arena)->capacity, \
|
||||
(arena)->current - (arena)->begin, (size)); \
|
||||
} else { \
|
||||
raise_debug("Arena from %d with capacity %d allocated on %d will allocate on %d", \
|
||||
(arena)->begin, (arena)->capacity, \
|
||||
(arena)->begin, (arena)->capacity, (size)); \
|
||||
mem__ = (arena)->current; \
|
||||
(arena)->current = (char*)(arena)->current + (size); \
|
||||
} \
|
||||
mem__; \
|
||||
})
|
||||
|
||||
Arena arena_init(size_t size);
|
||||
|
||||
void arena_reset(Arena *arena);
|
||||
|
||||
void arena_free(Arena *arena);
|
||||
|
||||
#define arena_alloc(arena, size) __extension__ ({ \
|
||||
void *mem__ = arena_alloc_or_null((arena), (size)); \
|
||||
if (!mem__) { \
|
||||
raise_exception("Arena out of memory"); \
|
||||
exit(1); \
|
||||
} \
|
||||
mem__; \
|
||||
})
|
||||
|
||||
|
||||
// TODO: mmap
|
||||
// TODO: dynamic array style
|
||||
// void *arena_realloc(Arena *arena, size_t size) {
|
||||
// void *mem = arena_alloc_or_null(arena, size);
|
||||
// if (!mem) {
|
||||
// raise_exception("Arena out of memory");
|
||||
// exit(1);
|
||||
// }
|
||||
// return mem;
|
||||
// }
|
||||
37
package/c/chectic/default.nix
Normal file
37
package/c/chectic/default.nix
Normal file
@@ -0,0 +1,37 @@
|
||||
{ stdenv, gcc, lib }:
|
||||
|
||||
stdenv.mkDerivation {
|
||||
pname = "chectic";
|
||||
version = "1.0";
|
||||
src = ./.;
|
||||
doCheck = true;
|
||||
|
||||
buildPhase = ''
|
||||
mkdir -p target
|
||||
${gcc}/bin/cc -Wall -Wextra -g \
|
||||
-std=c99 \
|
||||
-pedantic -fsanitize=address \
|
||||
-c chectic.c -o target/chectic.o
|
||||
${gcc}/bin/ar rcs target/libchectic.a target/chectic.o
|
||||
'';
|
||||
|
||||
checkPhase = ''
|
||||
mkdir -p target/test
|
||||
for test_file in test/*.c; do
|
||||
exe="target/test/$(basename ''${test_file%.c})"
|
||||
${gcc}/bin/cc -Wall -Wextra -g -pedantic -fsanitize=address -I. "$test_file" -Ltarget -lchectic -o "$exe"
|
||||
"$exe"
|
||||
done
|
||||
'';
|
||||
|
||||
installPhase = ''
|
||||
mkdir -p $out/lib $out/include
|
||||
cp target/libchectic.a $out/lib/
|
||||
cp chectic.h $out/include/
|
||||
'';
|
||||
|
||||
meta = {
|
||||
description = "libhectic";
|
||||
license = lib.licenses.mit;
|
||||
};
|
||||
}
|
||||
60
package/c/chectic/test/arena.c
Normal file
60
package/c/chectic/test/arena.c
Normal file
@@ -0,0 +1,60 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include "chectic.h"
|
||||
|
||||
void test_arena_init() {
|
||||
Arena arena = arena_init(128);
|
||||
assert(arena.begin != NULL);
|
||||
assert(arena.current == arena.begin);
|
||||
assert(arena.capacity == 128);
|
||||
arena_free(&arena);
|
||||
}
|
||||
|
||||
void test_arena_alloc() {
|
||||
Arena arena = arena_init(64);
|
||||
void *ptr1 = arena_alloc(&arena, 16);
|
||||
assert(ptr1 != NULL);
|
||||
void *ptr2 = arena_alloc(&arena, 16);
|
||||
assert(ptr2 != NULL);
|
||||
raise_debug("%d - %d = %d", (size_t)ptr2, (size_t)ptr1, (char *)ptr2 - (char *)ptr1);
|
||||
assert((char *)ptr2 - (char *)ptr1 == 16);
|
||||
arena_free(&arena);
|
||||
}
|
||||
|
||||
void test_arena_alloc_or_null_out_of_memory() {
|
||||
Arena arena = arena_init(32);
|
||||
void *ptr = arena_alloc_or_null(&arena, 64);
|
||||
raise_debug("%d %d %d %d", arena.begin, arena.current, arena.capacity, (size_t)ptr);
|
||||
assert(ptr == NULL);
|
||||
arena_free(&arena);
|
||||
}
|
||||
|
||||
void test_arena_reset() {
|
||||
Arena arena = arena_init(64);
|
||||
void *ptr1 = arena_alloc(&arena, 16);
|
||||
arena_reset(&arena);
|
||||
void *ptr2 = arena_alloc(&arena, 16);
|
||||
assert(ptr1 == ptr2); // same address after reset
|
||||
arena_free(&arena);
|
||||
}
|
||||
|
||||
void test_arena_null_init() {
|
||||
Arena arena = {0};
|
||||
void *ptr = arena_alloc_or_null(&arena, 32);
|
||||
assert(ptr != NULL);
|
||||
arena_free(&arena);
|
||||
}
|
||||
|
||||
int main() {
|
||||
set_output_color_mode(COLOR_MODE_DISABLE);
|
||||
logger_level(LOG_LEVEL_DEBUG); \
|
||||
|
||||
test_arena_init();
|
||||
test_arena_alloc();
|
||||
test_arena_alloc_or_null_out_of_memory();
|
||||
test_arena_reset();
|
||||
test_arena_null_init();
|
||||
printf("All tests passed.\n");
|
||||
}
|
||||
39
package/c/chectic/test/test.c
Normal file
39
package/c/chectic/test/test.c
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include "chectic.h"
|
||||
|
||||
#define TEST_RAISE_GENERIC(LOG_MACRO, LEVEL, LEVEL_STR) do { \
|
||||
FILE *orig_stderr = stderr; \
|
||||
FILE *temp = tmpfile(); \
|
||||
char result_buffer[256]; \
|
||||
if (!temp) { perror("tmpfile"); exit(EXIT_FAILURE); } \
|
||||
stderr = temp; \
|
||||
logger_level(LEVEL); \
|
||||
const char* time_str = LOG_MACRO("message"); \
|
||||
logger_level_reset(); \
|
||||
fflush(stderr); \
|
||||
fseek(temp, 0, SEEK_SET); \
|
||||
size_t nread = fread(result_buffer, 1, sizeof(result_buffer)-1, temp); \
|
||||
result_buffer[nread] = '\0'; \
|
||||
stderr = orig_stderr; \
|
||||
fclose(temp); \
|
||||
char expected_buffer[256]; \
|
||||
sprintf(expected_buffer, "%s " LEVEL_STR " " __FILE__ ":%d message\n", time_str, __LINE__); \
|
||||
printf("DEBUG: [%s] [%s]\n", result_buffer, expected_buffer); \
|
||||
assert(strcmp(result_buffer, expected_buffer) == 0); \
|
||||
} while(0)
|
||||
|
||||
int main(void) {
|
||||
set_output_color_mode(COLOR_MODE_DISABLE);
|
||||
|
||||
TEST_RAISE_GENERIC(raise_debug, LOG_LEVEL_DEBUG, "DEBUG");
|
||||
TEST_RAISE_GENERIC(raise_log, LOG_LEVEL_LOG, "LOG");
|
||||
TEST_RAISE_GENERIC(raise_info, LOG_LEVEL_INFO, "INFO");
|
||||
TEST_RAISE_GENERIC(raise_notice, LOG_LEVEL_NOTICE, "NOTICE");
|
||||
TEST_RAISE_GENERIC(raise_warn, LOG_LEVEL_WARN, "WARN");
|
||||
TEST_RAISE_GENERIC(raise_exception, LOG_LEVEL_EXCEPTION, "EXCEPTION");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user