C H A P T E R  9

malloc Library for Slow Path

This chapter describes the memory allocation (malloc) library API. Topics include:


malloc Library API Introduction

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.


Compiling Netra DPS Application with malloc Library

The malloc library has two components:

Declaring Memory Pools

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.


APPSWARCH_C = src/config/swarch.c src/config/malloc_mem_pool.c

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().

Including malloc Definition

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.


C += netra_dps_malloc_init.c
C += /opt/SUNWndps/src/libs/malloc/netra_dps_malloc.c

2. Call netra_dps_malloc_init() in the application initialization, to initialize the memory pools for malloc.


malloc Configuration File (malloc.conf)

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:


# node_size     total_nodes 
64              10000 
128             10000 
256             10000 

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


APIs

create_malloc_mem_pools

Description

Declares the memory pools as specified in the configuration file (malloc.conf) and generates the netra_dps_malloc_init.c.

Function

int

create_malloc_mem_pools(teja_thread_t threads[], const char *mem_bank);

Parameters

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.

Return Values

0 - on success

-1 - on error

netra_dps_malloc_init

Description

Initializes the malloc memory pool data structures.

Function

void

netra_dps_malloc_init(void);

malloc

Description

Allocates and returns the memory of size equal to or greater than the requested size.

Function

void *

malloc(size_t size);

Parameters

size - Required memory size.

Return Values

NULL on error, otherwise, the allocated memory.

free

Description

Frees the requested memory location.

Function

void

free(void *mem);

Parameters

mem - Memory location to be freed.