Skip to content

Commit

Permalink
Add fragmentation_check() to mspace-test
Browse files Browse the repository at this point in the history
  • Loading branch information
00pauln00 committed Aug 27, 2024
1 parent 17ec4e3 commit 645f348
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/contrib/include/dlmalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extern "C" {
/* BEGIN IM specific defs */
#define ONLY_MSPACES 1
#define HAVE_MMAP 0
//#define HAVE_MORECORE 0
/* END IM specific defs */

#ifndef ONLY_MSPACES
Expand Down
43 changes: 41 additions & 2 deletions test/mspace-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,46 @@

REGISTRY_ENTRY_FILE_GENERATE;

static void
fragmentation_check(mspace m)
{
// Perform a simple fragmentation test
void *y[8];

for (int i = 0; i < 8; i++)
{
y[i] = mspace_malloc(m, 1000);
NIOVA_ASSERT(y[i]);

// SIMPLE_LOG_MSG(LL_WARN, "y[%d]=%p", i, y[i]);
}

NIOVA_ASSERT(mspace_malloc(m, 2000) == NULL);

mspace_free(m, y[0]);
mspace_free(m, y[6]);
y[0] = NULL;
y[6] = NULL;

NIOVA_ASSERT(mspace_malloc(m, 2000) == NULL);

mspace_free(m, y[1]); // Free a slot adjacent to one already freed
y[1] = NULL;

void *x = mspace_malloc(m, 2000);
NIOVA_ASSERT(x);
mspace_free(m, x);

for (int i = 0; i < 8; i++)
{
if (y[i])
mspace_free(m, y[i]);
}
}

int
main(void)
{

#if defined(dlmalloc)
FATAL_MSG("dlmalloc is defined when only mspaces should be");
#endif
Expand All @@ -25,13 +61,16 @@ main(void)
m = create_mspace_with_base(p, 10240, 0);
NIOVA_ASSERT(m != NULL);

void *x = mspace_malloc(m, 10240);
void *x = mspace_malloc(m, 10200);
NIOVA_ASSERT(x == NULL);

x = mspace_malloc(m, 1024);
NIOVA_ASSERT(x);

mspace_free(m, x);

fragmentation_check(m);

destroy_mspace(m);
niova_free(p);

Expand Down

0 comments on commit 645f348

Please sign in to comment.