summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Cahill <michael.cahill@wiredtiger.com>2011-12-23 10:57:57 +1100
committerMichael Cahill <michael.cahill@wiredtiger.com>2011-12-23 10:57:57 +1100
commitb388ad874241a1ac76f58f21ee749ba6db70fc89 (patch)
tree0ddc08172a0ce9d05e2b9405b2738238ecc223a5
parentbe59205f2a3457c00a9a6ff94fc59f6a8a4b5c08 (diff)
downloadmongo-b388ad874241a1ac76f58f21ee749ba6db70fc89.tar.gz
Align allocations larger than 512 bytes to 512 byte boundaries.
--HG-- extra : rebase_source : 408f1713e49dd7d3564d08ec06ec48efe6debb36
-rw-r--r--build_posix/configure.ac.in2
-rw-r--r--src/os_posix/os_alloc.c48
2 files changed, 36 insertions, 14 deletions
diff --git a/build_posix/configure.ac.in b/build_posix/configure.ac.in
index bd47dda0d0f..6ad9d5d000d 100644
--- a/build_posix/configure.ac.in
+++ b/build_posix/configure.ac.in
@@ -68,7 +68,7 @@ AC_PROG_INSTALL
AC_CHECK_LIB(pthread, pthread_create)
AC_CHECK_LIB(dl, dlopen)
AC_CHECK_LIB(rt, sched_yield)
-AC_CHECK_FUNCS([fcntl])
+AC_CHECK_FUNCS([fcntl posix_memalign])
AC_SYS_LARGEFILE
AC_C_BIGENDIAN
diff --git a/src/os_posix/os_alloc.c b/src/os_posix/os_alloc.c
index c4f9dd97cc4..6a978520522 100644
--- a/src/os_posix/os_alloc.c
+++ b/src/os_posix/os_alloc.c
@@ -35,9 +35,23 @@ __wt_calloc(WT_SESSION_IMPL *session, size_t number, size_t size, void *retp)
if (session != NULL && S2C(session)->stats != NULL)
WT_CSTAT_INCR(session, memalloc);
+#ifdef HAVE_POSIX_MEMALIGN
+#define ALIGN_SIZE 512
+ if (size * number >= ALIGN_SIZE) {
+ int ret;
+ if ((ret = posix_memalign(&p, ALIGN_SIZE, number * size)) != 0)
+ WT_RET_MSG(session, ret, "memory allocation");
+ memset(p, 0, number * size);
+ goto done;
+ }
+#endif
+
if ((p = calloc(number, size)) == NULL)
WT_RET_MSG(session, __wt_errno(), "memory allocation");
+#ifdef HAVE_POSIX_MEMALIGN
+done:
+#endif
*(void **)retp = p;
return (0);
}
@@ -50,7 +64,7 @@ int
__wt_realloc(WT_SESSION_IMPL *session,
size_t *bytes_allocated_ret, size_t bytes_to_allocate, void *retp)
{
- void *p;
+ void *p, *newp;
size_t bytes_allocated;
/*
@@ -59,11 +73,6 @@ __wt_realloc(WT_SESSION_IMPL *session,
*/
WT_ASSERT(session, bytes_to_allocate != 0);
- if (session != NULL && S2C(session)->stats != NULL)
- WT_CSTAT_INCR(session, memalloc);
-
- p = *(void **)retp;
-
/*
* Sometimes we're allocating memory and we don't care about the
* final length -- bytes_allocated_ret may be NULL.
@@ -72,6 +81,23 @@ __wt_realloc(WT_SESSION_IMPL *session,
bytes_allocated_ret == NULL ? 0 : *bytes_allocated_ret;
WT_ASSERT(session, bytes_allocated < bytes_to_allocate);
+ p = *(void **)retp;
+
+#ifdef HAVE_POSIX_MEMALIGN
+ if (bytes_allocated >= ALIGN_SIZE || bytes_to_allocate >= ALIGN_SIZE) {
+ WT_RET(__wt_calloc(session, bytes_to_allocate, 1, &newp));
+ memcpy(newp, p, bytes_allocated);
+ __wt_free(session, p);
+ p = newp;
+ goto done;
+ }
+#else
+ WT_UNUSED(newp);
+#endif
+
+ if (p == NULL && session != NULL && S2C(session)->stats != NULL)
+ WT_CSTAT_INCR(session, memalloc);
+
if ((p = realloc(p, bytes_to_allocate)) == NULL)
WT_RET_MSG(session, __wt_errno(), "memory allocation");
@@ -86,6 +112,9 @@ __wt_realloc(WT_SESSION_IMPL *session,
memset((uint8_t *)
p + bytes_allocated, 0, bytes_to_allocate - bytes_allocated);
+#ifdef HAVE_POSIX_MEMALIGN
+done:
+#endif
/* Update caller's bytes allocated value. */
if (bytes_allocated_ret != NULL)
*bytes_allocated_ret = bytes_to_allocate;
@@ -108,13 +137,6 @@ __wt_strndup(WT_SESSION_IMPL *session, const char *str, size_t len, void *retp)
return (0);
}
- /*
- * !!!
- * This function MUST handle a NULL WT_SESSION_IMPL handle.
- */
- if (session != NULL && S2C(session)->stats != NULL)
- WT_CSTAT_INCR(session, memalloc);
-
WT_RET(__wt_calloc(session, len + 1, 1, &p));
/*