User: Password:
|
|
Subscribe / Log in / New account

Short topics in memory management

Short topics in memory management

Posted Mar 8, 2007 12:56 UTC (Thu) by ranmachan (subscriber, #21283)
Parent article: Short topics in memory management

Andrews pagecache-management tool works great for burning DVDs.
The script I use for that first md5sums all the files, burns them with growisofs and checks the result with md5sum again.
However, the stock script doesn't work with md5sum+growisofs because the use fread/fwrite.
I also had to include linux/fadvise.h to get it to compile on my Debian system.

diff -Naru pagecache-management/fadv.c pagecache-management/fadv.c
--- pagecache-management/fadv.c 2007-03-03 20:02:20.000000000 +0100
+++ pagecache-management/fadv.c 2007-03-04 11:35:56.000000000 +0100
@@ -9,6 +9,7 @@
#include <fcntl.h>
#include <limits.h>
#include <errno.h>
+#include <linux/fadvise.h>

int main(int argc, char *argv[])
{
diff -Naru pagecache-management/pagecache-management.c pagecache-management/pagecache-management.c
--- pagecache-management/pagecache-management.c 2007-03-03 21:14:00.000000000 +0100
+++ pagecache-management/pagecache-management.c 2007-03-04 14:55:27.000000000 +0100
@@ -15,6 +15,7 @@
#include <unistd.h>
#include <dlfcn.h>
#include <limits.h>
+#include <linux/fadvise.h>

#include "sync_file_range.h"

@@ -152,9 +157,12 @@

static ssize_t (*_write)(int fd, const void *buf, size_t count);
static ssize_t (*_pwrite)(int fd, const void *buf, size_t count, off_t offset);
+static size_t (*_fwrite)(const void *ptr, size_t size, size_t nmemb, FILE *stream);
static ssize_t (*_read)(int fd, void *buf, size_t count);
static ssize_t (*_pread)(int fd, void *buf, size_t count, off_t offset);
+static size_t (*_fread)(void *ptr, size_t size, size_t nmemb, FILE *stream);
static int (*_close)(int fd);
+static int (*_fclose)(FILE *fp);
static int (*_dup2)(int oldfd, int newfd);

static int symbols_loaded;
@@ -176,6 +184,10 @@
if (dlerror())
abort();

+ _fwrite = dlsym(handle, "fwrite");
+ if (dlerror())
+ abort();
+
dlerror();
_read = dlsym(handle, "read");
if (dlerror())
@@ -185,10 +197,18 @@
if (dlerror())
abort();

+ _fread = dlsym(handle, "fread");
+ if (dlerror())
+ abort();
+
_close = dlsym(handle, "close");
if (dlerror())
abort();

+ _fclose = dlsym(handle, "fclose");
+ if (dlerror())
+ abort();
+
_dup2 = dlsym(handle, "dup2");
if (dlerror())
abort();
@@ -222,6 +242,22 @@
return (*_pwrite)(fd, buf, count, offset);
}

+size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
+{
+ load_symbols();
+ write_was_called(fileno(stream), size*nmemb);
+ return (*_fwrite)(ptr, size, nmemb, stream);
+}
+
+#undef fwrite_unlocked
+
+size_t fwrite_unlocked(const void *ptr, size_t size, size_t nmemb, FILE *stream)
+{
+ load_symbols();
+ write_was_called(fileno(stream), size*nmemb);
+ return (*_fwrite)(ptr, size, nmemb, stream);
+}
+
ssize_t read(int fd, void *buf, size_t count)
{
load_symbols();
@@ -236,6 +272,29 @@
return (*_pread)(fd, buf, count, offset);
}

+size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
+{
+ load_symbols();
+ read_was_called(fileno(stream), size*nmemb);
+ return (*_fread)(ptr, size, nmemb, stream);
+}
+
+#undef fread_unlocked
+
+size_t fread_unlocked(void *ptr, size_t size, size_t nmemb, FILE *stream)
+{
+ load_symbols();
+ read_was_called(fileno(stream), size*nmemb);
+ return (*_fread)(ptr, size, nmemb, stream);
+}
+
+int fclose(FILE *fp)
+{
+ load_symbols();
+ close_was_called(fileno(fp));
+ return (*_fclose)(fp);
+}
+
int close(int fd)
{
load_symbols();


(Log in to post comments)


Copyright © 2017, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds