ACE的内存宏定义

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

OS_Memory.h

这个文件主要提供了内存分配的安全的宏定义和内存分配静态类。

ACE的内存宏定义

ACE_HAS_NEW_NOTHROW宏定义了是否使用new的异常抛出

POINTER,CONSTRUCTOR,RET_VAL

用法:ACE_*(指针,类型,失败返回值)

# if defined (ACE_HAS_NEW_NOTHROW)

# define ACE_NEW_RETURN(POINTER,CONSTRUCTOR,RET_VAL)

do { POINTER = new (ACE_nothrow) CONSTRUCTOR;

if (POINTER == 0) { errno = ENOMEM; return RET_VAL; }

} while (0)

# define ACE_NEW(POINTER,CONSTRUCTOR)

do { POINTER = new(ACE_nothrow) CONSTRUCTOR;

if (POINTER == 0) { errno = ENOMEM; return; }

} while (0)

# define ACE_NEW_NORETURN(POINTER,CONSTRUCTOR)

do { POINTER = new(ACE_nothrow) CONSTRUCTOR;

if (POINTER == 0) { errno = ENOMEM; }

} while (0)

# else

# define ACE_NEW_RETURN(POINTER,CONSTRUCTOR,RET_VAL)

do { try { POINTER = new CONSTRUCTOR; }

catch (ACE_bad_alloc) { errno = ENOMEM; POINTER = 0; return RET_VAL; }

} while (0)

# define ACE_NEW(POINTER,CONSTRUCTOR)

do { try { POINTER = new CONSTRUCTOR; }

catch (ACE_bad_alloc) { errno = ENOMEM; POINTER = 0; return; }

} while (0)

# define ACE_NEW_NORETURN(POINTER,CONSTRUCTOR)

do { try { POINTER = new CONSTRUCTOR; }

catch (ACE_bad_alloc) { errno = ENOMEM; POINTER = 0; }

} while (0)

# endif /* ACE_HAS_NEW_NOTHROW */

#else /* ACE_NEW_THROWS_EXCEPTIONS */

很明显使用NEW的抛出宏定义里使用到do{}while{} 语句和异常处理try{}catch{}的,保证分配内存的正确性。鼓励使用内存分配宏定义。在抛出的new的异常ACE_bad_alloc的定义如下:

// =========================================================================

// ACE_NEW macros

//

// A useful abstraction for expressions involving operator new since

// we can change memory allocation error handling policies (e.g.,

// depending on whether ANSI/ISO exception handling semantics are

// being used).

// =========================================================================

#if defined (ACE_NEW_THROWS_EXCEPTIONS)

// Since new() throws exceptions, we need a way to avoid passing

// exceptions past the call to new because ACE counts on having a 0

// return value for a failed allocation. Some compilers offer the

// new (nothrow) version, which does exactly what we want. Others

// do not. For those that do not, this sets up what exception is thrown,

// and then below we'll do a try/catch around the new to catch it and

// return a 0 pointer instead.

// 定义的惠普aCC编译器和各个版本

# if defined (__HP_aCC)

// I know this works for HP aC++... if <stdexcept> is used, it

// introduces other stuff that breaks things, like <memory>, which

// screws up auto_ptr.

# include /**/ <new>

// _HP_aCC was first defined at aC++ 03.13 on HP-UX 11. Prior to that

// (03.10 and before) a failed new threw bad_alloc. After that (03.13

// and above) the exception thrown is dependent on the below settings.

# if (HPUX_VERS >= 1100)

# if ((__HP_aCC < 32500 && !defined (RWSTD_NO_NAMESPACE)) ||

defined (ACE_USES_STD_NAMESPACE_FOR_STDCPP_LIB))

# define ACE_bad_alloc std::bad_alloc

# define ACE_nothrow std::nothrow

# define ACE_nothrow_t std::nothrow_t

# else

# define ACE_bad_alloc bad_alloc

# define ACE_nothrow nothrow

# define ACE_nothrow_t nothrow_t

# endif /* __HP_aCC */

# elif ((__HP_aCC < 12500 && !defined (RWSTD_NO_NAMESPACE)) ||

defined (ACE_USES_STD_NAMESPACE_FOR_STDCPP_LIB))

# define ACE_bad_alloc std::bad_alloc

# define ACE_nothrow std::nothrow

# define ACE_nothrow_t std::nothrow_t

# else

# define ACE_bad_alloc bad_alloc

# define ACE_nothrow nothrow

# define ACE_nothrow_t nothrow_t

# endif /* HPUX_VERS < 1100 */

# define ACE_throw_bad_alloc throw ACE_bad_alloc ()

// Sun的编译器和版本

# elif defined (__SUNPRO_CC)

# if (__SUNPRO_CC < 0x500) || (__SUNPRO_CC_COMPAT == 4)

# include /**/ <exception.h>

// Note: we catch ::xalloc rather than just xalloc because of

// a name clash with unsafe_ios::xalloc()

# define ACE_bad_alloc ::xalloc

# define ACE_throw_bad_alloc throw ACE_bad_alloc ("no more memory")

# else

# include /**/ <new>

# define ACE_bad_alloc std::bad_alloc

# define ACE_throw_bad_alloc throw ACE_bad_alloc ()

# endif /* __SUNPRO_CC < 0x500 */

// Borland的编译器或者使用标准名字空间的C++库

# elif defined (__BORLANDC__) || defined (ACE_USES_STD_NAMESPACE_FOR_STDCPP_LIB)

# include /**/ <new>

# define ACE_bad_alloc std::bad_alloc

# define ACE_throw_bad_alloc throw ACE_bad_alloc ()

# else

// 缺省的时候的编译器

# include /**/ <new>

# define ACE_bad_alloc bad_alloc

# define ACE_throw_bad_alloc throw ACE_bad_alloc ()

# endif /* __HP_aCC */

# define ACE_throw_bad_alloc

void* gcc_will_complain_if_literal_0_is_returned = 0;

return gcc_will_complain_if_literal_0_is_returned

另外一个是内存分配类ACE_OS_Memory,比较简单与C风格的内存分配函数类似。

class ACE_OS_Export ACE_OS_Memory

{

public:

// = A set of wrappers for memory managment.

static void *sbrk (int brk);

static void *calloc (size_t elements, size_t sizeof_elements);

static void *malloc (size_t);

static void *realloc (void *, size_t);

static void free (void *);

};

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航