aboutsummaryrefslogtreecommitdiffstats
path: root/src/myfprintf.c
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2006-07-30 03:43:38 +0000
committerEric Wong <normalperson@yhbt.net>2006-07-30 03:43:38 +0000
commit4cf5d04ca15bc28ee4636d1dccb858763513e571 (patch)
tree7898902e786f5763643a513ea96bcada8a2559cc /src/myfprintf.c
parent4d5b8509eb46cff40890b781018669233a79e414 (diff)
downloadmpd-4cf5d04ca15bc28ee4636d1dccb858763513e571.tar.gz
mpd-4cf5d04ca15bc28ee4636d1dccb858763513e571.tar.xz
mpd-4cf5d04ca15bc28ee4636d1dccb858763513e571.zip
interface/connection malloc reductions from mpd-ke
This patch massively reduces the amount of heap allocations at the interface/command layer. Most commands with minimal output should not allocate memory from the heap at all. Things like repeatedly polling status, currentsong, and volume changes should be faster as a result, and more importantly, not a source of memory fragmentation. These changes should be safe in that there's no way for a remote-client to corrupt memory or otherwise do bad stuff to MPD, but an extra set of eyes to review would be good. Of course there's never any warranty :) No longer do we use FILE * structures in the interface, which means we don't have to allocate any new memory for most connections. Now, before you go on about losing the buffering that FILE * +implies+, remember that myfprintf() never took advantage of any of the stdio buffering features. To reduce the diff and make bugs easier to spot in the diff, I've kept myfprintf in places where we write to files (and not network interfaces). Expect myfprintf to go away entirely soon (we'll use fprintf for writing regular files). git-svn-id: https://svn.musicpd.org/mpd/trunk@4483 09075e82-0dd4-0310-85a5-a0d7c8717e4f
Diffstat (limited to 'src/myfprintf.c')
-rw-r--r--src/myfprintf.c76
1 files changed, 29 insertions, 47 deletions
diff --git a/src/myfprintf.c b/src/myfprintf.c
index d9149499c..7a547f92e 100644
--- a/src/myfprintf.c
+++ b/src/myfprintf.c
@@ -21,6 +21,7 @@
#include "path.h"
#include "log.h"
#include "conf.h"
+#include "utils.h"
#include <stdarg.h>
#include <sys/param.h>
@@ -38,28 +39,41 @@ static FILE *myfprintf_err;
static char *myfprintf_outFilename;
static char *myfprintf_errFilename;
-static void blockingWrite(int fd, char *string, int len)
+static void blockingWrite(const int fd, const char *string, size_t len)
{
- int ret;
-
while (len) {
- ret = write(fd, string, len);
- if (ret == 0)
+ size_t ret = xwrite(fd, string, len);
+ if (ret == len)
return;
- if (ret < 0) {
- switch (errno) {
- case EAGAIN:
- case EINTR:
- continue;
- default:
- return;
- }
+ if (ret >= 0) {
+ len -= ret;
+ string += ret;
+ continue;
}
- len -= ret;
- string += ret;
+ return; /* error */
}
}
+void vfdprintf(const int fd, const char *fmt, va_list args)
+{
+ static char buffer[BUFFER_LENGTH + 1];
+ char *buf = buffer;
+ size_t len;
+
+ vsnprintf(buf, BUFFER_LENGTH, fmt, args);
+ len = strlen(buf);
+ if (interfacePrintWithFD(fd, buf, len) < 0)
+ blockingWrite(fd, buf, len);
+}
+
+mpd_fprintf void fdprintf(const int fd, const char *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ vfdprintf(fd, fmt, args);
+ va_end(args);
+}
+
void myfprintfStdLogMode(FILE * out, FILE * err)
{
myfprintf_stdLogMode = 1;
@@ -69,38 +83,6 @@ void myfprintfStdLogMode(FILE * out, FILE * err)
myfprintf_errFilename = getConfigParamValue(CONF_ERROR_FILE);
}
-void myfprintf(FILE * fp, char *format, ...)
-{
- static char buffer[BUFFER_LENGTH + 1];
- va_list arglist;
- int fd = fileno(fp);
-
- va_start(arglist, format);
- if (fd == 1 || fd == 2) {
- if (myfprintf_stdLogMode) {
- time_t t = time(NULL);
- if (fd == 1)
- fp = myfprintf_out;
- else
- fp = myfprintf_err;
- strftime(buffer, 14, "%b %e %R", localtime(&t));
- blockingWrite(fd, buffer, strlen(buffer));
- blockingWrite(fd, " : ", 3);
- }
- vsnprintf(buffer, BUFFER_LENGTH, format, arglist);
- blockingWrite(fd, buffer, strlen(buffer));
- } else {
- int len;
- vsnprintf(buffer, BUFFER_LENGTH, format, arglist);
- len = strlen(buffer);
- if (interfacePrintWithFD(fd, buffer, len) < 0) {
- blockingWrite(fd, buffer, len);
- }
- }
-
- va_end(arglist);
-}
-
int myfprintfCloseAndOpenLogFile(void)
{
if (myfprintf_stdLogMode) {