ACE memory macro definition

xiaoxiao2021-03-06  104

OS_MEMORY.H

This file mainly provides secure macro definitions and memory allocation static classes for memory allocation.

ACE memory macro definition

ACE_HAS_NEW_NOTHROW macro defines whether the exception of NEW is thrown

Pointer, Constructor, Ret_Val

Usage: ACE_ * (pointer, type, failed return value)

# 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 = EnMem; Return;} /

} while (0)

# Define ace_new_noreturn (Pointer, Constructor) / CONSTRUCTOR

Do {Pointer = New (ACE_NOTHROW) Constructor; /

IF (Pointer == 0) {Errno = EnMem;} /

} 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 = EnMem; Pointer = 0; Return;} /

} while (0)

# Define ace_new_noreturn (Pointer, Constructor) / CONSTRUCTOR

Do {Try {Pointer = New Constructor;} /

Catch (ACE_BAD_ALLOC) {Errno = EnMem; Pointer = 0;} /

} while (0)

# Endif / * ace_has_new_nothrow * /

#ELSE / * ace_new_throws_exceptions * /

Obviously use the DO {} while} statement and exception handling try {} Catch {} in the throne of New, to ensure the correctness of the allocated memory. Encourage the macro definition using the memory allocation. The definition of the unusual ACE_BAD_ALLOC thrown in the throwing NEW is as follows:

/ / =========================================================================================================================================================================================== ================================== // ACE_NEW Macros

//

// a useful Abstract 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_EXCES)

// Since new () throws Exceptions, We need a way to avoid passing

// Exceptions Past The Call to New Because Ace Count 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 dam below we'll do a try / catch arround the new to catch it and

// Return A 0 Pointer INSTEAD.

// Defined HP ACC compiler and various versions

# If defined (__hp_acc)

// i know this works for HP AC ... if is buy, IT

// Introduces Other Stuff That Breaks Things, Like , Which

// Screws Up auto_ptr.

# Include / ** /

// _HP_ACC WAS First Defined AT AT AT AT AT AT 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)

# Ix (__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 nothing

# 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 nothing

# Define ace_nothrow_t nothrow_t

# ENDIF / * HPUX_VERS <1100 * /

# Define ace_throw_bad_alloc throw ace_bad_alloc ()

// Sun compiler and version

# Elif Defined (__sunpro_cc)

# Ix (__sunpro_cc <0x500) || (__sunpro_cc_compat == 4)

# Include / ** /

// NOTE: WE CATCH :: Xalloc Rather Than Just Xalloc Because

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

# Define ace_bad_alloc :: xalloc

# Define ace_throw_bad_alloc throw ace_bad_alloc ("no more memory")

# Else

# Include / ** /

# Define ace_bad_alloc std :: bad_alloc

# Define ace_throw_bad_alloc throw ace_bad_alloc ()

# ENDIF / * __SUNPRO_CC <0x500 * /// Borland compiler or C library using standard namespace

# Elif Defined (__borlandc__) || Defined (ACE_USES_STD_NAMESPACE_FOR_STDCPP_LIB)

# Include / ** /

# Define ace_bad_alloc std :: bad_alloc

# Define ace_throw_bad_alloc throw ace_bad_alloc ()

# Else

/ / Default time compiler

# Include / ** /

# 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

The other is a memory allocation class ACE_OS_MEMORY, which is similar to the Memory allocation function of the C style.

Class ACE_OS_EXPORT ACE_OS_MEMORY

{

PUBLIC:

// = a set of wrappers for memory manage.

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 *);

}

转载请注明原文地址:https://www.9cbs.com/read-97983.html

New Post(0)