]> git.saurik.com Git - redis.git/blob - deps/jemalloc/test/allocated.c
Merge pull request #63 from djanowski/tcl
[redis.git] / deps / jemalloc / test / allocated.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <stdbool.h>
5 #include <pthread.h>
6 #include <assert.h>
7 #include <errno.h>
8 #include <string.h>
9
10 #define JEMALLOC_MANGLE
11 #include "jemalloc_test.h"
12
13 void *
14 thread_start(void *arg)
15 {
16 int err;
17 void *p;
18 uint64_t a0, a1, d0, d1;
19 uint64_t *ap0, *ap1, *dp0, *dp1;
20 size_t sz, usize;
21
22 sz = sizeof(a0);
23 if ((err = JEMALLOC_P(mallctl)("thread.allocated", &a0, &sz, NULL,
24 0))) {
25 if (err == ENOENT) {
26 #ifdef JEMALLOC_STATS
27 assert(false);
28 #endif
29 goto RETURN;
30 }
31 fprintf(stderr, "%s(): Error in mallctl(): %s\n", __func__,
32 strerror(err));
33 exit(1);
34 }
35 sz = sizeof(ap0);
36 if ((err = JEMALLOC_P(mallctl)("thread.allocatedp", &ap0, &sz, NULL,
37 0))) {
38 if (err == ENOENT) {
39 #ifdef JEMALLOC_STATS
40 assert(false);
41 #endif
42 goto RETURN;
43 }
44 fprintf(stderr, "%s(): Error in mallctl(): %s\n", __func__,
45 strerror(err));
46 exit(1);
47 }
48 assert(*ap0 == a0);
49
50 sz = sizeof(d0);
51 if ((err = JEMALLOC_P(mallctl)("thread.deallocated", &d0, &sz, NULL,
52 0))) {
53 if (err == ENOENT) {
54 #ifdef JEMALLOC_STATS
55 assert(false);
56 #endif
57 goto RETURN;
58 }
59 fprintf(stderr, "%s(): Error in mallctl(): %s\n", __func__,
60 strerror(err));
61 exit(1);
62 }
63 sz = sizeof(dp0);
64 if ((err = JEMALLOC_P(mallctl)("thread.deallocatedp", &dp0, &sz, NULL,
65 0))) {
66 if (err == ENOENT) {
67 #ifdef JEMALLOC_STATS
68 assert(false);
69 #endif
70 goto RETURN;
71 }
72 fprintf(stderr, "%s(): Error in mallctl(): %s\n", __func__,
73 strerror(err));
74 exit(1);
75 }
76 assert(*dp0 == d0);
77
78 p = JEMALLOC_P(malloc)(1);
79 if (p == NULL) {
80 fprintf(stderr, "%s(): Error in malloc()\n", __func__);
81 exit(1);
82 }
83
84 sz = sizeof(a1);
85 JEMALLOC_P(mallctl)("thread.allocated", &a1, &sz, NULL, 0);
86 sz = sizeof(ap1);
87 JEMALLOC_P(mallctl)("thread.allocatedp", &ap1, &sz, NULL, 0);
88 assert(*ap1 == a1);
89 assert(ap0 == ap1);
90
91 usize = JEMALLOC_P(malloc_usable_size)(p);
92 assert(a0 + usize <= a1);
93
94 JEMALLOC_P(free)(p);
95
96 sz = sizeof(d1);
97 JEMALLOC_P(mallctl)("thread.deallocated", &d1, &sz, NULL, 0);
98 sz = sizeof(dp1);
99 JEMALLOC_P(mallctl)("thread.deallocatedp", &dp1, &sz, NULL, 0);
100 assert(*dp1 == d1);
101 assert(dp0 == dp1);
102
103 assert(d0 + usize <= d1);
104
105 RETURN:
106 return (NULL);
107 }
108
109 int
110 main(void)
111 {
112 int ret = 0;
113 pthread_t thread;
114
115 fprintf(stderr, "Test begin\n");
116
117 thread_start(NULL);
118
119 if (pthread_create(&thread, NULL, thread_start, NULL)
120 != 0) {
121 fprintf(stderr, "%s(): Error in pthread_create()\n", __func__);
122 ret = 1;
123 goto RETURN;
124 }
125 pthread_join(thread, (void *)&ret);
126
127 thread_start(NULL);
128
129 if (pthread_create(&thread, NULL, thread_start, NULL)
130 != 0) {
131 fprintf(stderr, "%s(): Error in pthread_create()\n", __func__);
132 ret = 1;
133 goto RETURN;
134 }
135 pthread_join(thread, (void *)&ret);
136
137 thread_start(NULL);
138
139 RETURN:
140 fprintf(stderr, "Test end\n");
141 return (ret);
142 }