Implement DQN_HEADER_COPY_BEGIN/END ranged copy

This commit is contained in:
doyle 2019-08-26 23:56:50 +10:00
parent 0d0009ad5c
commit c36ea18a64
2 changed files with 55 additions and 26 deletions

View File

@ -307,6 +307,11 @@ enum struct Dqn_LogType
Memory, Memory,
}; };
// @ -------------------------------------------------------------------------------------------------
// @
// @ NOTE: Logging
// @
// @ -------------------------------------------------------------------------------------------------
DQN_HEADER_COPY_PROTOTYPE(constexpr inline char const *, Dqn_LogTypeTag(Dqn_LogType type)) DQN_HEADER_COPY_PROTOTYPE(constexpr inline char const *, Dqn_LogTypeTag(Dqn_LogType type))
{ {
if (type == Dqn_LogType::Debug) return " DBG"; if (type == Dqn_LogType::Debug) return " DBG";
@ -317,6 +322,7 @@ DQN_HEADER_COPY_PROTOTYPE(constexpr inline char const *, Dqn_LogTypeTag(Dqn_LogT
return " XXX"; return " XXX";
} }
DQN_HEADER_COPY_BEGIN
// NOTE: Set the callback to get called whenever a log message has been printed // NOTE: Set the callback to get called whenever a log message has been printed
#define DQN_LOG_CALLBACK(name) void name(Dqn_LogType type, char const *file, usize file_len, char const *func, usize func_len, usize line, char const *log_str) #define DQN_LOG_CALLBACK(name) void name(Dqn_LogType type, char const *file, usize file_len, char const *func, usize func_len, usize line, char const *log_str)
typedef DQN_LOG_CALLBACK(Dqn_LogCallback); typedef DQN_LOG_CALLBACK(Dqn_LogCallback);
@ -328,11 +334,13 @@ Dqn_LogCallback *Dqn_log_callback;
#define DQN_LOG_I(fmt, ...) Dqn_Log(Dqn_LogType::Info, DQN_STR_AND_LEN(__FILE__), DQN_STR_AND_LEN(__func__), __LINE__, fmt, ## __VA_ARGS__) #define DQN_LOG_I(fmt, ...) Dqn_Log(Dqn_LogType::Info, DQN_STR_AND_LEN(__FILE__), DQN_STR_AND_LEN(__func__), __LINE__, fmt, ## __VA_ARGS__)
#define DQN_LOG_M(fmt, ...) Dqn_Log(Dqn_LogType::Info, DQN_STR_AND_LEN(__FILE__), DQN_STR_AND_LEN(__func__), __LINE__, fmt, ## __VA_ARGS__) #define DQN_LOG_M(fmt, ...) Dqn_Log(Dqn_LogType::Info, DQN_STR_AND_LEN(__FILE__), DQN_STR_AND_LEN(__func__), __LINE__, fmt, ## __VA_ARGS__)
#define DQN_LOG(log_type, fmt, ...) Dqn_Log(log_type, DQN_STR_AND_LEN(__FILE__), DQN_STR_AND_LEN(__func__), __LINE__, fmt, ## __VA_ARGS__) #define DQN_LOG(log_type, fmt, ...) Dqn_Log(log_type, DQN_STR_AND_LEN(__FILE__), DQN_STR_AND_LEN(__func__), __LINE__, fmt, ## __VA_ARGS__)
DQN_HEADER_COPY_END
// @ ------------------------------------------------------------------------------------------------- // @ -------------------------------------------------------------------------------------------------
// @ // @
// @ NOTE: Math // @ NOTE: Math
// @ // @
// @ ------------------------------------------------------------------------------------------------- // @ -------------------------------------------------------------------------------------------------
DQN_HEADER_COPY_BEGIN
union Dqn_V2I union Dqn_V2I
{ {
struct { i32 x, y; }; struct { i32 x, y; };
@ -496,6 +504,7 @@ union Dqn_Mat4
f32 row_major[4][4]; f32 row_major[4][4];
f32 operator[](usize i) const { return e[i]; } f32 operator[](usize i) const { return e[i]; }
}; };
DQN_HEADER_COPY_END
template <typename T> template <typename T>
DQN_HEADER_COPY_PROTOTYPE(int, Dqn_MemCmpType(T const *ptr1, T const *ptr2)) DQN_HEADER_COPY_PROTOTYPE(int, Dqn_MemCmpType(T const *ptr1, T const *ptr2))
@ -562,6 +571,7 @@ struct Dqn_MemArenaScopedRegion
MemBlock *top_mem_block; MemBlock *top_mem_block;
}; };
DQN_HEADER_COPY_BEGIN
#define DQN_DEBUG_MEM_ARENA_LOGGING #define DQN_DEBUG_MEM_ARENA_LOGGING
#if defined(DQN_DEBUG_MEM_ARENA_LOGGING) #if defined(DQN_DEBUG_MEM_ARENA_LOGGING)
#define DQN_DEBUG_ARGS , char const *file, isize file_len, char const *func, isize func_len, isize line #define DQN_DEBUG_ARGS , char const *file, isize file_len, char const *func, isize func_len, isize line
@ -578,6 +588,7 @@ struct Dqn_MemArenaScopedRegion
#define MEM_ARENA_RESERVE_FROM(arena, src, size) Dqn_MemArena_ReserveFrom(arena, src, size DQN_DEBUG_PARAMS); #define MEM_ARENA_RESERVE_FROM(arena, src, size) Dqn_MemArena_ReserveFrom(arena, src, size DQN_DEBUG_PARAMS);
#define MEM_ARENA_CLEAR_USED(arena) Dqn_MemArena_ClearUsed(arena DQN_DEBUG_PARAMS); #define MEM_ARENA_CLEAR_USED(arena) Dqn_MemArena_ClearUsed(arena DQN_DEBUG_PARAMS);
#define MEM_ARENA_FREE(arena) Dqn_MemArena_Free #define MEM_ARENA_FREE(arena) Dqn_MemArena_Free
DQN_HEADER_COPY_END
// ------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------
// //
@ -789,9 +800,7 @@ struct Slice
}; };
#define SLICE_LITERAL(string) Slice<char const>(DQN_STR_AND_LEN(string)) #define SLICE_LITERAL(string) Slice<char const>(DQN_STR_AND_LEN(string))
DQN_HEADER_COPY_PROTOTYPE( DQN_HEADER_COPY_PROTOTYPE(template <typename T>, inline Slice<T> Slice_CopyNullTerminated(Dqn_MemArena *arena, T const *src, isize len))
template <typename T>,
inline Slice<T> Slice_CopyNullTerminated(Dqn_MemArena *arena, T const *src, isize len))
{ {
Slice<T> result = {}; Slice<T> result = {};
result.len = len; result.len = len;
@ -801,17 +810,13 @@ inline Slice<T> Slice_CopyNullTerminated(Dqn_MemArena *arena, T const *src, isiz
return result; return result;
} }
DQN_HEADER_COPY_PROTOTYPE( DQN_HEADER_COPY_PROTOTYPE(template <typename T>, inline Slice<T> Slice_CopyNullTerminated(Dqn_MemArena *arena, Slice<T> const src))
template <typename T>,
inline Slice<T> Slice_CopyNullTerminated(Dqn_MemArena *arena, Slice<T> const src))
{ {
Slice<T> result = Slice_CopyNullTerminated(arena, src.buf, src.len); Slice<T> result = Slice_CopyNullTerminated(arena, src.buf, src.len);
return result; return result;
} }
DQN_HEADER_COPY_PROTOTYPE( DQN_HEADER_COPY_PROTOTYPE(template <typename T>, inline Slice<T> Slice_Copy(Dqn_MemArena *arena, T const *src, isize len))
template <typename T>,
inline Slice<T> Slice_Copy(Dqn_MemArena *arena, T const *src, isize len))
{ {
Slice<T> result = {}; Slice<T> result = {};
result.len = len; result.len = len;
@ -820,9 +825,7 @@ inline Slice<T> Slice_Copy(Dqn_MemArena *arena, T const *src, isize len))
return result; return result;
} }
DQN_HEADER_COPY_PROTOTYPE( DQN_HEADER_COPY_PROTOTYPE(template <typename T>, inline Slice<T> Slice_Copy(Dqn_MemArena *arena, Slice<T> const src))
template <typename T>,
inline Slice<T> Slice_Copy(Dqn_MemArena *arena, Slice<T> const src))
{ {
Slice<T> result = Slice_Copy(arena, src.buf, src.len); Slice<T> result = Slice_Copy(arena, src.buf, src.len);
return result; return result;
@ -833,9 +836,7 @@ inline Slice<T> Slice_Copy(Dqn_MemArena *arena, Slice<T> const src))
// @ NOTE: Asprintf (Allocate Sprintf) // @ NOTE: Asprintf (Allocate Sprintf)
// @ // @
// @ ------------------------------------------------------------------------------------------------- // @ -------------------------------------------------------------------------------------------------
DQN_HEADER_COPY_PROTOTYPE( DQN_HEADER_COPY_PROTOTYPE(template <typename T> Slice<char>, AsprintfSlice(T *arena, char const *fmt, va_list va))
template <typename T> Slice<char>,
AsprintfSlice(T *arena, char const *fmt, va_list va))
{ {
Slice<char> result = {}; Slice<char> result = {};
result.len = stbsp_vsnprintf(nullptr, 0, fmt, va) + 1; result.len = stbsp_vsnprintf(nullptr, 0, fmt, va) + 1;
@ -845,9 +846,7 @@ AsprintfSlice(T *arena, char const *fmt, va_list va))
return result; return result;
} }
DQN_HEADER_COPY_PROTOTYPE( DQN_HEADER_COPY_PROTOTYPE(template <typename T> Slice<char>, AsprintfSlice(T *arena, char const *fmt, ...))
template <typename T> Slice<char>,
AsprintfSlice(T *arena, char const *fmt, ...))
{ {
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
@ -856,9 +855,7 @@ AsprintfSlice(T *arena, char const *fmt, ...))
return result; return result;
} }
DQN_HEADER_COPY_PROTOTYPE( DQN_HEADER_COPY_PROTOTYPE(template <typename T> char *, Asprintf(T *arena, int *len, char const *fmt, ...))
template <typename T> char *,
Asprintf(T *arena, int *len, char const *fmt, ...))
{ {
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
@ -868,9 +865,7 @@ Asprintf(T *arena, int *len, char const *fmt, ...))
return result.str; return result.str;
} }
DQN_HEADER_COPY_PROTOTYPE( DQN_HEADER_COPY_PROTOTYPE(template <typename T> char *, Asprintf(T *arena, char const *fmt, ...))
template <typename T> char *,
Asprintf(T *arena, char const *fmt, ...))
{ {
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
@ -1398,6 +1393,8 @@ Dqn_MemArenaScopedRegion::~Dqn_MemArenaScopedRegion()
while (this->top_mem_block != this->arena->top_mem_block) while (this->top_mem_block != this->arena->top_mem_block)
{ {
MemBlock *block_to_free = this->arena->top_mem_block; MemBlock *block_to_free = this->arena->top_mem_block;
if (this->arena->curr_mem_block == block_to_free)
this->arena->curr_mem_block = block_to_free->prev;
this->arena->top_mem_block = block_to_free->prev; this->arena->top_mem_block = block_to_free->prev;
Dqn_MemArena__FreeBlock(this->arena, block_to_free); Dqn_MemArena__FreeBlock(this->arena, block_to_free);
} }

View File

@ -1,6 +1,8 @@
#ifndef DQN_HEADER_H #ifndef DQN_HEADER_H
#define DQN_HEADER_H #define DQN_HEADER_H
#define DQN_HEADER_COPY_PROTOTYPE(func_return, func_name_and_types) func_return func_name_and_types #define DQN_HEADER_COPY_PROTOTYPE(func_return, func_name_and_types) func_return func_name_and_types
#define DQN_HEADER_COPY_BEGIN
#define DQN_HEADER_COPY_END
#endif /* DQN_HEADER_H */ #endif /* DQN_HEADER_H */
#ifdef DQN_HEADER_IMPLEMENTATION #ifdef DQN_HEADER_IMPLEMENTATION
@ -100,18 +102,22 @@ int main(int argc, char *argv[])
char constexpr HEADER_COPY_PROTOTYPE[] = "DQN_HEADER_COPY_PROTOTYPE"; char constexpr HEADER_COPY_PROTOTYPE[] = "DQN_HEADER_COPY_PROTOTYPE";
char constexpr HEADER_COMMENT[] = "// @"; char constexpr HEADER_COMMENT[] = "// @";
char constexpr HEADER_COPY_BEGIN[] = "DQN_HEADER_COPY_BEGIN";
char constexpr HEADER_COPY_END[] = "DQN_HEADER_COPY_END";
enum struct FindString enum struct FindString
{ {
HeaderPrototype, HeaderPrototype,
HeaderComment, HeaderComment,
HeaderCopyBegin,
Count Count
}; };
char const *find_list[] = {HEADER_COPY_PROTOTYPE, HEADER_COMMENT}; char const *find_list[] = {HEADER_COPY_PROTOTYPE, HEADER_COMMENT, HEADER_COPY_BEGIN};
isize constexpr find_string_lens[] = { isize constexpr find_string_lens[] = {
Dqn_CharCountI(HEADER_COPY_PROTOTYPE), Dqn_CharCountI(HEADER_COPY_PROTOTYPE),
Dqn_CharCountI(HEADER_COMMENT), Dqn_CharCountI(HEADER_COMMENT),
Dqn_CharCountI(HEADER_COPY_BEGIN),
}; };
char const *ptr = buf; char const *ptr = buf;
@ -147,6 +153,32 @@ int main(int argc, char *argv[])
fprintf(stdout, "%.*s\n", (int)comment_len, comment_start); fprintf(stdout, "%.*s\n", (int)comment_len, comment_start);
ptr = comment_start + comment_len; ptr = comment_start + comment_len;
} }
else if (matched_find_index == (isize)FindString::HeaderCopyBegin)
{
ptr = token + find_string_lens[matched_find_index];
ptr = Dqn_StrSkipWhitespace(ptr);
char const *copy_start = ptr;
char const *copy_end = Dqn_StrFind(ptr, HEADER_COPY_END, ptr_len, Dqn_CharCountI(HEADER_COPY_END));
if (!copy_end)
{
fprintf(stderr, "Header copy begin macro: %s not matched with a copy end macro: %s", HEADER_COPY_BEGIN, HEADER_COPY_END);
return -1;
}
isize copy_len = copy_end - copy_start;
auto mem_scope = Dqn_MemArenaScopedRegion(&arena);
char *sanitised_copy = (char *)MEM_ARENA_ALLOC(&arena, copy_len);
isize sanitised_len = 0;
DQN_FOR_EACH(copy_index, copy_len)
{
char ch = copy_start[copy_index];
if (ch == '\r') continue;
sanitised_copy[sanitised_len++] = ch;
}
ptr = copy_end;
fprintf(stdout, "%.*s\n", (int)sanitised_len, sanitised_copy);
}
ptr_len = ptr_end - ptr; ptr_len = ptr_end - ptr;
} }