Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
duk_alloc_logging.c File Reference
#include "duktape.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

Go to the source code of this file.

Data Structures

struct  alloc_hdr
 

Macros

#define ALLOC_LOG_FILE   "/tmp/duk-alloc-log.txt"
 

Functions

static void write_log (const char *fmt,...)
 
void * duk_alloc_logging (void *udata, duk_size_t size)
 
void * duk_realloc_logging (void *udata, void *ptr, duk_size_t size)
 
void duk_free_logging (void *udata, void *ptr)
 

Variables

static FILE * log_file = NULL
 

Macro Definition Documentation

◆ ALLOC_LOG_FILE

#define ALLOC_LOG_FILE   "/tmp/duk-alloc-log.txt"

Definition at line 23 of file duktape-1.5.2/examples/alloc-logging/duk_alloc_logging.c.

Referenced by write_log().

Function Documentation

◆ duk_alloc_logging()

void * duk_alloc_logging ( void * udata,
duk_size_t size )

Definition at line 53 of file duktape-1.5.2/examples/alloc-logging/duk_alloc_logging.c.

53 {
54 alloc_hdr *hdr;
55 void *ret;
56
57 (void) udata; /* Suppress warning. */
58
59 if (size == 0) {
60 write_log("A NULL %ld\n", (long) size);
61 return NULL;
62 }
63
64 hdr = (alloc_hdr *) malloc(size + sizeof(alloc_hdr));
65 if (!hdr) {
66 write_log("A FAIL %ld\n", (long) size);
67 return NULL;
68 }
69 hdr->u.sz = size;
70 ret = (void *) (hdr + 1);
71 write_log("A %p %ld\n", ret, (long) size);
72 return ret;
73}
#define malloc
Definition civetweb.c:1539
static void write_log(const char *fmt,...)
#define NULL
Definition gmacros.h:924
union alloc_hdr::@15 u

References malloc, NULL, alloc_hdr::sz, alloc_hdr::u, and write_log().

Referenced by create_duktape_heap(), and create_duktape_heap().

◆ duk_free_logging()

void duk_free_logging ( void * udata,
void * ptr )

Definition at line 126 of file duktape-1.5.2/examples/alloc-logging/duk_alloc_logging.c.

126 {
127 alloc_hdr *hdr;
128
129 (void) udata; /* Suppress warning. */
130
131 if (!ptr) {
132 write_log("F NULL 0\n");
133 return;
134 }
135 hdr = (alloc_hdr *) (void *) ((unsigned char *) ptr - sizeof(alloc_hdr));
136 write_log("F %p %ld\n", ptr, (long) hdr->u.sz);
137 free((void *) hdr);
138}
#define free
Definition civetweb.c:1542

References free, alloc_hdr::sz, alloc_hdr::u, and write_log().

Referenced by create_duktape_heap(), and create_duktape_heap().

◆ duk_realloc_logging()

void * duk_realloc_logging ( void * udata,
void * ptr,
duk_size_t size )

Definition at line 75 of file duktape-1.5.2/examples/alloc-logging/duk_alloc_logging.c.

75 {
76 alloc_hdr *hdr;
77 size_t old_size;
78 void *t;
79 void *ret;
80
81 (void) udata; /* Suppress warning. */
82
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.
86 */
87
88 if (ptr) {
89 hdr = (alloc_hdr *) (void *) ((unsigned char *) ptr - sizeof(alloc_hdr));
90 old_size = hdr->u.sz;
91
92 if (size == 0) {
93 write_log("R %p %ld NULL 0\n", ptr, (long) old_size);
94 free((void *) hdr);
95 return NULL;
96 } else {
97 t = realloc((void *) hdr, size + sizeof(alloc_hdr));
98 if (!t) {
99 write_log("R %p %ld FAIL %ld\n", ptr, (long) old_size, (long) size);
100 return NULL;
101 }
102 hdr = (alloc_hdr *) t;
103 hdr->u.sz = size;
104 ret = (void *) (hdr + 1);
105 write_log("R %p %ld %p %ld\n", ptr, (long) old_size, ret, (long) size);
106 return ret;
107 }
108 } else {
109 if (size == 0) {
110 write_log("R NULL 0 NULL 0\n");
111 return NULL;
112 } else {
113 hdr = (alloc_hdr *) malloc(size + sizeof(alloc_hdr));
114 if (!hdr) {
115 write_log("R NULL 0 FAIL %ld\n", (long) size);
116 return NULL;
117 }
118 hdr->u.sz = size;
119 ret = (void *) (hdr + 1);
120 write_log("R NULL 0 %p %ld\n", ret, (long) size);
121 return ret;
122 }
123 }
124}
#define realloc
Definition civetweb.c:1541

References free, malloc, NULL, realloc, alloc_hdr::sz, alloc_hdr::u, and write_log().

Referenced by create_duktape_heap(), and create_duktape_heap().

◆ write_log()

static void write_log ( const char * fmt,
... )
static

Definition at line 38 of file duktape-1.5.2/examples/alloc-logging/duk_alloc_logging.c.

38 {
39 va_list ap;
40
41 if (!log_file) {
42 log_file = fopen(ALLOC_LOG_FILE, "wb");
43 if (!log_file) {
44 return;
45 }
46 }
47
48 va_start(ap, fmt);
49 vfprintf(log_file, fmt, ap);
50 va_end(ap);
51}

References ALLOC_LOG_FILE, and log_file.

Referenced by duk_alloc_logging(), duk_free_logging(), and duk_realloc_logging().

Variable Documentation

◆ log_file

FILE* log_file = NULL
static

Definition at line 36 of file duktape-1.5.2/examples/alloc-logging/duk_alloc_logging.c.

Referenced by write_log().