BastEt 阅读(1203) 评论(0)
根据网上的引用,1、先从libcmt.lib里把对应的库删掉(得保存原来的)
注:我这里是vc2010的。
SET OUTCMT=newlib\libcmt.lib

LIB /IGNORE:4006,4221 /REMOVE:f:\dd\vctools\crt_bld\SELF_X86\crt\src\build\intel\mt_obj\malloc.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:f:\dd\vctools\crt_bld\SELF_X86\crt\src\build\intel\mt_obj\free.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:f:\dd\vctools\crt_bld\SELF_X86\crt\src\build\intel\mt_obj\realloc.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:f:\dd\vctools\crt_bld\SELF_X86\crt\src\build\intel\mt_obj\calloc.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:f:\dd\vctools\crt_bld\SELF_X86\crt\src\build\intel\mt_obj\new.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:f:\dd\vctools\crt_bld\SELF_X86\crt\src\build\intel\mt_obj\delete.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:f:\dd\vctools\crt_bld\SELF_X86\crt\src\build\intel\mt_obj\new2.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:f:\dd\vctools\crt_bld\SELF_X86\crt\src\build\intel\mt_obj\delete2.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:f:\dd\vctools\crt_bld\SELF_X86\crt\src\build\intel\mt_obj\align.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:f:\dd\vctools\crt_bld\SELF_X86\crt\src\build\intel\mt_obj\msize.obj %OUTCMT%

LIB /IGNORE:4006,4221 /REMOVE:f:\dd\vctools\crt_bld\SELF_X86\crt\src\build\intel\mt_obj\heapinit.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:f:\dd\vctools\crt_bld\SELF_X86\crt\src\build\intel\mt_obj\expand.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:f:\dd\vctools\crt_bld\SELF_X86\crt\src\build\intel\mt_obj\heapchk.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:f:\dd\vctools\crt_bld\SELF_X86\crt\src\build\intel\mt_obj\heapwalk.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:f:\dd\vctools\crt_bld\SELF_X86\crt\src\build\intel\mt_obj\heapmin.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:f:\dd\vctools\crt_bld\SELF_X86\crt\src\build\intel\mt_obj\sbheap.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:f:\dd\vctools\crt_bld\SELF_X86\crt\src\build\intel\mt_obj\smalheap.obj %OUTCMT%


LIB /IGNORE:4006,4221 /REMOVE:f:\dd\vctools\crt_bld\SELF_X86\crt\src\build\INTEL\mt_obj\recalloc.obj
LIB /IGNORE:4006,4221 /REMOVE:f:\dd\vctools\crt_bld\SELF_X86\crt\src\build\INTEL\mt_obj\calloc_impl.obj


2、自己写函数,我用的nedmalloc
#include
#include
#include

extern "C"
{
size_t nedmemsize(void * mem);
void nedsetvalue(void *v);
void * nedmalloc(size_t size);
void * nedcalloc(size_t no, size_t size);
void * nedrealloc(void *mem, size_t size);
void nedfree(void *mem);
void * nedmemalign(size_t alignment, size_t bytes);
void * nedmalloc2(size_t size, size_t alignment, unsigned flags);
void * nedrealloc2(void *mem, size_t size, size_t alignment, unsigned flags);
void nedfree2(void *mem, unsigned flags);
int nedmallopt(int parno, int value);
size_t nedmemsize(void *mem);


void * malloc(size_t size)
{
return nedmalloc(size);
}
void * calloc(size_t no, size_t size)
{
return nedcalloc(no,size);
}
void * realloc(void *mem, size_t size)
{
return nedrealloc(mem,size);
}
void free(void *mem)
{
return nedfree(mem);
}
}




extern "C" void* _recalloc(void* p, size_t n, size_t size)
{
void* result = nedrealloc(p, n * size);
memset(result, 0, n * size);
return result;
}

extern "C" void* _calloc_impl(size_t n, size_t size)
{
return nedcalloc(n, size);
}

extern "C" size_t _msize(void* p)
{
return nedmemsize(p);
}

extern "C" intptr_t _get_heap_handle()
{
return 0;
}

// The CRT heap initialization stub.
extern "C" int _heap_init()
{
return 1;
}

// The CRT heap cleanup stub.
extern "C" void _heap_term()
{
}

extern "C" int _set_new_mode(int flag)
{
return 1;
}

// We set this to 1 because part of the CRT uses a check of _crtheap != 0
// to test whether the CRT has been initialized. Once we've ripped out
// the allocators from libcmt, we need to provide this definition so that
// the rest of the CRT is still usable.
extern "C" void* _crtheap = (void*)(1);












void * operator new( size_t cb )
{
return nedmalloc(cb);
}

void operator delete( void * p )
{
nedfree( p );
}

void * operator new[]( size_t cb )
{
void *res = operator new(cb);
return res;
}


void operator delete[]( void * p )
{
operator delete(p);
}

把这个cpp加进你的工程或者生成一个库。
最后就可以了,这样你可以任意自定义你的这些函数了。

发表评论
切换编辑模式