TEST_END TEST_BEGIN(test_alignment_and_size) { int r; size_t nsz, rsz, sz, alignment, total; unsigned i; void *ps[NITER]; for (i = 0; i < NITER; i++) ps[i] = NULL; for (alignment = 8; alignment <= MAXALIGN; alignment <<= 1) { total = 0; for (sz = 1; sz < 3 * alignment && sz < (1U << 31); sz += (alignment >> (LG_SIZEOF_PTR-1)) - 1) { for (i = 0; i < NITER; i++) { nsz = 0; r = nallocm(&nsz, sz, ALLOCM_ALIGN(alignment) | ALLOCM_ZERO); assert_d_eq(r, ALLOCM_SUCCESS, "nallocm() error for alignment=%zu, " "size=%zu (%#zx): %d", alignment, sz, sz, r); rsz = 0; r = allocm(&ps[i], &rsz, sz, ALLOCM_ALIGN(alignment) | ALLOCM_ZERO); assert_d_eq(r, ALLOCM_SUCCESS, "allocm() error for alignment=%zu, " "size=%zu (%#zx): %d", alignment, sz, sz, r); assert_zu_ge(rsz, sz, "Real size smaller than expected for " "alignment=%zu, size=%zu", alignment, sz); assert_zu_eq(nsz, rsz, "nallocm()/allocm() rsize mismatch for " "alignment=%zu, size=%zu", alignment, sz); assert_ptr_null( (void *)((uintptr_t)ps[i] & (alignment-1)), "%p inadequately aligned for" " alignment=%zu, size=%zu", ps[i], alignment, sz); sallocm(ps[i], &rsz, 0); total += rsz; if (total >= (MAXALIGN << 1)) break; } for (i = 0; i < NITER; i++) { if (ps[i] != NULL) { dallocm(ps[i], 0); ps[i] = NULL; } } } } }
NEVER_INLINE void* MemoryManager::smartMallocSizeBigHelper(void*& ptr, size_t& szOut, size_t bytes) { m_stats.usage += bytes; allocm(&ptr, &szOut, debugAddExtra(bytes + sizeof(SweepNode)), 0); szOut = debugRemoveExtra(szOut - sizeof(SweepNode)); return debugPostAllocate( smartEnlist(static_cast<SweepNode*>(ptr)), bytes, szOut ); }
TEST_END TEST_BEGIN(test_no_move_fail) { void *p, *q; size_t sz, tsz; assert_d_eq(allocm(&p, &sz, 42, 0), ALLOCM_SUCCESS, "Unexpected allocm() error"); q = p; assert_d_eq(rallocm(&q, &tsz, sz + 5, 0, ALLOCM_NO_MOVE), ALLOCM_ERR_NOT_MOVED, "Unexpected rallocm() result"); assert_ptr_eq(q, p, "Unexpected object move"); assert_zu_eq(tsz, sz, "Unexpected size change: %zu --> %zu", sz, tsz); assert_d_eq(dallocm(p, 0), ALLOCM_SUCCESS, "Unexpected dallocm() error"); }
TEST_END TEST_BEGIN(test_grow_and_shrink) { void *p, *q; size_t tsz; #define NCYCLES 3 unsigned i, j; #define NSZS 2500 size_t szs[NSZS]; #define MAXSZ ZU(12 * 1024 * 1024) assert_d_eq(allocm(&p, &szs[0], 1, 0), ALLOCM_SUCCESS, "Unexpected allocm() error"); for (i = 0; i < NCYCLES; i++) { for (j = 1; j < NSZS && szs[j-1] < MAXSZ; j++) { q = p; assert_d_eq(rallocm(&q, &szs[j], szs[j-1]+1, 0, 0), ALLOCM_SUCCESS, "Unexpected rallocm() error for size=%zu-->%zu", szs[j-1], szs[j-1]+1); assert_zu_ne(szs[j], szs[j-1]+1, "Expected size to at least: %zu", szs[j-1]+1); p = q; } for (j--; j > 0; j--) { q = p; assert_d_eq(rallocm(&q, &tsz, szs[j-1], 0, 0), ALLOCM_SUCCESS, "Unexpected rallocm() error for size=%zu-->%zu", szs[j], szs[j-1]); assert_zu_eq(tsz, szs[j-1], "Expected size=%zu, got size=%zu", szs[j-1], tsz); p = q; } } assert_d_eq(dallocm(p, 0), ALLOCM_SUCCESS, "Unexpected dallocm() error"); }
NEVER_INLINE void* MemoryManager::smartMallocSizeBigHelper(void*& ptr, size_t& szOut, size_t bytes) { #ifdef USE_JEMALLOC_MALLOCX ptr = mallocx(debugAddExtra(bytes + sizeof(SweepNode)), 0); szOut = debugRemoveExtra(sallocx(ptr, 0) - sizeof(SweepNode)); #else allocm(&ptr, &szOut, debugAddExtra(bytes + sizeof(SweepNode)), 0); szOut = debugRemoveExtra(szOut - sizeof(SweepNode)); #endif // NB: We don't report the SweepNode size in the stats. auto const delta = callerSavesActualSize ? szOut : bytes; m_stats.usage += int64_t(delta); // Adjust jemalloc otherwise we'll double count the direct allocation. JEMALLOC_STATS_ADJUST(&m_stats, delta); return debugPostAllocate( smartEnlist(static_cast<SweepNode*>(ptr)), bytes, szOut ); }
int main(void) { size_t pagesize; void *p, *q; size_t sz, tsz; int r; malloc_printf("Test begin\n"); /* Get page size. */ { #ifdef _WIN32 SYSTEM_INFO si; GetSystemInfo(&si); pagesize = (size_t)si.dwPageSize; #else long result = sysconf(_SC_PAGESIZE); assert(result != -1); pagesize = (size_t)result; #endif } r = allocm(&p, &sz, 42, 0); if (r != ALLOCM_SUCCESS) { malloc_printf("Unexpected allocm() error\n"); abort(); } q = p; r = rallocm(&q, &tsz, sz, 0, ALLOCM_NO_MOVE); if (r != ALLOCM_SUCCESS) malloc_printf("Unexpected rallocm() error\n"); if (q != p) malloc_printf("Unexpected object move\n"); if (tsz != sz) { malloc_printf("Unexpected size change: %zu --> %zu\n", sz, tsz); } q = p; r = rallocm(&q, &tsz, sz, 5, ALLOCM_NO_MOVE); if (r != ALLOCM_SUCCESS) malloc_printf("Unexpected rallocm() error\n"); if (q != p) malloc_printf("Unexpected object move\n"); if (tsz != sz) { malloc_printf("Unexpected size change: %zu --> %zu\n", sz, tsz); } q = p; r = rallocm(&q, &tsz, sz + 5, 0, ALLOCM_NO_MOVE); if (r != ALLOCM_ERR_NOT_MOVED) malloc_printf("Unexpected rallocm() result\n"); if (q != p) malloc_printf("Unexpected object move\n"); if (tsz != sz) { malloc_printf("Unexpected size change: %zu --> %zu\n", sz, tsz); } q = p; r = rallocm(&q, &tsz, sz + 5, 0, 0); if (r != ALLOCM_SUCCESS) malloc_printf("Unexpected rallocm() error\n"); if (q == p) malloc_printf("Expected object move\n"); if (tsz == sz) { malloc_printf("Expected size change: %zu --> %zu\n", sz, tsz); } p = q; sz = tsz; r = rallocm(&q, &tsz, pagesize*2, 0, 0); if (r != ALLOCM_SUCCESS) malloc_printf("Unexpected rallocm() error\n"); if (q == p) malloc_printf("Expected object move\n"); if (tsz == sz) { malloc_printf("Expected size change: %zu --> %zu\n", sz, tsz); } p = q; sz = tsz; r = rallocm(&q, &tsz, pagesize*4, 0, 0); if (r != ALLOCM_SUCCESS) malloc_printf("Unexpected rallocm() error\n"); if (tsz == sz) { malloc_printf("Expected size change: %zu --> %zu\n", sz, tsz); } p = q; sz = tsz; r = rallocm(&q, &tsz, pagesize*2, 0, ALLOCM_NO_MOVE); if (r != ALLOCM_SUCCESS) malloc_printf("Unexpected rallocm() error\n"); if (q != p) malloc_printf("Unexpected object move\n"); if (tsz == sz) { malloc_printf("Expected size change: %zu --> %zu\n", sz, tsz); } sz = tsz; r = rallocm(&q, &tsz, pagesize*4, 0, ALLOCM_NO_MOVE); if (r != ALLOCM_SUCCESS) malloc_printf("Unexpected rallocm() error\n"); if (q != p) malloc_printf("Unexpected object move\n"); if (tsz == sz) { malloc_printf("Expected size change: %zu --> %zu\n", sz, tsz); } sz = tsz; dallocm(p, 0); malloc_printf("Test end\n"); return (0); }