Skip Navigation Links | |
Exit Print View | |
Programming Interfaces Guide Oracle Solaris 11.1 Information Library |
2. Session Description Protocol API
8. Programming With XTI and TLI
10. Transport Selection and Name-to-Address Mapping
11. Real-time Programming and Administration
Library-level dynamic memory allocation provides an easy-to-use interface to dynamic memory allocation.
The most often used interfaces are:
Other dynamic memory allocation interfaces are memalign(3C), valloc(3C), and realloc(3C)
malloc returns a pointer to a block of memory at least as large as the amount of memory that is requested. The block is aligned to store any type of data.
free returns the memory that is obtained from malloc, calloc, realloc, memalign, or valloc to system memory. Trying to free a block that was not reserved by a dynamic memory allocation interface is an error that can cause a process to crash.
calloc returns a pointer to a block of memory that is initialized to zeros. Memory reserved by calloc can be returned to the system through either watchmalloc or free. The memory is allocated and aligned to contain an array of a specified number of elements of a specified size.
memalign allocates a specified number of bytes on a specified alignment boundary. The alignment boundary must be a power of 2.
valloc allocates a specified number of bytes that are aligned on a page boundary.
realloc changes the size of the memory block allocated to a process. realloc can be used to increase or reduce the size of an allocated block of memory. realloc is the only way to shrink a memory allocation without causing a problem. The location in memory of the reallocated block might be changed, but the contents up to the point of the allocation size change remain the same.
The Oracle Solaris Studio software included tools that are useful in finding and eliminating errors in dynamic memory use.
dbx is an interactive, source-level, command-line debugging tool. You can use it to run a program in a controlled manner and to inspect the state of a stopped program. dbx gives you complete control of the dynamic execution of a program, including collecting performance and memory usage data, monitoring memory access, and detecting memory leaks. dbxtool provides a graphical user interface for dbx. See Oracle Solaris Studio 12.3: Debugging a Program With dbx for detailed information.
The Run Time Checking (RTC) tool in the Oracle Solaris Studio software lets you automatically detect runtime errors, such as memory access errors and memory leak, in a native code application during the development phase. It also lets you monitor memory usage. You cannot use runtime checking on Java code. See Chapter 9, Using Runtime Checking, in Oracle Solaris Studio 12.3: Debugging a Program With dbx for details on using the RTC facility.
You can also use the advanced development tool Memory Error Discovery Tool (Discover) for detecting memory access error. See the Oracle Solaris Studio 12.3 Discover and Uncover User's Guide for detailed information.
Oracle Solaris Studio is available on as a package to download and install on the Oracle Solaris 11 OS. For more information, see the Oracle Solaris Studio website.
This section discusses additional memory control interfaces.
sysconf(3C) returns system dependent sizes of memory pages and applications should use getpagesizes(3C) to find out which memory pages are available to a running process. For portability, applications should not embed any constants that specify the size of a page. Note that varying page sizes are not unusual, even among implementations of the same instruction set.
mprotect(2) assigns the specified protection to all pages in the specified address range. The protection cannot exceed the permissions that are allowed on the underlying object.
A break is the greatest valid data address in the process image that is not in the stack. When a program starts executing, the break value is normally set by execve(2) to the greatest address defined by the program and its data storage.
Use brk(2) to set the break to a greater address. You can also use sbrk(2) to add an increment of storage to the data segment of a process. You can get the maximum possible size of the data segment by a call to getrlimit(2).
caddr_t brk(caddr_t addr); caddr_t sbrk(intptr_t incr);
brk identifies the lowest data segment location not used by the caller as addr. This location is rounded up to the next multiple of the system page size.
sbrk, the alternate interface, adds incr bytes to the caller data space and returns a pointer to the start of the new data area.