Memory organization

Name Server memory

Name Server requires a user to provide memory for the Name Server stack and the database (hash table structure + entries). The memory is organized according to the following image:

memory organization
Fig. 1. Name Server memory organization

The Name Server checks whether the memory size provided by the user is sufficient. The calculation is as follows:

minimum memory size = stack + hash table + "guaranteed" number of entries

Which in numbers is:

minimum memory size = NAMESRV_STACKSIZE * sizeof(PxMemAligned_t) + sizeof(hashtab_T)
                     + NUMBER_OF_START_ENTRIES * sizeof(link_T)
                   = 32 * 8 + 1024 + 10 * 16
                   = 1440 B

So, the user must provide at least 1440 B not to fail the Name Server initialization. The remaining memory is inserted in the memory class.

Even though the NUMBER_OF_START_ENTRIES definition should guarantee the number of entries, it is not truly guaranteed. The memory class overhead for allocating an entry equals the memory class overhead when taking a block from it. This memory class overhead is 24 B, which is not taken into account during the calculation, resulting in entries not being guaranteed to fit.

Name Server hash table

The following image shows the hash table memory organization and the possible state of the hash table after registering three entries. Two of them have the same hash key (index 2 in the hash table), resulting in chaining these two entries into a linked list under the hash key.

hash table
Fig. 2. Name Server hash table memory organization

The image below shows the same state of the hash table but simplified and with more abstraction.

simplified hash table
Fig. 3. Simplified Name Server hash table memory organization

Memory overhead

Two factors influence the memory needed for the Name Server to work.

  1. Size of the hash table

    A hash table is a very efficient data structure that provides fast both read and write operations. The speed is traded for space. The Name Server’s hash table uses the 8-bit hash key (256 values). The hash table is filled with pointers to entries, so in the end, 1KB of the memory is needed only for the hash table structure.

  2. Memory class overhead

    Name Server uses a PXROS-HR memory allocator to manage entries' memory space. Each entry is allocated independently, which means there is an overhead of the PXROS-HR memory allocator (memory class overhead), which occupies 24B of the space reserved for Name Server entries.

Known limitations

  • Four bytes are always unused — wasted memory.

    The code uses a pointer variable called 'name_info' to access the buffer with stored information (data). The information is written from the address of this variable, meaning the "preallocated" 4 bytes within this pointer variable (part of the Name Server entry header) are also used for storing the data. However, the whole data size is not reduced by these 4 bytes. The last 4 bytes of the allocated memory are not used.

Memory section defined in a linker script

One of the ways to provide memory is by using a linker script to define a dedicated memory space that will be later assigned to be used by the Name Server. In the linker file, two symbols are defined:

  • PxNameServerSpaceBase — base address of Name Server memory space

  • PxNameServerSpaceEnd — end address of Name Server memory space

Code 1. Defininfg a memory space for Name Server using a linker script
__NAMESRV_SIZE = DEFINED (__NAMESRV_SIZE) ? __NAMESRV_SIZE : 4096;

SECTIONS
{
    .NameServer :
    {
        . = ALIGN(8); PxNameServerSpaceBase = .;
        . += __NAMESRV_SIZE;
        . = ALIGN(8); PxNameServerSpaceEnd = .;
    } > CORE_SYM(BSS)

    CORE_SEC(.clear_sec) :
    {
        LONG(0 + ADDR(.NameServer)); LONG(SIZEOF(.NameServer));
    } > RODATA_MEM
}

The symbols from the linker file must be defined as extern variables to be used in the application. The symbol’s name in the application must match the symbol in the linker file.

Code 2. Defining the linker file symbols so they can be used in the application
/* ========================================================================================
 * EXTERN SYMBOLS
 * ======================================================================================*/

/* NameServer memory coordinates needed during NameServer Task creation.
 * Coordinates come from Linker files.
 */
extern PxMemAligned_t PxNameServerSpaceBase[];
extern PxMemAligned_t PxNameServerSpaceEnd[];