update copyright date to 2013
[dpdk.git] / lib / librte_malloc / rte_malloc.c
index 607da3c..03db51f 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  *   BSD LICENSE
  * 
- *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
+ *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
  *   All rights reserved.
  * 
  *   Redistribution and use in source and binary forms, with or without 
 #include <rte_malloc.h>
 #include "malloc_elem.h"
 #include "malloc_heap.h"
+#include "malloc_heap.c"
 
-static struct malloc_heap malloc_heap[RTE_MAX_NUMA_NODES] = {
-               { .initialised = NOT_INITIALISED }
-};
 
 /* Free the memory space back to heap */
 void rte_free(void *addr)
@@ -68,32 +66,68 @@ void rte_free(void *addr)
 }
 
 /*
- * Allocate memory on default heap.
+ * Allocate memory on specified heap.
  */
 void *
-rte_malloc(const char *type, size_t size, unsigned align)
+rte_malloc_socket(const char *type, size_t size, unsigned align, int socket)
 {
-       unsigned malloc_socket = malloc_get_numa_socket();
+       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
+
        /* return NULL if size is 0 or alignment is not power-of-2 */
        if (size == 0 || !rte_is_power_of_2(align))
                return NULL;
-       return malloc_heap_alloc(&malloc_heap[malloc_socket], type,
+
+       if (socket == SOCKET_ID_ANY)
+               socket = malloc_get_numa_socket();
+
+       /* Check socket parameter */
+       if (socket >= RTE_MAX_NUMA_NODES)
+               return NULL;
+
+       return malloc_heap_alloc(&mcfg->malloc_heaps[socket], type,
                        size, align == 0 ? 1 : align);
 }
 
 /*
- * Allocate zero'd memory on default heap.
+ * Allocate memory on default heap.
  */
 void *
-rte_zmalloc(const char *type, size_t size, unsigned align)
+rte_malloc(const char *type, size_t size, unsigned align)
 {
-       void *ptr = rte_malloc(type, size, align);
+       return rte_malloc_socket(type, size, align, SOCKET_ID_ANY);
+}
+
+/*
+ * Allocate zero'd memory on specified heap.
+ */
+void *
+rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket)
+{
+       void *ptr = rte_malloc_socket(type, size, align, socket);
 
        if (ptr != NULL)
                memset(ptr, 0, size);
        return ptr;
 }
 
+/*
+ * Allocate zero'd memory on default heap.
+ */
+void *
+rte_zmalloc(const char *type, size_t size, unsigned align)
+{
+       return rte_zmalloc_socket(type, size, align, SOCKET_ID_ANY);
+}
+
+/*
+ * Allocate zero'd memory on specified heap.
+ */
+void *
+rte_calloc_socket(const char *type, size_t num, size_t size, unsigned align, int socket)
+{
+       return rte_zmalloc_socket(type, num * size, align, socket);
+}
+
 /*
  * Allocate zero'd memory on default heap.
  */
@@ -144,12 +178,44 @@ rte_malloc_validate(void *ptr, size_t *size)
                *size = elem->size - elem->pad - MALLOC_ELEM_OVERHEAD;
        return 0;
 }
+
 /*
- * TODO: Print stats on memory type. If type is NULL, info on all types is printed
+ * Function to retrieve data for heap on given socket
+ */
+int
+rte_malloc_get_socket_stats(int socket,
+               struct rte_malloc_socket_stats *socket_stats)
+{
+       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
+
+       if (socket >= RTE_MAX_NUMA_NODES || socket < 0)
+               return -1;
+
+       return malloc_heap_get_stats(&mcfg->malloc_heaps[socket], socket_stats);
+}
+
+/*
+ * Print stats on memory type. If type is NULL, info on all types is printed
  */
 void
 rte_malloc_dump_stats(__rte_unused const char *type)
 {
+       unsigned int socket;
+       struct rte_malloc_socket_stats sock_stats;
+       /* Iterate through all initialised heaps */
+       for (socket=0; socket< RTE_MAX_NUMA_NODES; socket++) {
+               if ((rte_malloc_get_socket_stats(socket, &sock_stats) < 0))
+                       continue;
+
+               printf("Socket:%u\n", socket);
+               printf("\tHeap_size:%zu,\n", sock_stats.heap_totalsz_bytes);
+               printf("\tFree_size:%zu,\n", sock_stats.heap_freesz_bytes);
+               printf("\tAlloc_size:%zu,\n", sock_stats.heap_allocsz_bytes);
+               printf("\tGreatest_free_size:%zu,\n",
+                               sock_stats.greatest_free_size);
+               printf("\tAlloc_count:%u,\n",sock_stats.alloc_count);
+               printf("\tFree_count:%u,\n", sock_stats.free_count);
+       }
        return;
 }
 
@@ -162,4 +228,3 @@ rte_malloc_set_limit(__rte_unused const char *type,
 {
        return 0;
 }
-