1
1

* clean up the memory check test a little bit - still needs some work

This commit was SVN r8343.
Этот коммит содержится в:
Brian Barrett 2005-11-30 22:32:18 +00:00
родитель 8c443832ae
Коммит f301d06fd4

Просмотреть файл

@ -21,41 +21,44 @@
#include <stdlib.h>
#include <stdio.h>
#include <sys/mman.h>
#include <assert.h>
#include "ompi/include/constants.h"
#include "opal/runtime/opal.h"
#include "opal/memoryhooks/memory.h"
int ret = 1; /* because of the munmap */
int size = 10 * 1024 * 1024;
int counter = 0;
const int bigsize = 16 * 1024 * 1024;
static void
alloc_callback(void *buf, size_t length, void *cbdata)
{
printf("\talloc callback with %lx, %d\n", (unsigned long) buf, (int) length);
ret++;
counter++;
}
static void
release_callback(void *buf, size_t length, void *cbdata)
{
printf("\trelease callback with %lx, %d\n", (unsigned long) buf, (int) length);
ret--;
counter--;
}
int
main(int argc, char *argv[])
{
void * foo, *bar;
int retval;
return 77;
opal_init();
if (0 == ((OPAL_MEMORY_FREE_SUPPORT|OPAL_MEMORY_MALLOC_SUPPORT) &
opal_mem_hooks_support_level())) {
printf("no memory registration supported. skipping\n");
return 77;
}
static int
free_only_test(void)
{
/* BWB - finish me! */
printf("test not written yet - skipping\n");
return 77;
}
static int
alloc_free_test(void)
{
void *foo, *bar;
int retval;
retval = opal_mem_hooks_register_release(release_callback, NULL);
retval |= opal_mem_hooks_register_alloc(alloc_callback, NULL);
if (retval != OMPI_SUCCESS) {
@ -63,42 +66,60 @@ main(int argc, char *argv[])
return retval;
}
/* make some big malloc that should trip an unmap */
foo = malloc(size);
printf(" - free of first big buffer\n");
counter = 0;
/* make some big malloc that should always trip a release on free */
printf(" - malloc big buffer\n");
foo = malloc(bigsize);
assert(counter >= 1);
printf(" - free of big buffer\n");
free(foo);
assert(counter == 0);
/* and check munmap directly */
printf(" - munmap of small buffer\n");
/* check mmap / munmap */
printf(" - mmap of buffer\n");
counter = 0;
bar = mmap(NULL, 4096, PROT_READ, MAP_ANON, -1, 0);
assert(counter >= 1);
printf(" - munmap of buffer\n");
/* mmap might call malloc internally, so do this or we might
appear to leak memory */
counter = 1;
munmap(NULL, 0);
/* see if realloc works. This is kind of a huristic (that going
from a small block to a big one will fail), so don't make this
an error */
if (ret == 0) {
ret = 0;
printf(" - realloc\n");
foo = malloc(size);
bar = malloc(10);
foo = realloc(foo, size * 2);
free(bar);
free(foo);
if (ret != 0) {
printf("WARNING - It appears that realloc does not trigger a callback\n");
printf("WARNING - this may be a problem or it may be a sign that your\n");
printf("WARNING - memory manager is better than mine\n");
printf("ret: %d\n", ret);
}
ret = 0;
printf("here\n");
}
assert(counter == 0);
retval = opal_mem_hooks_unregister_release(release_callback);
retval |= opal_mem_hooks_unregister_alloc(alloc_callback);
if (retval != OMPI_SUCCESS) return retval;
return OPAL_SUCCESS;
}
int
main(int argc, char *argv[])
{
int ret;
int support;
opal_init();
/* this printf needs to be here for the test to work! */
printf("running malloc hooks test\n");
support = opal_mem_hooks_support_level();
if (0 == support) {
printf("no memory registration supported. skipping test.\n");
ret = 77;
} else if ((OPAL_MEMORY_FREE_SUPPORT|OPAL_MEMORY_MALLOC_SUPPORT) & support) {
ret = alloc_free_test();
} else if (OPAL_MEMORY_FREE_SUPPORT & support) {
ret = free_only_test();
} else {
printf("Odd support response: %d\n", support);
ret = 1;
}
opal_finalize();
printf("ret: %d\n", ret);
return ret;
}