Chinaunix首页 | 论坛 | 博客
  • 博客访问: 979797
  • 博文数量: 184
  • 博客积分: 10030
  • 博客等级: 上将
  • 技术积分: 1532
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-27 18:32
文章分类

全部博文(184)

文章存档

2009年(1)

2008年(63)

2007年(39)

2006年(79)

2005年(2)

我的朋友

分类: C/C++

2008-06-13 06:58:30

Technical Reference: Base Operating System and Extensions, Volume 1

malloc, free, realloc, calloc, mallopt, mallinfo, mallinfo_heap, alloca, valloc, or posix_memalign Subroutine

Purpose

Provides a complete set of memory allocation, reallocation, deallocation, and heap management tools.

Libraries

Berkeley Compatibility Library (libbsd.a)

Standard C Library (libc.a)

Malloc Subsystem APIs

malloc

Syntax

#include 
void *malloc ()
size_t Size;

Description

The malloc subroutine returns a pointer to a block of memory of at least the number of bytes specified by the Size parameter. The block is aligned so that it can be used for any type of data. Undefined results occur if the space assigned by the malloc subroutine is overrun.

Parameters

Size Specifies the size, in bytes, of memory to allocate.

Return Values

Upon successful completion, the malloc subroutine returns a pointer to space suitably aligned for the storage of any type of object. If the size requested is 0, malloc returns NULL in normal circumstances. However, if the program was compiled with the defined _LINUX_SOURCE_COMPAT macro, malloc returns a valid pointer to a space of size 0.

If the request cannot be satisfied for any reason, the malloc subroutine returns NULL.

Error Codes

ENOMEM Insufficient storage space is available to service the request.

free

Syntax

#include 
void free ()
void * Pointer;

Description

The free subroutine deallocates a block of memory previously allocated by the malloc subsystem. Undefined results occur if the Pointer parameter is not an address that has previously been allocated by the malloc subsystem, or if the Pointer parameter has already been deallocated. If the Pointer parameter is NULL, no action occurs.

Parameters

Pointer Specifies a pointer to space previously allocated by the malloc subsystem.

Return Values

The free subroutine does not return a value. Upon successful completion with nonzero arguments, the realloc subroutine returns a pointer to the (possibly moved) allocated space. If the Size parameter is 0 and the Pointer parameter is not null, no action occurs.

Error Codes

The free subroutine does not set errno.

realloc

Syntax

#include 
void *realloc (, )
void *Pointer;
size_t Size;

Description

The realloc subroutine changes the size of the memory object pointed to by the Pointer parameter to the number of bytes specified by the Size parameter. The Pointer must point to an address returned by a malloc subsystem allocation routine, and must not have been previously deallocated. Undefined results occur if Pointer does not meet these criteria.

The contents of the memory object remain unchanged up to the lesser of the old and new sizes. If the current memory object cannot be enlarged to satisfy the request, the realloc subroutine acquires a new memory object and copies the existing data to the new space. The old memory object is then freed. If no memory object can be acquired to accommodate the request, the object remains unchanged.

If the Pointer parameter is null, the realloc subroutine is equivalent to a malloc subroutine of the same size.

If the Size parameter is 0 and the Pointer parameter is not null, the realloc subroutine is equivalent to a free subroutine of the same size.

Parameters

Pointer Specifies a Pointer to space previously allocated by the malloc subsystem.
Size Specifies the new size, in bytes, of the memory object.

Return Values

Upon successful completion with nonzero arguments, the realloc subroutine returns a pointer to the (possibly moved) allocated space. If the Size parameter is 0 and the Pointer parameter is not null, return behavior is equivalent to that of the free subroutine. If the Pointer parameter is null and the Size parameter is not zero, return behavior is equivalent to that of the malloc subroutine.

Error Codes

ENOMEM Insufficient storage space is available to service the request.

calloc

Syntax

#include 
void *calloc (, )
size_t NumberOfElements;
size_t ElementSize;

Description

The calloc subroutine allocates space for an array containing the NumberOfElements objects. The ElementSize parameter specifies the size of each element in bytes. After the array is allocated, all bits are initialized to 0.

The order and contiguity of storage allocated by successive calls to the calloc subroutine is unspecified. The pointer returned points to the first (lowest) byte address of the allocated space. The allocated space is aligned so that it can be used for any type of data. Undefined results occur if the space assigned by the calloc subroutine is overrun.

Parameters

NumberOfElements Specifies the number of elements in the array.
ElementSize Specifies the size, in bytes, of each element in the array.

Return Values

Upon successful completion, the calloc subroutine returns a pointer to the allocated, zero-initialized array. If the size requested is 0, the calloc subroutine returns NULL in normal circumstances. However, if the program was compiled with the macro _LINUX_SOURCE_COMPAT defined, the calloc subroutine returns a valid pointer to a space of size 0.

If the request cannot be satisfied for any reason, the calloc subroutine returns NULL.

Error Codes

ENOMEM Insufficient storage space is available to service the request.

mallopt

Syntax

#include 
#include
int mallopt (, )
int Command;
int Value;

Description

The mallopt subroutine is provided for source-level compatibility with the System V malloc subroutine. The mallopt subroutine supports the following commands:

Command Value Effect
M_MXFAST 0 If called before any other malloc subsystem subroutine, this enables the Default allocation policy for the process.
M_MXFAST 1 If called before any other malloc subsystem subroutine, this enables the 3.1 allocation policy for the process.
M_DISCLAIM 0 If called while the Default Allocator is enabled, all free memory in the process heap is disclaimed.
M_MALIGN N If called at runtime, sets the default malloc allocation alignment to the value N. The N value must be a power of 2 (greater than or equal to the size of a pointer).

Parameters

Command Specifies the mallopt command to be executed.
Value Specifies the size of each element in the array.

Return Values

Upon successful completion, the mallopt subroutine returns 0. Otherwise, 1 is returned. If an invalid alignment is requested (one that is not a power of 2), mallopt fails with a return value of 1, although subsequent calls to malloc are unaffected and continue to provide the alignment value from before the failed mallopt call.

Error Codes

The mallopt subroutine does not set errno.

mallinfo

Syntax

#include 
#include
struct mallinfo mallinfo();

Description

The mallinfo subroutine can be used to obtain information about the heap managed by the malloc subsystem.

Return Values

The mallinfo subroutine returns a structure of type struct mallinfo, filled in with relevant information and statistics about the heap. The contents of this structure can be interpreted using the definition of struct mallinfo in the /usr/include/malloc.h file.

Error Codes

The mallinfo subroutine does not set errno.

mallinfo_heap

Syntax

#include 
#include
struct mallinfo_heap mallinfo_heap ()
int Heap;

Description

In a multiheap context, the mallinfo_heap subroutine can be used to obtain information about a specific heap managed by the malloc subsystem.

Parameters

Heap Specifies which heap to query.

Return Values

mallinfo_heap returns a structure of type struct mallinfo_heap, filled in with relevant information and statistics about the heap. The contents of this structure can be interpreted using the definition of struct mallinfo_heap in the /usr/include/malloc.h file.

Error Codes

The mallinfo_heap subroutine does not set errno.

alloca

Syntax

#include 
char *alloca ()
int Size;

Description

The alloca subroutine returns a pointer to a block of memory of at least the number of bytes specified by the Size parameter. The space is allocated from the stack frame of the caller and is automatically freed when the calling subroutine returns.

If alloca is used in code compiled with the C++ compiler, #pragma alloca must be added to the source before the reference to alloca. Alternatively, the -ma compiler option can be used during compilation.

Parameters

Size Specifies the size, in bytes, of memory to allocate.

Return Values

The alloca subroutine returns a pointer to space of the requested size.

Error Codes

The alloca subroutine does not set errno.

valloc

Syntax

#include 
void *valloc ()
size_t Size;

Description

The valloc subroutine is supported as a compatibility interface in the Berkeley Compatibility Library (libbsd.a), as well as in libc.a. The valloc subroutine has the same effect as malloc, except that the allocated memory is aligned to a multiple of the value returned by sysconf (_ SC_PAGESIZE).

Parameters

Size Specifies the size, in bytes, of memory to allocate.

Return Values

Upon successful completion, the valloc subroutine returns a pointer to a memory object that is Size bytes in length, aligned to a page-boundary. Undefined results occur if the space assigned by the valloc subroutine is overrun.

If the request cannot be satisfied for any reason, valloc returns NULL.

Error Codes

ENOMEM Insufficient storage space is available to service the request.

posix_memalign

Syntax

#include 
int posix_memalign(void **, , )
void ** Pointer2Pointer;
size_t Align;
size_t Size;

Description

The posix_memalign subroutine allocates Size bytes of memory aligned on a boundary specified by Align. The address of this memory is stored in Pointer2Pointer.

Parameters

Pointer2Pointer Specifies the location in which the address should be copied.
Align Specifies the alignment of the allocated memory, in bytes. The Align parameter must be a power-of-two multiple of the size of a pointer.
Size Specifies the size, in bytes, of memory to allocate.

Return Values

Upon successful completion, posix_memalign returns 0. Otherwise, an error number is returned to indicate the error.

Error Codes

EINVAL The value of Align is not a power-of-two multiple of the size of a pointer.
ENOMEM Insufficient storage space is available to service the request.

Related Information

The _end, _etext, or _edata () identifier.

, , , , , , , in AIX Version 6.1 General Programming Concepts: Writing and Debugging Programs, and in Operating system and device management.

阅读(2430) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~