aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2001-10-15 19:23:15 +0000
committerH. Peter Anvin <hpa@zytor.com>2001-10-15 19:23:15 +0000
commit3c4b69a164ad54ff868985771d861f4ddd015619 (patch)
tree93c739f61a4e2717eb3173821dd4f3426b96fe44
parent2c0186aa66ef9d7beb30b033fa49b7f5c05e306f (diff)
downloadlpsm-3c4b69a164ad54ff868985771d861f4ddd015619.tar.gz
lpsm-3c4b69a164ad54ff868985771d861f4ddd015619.tar.xz
lpsm-3c4b69a164ad54ff868985771d861f4ddd015619.zip
Add code to verify that none of the allocated memory regions overlap.
-rw-r--r--testalloc.c29
1 files changed, 27 insertions, 2 deletions
diff --git a/testalloc.c b/testalloc.c
index 05e2956..9c66d07 100644
--- a/testalloc.c
+++ b/testalloc.c
@@ -11,6 +11,7 @@
*
* ----------------------------------------------------------------------- */
+#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@@ -23,10 +24,32 @@
#define SLOTS 1024
#define MAXLOG 26
+void *areas[COUNT];
+int sizes[COUNT];
+
+void verify_no_overlap(int slot)
+{
+ char *start = areas[slot];
+ char *end = start+sizes[slot];
+ char *xs, *xe;
+ int i;
+
+ for ( i = 0 ; i < SLOTS ; i++ ) {
+ if ( i != slot && areas[i] ) {
+ xs = areas[i];
+ xe = xs + sizes[i];
+
+ if ( xs < end && xe > start ) {
+ printf("OVERLAP ALERT: [%p,%p) -> [%p,%p)\n",
+ start, end, xs, xe);
+ abort();
+ }
+ }
+ }
+}
+
int main(int argc, char *argv[])
{
- void *areas[COUNT];
- int sizes[COUNT];
void *buf;
int i, slot, newsize;
double rnd;
@@ -69,6 +92,7 @@ int main(int argc, char *argv[])
occupied -= sizes[slot];
sizes[slot] = newsize;
areas[slot] = nptr;
+ verify_no_overlap(slot);
} else {
printf("Realloc: %d bytes at %p -> %d bytes FAILED\n",
sizes[slot], areas[slot], newsize);
@@ -81,6 +105,7 @@ int main(int argc, char *argv[])
printf("Alloc: %d (0x%08x) bytes at %p\n",
sizes[slot], sizes[slot], areas[slot]);
occupied += sizes[slot];
+ verify_no_overlap(slot);
}
if ( (i & 255) == 0 ) {