Skip to content

Commit 9260c5f

Browse files
committed
Allow mpack_stdio to be true, without adding malloc
stdio, stdlib and debug is used for print functions, and only available on systems where list dynamic was available. This also had the effect of bringing in malloc and some nasty long assert functions with 512 bytes on the stack. The asserts have been gutted from the implementation, and the malloc manually removed from the files.
1 parent 1576fad commit 9260c5f

4 files changed

Lines changed: 14 additions & 41 deletions

File tree

include/mpack/mpack-config.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
* reading/writing C files and makes debugging easier.
9898
*/
9999
#ifndef MPACK_STDIO
100-
#define MPACK_STDIO 0
100+
#define MPACK_STDIO 1
101101
#endif
102102

103103
/**
@@ -120,9 +120,7 @@
120120
*
121121
* The default is malloc() if @ref MPACK_STDLIB is enabled.
122122
*/
123-
#ifdef PARAM_LIST_DYNAMIC
124-
#define MPACK_MALLOC malloc
125-
#endif
123+
#undef MPACK_MALLOC
126124

127125
/**
128126
* @def MPACK_FREE
@@ -134,9 +132,8 @@
134132
* The default is free() if @ref MPACK_MALLOC has not been customized and
135133
* @ref MPACK_STDLIB is enabled.
136134
*/
137-
#ifdef PARAM_LIST_DYNAMIC
138-
#define MPACK_FREE free
139-
#endif
135+
#undef MPACK_FREE
136+
140137

141138
/**
142139
* @def MPACK_REALLOC

include/mpack/mpack.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@
469469
#endif
470470

471471
// Use defaults in stdlib if we have them. Without it we don't use malloc.
472-
#if defined(MPACK_STDLIB)
472+
#if 0
473473
#if MPACK_STDLIB && !defined(MPACK_MALLOC)
474474
#define MPACK_MALLOC malloc
475475
#define MPACK_REALLOC realloc
@@ -1681,7 +1681,7 @@ MPACK_EXTERN_C_BEGIN
16811681

16821682
#if MPACK_DEBUG
16831683
MPACK_NORETURN(void mpack_assert_fail_wrapper(const char* message));
1684-
#if MPACK_STDIO
1684+
#if 0
16851685
MPACK_NORETURN(void mpack_assert_fail_format(const char* format, ...));
16861686
#define mpack_assert_fail_at(line, file, exprstr, format, ...) \
16871687
MPACK_EXPAND(mpack_assert_fail_format("mpack assertion failed at " file ":" #line "\n%s\n" format, exprstr, __VA_ARGS__))
@@ -1712,7 +1712,7 @@ MPACK_EXTERN_C_BEGIN
17121712
(void)0))
17131713

17141714
void mpack_break_hit(const char* message);
1715-
#if MPACK_STDIO
1715+
#if 0
17161716
void mpack_break_hit_format(const char* format, ...);
17171717
#define mpack_break_hit_at(line, file, ...) \
17181718
MPACK_EXPAND(mpack_break_hit_format("mpack breakpoint hit at " file ":" #line "\n" __VA_ARGS__))
@@ -1813,9 +1813,9 @@ MPACK_EXTERN_C_BEGIN
18131813

18141814
/* Make sure our configuration makes sense */
18151815
#ifndef MPACK_MALLOC
1816-
#if MPACK_STDIO
1817-
#error "MPACK_STDIO requires preprocessor definitions for MPACK_MALLOC and MPACK_FREE."
1818-
#endif
1816+
//#if MPACK_STDIO
1817+
// #error "MPACK_STDIO requires preprocessor definitions for MPACK_MALLOC and MPACK_FREE."
1818+
//#endif
18191819
#if MPACK_READ_TRACKING
18201820
#error "MPACK_READ_TRACKING requires preprocessor definitions for MPACK_MALLOC and MPACK_FREE."
18211821
#endif

meson.build

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
project('param', 'c', subproject_dir:'lib', default_options: ['warning_level=2', 'werror=true'])
2-
#add_project_arguments(['-Wstrict-prototypes', '-Wmissing-prototypes', '-Wmissing-declarations', '-Wcast-align', '-Wpointer-arith', '-Wshadow'], language: 'c')
3-
# We currently dos not build with cast align due to mpack.c in dynamic mode, todo update this and fix warnings
4-
add_project_arguments(['-Wstrict-prototypes', '-Wmissing-prototypes', '-Wmissing-declarations', '-Wno-cast-align', '-Wpointer-arith', '-Wshadow'], language: 'c')
2+
add_project_arguments(['-Wstrict-prototypes', '-Wmissing-prototypes', '-Wmissing-declarations', '-Wcast-align', '-Wpointer-arith', '-Wshadow'], language: 'c')
53

64
conf = configuration_data()
75
conf.set('PARAM_HAVE_SYS_QUEUE', get_option('list_dynamic') or get_option('list_pool') > 0)

src/mpack/mpack.c

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,34 +51,12 @@ MPACK_SILENCE_WARNINGS_BEGIN
5151

5252
#if MPACK_DEBUG
5353

54-
#if MPACK_STDIO
55-
void mpack_assert_fail_format(const char* format, ...) {
56-
char buffer[512];
57-
va_list args;
58-
va_start(args, format);
59-
vsnprintf(buffer, sizeof(buffer), format, args);
60-
va_end(args);
61-
buffer[sizeof(buffer) - 1] = 0;
62-
mpack_assert_fail_wrapper(buffer);
63-
}
64-
65-
void mpack_break_hit_format(const char* format, ...) {
66-
char buffer[512];
67-
va_list args;
68-
va_start(args, format);
69-
vsnprintf(buffer, sizeof(buffer), format, args);
70-
va_end(args);
71-
buffer[sizeof(buffer) - 1] = 0;
72-
mpack_break_hit(buffer);
73-
}
74-
#endif
75-
7654
#if !MPACK_CUSTOM_ASSERT
7755
void mpack_assert_fail(const char* message) {
7856
MPACK_UNUSED(message);
7957

8058
#if MPACK_STDIO
81-
fprintf(stderr, "%s\n", message);
59+
printf("%s\n", message);
8260
#endif
8361
}
8462
#endif
@@ -1254,7 +1232,7 @@ void mpack_writer_init_growable(mpack_writer_t* writer, char** target_data, size
12541232
}
12551233
#endif
12561234

1257-
#if MPACK_STDIO
1235+
#if MPACK_MALLOC
12581236
static void mpack_file_writer_flush(mpack_writer_t* writer, const char* buffer, size_t count) {
12591237
FILE* file = (FILE*)writer->context;
12601238
size_t written = fwrite((const void*)buffer, 1, count, file);
@@ -2860,7 +2838,7 @@ void mpack_reader_set_skip(mpack_reader_t* reader, mpack_reader_skip_t skip) {
28602838
reader->skip = skip;
28612839
}
28622840

2863-
#if MPACK_STDIO
2841+
#if MPACK_MALLOC
28642842
static size_t mpack_file_reader_fill(mpack_reader_t* reader, char* buffer, size_t count) {
28652843
if (feof((FILE *)reader->context)) {
28662844
mpack_reader_flag_error(reader, mpack_error_eof);

0 commit comments

Comments
 (0)