aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2009-08-02 17:38:25 -0700
committerH. Peter Anvin <hpa@zytor.com>2009-08-02 17:38:25 -0700
commit71beeca0edbae0cd7bd92b55752b016d0b5d676f (patch)
tree5d1be564c9d0f615029de7fb033b868abc39e21f
parent304ab3ccaeb7cc450e11579fcc027da7265b3394 (diff)
downloadbak.git-71beeca0edbae0cd7bd92b55752b016d0b5d676f.tar.gz
bak.git-71beeca0edbae0cd7bd92b55752b016d0b5d676f.tar.xz
bak.git-71beeca0edbae0cd7bd92b55752b016d0b5d676f.zip
file: use malloc/free to allocate and free buffers
Instead of statically allocating huge buffers, use malloc/free to dynamically allocate them from the heap as needed. It really doesn't make any sense to create a half-megabyte .bss little of which will actually get used... Signed-off-by: H. Peter Anvin <hpa@zytor.com>
-rw-r--r--com32/lib/sys/file.h7
-rw-r--r--com32/lib/sys/fileclose.c3
-rw-r--r--com32/lib/sys/open.c23
-rw-r--r--com32/lib/sys/opendev.c4
-rw-r--r--com32/lib/sys/stdcon_read.c20
5 files changed, 45 insertions, 12 deletions
diff --git a/com32/lib/sys/file.h b/com32/lib/sys/file.h
index 66192fbf..12492264 100644
--- a/com32/lib/sys/file.h
+++ b/com32/lib/sys/file.h
@@ -1,6 +1,7 @@
/* ----------------------------------------------------------------------- *
*
- * Copyright 2003-2008 H. Peter Anvin - All Rights Reserved
+ * Copyright 2003-2009 H. Peter Anvin - All Rights Reserved
+ * Copyright 2009 Intel Corporation; author: H. Peter Anvin
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
@@ -74,7 +75,7 @@ struct output_dev {
/* File structure */
#define NFILES 32 /* Number of files to support */
-#define MAXBLOCK 16384 /* Defined by ABI */
+#define MAXBLOCK 65536 /* 16K minimum per ABI */
struct file_info {
const struct input_dev *iop; /* Input operations */
@@ -95,7 +96,7 @@ struct file_info {
size_t nbytes; /* Number of bytes available in buffer */
char *datap; /* Current data pointer */
void *pvt; /* Private pointer for driver */
- char buf[MAXBLOCK];
+ char *buf; /* Data buffer */
} i;
};
diff --git a/com32/lib/sys/fileclose.c b/com32/lib/sys/fileclose.c
index e005567e..bbf6077d 100644
--- a/com32/lib/sys/fileclose.c
+++ b/com32/lib/sys/fileclose.c
@@ -34,6 +34,7 @@
#include <errno.h>
#include <com32.h>
#include <string.h>
+#include <stdlib.h>
#include "file.h"
int __file_close(struct file_info *fp)
@@ -48,5 +49,7 @@ int __file_close(struct file_info *fp)
__com32.cs_intcall(0x22, &regs, NULL);
}
+ free(fp->i.buf);
+
return 0;
}
diff --git a/com32/lib/sys/open.c b/com32/lib/sys/open.c
index 114728ec..276b2c73 100644
--- a/com32/lib/sys/open.c
+++ b/com32/lib/sys/open.c
@@ -30,6 +30,7 @@
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
+#include <stdlib.h>
#include "file.h"
/*
@@ -53,16 +54,20 @@ const struct input_dev __file_dev = {
int open(const char *pathname, int flags, ...)
{
com32sys_t regs;
- int fd;
- struct file_info *fp;
+ int fd, e;
+ struct file_info *fp = NULL;
fd = opendev(&__file_dev, NULL, flags);
if (fd < 0)
- return -1;
+ goto err;
fp = &__file_info[fd];
+ fp->i.datap = fp->i.buf = malloc(MAXBLOCK);
+ if (!fp->i.buf)
+ goto err;
+
strlcpy(__com32.cs_bounce, pathname, __com32.cs_bounce_size);
regs.eax.w[0] = 0x0006;
@@ -73,7 +78,7 @@ int open(const char *pathname, int flags, ...)
if ((regs.eflags.l & EFLAGS_CF) || regs.esi.w[0] == 0) {
errno = ENOENT;
- return -1;
+ goto err;
}
{
@@ -87,4 +92,14 @@ int open(const char *pathname, int flags, ...)
fp->i.nbytes = 0;
return fd;
+
+err:
+ e = errno;
+ if (fp) {
+ if (fp->i.buf)
+ free(fp->i.buf);
+ memset(fp, 0, sizeof *fp); /* Mark file structure unused */
+ }
+ errno = e;
+ return -1;
}
diff --git a/com32/lib/sys/opendev.c b/com32/lib/sys/opendev.c
index 1df9d070..4b8f2be7 100644
--- a/com32/lib/sys/opendev.c
+++ b/com32/lib/sys/opendev.c
@@ -1,6 +1,7 @@
/* ----------------------------------------------------------------------- *
*
- * Copyright 2003-2008 H. Peter Anvin - All Rights Reserved
+ * Copyright 2003-2009 H. Peter Anvin - All Rights Reserved
+ * Copyright 2009 Intel Corporation; author: H. Peter Anvin
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
@@ -64,7 +65,6 @@ int opendev(const struct input_dev *idev,
/* The file structure is already zeroed */
fp->iop = &dev_error_r;
fp->oop = &dev_error_w;
- fp->i.datap = fp->i.buf;
if (idev) {
if (idev->open && (e = idev->open(fp))) {
diff --git a/com32/lib/sys/stdcon_read.c b/com32/lib/sys/stdcon_read.c
index 967e564b..8d8150dd 100644
--- a/com32/lib/sys/stdcon_read.c
+++ b/com32/lib/sys/stdcon_read.c
@@ -35,10 +35,24 @@
#include <string.h>
#include <com32.h>
#include <minmax.h>
+#include <stdlib.h>
#include "file.h"
+#define BUFFER 4096
+
extern ssize_t __rawcon_read(struct file_info *fp, void *buf, size_t count);
+static ssize_t __stdcon_open(struct file_info *fp)
+{
+ fp->i.datap = fp->i.buf = malloc(BUFFER);
+ return !fp->i.buf ? -1 : 0;
+}
+
+static int __stdcon_close(struct file_info *fp)
+{
+ free(fp->i.buf);
+}
+
static ssize_t __stdcon_read(struct file_info *fp, void *buf, size_t count)
{
char *bufp = buf;
@@ -55,7 +69,7 @@ static ssize_t __stdcon_read(struct file_info *fp, void *buf, size_t count)
if (ch == '\n')
return n;
} else {
- fp->i.nbytes = __line_input(fp, fp->i.buf, MAXBLOCK, __rawcon_read);
+ fp->i.nbytes = __line_input(fp, fp->i.buf, BUFFER, __rawcon_read);
fp->i.datap = fp->i.buf;
if (fp->i.nbytes == 0)
@@ -71,6 +85,6 @@ const struct input_dev dev_stdcon_r = {
.flags = __DEV_TTY | __DEV_INPUT,
.fileflags = O_RDONLY,
.read = __stdcon_read,
- .close = NULL,
- .open = NULL,
+ .open = __stdcon_open,
+ .close = __stdcon_close,
};