4 * @brief Memory handling routines for the MAC address database
7 * IXP400 SW Release version 2.0
9 * -- Copyright Notice --
12 * Copyright 2001-2005, Intel Corporation.
13 * All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the Intel Corporation nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
30 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * -- End of Copyright Notice --
46 #include "IxEthDB_p.h"
48 IX_ETH_DB_PRIVATE HashNode *nodePool = NULL;
49 IX_ETH_DB_PRIVATE MacDescriptor *macPool = NULL;
50 IX_ETH_DB_PRIVATE MacTreeNode *treePool = NULL;
52 IX_ETH_DB_PRIVATE HashNode nodePoolArea[NODE_POOL_SIZE];
53 IX_ETH_DB_PRIVATE MacDescriptor macPoolArea[MAC_POOL_SIZE];
54 IX_ETH_DB_PRIVATE MacTreeNode treePoolArea[TREE_POOL_SIZE];
56 IX_ETH_DB_PRIVATE IxOsalMutex nodePoolLock;
57 IX_ETH_DB_PRIVATE IxOsalMutex macPoolLock;
58 IX_ETH_DB_PRIVATE IxOsalMutex treePoolLock;
60 #define LOCK_NODE_POOL { ixOsalMutexLock(&nodePoolLock, IX_OSAL_WAIT_FOREVER); }
61 #define UNLOCK_NODE_POOL { ixOsalMutexUnlock(&nodePoolLock); }
63 #define LOCK_MAC_POOL { ixOsalMutexLock(&macPoolLock, IX_OSAL_WAIT_FOREVER); }
64 #define UNLOCK_MAC_POOL { ixOsalMutexUnlock(&macPoolLock); }
66 #define LOCK_TREE_POOL { ixOsalMutexLock(&treePoolLock, IX_OSAL_WAIT_FOREVER); }
67 #define UNLOCK_TREE_POOL { ixOsalMutexUnlock(&treePoolLock); }
69 /* private function prototypes */
70 IX_ETH_DB_PRIVATE MacDescriptor* ixEthDBPoolAllocMacDescriptor(void);
71 IX_ETH_DB_PRIVATE void ixEthDBPoolFreeMacDescriptor(MacDescriptor *macDescriptor);
74 * @addtogroup EthMemoryManagement
80 * @brief initializes the memory pools used by the ethernet database component
82 * Initializes the hash table node, mac descriptor and mac tree node pools.
83 * Called at initialization time by @ref ixEthDBInit().
88 void ixEthDBInitMemoryPools(void)
93 ixOsalMutexInit(&nodePoolLock);
95 for (local_index = 0 ; local_index < NODE_POOL_SIZE ; local_index++)
97 HashNode *freeNode = &nodePoolArea[local_index];
99 freeNode->nextFree = nodePool;
103 /* MacDescriptor pool */
104 ixOsalMutexInit(&macPoolLock);
106 for (local_index = 0 ; local_index < MAC_POOL_SIZE ; local_index++)
108 MacDescriptor *freeDescriptor = &macPoolArea[local_index];
110 freeDescriptor->nextFree = macPool;
111 macPool = freeDescriptor;
114 /* MacTreeNode pool */
115 ixOsalMutexInit(&treePoolLock);
117 for (local_index = 0 ; local_index < TREE_POOL_SIZE ; local_index++)
119 MacTreeNode *freeNode = &treePoolArea[local_index];
121 freeNode->nextFree = treePool;
127 * @brief allocates a hash node from the pool
129 * Allocates a hash node and resets its value.
131 * @return the allocated hash node or NULL if the pool is empty
136 HashNode* ixEthDBAllocHashNode(void)
138 HashNode *allocatedNode = NULL;
140 if (nodePool != NULL)
144 allocatedNode = nodePool;
145 nodePool = nodePool->nextFree;
149 memset(allocatedNode, 0, sizeof(HashNode));
152 return allocatedNode;
156 * @brief frees a hash node into the pool
158 * @param hashNode node to be freed
163 void ixEthDBFreeHashNode(HashNode *hashNode)
165 if (hashNode != NULL)
169 hashNode->nextFree = nodePool;
177 * @brief allocates a mac descriptor from the pool
179 * Allocates a mac descriptor and resets its value.
180 * This function is not used directly, instead @ref ixEthDBAllocMacDescriptor()
181 * is used, which keeps track of the pointer reference count.
183 * @see ixEthDBAllocMacDescriptor()
185 * @warning this function is not used directly by any other function
186 * apart from ixEthDBAllocMacDescriptor()
188 * @return the allocated mac descriptor or NULL if the pool is empty
193 MacDescriptor* ixEthDBPoolAllocMacDescriptor(void)
195 MacDescriptor *allocatedDescriptor = NULL;
201 allocatedDescriptor = macPool;
202 macPool = macPool->nextFree;
206 memset(allocatedDescriptor, 0, sizeof(MacDescriptor));
209 return allocatedDescriptor;
213 * @brief allocates and initializes a mac descriptor smart pointer
215 * Uses @ref ixEthDBPoolAllocMacDescriptor() to allocate a mac descriptor
216 * from the pool and initializes its reference count.
218 * @see ixEthDBPoolAllocMacDescriptor()
220 * @return the allocated mac descriptor or NULL if the pool is empty
225 MacDescriptor* ixEthDBAllocMacDescriptor(void)
227 MacDescriptor *allocatedDescriptor = ixEthDBPoolAllocMacDescriptor();
229 if (allocatedDescriptor != NULL)
233 allocatedDescriptor->refCount++;
238 return allocatedDescriptor;
242 * @brief frees a mac descriptor back into the pool
244 * @param macDescriptor mac descriptor to be freed
246 * @warning this function is not to be called by anyone but
247 * ixEthDBFreeMacDescriptor()
249 * @see ixEthDBFreeMacDescriptor()
254 void ixEthDBPoolFreeMacDescriptor(MacDescriptor *macDescriptor)
258 macDescriptor->nextFree = macPool;
259 macPool = macDescriptor;
265 * @brief frees or reduces the usage count of a mac descriptor smart pointer
267 * If the reference count reaches 0 (structure is no longer used anywhere)
268 * then the descriptor is freed back into the pool using ixEthDBPoolFreeMacDescriptor().
270 * @see ixEthDBPoolFreeMacDescriptor()
275 void ixEthDBFreeMacDescriptor(MacDescriptor *macDescriptor)
277 if (macDescriptor != NULL)
281 if (macDescriptor->refCount > 0)
283 macDescriptor->refCount--;
285 if (macDescriptor->refCount == 0)
289 ixEthDBPoolFreeMacDescriptor(macDescriptor);
304 * @brief clones a mac descriptor smart pointer
306 * @param macDescriptor mac descriptor to clone
308 * Increments the usage count of the smart pointer
310 * @returns the cloned smart pointer
315 MacDescriptor* ixEthDBCloneMacDescriptor(MacDescriptor *macDescriptor)
319 if (macDescriptor->refCount == 0)
326 macDescriptor->refCount++;
330 return macDescriptor;
334 * @brief allocates a mac tree node from the pool
336 * Allocates and initializes a mac tree node from the pool.
338 * @return the allocated mac tree node or NULL if the pool is empty
343 MacTreeNode* ixEthDBAllocMacTreeNode(void)
345 MacTreeNode *allocatedNode = NULL;
347 if (treePool != NULL)
351 allocatedNode = treePool;
352 treePool = treePool->nextFree;
356 memset(allocatedNode, 0, sizeof(MacTreeNode));
359 return allocatedNode;
363 * @brief frees a mac tree node back into the pool
365 * @param macNode mac tree node to be freed
367 * @warning not to be used except from ixEthDBFreeMacTreeNode().
369 * @see ixEthDBFreeMacTreeNode()
373 void ixEthDBPoolFreeMacTreeNode(MacTreeNode *macNode)
379 macNode->nextFree = treePool;
387 * @brief frees or reduces the usage count of a mac tree node smart pointer
389 * @param macNode mac tree node to free
391 * Reduces the usage count of the given mac node. If the usage count
392 * reaches 0 the node is freed back into the pool using ixEthDBPoolFreeMacTreeNode()
397 void ixEthDBFreeMacTreeNode(MacTreeNode *macNode)
399 if (macNode->descriptor != NULL)
401 ixEthDBFreeMacDescriptor(macNode->descriptor);
404 if (macNode->left != NULL)
406 ixEthDBFreeMacTreeNode(macNode->left);
409 if (macNode->right != NULL)
411 ixEthDBFreeMacTreeNode(macNode->right);
414 ixEthDBPoolFreeMacTreeNode(macNode);
418 * @brief clones a mac tree node
420 * @param macNode mac tree node to be cloned
422 * Increments the usage count of the node, <i>its associated descriptor
423 * and <b>recursively</b> of all its child nodes</i>.
425 * @warning this function is recursive and clones whole trees/subtrees, use only for
431 MacTreeNode* ixEthDBCloneMacTreeNode(MacTreeNode *macNode)
435 MacTreeNode *clonedMacNode = ixEthDBAllocMacTreeNode();
437 if (clonedMacNode != NULL)
439 if (macNode->right != NULL)
441 clonedMacNode->right = ixEthDBCloneMacTreeNode(macNode->right);
444 if (macNode->left != NULL)
446 clonedMacNode->left = ixEthDBCloneMacTreeNode(macNode->left);
449 if (macNode->descriptor != NULL)
451 clonedMacNode->descriptor = ixEthDBCloneMacDescriptor(macNode->descriptor);
455 return clonedMacNode;
464 /* Debug statistical functions for memory usage */
466 extern HashTable dbHashtable;
467 int ixEthDBNumHashElements(void);
469 int ixEthDBNumHashElements(void)
473 HashTable *hashTable = &dbHashtable;
475 for (bucketIndex = 0 ; bucketIndex < hashTable->numBuckets ; bucketIndex++)
477 if (hashTable->hashBuckets[bucketIndex] != NULL)
479 HashNode *node = hashTable->hashBuckets[bucketIndex];
493 UINT32 ixEthDBSearchTreeUsageGet(MacTreeNode *tree)
501 return 1 /* this node */ + ixEthDBSearchTreeUsageGet(tree->left) + ixEthDBSearchTreeUsageGet(tree->right);
505 int ixEthDBShowMemoryStatus(void)
515 int totalTreeUsage = 0;
516 int totalDescriptorUsage = 0;
517 int totalCloneDescriptorUsage = 0;
518 int totalNodeUsage = 0;
536 if (macCounter > MAC_POOL_SIZE)
546 tree = tree->nextFree;
548 if (treeCounter > TREE_POOL_SIZE)
558 node = node->nextFree;
560 if (nodeCounter > NODE_POOL_SIZE)
566 for (portIndex = 0 ; portIndex < IX_ETH_DB_NUMBER_OF_PORTS ; portIndex++)
568 int treeUsage = ixEthDBSearchTreeUsageGet(ixEthDBPortInfo[portIndex].updateMethod.searchTree);
570 totalTreeUsage += treeUsage;
571 totalCloneDescriptorUsage += treeUsage; /* each tree node contains a descriptor */
574 totalNodeUsage = ixEthDBNumHashElements();
575 totalDescriptorUsage += totalNodeUsage; /* each hash table entry contains a descriptor */
581 printf("Ethernet database memory usage stats:\n\n");
583 if (macCounter <= MAC_POOL_SIZE)
585 printf("\tMAC descriptor pool : %d free out of %d entries (%d%%)\n", macCounter, MAC_POOL_SIZE, macCounter * 100 / MAC_POOL_SIZE);
589 printf("\tMAC descriptor pool : invalid state (ring within the pool), normally %d entries\n", MAC_POOL_SIZE);
592 if (treeCounter <= TREE_POOL_SIZE)
594 printf("\tTree node pool : %d free out of %d entries (%d%%)\n", treeCounter, TREE_POOL_SIZE, treeCounter * 100 / TREE_POOL_SIZE);
598 printf("\tTREE descriptor pool : invalid state (ring within the pool), normally %d entries\n", TREE_POOL_SIZE);
601 if (nodeCounter <= NODE_POOL_SIZE)
603 printf("\tHash node pool : %d free out of %d entries (%d%%)\n", nodeCounter, NODE_POOL_SIZE, nodeCounter * 100 / NODE_POOL_SIZE);
607 printf("\tNODE descriptor pool : invalid state (ring within the pool), normally %d entries\n", NODE_POOL_SIZE);
611 printf("\tMAC descriptor usage : %d entries, %d cloned\n", totalDescriptorUsage, totalCloneDescriptorUsage);
612 printf("\tTree node usage : %d entries\n", totalTreeUsage);
613 printf("\tHash node usage : %d entries\n", totalNodeUsage);
616 /* search for duplicate nodes in the mac pool */
618 MacDescriptor *reference = macPool;
620 while (reference != NULL)
622 MacDescriptor *comparison = reference->nextFree;
624 while (comparison != NULL)
626 if (reference == comparison)
628 printf("Warning: reached a duplicate (%p), invalid MAC pool state\n", reference);
633 comparison = comparison->nextFree;
636 reference = reference->nextFree;
640 printf("No duplicates found in the MAC pool (sanity check ok)\n");
648 * @} EthMemoryManagement