Is it possible to install a global new/delete function?

I am trying to use my own RAMPool to solve fragmentation issue, but there's a vendor library that I don't have the source code(only the header and so). I tried to do the following but it doesn't seem to work for that vendor lib, however it does work if I allocate a std::string or my own class etc. What did I miss?

void* operator new(std::size_t t) throw (std::bad_alloc)
{
//call my RAMPool
return p ;
}
void* operator new[](std::size_t t) throw (std::bad_alloc)
{
//call my RAMPool
return p ;
}
void operator delete(void* p) throw()
{
//call my RAMPool
}
void operator delete[](void* p) throw()
{
//call my RAMPool
}

void *myMalloc(size_t t)
{
//call my RAMPool
}

void myFree(void *ptr)
{
//call my RAMPool
}

#define malloc(size) myMalloc(size)
#define calloc(nmemb, size) myMalloc(nmemb * size)
#define realloc(ptr, size) myFree(ptr); ptr = myMalloc(size)
#define valloc(size) myMalloc(size)
#define free(ptr) myFree(ptr)
Youre definition of realloc is incorrect.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void* operator new(size_t size) throw(std::bad_alloc)
{
    return operator new(size, (char*)0, 0);	
}

void* operator new[](size_t size) throw(std::bad_alloc)
{
    return operator new[](size, (char*)0, 0);
	
}

//overloaded version
void* operator new(size_t size, const char* file, int line)
{
    return allocate_memory(size, file, line);
}

void* operator new[](size_t size, const char* file, int line)
{
	return allocate_memory(size, file, line);
}


//original version
void operator delete(void* pointer) throw()
{
    free_memory(pointer);
}

void operator delete[](void* pointer) throw()
{
    free_memory(pointer);
}



1
2
3
4
5
6
#undef malloc
#undef calloc
#undef free
#define malloc(size)	xmalloc(size, __FILE__, __LINE__)
#define calloc(elements, size)  	xcalloc (elements, size, __FILE__, __LINE__)
#define free(ptr)	xfree(ptr) 



hope you get the idea.
Topic archived. No new replies allowed.