memory/jemalloc/src/test/allocated.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/memory/jemalloc/src/test/allocated.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,118 @@
     1.4 +#define	JEMALLOC_MANGLE
     1.5 +#include "jemalloc_test.h"
     1.6 +
     1.7 +void *
     1.8 +je_thread_start(void *arg)
     1.9 +{
    1.10 +	int err;
    1.11 +	void *p;
    1.12 +	uint64_t a0, a1, d0, d1;
    1.13 +	uint64_t *ap0, *ap1, *dp0, *dp1;
    1.14 +	size_t sz, usize;
    1.15 +
    1.16 +	sz = sizeof(a0);
    1.17 +	if ((err = mallctl("thread.allocated", &a0, &sz, NULL, 0))) {
    1.18 +		if (err == ENOENT) {
    1.19 +#ifdef JEMALLOC_STATS
    1.20 +			assert(false);
    1.21 +#endif
    1.22 +			goto label_return;
    1.23 +		}
    1.24 +		malloc_printf("%s(): Error in mallctl(): %s\n", __func__,
    1.25 +		    strerror(err));
    1.26 +		exit(1);
    1.27 +	}
    1.28 +	sz = sizeof(ap0);
    1.29 +	if ((err = mallctl("thread.allocatedp", &ap0, &sz, NULL, 0))) {
    1.30 +		if (err == ENOENT) {
    1.31 +#ifdef JEMALLOC_STATS
    1.32 +			assert(false);
    1.33 +#endif
    1.34 +			goto label_return;
    1.35 +		}
    1.36 +		malloc_printf("%s(): Error in mallctl(): %s\n", __func__,
    1.37 +		    strerror(err));
    1.38 +		exit(1);
    1.39 +	}
    1.40 +	assert(*ap0 == a0);
    1.41 +
    1.42 +	sz = sizeof(d0);
    1.43 +	if ((err = mallctl("thread.deallocated", &d0, &sz, NULL, 0))) {
    1.44 +		if (err == ENOENT) {
    1.45 +#ifdef JEMALLOC_STATS
    1.46 +			assert(false);
    1.47 +#endif
    1.48 +			goto label_return;
    1.49 +		}
    1.50 +		malloc_printf("%s(): Error in mallctl(): %s\n", __func__,
    1.51 +		    strerror(err));
    1.52 +		exit(1);
    1.53 +	}
    1.54 +	sz = sizeof(dp0);
    1.55 +	if ((err = mallctl("thread.deallocatedp", &dp0, &sz, NULL, 0))) {
    1.56 +		if (err == ENOENT) {
    1.57 +#ifdef JEMALLOC_STATS
    1.58 +			assert(false);
    1.59 +#endif
    1.60 +			goto label_return;
    1.61 +		}
    1.62 +		malloc_printf("%s(): Error in mallctl(): %s\n", __func__,
    1.63 +		    strerror(err));
    1.64 +		exit(1);
    1.65 +	}
    1.66 +	assert(*dp0 == d0);
    1.67 +
    1.68 +	p = malloc(1);
    1.69 +	if (p == NULL) {
    1.70 +		malloc_printf("%s(): Error in malloc()\n", __func__);
    1.71 +		exit(1);
    1.72 +	}
    1.73 +
    1.74 +	sz = sizeof(a1);
    1.75 +	mallctl("thread.allocated", &a1, &sz, NULL, 0);
    1.76 +	sz = sizeof(ap1);
    1.77 +	mallctl("thread.allocatedp", &ap1, &sz, NULL, 0);
    1.78 +	assert(*ap1 == a1);
    1.79 +	assert(ap0 == ap1);
    1.80 +
    1.81 +	usize = malloc_usable_size(p);
    1.82 +	assert(a0 + usize <= a1);
    1.83 +
    1.84 +	free(p);
    1.85 +
    1.86 +	sz = sizeof(d1);
    1.87 +	mallctl("thread.deallocated", &d1, &sz, NULL, 0);
    1.88 +	sz = sizeof(dp1);
    1.89 +	mallctl("thread.deallocatedp", &dp1, &sz, NULL, 0);
    1.90 +	assert(*dp1 == d1);
    1.91 +	assert(dp0 == dp1);
    1.92 +
    1.93 +	assert(d0 + usize <= d1);
    1.94 +
    1.95 +label_return:
    1.96 +	return (NULL);
    1.97 +}
    1.98 +
    1.99 +int
   1.100 +main(void)
   1.101 +{
   1.102 +	int ret = 0;
   1.103 +	je_thread_t thread;
   1.104 +
   1.105 +	malloc_printf("Test begin\n");
   1.106 +
   1.107 +	je_thread_start(NULL);
   1.108 +
   1.109 +	je_thread_create(&thread, je_thread_start, NULL);
   1.110 +	je_thread_join(thread, (void *)&ret);
   1.111 +
   1.112 +	je_thread_start(NULL);
   1.113 +
   1.114 +	je_thread_create(&thread, je_thread_start, NULL);
   1.115 +	je_thread_join(thread, (void *)&ret);
   1.116 +
   1.117 +	je_thread_start(NULL);
   1.118 +
   1.119 +	malloc_printf("Test end\n");
   1.120 +	return (ret);
   1.121 +}

mercurial