ftmemsim-valgrind/massif/tests/custom_alloc.c
Nicholas Nethercote 62eed24c67 Merged the MASSIF2 branch to the trunk. Main changes:
- ms_main.c: completely overhauled.

- massif/tests/*:  lots of them now.

- massif/perf/:  added.

- massif/hp2ps:  removed.  No longer used.

- vg_regtest: renamed the previously unused "posttest" notion to "post".
  Using it for checking ms_print's output.

Although the code has changed dramatically, as has the form of the tool's
output, the information presented in the output is basically the same,
although it's now (hopefully) much more useful.  So the tool name is
unchanged.



git-svn-id: svn://svn.valgrind.org/valgrind/trunk@7069
2007-11-01 04:40:37 +00:00

74 lines
1.5 KiB
C

#include <unistd.h>
#include <sys/mman.h>
#include <assert.h>
#include <stdlib.h>
#include "valgrind.h"
#define SUPERBLOCK_SIZE 100000
//-------------------------------------------------------------------------
// Allocator
//-------------------------------------------------------------------------
void* get_superblock(void)
{
void* p = mmap( 0, SUPERBLOCK_SIZE, PROT_READ|PROT_WRITE|PROT_EXEC,
MAP_PRIVATE|MAP_ANON, -1, 0 );
assert(p != ((void*)(-1)));
return p;
}
// has a redzone
static void* custom_alloc(int size)
{
#define RZ 8
static void* hp = 0; // current heap pointer
static void* hp_lim = 0; // maximum usable byte in current block
int size2 = size + RZ*2;
void* p;
if (hp + size2 > hp_lim) {
hp = get_superblock();
hp_lim = hp + SUPERBLOCK_SIZE - 1;
}
p = hp + RZ;
hp += size2;
VALGRIND_MALLOCLIKE_BLOCK( p, size, RZ, /*is_zeroed*/1 );
return (void*)p;
}
static void custom_free(void* p)
{
// don't actually free any memory... but mark it as freed
VALGRIND_FREELIKE_BLOCK( p, RZ );
}
#undef RZ
//-------------------------------------------------------------------------
// Rest
//-------------------------------------------------------------------------
int main(void)
{
int* a = custom_alloc(100);
custom_free(a);
a = custom_alloc(200);
custom_free(a);
a = malloc(100);
free(a);
a = malloc(200);
free(a);
return 0;
}