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


Compiling Sun Netra DPS Application with malloc Library

The malloc library has two components:


procedure icon  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.


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

4. Create an empty file, netra_dps_malloc_init.c.

Declare the memory pools needed to call create_malloc_mem_pools().


procedure icon  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.


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

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:


# 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


malloc Library 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.

Syntax

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 - Success

-1 - Error

netra_dps_malloc_init

Description

Initializes the malloc memory pool data structures.

Syntax

void

netra_dps_malloc_init(void);

malloc

Description

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

Syntax

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.

Syntax

void

free(void *mem);

Parameters

mem - Memory location to be freed.