## shrealloc

#### Purpose

Resizes a symmetric object that was allocated by shmalloc, shmemalign, or shrealloc.

#### C syntax

    #include <shmem.h>

    void *shrealloc(void *ptr, size_t size);

    extern long malloc_error;

#### Parameters

INPUT
size
  The minimum new desired size of the symmetric object.
ptr
  pointer to a symmetric object that allocated by shmalloc.

#### Description

The shrealloc function changes the size of the symmetric object to which ptr points to the size (in bytes) specified by size.

The contents of the block are unchanged up to the lesser of the new and old sizes, even if the block is moved. If the new size is larger, the value of the newly allocated portion of the block is indeterminate. If ptr is a null pointer, the shrealloc function behaves like the shmalloc function for the specified size. If size is 0 and ptr is not NULL, the object to which ptr points is freed. Otherwise, if ptr is not a pointer earlier returned by a symmetric heap function, or if the space has already been deallocated, the malloc_error variable is set to indicate the error and shrealloc returns NULL. If the space cannot be allocated, the block to which ptr points is unchanged and NULL is returned

It is required that all PEs must participate in the memory allocation and with the identical parameter. Internally, shmem_barrier_all is called before returning from the function, thus the allocated object can be used right away on all PEs. User has to ensure the same size parameter is used on all PEs when calling the function; otherwise, subsequent calls to the symmetric object allocation functions may not return the same address.

#### C examples

    #include <stdio.h>
    #include <string.h>
    #include <strings.h>

    #include <shmem.h>

    int main (int argc, char* argv[])
    {
        int total_tasks = -1;
        int my_task = -1;

        start_pes(0);

        total_tasks = _num_pes();

        if (total_tasks <= 0) {
            printf("FAILED\n");
            return 0;
        } else {
            printf("number of pes is %d\n", total_tasks);
        }

        my_task = _my_pe();

        if (my_task < 0){
            printf("FAILED\n");
            return 0;
        } else {
            printf("my pe id is %d\n", my_task);
        }

        int *addrs[10];
        for (int i=0; i<10; i++) {
            addrs[i] = (int *) shmalloc(sizeof(int)*1024);
            printf("addrs[%d] is %p after malloc\n", i, addrs[i]);
            bzero(addrs[i], sizeof(int)*1024);
            for (int j=0; j<1024; j++) {
                addrs[i][j] = j;
            }
        }

        for (int i=0; i<10; i++) {
            shfree(addrs[i]);
        }

        int *addr = NULL;
        addr = (int *) shmalloc(sizeof(int)*1024);
        printf("addr is %p after malloc\n", addr);

        addr = (int *) shrealloc(addr, sizeof(int)*1024);
        printf("addr is %p after realloc\n", addr);

        shfree(addr);

        for (int i=0; i<10; i++) {
            addrs[i] = (int *) shmemalign(0x2000, sizeof(int)*1024);
            printf("addrs[%d] is %p after memalign\n", i, addrs[i]);
        }

        for (int i=0; i<10; i++) {
            shfree(addrs[i]);
        }

        printf("PASSED\n");
        return 0;
    }

#### Related information

Subroutines: shfree, shmemallign, shmalloc

OpenSHMEM API Index