C H A P T E R 9 |
malloc Library for Slow Path |
This chapter describes the memory allocation (malloc) library API. Topics include:
All applications need memory. Teja APIs such as teja_memory_pool_get_node(), which can be used in Sun Netra DPS, enables you to get a block of memory. Using teja APIs ensures high performance, but there is an overhead to the application writer finding the optimum memory pool for the required memory size.
Slow path requires various sizes of memory, but not high performance. The malloc/free implementation in this library can be used in slow path.
You also can use this library to use the LDC and IPC libraries, which require a malloc/free implementation.
The malloc library has two components:
Declare Memory Pools |
In the software architecture of the application, you must declare the memory pools for malloc library.
1. Add the include path /opt/SUNWndps/src/libs/malloc/include to tajacc flags and to CFLAGS.
TEJACC_FLAGS += /opt/SUNWndps/src/libs/malloc/include CFLAGS += /opt/SUNWndps/src/libs/malloc/include |
2. To carry out the following steps, add the following makefile target and call it at the beginning:
all: init $(APPHWARCH_LIB) $(APPSWARCH_LIB) $(APPMAP_LIB) app init: # cp -f /opt/SUNWndps/src/libs/malloc/malloc_mem_pool.c src/config/malloc_mem_pool.c touch netra_dps_malloc_init.c |
3. Copy /opt/SUNWndps/src/libs/malloc/malloc_mem_pool.c to the application directory.
For example, create src/config/malloc_mem_pool.c and compile it along with the software architecture file.
4. Create an empty file, netra_dps_malloc_init.c.
Declare the memory pools needed to call create_malloc_mem_pools().
Include malloc Definition |
1. Add the netra_dps_malloc_init.c and /opt/SUNWndps/src/libs/malloc/netra_dps_malloc.c files to the list of C files that are passed to tejacc.
2. Call netra_dps_malloc_init() in the application initialization, to initialize the memory pools for malloc.
The user must create the malloc.conf configuration file to create the memory pools of the desired size and node count. In this file, the user needs to enter the memory pool node size, followed by the total number of nodes of that size:
For example, the first entry above in malloc.conf is set to create 10000 nodes of size 64 byte, and so on.
If the application does not have its own malloc.conf file, then it picks the configuration file from the malloc library that is in: /opt/SUNWndps/src/libs/malloc/malloc.conf
Declares the memory pools as specified in the configuration file (malloc.conf) and generates the netra_dps_malloc_init.c.
create_malloc_mem_pools(teja_thread_t threads[], const char *mem_bank);
threads - NULL terminated list of all the threads, from where the application is going to call malloc/free.
mem_bank - Name of the memory bank (in the hardware architecture) from which the memory is allocated.
Initializes the malloc memory pool data structures.
Allocates and returns the memory of size equal to or greater than the requested size.
NULL on error, otherwise, the allocated memory.
Frees the requested memory location.
mem - Memory location to be freed.
Copyright © 2010, Oracle and/or its affiliates. All rights reserved.