]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/third_party/prometheus-cpp/3rdparty/civetweb/src/third_party/duktape-1.5.2/examples/alloc-logging/duk_alloc_logging.c
2 * Example memory allocator with machine parseable logging.
4 * Also sizes for reallocs and frees are logged so that the memory
5 * behavior can be essentially replayed to accurately determine e.g.
6 * optimal pool sizes for a pooled allocator.
8 * Allocation structure:
10 * [ alloc_hdr | user area ]
13 * | `--- pointer returned to Duktape
14 * `--- underlying malloc ptr
23 #define ALLOC_LOG_FILE "/tmp/duk-alloc-log.txt"
26 /* The double value in the union is there to ensure alignment is
27 * good for IEEE doubles too. In many 32-bit environments 4 bytes
28 * would be sufficiently aligned and the double value is unnecessary.
36 static FILE *log_file
= NULL
;
38 static void write_log(const char *fmt
, ...) {
42 log_file
= fopen(ALLOC_LOG_FILE
, "wb");
49 vfprintf(log_file
, fmt
, ap
);
53 void *duk_alloc_logging(void *udata
, duk_size_t size
) {
57 (void) udata
; /* Suppress warning. */
60 write_log("A NULL %ld\n", (long) size
);
64 hdr
= (alloc_hdr
*) malloc(size
+ sizeof(alloc_hdr
));
66 write_log("A FAIL %ld\n", (long) size
);
70 ret
= (void *) (hdr
+ 1);
71 write_log("A %p %ld\n", ret
, (long) size
);
75 void *duk_realloc_logging(void *udata
, void *ptr
, duk_size_t size
) {
81 (void) udata
; /* Suppress warning. */
83 /* Handle the ptr-NULL vs. size-zero cases explicitly to minimize
84 * platform assumptions. You can get away with much less in specific
85 * well-behaving environments.
89 hdr
= (alloc_hdr
*) (void *) ((unsigned char *) ptr
- sizeof(alloc_hdr
));
93 write_log("R %p %ld NULL 0\n", ptr
, (long) old_size
);
97 t
= realloc((void *) hdr
, size
+ sizeof(alloc_hdr
));
99 write_log("R %p %ld FAIL %ld\n", ptr
, (long) old_size
, (long) size
);
102 hdr
= (alloc_hdr
*) t
;
104 ret
= (void *) (hdr
+ 1);
105 write_log("R %p %ld %p %ld\n", ptr
, (long) old_size
, ret
, (long) size
);
110 write_log("R NULL 0 NULL 0\n");
113 hdr
= (alloc_hdr
*) malloc(size
+ sizeof(alloc_hdr
));
115 write_log("R NULL 0 FAIL %ld\n", (long) size
);
119 ret
= (void *) (hdr
+ 1);
120 write_log("R NULL 0 %p %ld\n", ret
, (long) size
);
126 void duk_free_logging(void *udata
, void *ptr
) {
129 (void) udata
; /* Suppress warning. */
132 write_log("F NULL 0\n");
135 hdr
= (alloc_hdr
*) (void *) ((unsigned char *) ptr
- sizeof(alloc_hdr
));
136 write_log("F %p %ld\n", ptr
, (long) hdr
->u
.sz
);