e576bb2002-10-11Martin Nilsson /* || This file is part of Pike. For copyright information see COPYRIGHT. || Pike is distributed under GPL, LGPL and MPL. See the file COPYING || for more information. */
61e9a01998-01-25Fredrik Hübinette (Hubbe) 
fbacc22004-04-03Martin Stjernholm #ifndef DMALLOC_H #define DMALLOC_H
80a8912001-10-04Martin Nilsson PMOD_EXPORT extern void *debug_xalloc(size_t);
f1792d2000-12-13Fredrik Hübinette (Hubbe) PMOD_EXPORT extern void debug_xfree(void *); PMOD_EXPORT extern void *debug_xmalloc(size_t); PMOD_EXPORT extern void *debug_xcalloc(size_t,size_t); PMOD_EXPORT extern void *debug_xrealloc(void *,size_t);
265d602008-06-28Martin Stjernholm PMOD_EXPORT char *debug_xstrdup(const char *src);
20f7a02000-03-20Fredrik Hübinette (Hubbe) 
3586b42008-09-04Martin Stjernholm #if defined (HAVE_EXECINFO_H) && defined (HAVE_BACKTRACE)
e2d33e2008-06-24Martin Stjernholm /* GNU libc provides some tools to inspect the stack. */ #include <execinfo.h> typedef void *c_stack_frame;
c5872d2008-08-05Martin Stjernholm  #define DUMP_C_STACK_TRACE() do { \ c_stack_frame bt[100]; \ int n = backtrace (bt, 100); \ backtrace_symbols_fd (bt, n, 2); \ } while (0) #else
3586b42008-09-04Martin Stjernholm #undef DMALLOC_C_STACK_TRACE
c5872d2008-08-05Martin Stjernholm #define DUMP_C_STACK_TRACE() do {} while (0)
e2d33e2008-06-24Martin Stjernholm #endif
24bce62003-03-14Henrik Grubbström (Grubba) #define DMALLOC_NAMED_LOCATION(NAME) \ (("NS" __FILE__ ":" DEFINETOSTR(__LINE__) NAME )+1) #define DMALLOC_LOCATION() DMALLOC_NAMED_LOCATION("")
20f7a02000-03-20Fredrik Hübinette (Hubbe) 
e2d33e2008-06-24Martin Stjernholm /* Location types in loc[0]: * 'S': static (DMALLOC_NAMED_LOCATION) * 'D': dynamic (dynamic_location) * 'B': dynamic with backtrace (debug_malloc_update_location_bt) * 'T': memory map template * 'M': memory map */
fbacc22004-04-03Martin Stjernholm typedef char *LOCATION;
e2d33e2008-06-24Martin Stjernholm #define LOCATION_TYPE(X) ((X)[0])
fbacc22004-04-03Martin Stjernholm #define LOCATION_NAME(X) ((X)+1)
e2d33e2008-06-24Martin Stjernholm #define LOCATION_IS_DYNAMIC(X) \ (LOCATION_TYPE (X)=='D' || LOCATION_TYPE (X) == 'B')
fbacc22004-04-03Martin Stjernholm 
e9670c2001-01-30Fredrik Hübinette (Hubbe) #ifdef DMALLOC_TRACE #define DMALLOC_TRACELOGSIZE 131072 extern char *dmalloc_tracelog[DMALLOC_TRACELOGSIZE]; extern size_t dmalloc_tracelogptr; #define DMALLOC_TRACE_LOG(X) (dmalloc_tracelog[ dmalloc_tracelogptr = (dmalloc_tracelogptr +1 )%DMALLOC_TRACELOGSIZE ] = (X))
bd29772002-10-27Martin Nilsson #endif /* DMALLOC_TRACE */
e9670c2001-01-30Fredrik Hübinette (Hubbe) 
31a8682004-09-27Martin Stjernholm #ifdef PIKE_DEBUG
a5a3342006-07-05Martin Stjernholm PMOD_EXPORT extern int gc_external_refs_zapped;
ef24a82012-01-12Henrik Grubbström (Grubba) PMOD_EXPORT void gc_check_zapped (void *a, TYPE_T type, const char *file, INT_TYPE line);
31a8682004-09-27Martin Stjernholm #endif #ifdef DO_PIKE_CLEANUP extern int exit_with_cleanup;
a91a3c2006-01-24Martin Stjernholm extern int exit_cleanup_in_progress;
78a4782004-09-27Martin Stjernholm #define DO_IF_PIKE_CLEANUP(X) X
6ff38a2004-04-03Martin Stjernholm #else
78a4782004-09-27Martin Stjernholm #define DO_IF_PIKE_CLEANUP(X)
6ff38a2004-04-03Martin Stjernholm #endif
8e5c3f2004-04-03Martin Stjernholm typedef void describe_block_fn (void *);
1e06921998-10-12Fredrik Hübinette (Hubbe) #ifdef DEBUG_MALLOC
2043ba1998-02-10Fredrik Hübinette (Hubbe) struct memhdr; void dump_memhdr_locations(struct memhdr *from,
a4033e2000-04-14Fredrik Hübinette (Hubbe)  struct memhdr *notfrom, int indent);
2043ba1998-02-10Fredrik Hübinette (Hubbe) struct memhdr *alloc_memhdr(void);
4218011999-01-31Fredrik Hübinette (Hubbe) void really_free_memhdr(struct memhdr *mh);
2043ba1998-02-10Fredrik Hübinette (Hubbe) void add_marks_to_memhdr(struct memhdr *to,void *ptr);
61e9a01998-01-25Fredrik Hübinette (Hubbe) extern int verbose_debug_malloc;
11a5af2006-08-06Martin Stjernholm PMOD_EXPORT void dmalloc_trace(void *); PMOD_EXPORT void dmalloc_register(void *, int, LOCATION); PMOD_EXPORT int dmalloc_unregister(void *, int); PMOD_EXPORT void *debug_malloc(size_t, LOCATION); PMOD_EXPORT void *debug_calloc(size_t, size_t, LOCATION); PMOD_EXPORT void *debug_realloc(void *, size_t, LOCATION); PMOD_EXPORT void debug_free(void *, LOCATION, int); PMOD_EXPORT char *debug_strdup(const char *, LOCATION);
8f797a2006-08-05Martin Stjernholm void reset_debug_malloc(void);
11a5af2006-08-06Martin Stjernholm PMOD_EXPORT int dmalloc_check_allocated (void *p, int must_be_freed);
19f2dc2006-01-24Martin Stjernholm #if 0
8e5c3f2004-04-03Martin Stjernholm void dmalloc_check_block_free(void *, LOCATION, char *, describe_block_fn *);
19f2dc2006-01-24Martin Stjernholm #endif
11a5af2006-08-06Martin Stjernholm PMOD_EXPORT void dmalloc_free(void *p); PMOD_EXPORT int debug_malloc_touch_fd(int, LOCATION); PMOD_EXPORT int debug_malloc_register_fd(int, LOCATION); PMOD_EXPORT void debug_malloc_accept_leak_fd(int); PMOD_EXPORT int debug_malloc_close_fd(int, LOCATION); PMOD_EXPORT int dmalloc_mark_as_free(void*,int); PMOD_EXPORT void *debug_malloc_update_location(void *, LOCATION); PMOD_EXPORT void *debug_malloc_update_location_ptr(void *, ptrdiff_t, LOCATION);
e2d33e2008-06-24Martin Stjernholm PMOD_EXPORT void *debug_malloc_update_location_bt (void *p, const char *file,
ef24a82012-01-12Henrik Grubbström (Grubba)  INT_TYPE line, const char *name);
25479a2000-03-07Fredrik Hübinette (Hubbe) void search_all_memheaders_for_references(void);
8975b81999-10-18Fredrik Hübinette (Hubbe)  /* Beware! names of named memory regions are never ever freed!! /Hubbe */
ef24a82012-01-12Henrik Grubbström (Grubba) PMOD_EXPORT void *debug_malloc_name(void *p, const char *fn, INT_TYPE line);
11a5af2006-08-06Martin Stjernholm PMOD_EXPORT int debug_malloc_copy_names(void *p, void *p2);
1e4e5f2000-04-07Fredrik Hübinette (Hubbe) char *dmalloc_find_name(void *p);
8975b81999-10-18Fredrik Hübinette (Hubbe) 
2e66531999-10-30Martin Stjernholm /* glibc 2.1 defines this as a macro. */ #ifdef strdup #undef strdup #endif
24bce62003-03-14Henrik Grubbström (Grubba) #define malloc(x) debug_malloc((x), DMALLOC_NAMED_LOCATION(" malloc")) #define calloc(x, y) debug_calloc((x), (y), DMALLOC_NAMED_LOCATION(" calloc")) #define realloc(x, y) debug_realloc((x), (y), DMALLOC_NAMED_LOCATION(" realloc")) #define free(x) debug_free((x), DMALLOC_NAMED_LOCATION(" free"),0) #define dmfree(x) debug_free((x),DMALLOC_NAMED_LOCATION(" free"),1) #define strdup(x) debug_strdup((x), DMALLOC_NAMED_LOCATION(" strdup"))
61e9a01998-01-25Fredrik Hübinette (Hubbe) #define DO_IF_DMALLOC(X) X
f02e912002-12-01Martin Stjernholm #define DO_IF_NOT_DMALLOC(X)
33a10b2003-02-21Martin Stjernholm #define debug_malloc_touch(X) debug_malloc_update_location((void *)(X),DMALLOC_LOCATION())
cd7a492006-02-25Martin Stjernholm #define debug_malloc_touch_named(X,NAME) debug_malloc_update_location((void *)(X),DMALLOC_NAMED_LOCATION(" " NAME))
33a10b2003-02-21Martin Stjernholm #define debug_malloc_pass(X) debug_malloc_update_location((void *)(X),DMALLOC_LOCATION())
cd7a492006-02-25Martin Stjernholm #define debug_malloc_pass_named(X,NAME) debug_malloc_update_location((void *)(X),DMALLOC_NAMED_LOCATION(" " NAME))
33a10b2003-02-21Martin Stjernholm #define dmalloc_touch_struct_ptr(TYPE,X,MEMBER) ((TYPE)debug_malloc_update_location_ptr((void *)(X), ((ptrdiff_t)& (((TYPE)0)->MEMBER)), DMALLOC_LOCATION()))
54717e2001-06-28Fredrik Hübinette (Hubbe) 
e2d33e2008-06-24Martin Stjernholm /* These also save the call stack if support for that exists. Beware * that this can take a lot of extra memory - for temporary use only. */ #define debug_malloc_touch_bt(X) debug_malloc_update_location_bt ((void *)(X), __FILE__, __LINE__, NULL) #define debug_malloc_touch_named_bt(X,NAME) debug_malloc_update_location_bt ((void *)(X), __FILE__, __LINE__, (NAME))
24bce62003-03-14Henrik Grubbström (Grubba) #define xalloc(X) ((void *)debug_malloc_update_location((void *)debug_xalloc(X), DMALLOC_NAMED_LOCATION(" xalloc")))
ecdc952010-07-01Henrik Grubbström (Grubba) #define xcalloc(N, S) ((void *)debug_malloc_update_location((void *)debug_xcalloc(N, S), DMALLOC_NAMED_LOCATION(" xcalloc")))
24bce62003-03-14Henrik Grubbström (Grubba) #define xfree(X) debug_xfree(debug_malloc_update_location((X), DMALLOC_NAMED_LOCATION(" free")))
11a5af2006-08-06Martin Stjernholm PMOD_EXPORT void debug_malloc_dump_references(void *x, int indent, int depth, int flags);
382db82006-02-24Martin Stjernholm #define dmalloc_touch(TYPE,X) ((TYPE) debug_malloc_pass (X)) #define dmalloc_touch_named(TYPE,X,NAME) ((TYPE) debug_malloc_pass_named (X, NAME))
b4519f2001-07-18Fredrik Hübinette (Hubbe) void debug_malloc_dump_fd(int fd);
017b572011-10-28Henrik Grubbström (Grubba) #define dmalloc_touch_svalue(X) do { \ const struct svalue *_tmp = (X); \ if (TYPEOF(*_tmp) <= MAX_REF_TYPE) { \ debug_malloc_touch(_tmp->u.refs); \ } \ } while(0) #define dmalloc_touch_svalue_named(X,NAME) do { \ const struct svalue *_tmp = (X); \ if (TYPEOF(*_tmp) <= MAX_REF_TYPE) { \ debug_malloc_touch_named(_tmp->u.refs,NAME); \ } \ } while(0)
424d9c1999-05-02Fredrik Hübinette (Hubbe) 
20f7a02000-03-20Fredrik Hübinette (Hubbe) #define DMALLOC_LINE_ARGS ,char * dmalloc_location #define DMALLOC_POS ,DMALLOC_LOCATION() #define DMALLOC_PROXY_ARGS ,dmalloc_location
11a5af2006-08-06Martin Stjernholm PMOD_EXPORT void dmalloc_accept_leak(void *);
20f7a02000-03-20Fredrik Hübinette (Hubbe) #define dmalloc_touch_fd(X) debug_malloc_touch_fd((X),DMALLOC_LOCATION()) #define dmalloc_register_fd(X) debug_malloc_register_fd((X),DMALLOC_LOCATION())
97ce632004-09-02Henrik Grubbström (Grubba) #define dmalloc_accept_leak_fd(X) debug_malloc_accept_leak_fd(X)
20f7a02000-03-20Fredrik Hübinette (Hubbe) #define dmalloc_close_fd(X) debug_malloc_close_fd((X),DMALLOC_LOCATION())
1e4e5f2000-04-07Fredrik Hübinette (Hubbe)  /* Beware, these do not exist without DMALLOC */ struct memory_map; void dmalloc_set_mmap(void *ptr, struct memory_map *m); void dmalloc_set_mmap_template(void *ptr, struct memory_map *m); void dmalloc_set_mmap_from_template(void *p, void *p2);
a4033e2000-04-14Fredrik Hübinette (Hubbe) void dmalloc_describe_location(void *p, int offset, int indent);
ef24a82012-01-12Henrik Grubbström (Grubba) struct memory_map *dmalloc_alloc_mmap(char *name, INT_TYPE line);
1e4e5f2000-04-07Fredrik Hübinette (Hubbe) void dmalloc_add_mmap_entry(struct memory_map *m, char *name, int offset, int size, int count, struct memory_map *recur, int recur_offset);
a1a8c92000-09-03Martin Stjernholm int dmalloc_is_invalid_memory_block(void *block);
1e4e5f2000-04-07Fredrik Hübinette (Hubbe) 
bd29772002-10-27Martin Nilsson #else /* DEBUG_MALLOC */
8f797a2006-08-05Martin Stjernholm #ifdef USE_DL_MALLOC PMOD_EXPORT void* dlmalloc(size_t); PMOD_EXPORT void dlfree(void*); PMOD_EXPORT void* dlcalloc(size_t, size_t); PMOD_EXPORT void* dlrealloc(void*, size_t); PMOD_EXPORT void* dlmemalign(size_t, size_t); PMOD_EXPORT void* dlvalloc(size_t); PMOD_EXPORT void* dlpvalloc(size_t);
b249792012-08-20Martin Stjernholm PMOD_EXPORT struct mallinfo dlmallinfo(void);
8f797a2006-08-05Martin Stjernholm #define malloc dlmalloc #define free dlfree #define calloc dlcalloc #define realloc dlrealloc #define memalign dlmemalign #define valloc dlvalloc #define pvalloc dlpvalloc
b249792012-08-20Martin Stjernholm /* No define for mallinfo since there's a struct with the same name. */
8f797a2006-08-05Martin Stjernholm #ifdef strdup #undef strdup #endif #define strdup debug_xstrdup #endif
ec7d6d1999-05-13Fredrik Hübinette (Hubbe) #define dmalloc_touch_fd(X) (X) #define dmalloc_register_fd(X) (X)
97ce632004-09-02Henrik Grubbström (Grubba) #define dmalloc_accept_leak_fd(X)
ec7d6d1999-05-13Fredrik Hübinette (Hubbe) #define dmalloc_close_fd(X) (X)
424d9c1999-05-02Fredrik Hübinette (Hubbe) #define dmfree(X) free((X))
4093941999-05-03Fredrik Hübinette (Hubbe) #define dmalloc_accept_leak(X) (void)(X)
424d9c1999-05-02Fredrik Hübinette (Hubbe) #define DMALLOC_LINE_ARGS #define DMALLOC_POS #define DMALLOC_PROXY_ARGS
a4033e2000-04-14Fredrik Hübinette (Hubbe) #define debug_malloc_dump_references(X,x,y,z)
b4519f2001-07-18Fredrik Hübinette (Hubbe) #define debug_malloc_dump_fd(fd)
24dd881998-01-27Fredrik Hübinette (Hubbe) #define xalloc debug_xalloc
ecdc952010-07-01Henrik Grubbström (Grubba) #define xcalloc debug_xcalloc
bd29772002-10-27Martin Nilsson 
8f797a2006-08-05Martin Stjernholm #if defined(DYNAMIC_MODULE) && defined(__NT__) && !defined(USE_DLL)
f1792d2000-12-13Fredrik Hübinette (Hubbe) #define xmalloc debug_xmalloc #define xrealloc debug_xrealloc #define xfree debug_xfree
8f797a2006-08-05Martin Stjernholm #define xstrdup debug_xstrdup #else
f1792d2000-12-13Fredrik Hübinette (Hubbe) #define xmalloc malloc #define xrealloc realloc #define xfree free
8f797a2006-08-05Martin Stjernholm #define xstrdup strdup #endif
bd29772002-10-27Martin Nilsson 
3c0c281998-01-26Fredrik Hübinette (Hubbe) #define dbm_main main
61e9a01998-01-25Fredrik Hübinette (Hubbe) #define DO_IF_DMALLOC(X)
f02e912002-12-01Martin Stjernholm #define DO_IF_NOT_DMALLOC(X) X
e07d632002-11-30Henrik Grubbström (Grubba) #define dmalloc_trace(X)
10434f2000-03-26Fredrik Hübinette (Hubbe) #define dmalloc_register(X,Y,Z)
719a4f2008-05-28Martin Stjernholm #define dmalloc_unregister(X,Y) 1
e7f8392000-03-21Martin Stjernholm #define debug_free(X,Y,Z) free((X))
8975b81999-10-18Fredrik Hübinette (Hubbe) #define debug_malloc_name(P,FN,LINE)
1e4e5f2000-04-07Fredrik Hübinette (Hubbe) #define debug_malloc_copy_names(p,p2) 0
25479a2000-03-07Fredrik Hübinette (Hubbe) #define search_all_memheaders_for_references()
1e4e5f2000-04-07Fredrik Hübinette (Hubbe) #define dmalloc_find_name(X) "unknown (no dmalloc)"
54717e2001-06-28Fredrik Hübinette (Hubbe) #define dmalloc_touch_struct_ptr(TYPE,X,MEMBER) (X)
e9670c2001-01-30Fredrik Hübinette (Hubbe)  #ifdef DMALLOC_TRACE #define debug_malloc_update_location(X,Y) (DMALLOC_TRACE_LOG(DMALLOC_LOCATION()),(X)) #define dmalloc_touch_svalue(X) DMALLOC_TRACE_LOG(DMALLOC_LOCATION())
2216152006-02-25Martin Stjernholm #define dmalloc_touch_svalue_named(X,NAME) DMALLOC_TRACE_LOG(DMALLOC_NAMED_LOCATION(" " NAME))
e9670c2001-01-30Fredrik Hübinette (Hubbe) #define debug_malloc_touch(X) DMALLOC_TRACE_LOG(DMALLOC_LOCATION())
2216152006-02-25Martin Stjernholm #define debug_malloc_touch_named(X,NAME) DMALLOC_TRACE_LOG(DMALLOC_NAMED_LOCATION(" " NAME))
e9670c2001-01-30Fredrik Hübinette (Hubbe) #define debug_malloc_pass(X) (DMALLOC_TRACE_LOG(DMALLOC_LOCATION()),(X))
2216152006-02-25Martin Stjernholm #define debug_malloc_pass_named(X,NAME) (DMALLOC_TRACE_LOG(DMALLOC_NAMED_LOCATION(" " NAME)), (X))
e2d33e2008-06-24Martin Stjernholm #define debug_malloc_touch_bt(X) DMALLOC_TRACE_LOG(DMALLOC_LOCATION()) #define debug_malloc_touch_named_bt(X,NAME) DMALLOC_TRACE_LOG(DMALLOC_NAMED_LOCATION(" " NAME))
2216152006-02-25Martin Stjernholm #define dmalloc_touch(TYPE,X) debug_malloc_pass (X) #define dmalloc_touch_named(TYPE,X,NAME) debug_malloc_pass_named (X, NAME)
bd29772002-10-27Martin Nilsson #else /* DMALLOC_TRACE */
e9670c2001-01-30Fredrik Hübinette (Hubbe) #define debug_malloc_update_location(X,Y) (X) #define dmalloc_touch_svalue(X)
2216152006-02-25Martin Stjernholm #define dmalloc_touch_svalue_named(X,NAME)
e9670c2001-01-30Fredrik Hübinette (Hubbe) #define debug_malloc_touch(X)
2216152006-02-25Martin Stjernholm #define debug_malloc_touch_named(X,NAME)
e9670c2001-01-30Fredrik Hübinette (Hubbe) #define debug_malloc_pass(X) (X)
2216152006-02-25Martin Stjernholm #define debug_malloc_pass_named(X,NAME) (X)
e2d33e2008-06-24Martin Stjernholm #define debug_malloc_touch_bt(X) #define debug_malloc_touch_named_bt(X,NAME)
e9670c2001-01-30Fredrik Hübinette (Hubbe) #define dmalloc_touch(TYPE,X) (X)
2216152006-02-25Martin Stjernholm #define dmalloc_touch_named(TYPE,X,NAME) (X)
bd29772002-10-27Martin Nilsson #endif /* !MALLOC_TRACE */
e9670c2001-01-30Fredrik Hübinette (Hubbe) 
bd29772002-10-27Martin Nilsson #endif /* !DEBUG_MALLOC */
fbacc22004-04-03Martin Stjernholm  #endif /* !DMALLOC_H */