+test_realloc_numa(void)
+{
+ /* check realloc_socket part */
+ int32_t socket_count = 0, socket_allocated, socket;
+ void *ptr1, *ptr2;
+ int ret = -1;
+ size_t size = 1024;
+
+ ptr1 = NULL;
+ for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
+ if (is_mem_on_socket(socket)) {
+ int j = 2;
+
+ socket_count++;
+ while (j--) {
+ /* j == 1 -> resizing */
+ ptr2 = rte_realloc_socket(ptr1, size,
+ RTE_CACHE_LINE_SIZE,
+ socket);
+ if (ptr2 == NULL) {
+ printf("NULL pointer returned from rte_realloc_socket\n");
+ goto end;
+ }
+
+ ptr1 = ptr2;
+ socket_allocated = addr_to_socket(ptr2);
+ if (socket_allocated != socket) {
+ printf("Requested socket (%d) doesn't mach allocated one (%d)\n",
+ socket, socket_allocated);
+ goto end;
+ }
+ size += RTE_CACHE_LINE_SIZE;
+ }
+ }
+ }
+
+ /* Print warnign if only a single socket, but don't fail the test */
+ if (socket_count < 2)
+ printf("WARNING: realloc_socket test needs memory on multiple sockets!\n");
+
+ ret = 0;
+end:
+ rte_free(ptr1);
+ return ret;
+}
+
+static int
+test_realloc(void)
+{
+ const char *heap_name = "realloc_heap";
+ int realloc_heap_socket;
+ unsigned int mem_sz = 1U << 13; /* 8K */
+ unsigned int page_sz = sysconf(_SC_PAGESIZE);
+ void *mem;
+ int ret;
+
+ /* page size may be bigger than total mem size, so adjust */
+ mem_sz = RTE_MAX(mem_sz, page_sz);
+
+ /*
+ * the realloc tests depend on specific layout of underlying memory, so
+ * to prevent accidental failures to do fragmented main heap, we will
+ * do all of our tests on an artificially created memory.
+ */
+ if (rte_malloc_heap_create(heap_name) != 0) {
+ printf("Failed to create external heap\n");
+ ret = -1;
+ goto end;
+ }
+ realloc_heap_socket = rte_malloc_heap_get_socket(heap_name);
+
+ mem = mmap(NULL, mem_sz, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ if (mem == MAP_FAILED) {
+ printf("Failed to allocate memory for external heap\n");
+ ret = -1;
+ goto heap_destroy;
+ }
+
+ if (rte_malloc_heap_memory_add(
+ heap_name, mem, mem_sz, NULL, 0, page_sz) != 0) {
+ printf("Failed to add memory to external heap\n");
+ ret = -1;
+ goto mem_free;
+ }
+
+ /* run the socket-bound tests */
+ ret = test_realloc_socket(realloc_heap_socket);
+ if (ret != 0)
+ goto mem_remove;
+
+ /* now, run the NUMA node tests */
+ ret = test_realloc_numa();
+
+mem_remove:
+ rte_malloc_heap_memory_remove(heap_name, mem, mem_sz);
+mem_free:
+ munmap(mem, mem_sz);
+heap_destroy:
+ rte_malloc_heap_destroy(heap_name);
+end:
+ return ret;
+}
+
+static int
+test_random_alloc_free(void *_ __rte_unused)