11-06-2008
GCC has a number of hooks which can be used to modify the behavior of malloc, realloc, and free by specifying appropriate hook functions. Check out __malloc_hook, __realloc_hook, etc. You can do heap consistency checking using mcheck() and probing using mprobe(). You can use the mallinfo structure to return information about the allocated memory and mallopt to tune memory allocation. However to implement garbage collection in C you are going to have to basically replace malloc and not just "modify" it. Not for the faint hearted!
There are a number of publicly available libraries which implement garbage collection generally using some variation of a mark-sweep algorithm. These libraries allow you to allocate memory as you normally would, i.e. using C malloc or C++ new, without the need to explicitly deallocate memory. One such implementation is the Boehm conservative garbage collector. Have a look at the source code to get an idea of the algorithms used. Another is the Solaris libgc library.
10 More Discussions You Might Find Interesting
1. Programming
Hey,
I want to access oracle database through Unix C programming.. Can you through me some light on that... (5 Replies)
Discussion started by: kavi
5 Replies
2. Shell Programming and Scripting
hi
I wanted to access the C program variables in shell script.
This script is called from the same C program.
What are the ways in which i can access variables
thankx (3 Replies)
Discussion started by: bhakti
3 Replies
3. Programming
Is there a way to find the address of stack memory writing a program? Please guide me (12 Replies)
Discussion started by: jacques83
12 Replies
4. Programming
I have read a number of references to libraries that could be linked into a C program to access various databases. I have been tasked with writing an oracle library that would be able to access an Microsoft access database. The oracle database is running on a Unix server and would have to access... (2 Replies)
Discussion started by: beilstwh
2 Replies
5. Filesystems, Disks and Memory
I'm on solaris 8. I need to check the stack trace inside my C program. I don't have printstack or walkstack. I tested getcontext and it works. But how do I get the symbols from "stack_t" ? Help please. Many thanks! (4 Replies)
Discussion started by: rydahl
4 Replies
6. Shell Programming and Scripting
Hello,
I was wondering if I have something in my bin dir and I want to access it from another directory to make a change how can I go about it. Thank you. (7 Replies)
Discussion started by: gingburg
7 Replies
7. UNIX for Dummies Questions & Answers
Hi,
I need to install a program on my Centos 5.3 server that will block unauthorized ssh/ftp access attempts. The two features I require is that I should be able to configure the program to block the IP of the intruder after a a certain amount of access attempts and that it should display a... (3 Replies)
Discussion started by: mojoman
3 Replies
8. Programming
I need to know what is the maximum stack size i.e. -Xss my java program is running with.
Is there a way to find that out from inside my java program code and outside of it.
What i am looking for is to read whatever the current set max limit -Xss (stack sie) is for a particular JVM(not... (3 Replies)
Discussion started by: mohtashims
3 Replies
9. Programming
Hi All,
I am new to c programming. I am getting compilation error in the below program. Can somebody help me?
#include<stdio.h>
#include<string.h>
void main()
{
int i=j=0;
char a={'f1',4,'f2','4'};
char count;
for(i=0;i<2;i++)
{
for(j=1;j<=2;j++)
{
... (2 Replies)
Discussion started by: sam_14189
2 Replies
10. Programming
Hi,
I am working in UEFI EDK2 Bios source. We created a platform related new package in the EDK2 source. I find a strange issue with the platform related code we added.
When I did source level debugging I noticed the
local variable in a C function is not getting created in stack when its... (6 Replies)
Discussion started by: Divya R
6 Replies
LEARN ABOUT LINUX
__malloc_initialize_hook
MALLOC_HOOK(3) Linux Programmer's Manual MALLOC_HOOK(3)
NAME
__malloc_hook, __malloc_initialize_hook, __memalign_hook, __free_hook, __realloc_hook, __after_morecore_hook - malloc debugging variables
SYNOPSIS
#include <malloc.h>
void *(*__malloc_hook)(size_t size, const void *caller);
void *(*__realloc_hook)(void *ptr, size_t size, const void *caller);
void *(*__memalign_hook)(size_t alignment, size_t size,
const void *caller);
void (*__free_hook)(void *ptr, const void *caller);
void (*__malloc_initialize_hook)(void);
void (*__after_morecore_hook)(void);
DESCRIPTION
The GNU C library lets you modify the behavior of malloc(3), realloc(3), and free(3) by specifying appropriate hook functions. You can use
these hooks to help you debug programs that use dynamic memory allocation, for example.
The variable __malloc_initialize_hook points at a function that is called once when the malloc implementation is initialized. This is a
weak variable, so it can be overridden in the application with a definition like the following:
void (*__malloc_initialize_hook)(void) = my_init_hook;
Now the function my_init_hook() can do the initialization of all hooks.
The four functions pointed to by __malloc_hook, __realloc_hook, __memalign_hook, __free_hook have a prototype like the functions malloc(3),
realloc(3), memalign(3), free(3), respectively, except that they have a final argument caller that gives the address of the caller of mal-
loc(3), etc.
The variable __after_morecore_hook points at a function that is called each time after sbrk(2) was asked for more memory.
CONFORMING TO
These functions are GNU extensions.
EXAMPLE
Here is a short example of how to use these variables.
#include <stdio.h>
#include <malloc.h>
/* Prototypes for our hooks. */
static void my_init_hook(void);
static void *my_malloc_hook(size_t, const void *);
/* Variables to save original hooks. */
static void *(*old_malloc_hook)(size_t, const void *);
/* Override initializing hook from the C library. */
void (*__malloc_initialize_hook) (void) = my_init_hook;
static void
my_init_hook(void)
{
old_malloc_hook = __malloc_hook;
__malloc_hook = my_malloc_hook;
}
static void *
my_malloc_hook(size_t size, const void *caller)
{
void *result;
/* Restore all old hooks */
__malloc_hook = old_malloc_hook;
/* Call recursively */
result = malloc(size);
/* Save underlying hooks */
old_malloc_hook = __malloc_hook;
/* printf() might call malloc(), so protect it too. */
printf("malloc(%u) called from %p returns %p
",
(unsigned int) size, caller, result);
/* Restore our own hooks */
__malloc_hook = my_malloc_hook;
return result;
}
SEE ALSO
mallinfo(3), malloc(3), mcheck(3), mtrace(3)
COLOPHON
This page is part of release 3.27 of the Linux man-pages project. A description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
GNU
2002-07-20 MALLOC_HOOK(3)