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.

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

mercurial