memory/jemalloc/src/test/allocated.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 #define	JEMALLOC_MANGLE
     2 #include "jemalloc_test.h"
     4 void *
     5 je_thread_start(void *arg)
     6 {
     7 	int err;
     8 	void *p;
     9 	uint64_t a0, a1, d0, d1;
    10 	uint64_t *ap0, *ap1, *dp0, *dp1;
    11 	size_t sz, usize;
    13 	sz = sizeof(a0);
    14 	if ((err = mallctl("thread.allocated", &a0, &sz, NULL, 0))) {
    15 		if (err == ENOENT) {
    16 #ifdef JEMALLOC_STATS
    17 			assert(false);
    18 #endif
    19 			goto label_return;
    20 		}
    21 		malloc_printf("%s(): Error in mallctl(): %s\n", __func__,
    22 		    strerror(err));
    23 		exit(1);
    24 	}
    25 	sz = sizeof(ap0);
    26 	if ((err = mallctl("thread.allocatedp", &ap0, &sz, NULL, 0))) {
    27 		if (err == ENOENT) {
    28 #ifdef JEMALLOC_STATS
    29 			assert(false);
    30 #endif
    31 			goto label_return;
    32 		}
    33 		malloc_printf("%s(): Error in mallctl(): %s\n", __func__,
    34 		    strerror(err));
    35 		exit(1);
    36 	}
    37 	assert(*ap0 == a0);
    39 	sz = sizeof(d0);
    40 	if ((err = mallctl("thread.deallocated", &d0, &sz, NULL, 0))) {
    41 		if (err == ENOENT) {
    42 #ifdef JEMALLOC_STATS
    43 			assert(false);
    44 #endif
    45 			goto label_return;
    46 		}
    47 		malloc_printf("%s(): Error in mallctl(): %s\n", __func__,
    48 		    strerror(err));
    49 		exit(1);
    50 	}
    51 	sz = sizeof(dp0);
    52 	if ((err = mallctl("thread.deallocatedp", &dp0, &sz, NULL, 0))) {
    53 		if (err == ENOENT) {
    54 #ifdef JEMALLOC_STATS
    55 			assert(false);
    56 #endif
    57 			goto label_return;
    58 		}
    59 		malloc_printf("%s(): Error in mallctl(): %s\n", __func__,
    60 		    strerror(err));
    61 		exit(1);
    62 	}
    63 	assert(*dp0 == d0);
    65 	p = malloc(1);
    66 	if (p == NULL) {
    67 		malloc_printf("%s(): Error in malloc()\n", __func__);
    68 		exit(1);
    69 	}
    71 	sz = sizeof(a1);
    72 	mallctl("thread.allocated", &a1, &sz, NULL, 0);
    73 	sz = sizeof(ap1);
    74 	mallctl("thread.allocatedp", &ap1, &sz, NULL, 0);
    75 	assert(*ap1 == a1);
    76 	assert(ap0 == ap1);
    78 	usize = malloc_usable_size(p);
    79 	assert(a0 + usize <= a1);
    81 	free(p);
    83 	sz = sizeof(d1);
    84 	mallctl("thread.deallocated", &d1, &sz, NULL, 0);
    85 	sz = sizeof(dp1);
    86 	mallctl("thread.deallocatedp", &dp1, &sz, NULL, 0);
    87 	assert(*dp1 == d1);
    88 	assert(dp0 == dp1);
    90 	assert(d0 + usize <= d1);
    92 label_return:
    93 	return (NULL);
    94 }
    96 int
    97 main(void)
    98 {
    99 	int ret = 0;
   100 	je_thread_t thread;
   102 	malloc_printf("Test begin\n");
   104 	je_thread_start(NULL);
   106 	je_thread_create(&thread, je_thread_start, NULL);
   107 	je_thread_join(thread, (void *)&ret);
   109 	je_thread_start(NULL);
   111 	je_thread_create(&thread, je_thread_start, NULL);
   112 	je_thread_join(thread, (void *)&ret);
   114 	je_thread_start(NULL);
   116 	malloc_printf("Test end\n");
   117 	return (ret);
   118 }

mercurial