PERFORCE change 219886 for review
Brooks Davis
brooks at FreeBSD.org
Wed Dec 5 18:29:01 UTC 2012
http://p4web.freebsd.org/@@219886?ac=10
Change 219886 by brooks at brooks_zenith on 2012/12/05 18:28:12
Improve the stub malloc to allocated 32-byte aligned chunks with
size data and red zone padding in front of each allocation. ~1%
performance improvement.
Don't verify the readzones for now. It's slower and there's a bug
I've not yet tracked down in the case of files smaller than a page.
Affected files ...
.. //depot/projects/ctsrd/cheribsd/src/ctsrd/minifile/cheri/malloc.c#2 edit
Differences ...
==== //depot/projects/ctsrd/cheribsd/src/ctsrd/minifile/cheri/malloc.c#2 (text+ko) ====
@@ -34,6 +34,19 @@
#include <stdlib.h>
#include <string.h>
+#define CTASSERT(x) _Static_assert(x, "compile-time assertion failed");
+
+static const uint64_t red0 = 0x0123456789ABCDEF;
+static const uint64_t red1 = 0xFEDCBA9876543210;
+
+struct minfo {
+ uint64_t red0;
+ size_t size;
+ size_t rsize;
+ uint64_t red1;
+};
+CTASSERT(sizeof(struct minfo) == 32);
+
void *_sb_heapbase;
size_t _sb_heaplen;
@@ -41,13 +54,22 @@
malloc(size_t size)
{
size_t rsize;
+ struct minfo *minfo;
char *ptr;
- rsize = roundup2(size, 4096);
- if (_sb_heaplen < rsize)
+ rsize = roundup2(size, sizeof(minfo));
+ if (_sb_heaplen < rsize + sizeof(minfo))
return (NULL);
- ptr = _sb_heapbase;
+ minfo = _sb_heapbase;
+ *minfo = (struct minfo) {
+ .red0 = red0,
+ .size = size,
+ .rsize = rsize,
+ .red1 = red1
+ };
+ _sb_heaplen -= sizeof(minfo);
+ ptr = (char *)(minfo + 1);
_sb_heaplen -= rsize;
_sb_heapbase = ptr + rsize;
@@ -67,22 +89,42 @@
}
void
-free(void *ptr __unused)
+free(void *ptr)
{
+#ifdef CHECK_RZ
+ struct minfo *minfo;
+ minfo = ptr;
+ minfo--;
+ if (minfo->red0 != red0 || minfo->red1 != red1)
+ abort();
+#endif
}
void *
realloc(void *ptr, size_t size)
{
char *nptr;
+ struct minfo *minfo;
+
+ if (ptr == NULL)
+ return (malloc(size));
+
+ minfo = ptr;
+ minfo--;
+#ifdef CHECK_RZ
+ if (minfo->red0 != red0 || minfo->red1 != red1)
+ abort();
+#endif
- if (size <= 4096 && ptr != NULL)
+ if (size <= minfo->rsize) {
+ minfo->size = size;
return (ptr);
+ }
nptr = malloc(size);
- if (nptr != NULL && ptr != NULL) {
- memcpy(nptr, ptr, MIN(size, (size_t)nptr - (size_t)ptr));
+ if (nptr != NULL) {
+ memcpy(nptr, ptr, minfo->size);
free(ptr);
}
More information about the p4-projects
mailing list