Skip to content

Remove message_str to save 1KB memory #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 38 additions & 26 deletions append.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ char *append_pointer(char *dest, char *limit, PNT_ARITH_TYPE value, int base)
* characters added will be returned. No \0 character will be added.
*/
char *append_vformat(char *dest, char *limit, const char *format,
va_list args)
va_list *args)
{
const char *format_p = format;
char value_buf[64];
Expand Down Expand Up @@ -309,9 +309,9 @@ char *append_vformat(char *dest, char *limit, const char *format,
trunc = 1;
} else if (ch == '*') {
if (trunc) {
trunc_len = va_arg(args, int);
trunc_len = va_arg(*args, int);
} else {
width_len = va_arg(args, int);
width_len = va_arg(*args, int);
}
} else if (ch >= '0' && ch <= '9') {
if (trunc) {
Expand All @@ -338,20 +338,20 @@ char *append_vformat(char *dest, char *limit, const char *format,

const char *value;
if (ch == 'c') {
value_buf[0] = (char)va_arg(args, int);
value_buf[0] = (char)va_arg(*args, int);
value_buf[1] = '\0';
value = value_buf;
} else if (ch == 'd') {
long num;
if (long_arg) {
num = va_arg(args, long);
num = va_arg(*args, long);
} else {
num = va_arg(args, int);
num = va_arg(*args, int);
}
handle_decimal(value_buf, value_limit, num, 10);
value = value_buf;
} else if (ch == 'f') {
double num = va_arg(args, double);
double num = va_arg(*args, double);
if (trunc_len < 0) {
handle_float(value_buf, value_limit, num, DEFAULT_DECIMAL_PRECISION, 1);
} else {
Expand All @@ -363,9 +363,9 @@ char *append_vformat(char *dest, char *limit, const char *format,
} else if (ch == 'o') {
long num;
if (long_arg) {
num = va_arg(args, long);
num = va_arg(*args, long);
} else {
num = va_arg(args, int);
num = va_arg(*args, int);
}
handle_decimal(value_buf, value_limit, num, 8);
value = value_buf;
Expand All @@ -374,21 +374,26 @@ char *append_vformat(char *dest, char *limit, const char *format,
prefix_len = 1;
}
} else if (ch == 'p') {
DMALLOC_PNT pnt = va_arg(args, DMALLOC_PNT);
PNT_ARITH_TYPE num = (PNT_ARITH_TYPE)pnt;
handle_pointer(value_buf, value_limit, num, 16);
DMALLOC_PNT pnt = va_arg(*args, DMALLOC_PNT);
if (*format_p == 'V') {
struct va_format *vaf = (struct va_format *)pnt;
dest_p = append_vformat(dest_p, limit, vaf->fmt, vaf->va);
format_p++;
break;
}
handle_pointer(value_buf, value_limit, (PNT_ARITH_TYPE)pnt, 16);
value = value_buf;
// because %#p throws a gcc warning, I've decreed that %p has a 0x hex prefix
prefix = "0x";
prefix_len = 2;
} else if (ch == 's') {
value = va_arg(args, char *);
value = va_arg(*args, char *);
} else if (ch == 'u') {
unsigned long num;
if (long_arg) {
num = va_arg(args, unsigned long);
num = va_arg(*args, unsigned long);
} else {
num = va_arg(args, unsigned int);
num = va_arg(*args, unsigned int);
}
char *value_buf_p = value_buf;
value_buf_p = append_ulong(value_buf_p, value_limit, num, 10);
Expand All @@ -397,9 +402,9 @@ char *append_vformat(char *dest, char *limit, const char *format,
} else if (ch == 'x') {
long num;
if (long_arg) {
num = va_arg(args, long);
num = va_arg(*args, long);
} else {
num = va_arg(args, int);
num = va_arg(*args, int);
}
handle_decimal(value_buf, value_limit, num, 16);
if (format_prefix) {
Expand Down Expand Up @@ -469,7 +474,7 @@ char *append_format(char *dest, char *limit, const char *format, ...)
char *dest_p;

va_start(args, format);
dest_p = append_vformat(dest, limit, format, args);
dest_p = append_vformat(dest, limit, format, &args);
va_end(args);

return dest_p;
Expand All @@ -496,7 +501,7 @@ char *append_null(char *dest, char *limit)
* causing the library to go recursive.
*/
int loc_vsnprintf(char *buf, const int size, const char *format,
va_list args)
va_list *args)
{
char *limit, *buf_p;
limit = buf + size;
Expand All @@ -516,7 +521,7 @@ int loc_snprintf(char *buf, const int size, const char *format, ...)
int len;

va_start(args, format);
len = loc_vsnprintf(buf, size, format, args);
len = loc_vsnprintf(buf, size, format, &args);
va_end(args);

return len;
Expand All @@ -529,7 +534,7 @@ void loc_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
loc_vfprintf(stdout, format, args);
loc_vfprintf(stdout, format, &args);
va_end(args);
}

Expand All @@ -540,14 +545,14 @@ void loc_fprintf(FILE *file, const char *format, ...)
{
va_list args;
va_start(args, format);
loc_vfprintf(file, format, args);
loc_vfprintf(file, format, &args);
va_end(args);
}

/*
* Local implementation of vfprintf so we can use %p and other non-standard formats.
*/
void loc_vfprintf(FILE *file, const char *format, va_list args)
void loc_vfprintf(FILE *file, const char *format, va_list *args)
{
// these are simple messages so this limit is ok
char buf[256];
Expand All @@ -561,24 +566,31 @@ void loc_vfprintf(FILE *file, const char *format, va_list args)
/*
* Local implementation of dprintf so we can use %p and other non-standard formats.
*/
void loc_dprintf(int fd, const char *format, ...)
void loc_message(int fd, const char *format, ...)
{
va_list args;
va_start(args, format);
loc_vdprintf(fd, format, args);
loc_vmessage(fd, format, &args);
va_end(args);
}

/*
* Local implementation of vdprintf so we can use %p and other non-standard formats.
*/
void loc_vdprintf(int fd, const char *format, va_list args)
void loc_vmessage(int fd, const char *format, va_list *args)
{
// these are simple messages so this limit is ok
char buf[256];
char *buf_p, *limit;

limit = buf + sizeof(buf);
buf_p = append_vformat(buf, limit, format, args);
if (buf_p != buf && *(buf_p - 1) != '\n') {
if (buf_p == limit) {
buf_p--;
}
*buf_p++ = '\n';
}

(void)!write(fd, buf, buf_p - buf);
}
15 changes: 10 additions & 5 deletions append.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@

/*<<<<<<<<<< The below prototypes are auto-generated by fillproto */

struct va_format {
const char *fmt;
va_list *va;
};

/*
* Append string argument to destination up to limit pointer. Pointer
* to the end of the characters added will be returned. No \0
Expand Down Expand Up @@ -74,7 +79,7 @@ char *append_pointer(char *dest, char *limit, PNT_ARITH_TYPE value, int base);
*/
extern
char *append_vformat(char *dest, char *limit, const char *format,
va_list args);
va_list *args);

/*
* Append a varargs format to destination. Pointer to the end of the
Expand All @@ -98,7 +103,7 @@ char *append_null(char *dest, char *limit);
*/
extern
int loc_vsnprintf(char *buf, const int size, const char *format,
va_list args);
va_list *args);

/*
* Local implementation of snprintf. We are doing this trying to not
Expand All @@ -124,19 +129,19 @@ void loc_fprintf(FILE *file, const char *format, ...);
* Local implementation of vfprintf so we can use %p and other non-standard formats.
*/
extern
void loc_vfprintf(FILE *file, const char *format, va_list args);
void loc_vfprintf(FILE *file, const char *format, va_list *args);

/*
* Local implementation of dprintf so we can use %p and other non-standard formats.
*/
extern
void loc_dprintf(int fd, const char *format, ...);
void loc_message(int fd, const char *format, ...);

/*
* Local implementation of vdprintf so we can use %p and other non-standard formats.
*/
extern
void loc_vdprintf(int fd, const char *format, va_list args);
void loc_vmessage(int fd, const char *format, va_list *args);

/*<<<<<<<<<< This is end of the auto-generated output from fillproto. */

Expand Down
54 changes: 22 additions & 32 deletions error.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ int _dmalloc_aborting_b = 0;

/* local variables */
static int outfile_fd = -1; /* output file descriptor */
/* the following are here to reduce stack overhead */
static char message_str[1024]; /* message string buffer */

/*
* void _dmalloc_open_log
Expand Down Expand Up @@ -218,8 +216,8 @@ static void build_logfile_path(char *buf, const int buf_len)

if (buf_p >= bounds_p - 1) {
/* NOTE: we can't use dmalloc_message of course so do it the hard way */
loc_dprintf(STDERR,
"debug-malloc library: logfile path too large '%s'\r\n",
loc_message(STDERR,
"debug-malloc library: logfile path too large '%s'",
dmalloc_logpath);
}

Expand Down Expand Up @@ -247,8 +245,8 @@ void _dmalloc_open_log(void)
outfile_fd = open(log_path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (outfile_fd < 0) {
/* NOTE: we can't use dmalloc_message of course so do it the hardway */
loc_dprintf(STDERR,
"debug-malloc library: could not open '%s'\r\n",
loc_message(STDERR,
"debug-malloc library: could not open '%s'",
log_path);
/* disable log_path */
dmalloc_logpath = NULL;
Expand Down Expand Up @@ -435,13 +433,15 @@ char *_dmalloc_ptime(const TIME_TYPE *time_p, char *buf, const int buf_size,
*
* args -> Already converted pointer to a stdarg list.
*/
void _dmalloc_vmessage(const char *format, va_list args)
void _dmalloc_vmessage(const char *format, va_list *args)
{
char *str_p, *bounds_p;
int len;
char *str_p, *bounds_p;
char buf[64];
int len;
struct va_format vaf;

str_p = message_str;
bounds_p = str_p + sizeof(message_str);
str_p = buf;
bounds_p = str_p + sizeof(buf);

/* no logpath and no print then no workie */
if (dmalloc_logpath == NULL
Expand Down Expand Up @@ -525,30 +525,20 @@ void _dmalloc_vmessage(const char *format, va_list args)
* specifications if necessary.
*/

/* write the format + info into str */
char *start_p = str_p;
str_p = append_vformat(str_p, bounds_p, format, args);

/* was it an empty format? */
if (str_p == start_p) {
return;
}

/* tack on a '\n' if necessary */
if (*(str_p - 1) != '\n') {
*str_p++ = '\n';
*str_p = '\0';
}
len = str_p - message_str;
/* write the format + info into file */
append_null(str_p, bounds_p);

vaf.fmt = format;
vaf.va = args;

/* do we need to write the message to the logfile */
if (dmalloc_logpath != NULL) {
(void)write(outfile_fd, message_str, len);
loc_message(outfile_fd, "%s%pV", buf, &vaf);
}

/* do we need to print the message? */
if (BIT_IS_SET(_dmalloc_flags, DMALLOC_DEBUG_PRINT_MESSAGES)) {
(void)write(STDERR, message_str, len);
loc_message(STDERR, "%s%pV", buf, &vaf);
}
}

Expand All @@ -574,12 +564,12 @@ void _dmalloc_die(const int silent_b)
}

/* print a message that we are going down */
loc_dprintf(STDERR,
"debug-malloc library: %s program, fatal error\r\n",
loc_message(STDERR,
"debug-malloc library: %s program, fatal error",
stop_str);
if (dmalloc_errno != DMALLOC_ERROR_NONE) {
loc_dprintf(STDERR,
" Error: %s (err %d)\r\n",
loc_message(STDERR,
" Error: %s (err %d)",
dmalloc_strerror(dmalloc_errno), dmalloc_errno);
}
}
Expand Down
2 changes: 1 addition & 1 deletion error.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ char *_dmalloc_ptime(const TIME_TYPE *time_p, char *buf, const int buf_size,
* args -> Already converted pointer to a stdarg list.
*/
extern
void _dmalloc_vmessage(const char *format, va_list args);
void _dmalloc_vmessage(const char *format, va_list *args);

/*
* void _dmalloc_die
Expand Down
4 changes: 2 additions & 2 deletions heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ static void *heap_extend(const int incr)

if (ret == SBRK_ERROR) {
if (BIT_IS_SET(_dmalloc_flags, DMALLOC_DEBUG_CATCH_NULL)) {
loc_dprintf(STDERR,
"\r\ndmalloc: critical error: could not extend heap %u more bytes\r\n", incr);
loc_message(STDERR,
"dmalloc: critical error: could not extend heap %u more bytes", incr);
_dmalloc_die(0);
}
dmalloc_errno = DMALLOC_ERROR_ALLOC_FAILED;
Expand Down
Loading