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 and to get a block of memory there exists teja APIs like teja_memory_pool_get_node() which can be used in Netra DPS. Using teja APIs ensure high performance but it comes with an overhead to the application writer of finding the optimum memory pool for the required memory size.
Since in slow path, high performance is not needed but requires memory of various size. Hence, this library provides malloc/free implementation which can be used in slow path.
To make use of LDC or IPC library, the user needs to have an implementation of malloc/free and for this purpose too, you can use this library.
The malloc library has two components:
In the software architecture of the application, the user needs to declare the memory pools for malloc library. To do so, do the following:
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. Copy /opt/SUNWndps/src/libs/malloc/malloc_mem_pool.c to the application directory.
For example, src/config/malloc_mem_pool.c and compile it along with the software-architecture file.
3. Create an empty file netra_dps_malloc_init.c
To carry out steps 2 and 3, 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 |
4. To declare the memory pools needed to call create_malloc_mem_pools().
1. Add the netra_dps_malloc_init.c and /opt/SUNWndps/src/libs/malloc/netra_dps_malloc.c to the list of C files which are passed to tejacc.
2. Call netra_dps_malloc_init() in the application initialization, to initialize the memory pools for malloc.
The user needs to 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 © 2008 Sun Microsystems, Inc. All Rights Reserved.