aboutsummaryrefslogtreecommitdiff
path: root/morecore.c
diff options
context:
space:
mode:
authorDavid Gibson <dgibson@sneetch.ozlabs.ibm.com>2005-11-18 16:07:53 +1100
committerDavid Gibson <dgibson@sneetch.ozlabs.ibm.com>2005-11-18 16:07:53 +1100
commitc884caf4c39ed3c5ee41009fa5573dc5d4bf5dbf (patch)
tree1aa5c09a66a6422ffc7657893bf4c98ec99208e6 /morecore.c
downloadlibhugetlbfs-c884caf4c39ed3c5ee41009fa5573dc5d4bf5dbf.tar.gz
First cut.
Diffstat (limited to 'morecore.c')
-rw-r--r--morecore.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/morecore.c b/morecore.c
new file mode 100644
index 0000000..b1f9868
--- /dev/null
+++ b/morecore.c
@@ -0,0 +1,70 @@
+#include <stdlib.h>
+#include <malloc.h>
+#include <sys/mman.h>
+#include <errno.h>
+
+#include "hugetlbfs.h"
+
+#if 0
+#ifdef SMALL
+#define BASE ((void *)(0x09000000000UL))
+#else
+#define BASE ((void *)(0x11000000000UL))
+#endif
+#define BLOCK (16384*1024 * 4)
+
+static void *current = 0;
+static void *highest = 0;
+
+#endif
+
+/*
+ * Our plan is to ask for pages 'roughly' at the BASE. We expect nee require
+ * the kernel to offer us sequential pages from wherever it first gave us a
+ * page. If it does not do so, we return the page and pretend there are none
+ * this covers us for the case where another map is in the way. This is
+ * required because 'morecore' must have 'sbrk' semantics, ie. return
+ * sequential, contigious memory blocks. Luckily, if it does not do so
+ * and we error out malloc will happily go back to small pages and use mmap
+ * to get them. Hurrah.
+ */
+
+static void *(*orig_morecore)(ptrdiff_t);
+
+static void *hugetlbfs_morecore(ptrdiff_t increment)
+{
+ return (*orig_morecore)(increment);
+#if 0
+ void *b;
+ if ((current + increment) >= highest) {
+ b = mmap(BASE, BLOCK, PROT_READ|PROT_WRITE,
+ MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
+ if (b == MAP_FAILED)
+ return(0);
+ if (highest != 0) {
+ if (b != highest) {
+/* printf("APW: it failed ... b<%p>\n", b); */
+ munmap(b, BLOCK);
+ return(0);
+ }
+ } else
+ current = b;
+ highest = b + BLOCK;
+ }
+ b = current;
+ current += increment;
+
+ return b;
+#endif
+}
+
+void __attribute__ ((constructor)) setup_morecore(void)
+{
+ char *env;
+
+ orig_morecore = __morecore;
+
+ env = getenv("HUGETLB_MORECORE");
+ if (env)
+ __morecore = &hugetlbfs_morecore;
+}