Derive-C
Loading...
Searching...
No Matches
dump.h File Reference
#include <stdio.h>
#include <string.h>
#include <derive-c/core/attributes.h>
#include <derive-c/core/namespace.h>
#include <derive-c/core/debug/fmt.h>

Go to the source code of this file.

Macros

#define _DC_DEBUG_MAX_CAPACITY   4096
#define DC_DEBUG(DEBUG_FN, DEBUG_PTR)

Functions

static DC_INTERNAL FILE * _dc_debug_dump_start (void)
static DC_INTERNAL char const * _dc_debug_dump_end (FILE *mem_stream)
static DC_PUBLIC void dc_debug_dump_reset (void)

Variables

static const char * _dc_debug_trailing_space = "..."
 A no-alloc, debug dump utility for use in asserts.
static DC_THREAD_LOCAL char _dc_debug_dump_buf [_DC_DEBUG_MAX_CAPACITY+4] = {}
static DC_THREAD_LOCAL size_t _dc_debug_dump_head = 0
static DC_THREAD_LOCAL size_t _dc_debug_dump_start_pos = 0

Macro Definition Documentation

◆ _DC_DEBUG_MAX_CAPACITY

#define _DC_DEBUG_MAX_CAPACITY   4096

Definition at line 15 of file dump.h.

◆ DC_DEBUG

#define DC_DEBUG ( DEBUG_FN,
DEBUG_PTR )
Value:
({ \
char const* result; \
if (stream == NULL) { \
} else { \
DEBUG_FN(DEBUG_PTR, dc_debug_fmt_new(), stream); \
} \
result; \
})
static DC_INTERNAL char const * _dc_debug_dump_end(FILE *mem_stream)
Definition dump.h:39
static DC_INTERNAL FILE * _dc_debug_dump_start(void)
Definition dump.h:20
static const char * _dc_debug_trailing_space
A no-alloc, debug dump utility for use in asserts.
Definition dump.h:14
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_new()
Definition fmt.h:15
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108

Definition at line 92 of file dump.h.

92#define DC_DEBUG(DEBUG_FN, DEBUG_PTR) \
93 ({ \
94 FILE* stream = _dc_debug_dump_start(); \
95 char const* result; \
96 if (stream == NULL) { \
97 result = _dc_debug_trailing_space; \
98 } else { \
99 DEBUG_FN(DEBUG_PTR, dc_debug_fmt_new(), stream); \
100 result = _dc_debug_dump_end(stream); \
101 } \
102 result; \
103 })

Function Documentation

◆ _dc_debug_dump_end()

DC_INTERNAL char const * _dc_debug_dump_end ( FILE * mem_stream)
static

Definition at line 39 of file dump.h.

39 {
40 if (mem_stream == NULL) {
42 }
43
44 fflush(mem_stream);
45 long used_long = ftell(mem_stream);
46 fclose(mem_stream);
47
48 if (used_long < 0) {
50 }
51
52 size_t used = (size_t)used_long;
53
54 size_t start = _dc_debug_dump_start_pos;
55 size_t available = _DC_DEBUG_MAX_CAPACITY - start;
56 size_t trailing_len = strlen(_dc_debug_trailing_space);
57
58 if (used > available) {
59 size_t content_size = available - trailing_len;
60
61 for (size_t i = 0; i < trailing_len; i++) {
62 _dc_debug_dump_buf[start + content_size + i] = _dc_debug_trailing_space[i];
63 }
64 _dc_debug_dump_buf[start + content_size + trailing_len] = '\0';
65
66 _dc_debug_dump_head = start + content_size + trailing_len + 1;
67 return &_dc_debug_dump_buf[start];
68 }
69 _dc_debug_dump_buf[start + used] = '\0';
70 _dc_debug_dump_head = start + used + 1;
71 return &_dc_debug_dump_buf[start];
72}
#define _DC_DEBUG_MAX_CAPACITY
Definition dump.h:15
static DC_THREAD_LOCAL size_t _dc_debug_dump_head
Definition dump.h:17
static DC_THREAD_LOCAL char _dc_debug_dump_buf[_DC_DEBUG_MAX_CAPACITY+4]
Definition dump.h:16
static DC_THREAD_LOCAL size_t _dc_debug_dump_start_pos
Definition dump.h:18

◆ _dc_debug_dump_start()

DC_INTERNAL FILE * _dc_debug_dump_start ( void )
static

Definition at line 20 of file dump.h.

20 {
21 // JUSTIFY: Single static buffer
22 // - Thread local required for thread safety
23 // - The returned strings must outlive the function call (e.g. to be used in a printf)
24 // - Multiple prints are possible.
25 // A single thread local buffer suits this task well.
26
28 return NULL;
29 }
30
33 size_t writable = available + 1;
34
35 FILE* mem_stream = fmemopen(_dc_debug_dump_buf + _dc_debug_dump_start_pos, writable, "w");
36 return mem_stream;
37}

◆ dc_debug_dump_reset()

DC_PUBLIC void dc_debug_dump_reset ( void )
static

Definition at line 77 of file dump.h.

77 {
80
81 // JUSTIFY: For easier debugging in coredumps from unit tests, clear the buffer.
82 memset(_dc_debug_dump_buf, 0, sizeof(_dc_debug_dump_buf));
83}

Variable Documentation

◆ _dc_debug_dump_buf

DC_THREAD_LOCAL char _dc_debug_dump_buf[_DC_DEBUG_MAX_CAPACITY+4] = {}
static

Definition at line 16 of file dump.h.

16{};

◆ _dc_debug_dump_head

DC_THREAD_LOCAL size_t _dc_debug_dump_head = 0
static

Definition at line 17 of file dump.h.

◆ _dc_debug_dump_start_pos

DC_THREAD_LOCAL size_t _dc_debug_dump_start_pos = 0
static

Definition at line 18 of file dump.h.

◆ _dc_debug_trailing_space

const char* _dc_debug_trailing_space = "..."
static

A no-alloc, debug dump utility for use in asserts.

  • Writes debug formatted data to a static buffer, so stable & static-lifetime references can be used with string formatting.
  • No heap allocation, so no leaking on format.

Definition at line 14 of file dump.h.