Actual source code: memc.c

petsc-3.7.5 2017-01-01
Report Typos and Errors
  2: /*
  3:     We define the memory operations here. The reason we just do not use
  4:   the standard memory routines in the PETSc code is that on some machines
  5:   they are broken.

  7: */
  8: #include <petscsys.h>        /*I  "petscsys.h"   I*/
  9: #include <petscbt.h>
 10: #include <../src/sys/utils/ftn-kernels/fcopy.h>
 11: #if defined(PETSC_HAVE_STRING_H)
 12: #include <string.h>
 13: #endif

 17: /*@
 18:    PetscMemcmp - Compares two byte streams in memory.

 20:    Not Collective

 22:    Input Parameters:
 23: +  str1 - Pointer to the first byte stream
 24: .  str2 - Pointer to the second byte stream
 25: -  len  - The length of the byte stream
 26:          (both str1 and str2 are assumed to be of length len)

 28:    Output Parameters:
 29: .   e - PETSC_TRUE if equal else PETSC_FALSE.

 31:    Level: intermediate

 33:    Note:
 34:    This routine is anologous to memcmp()
 35: @*/
 36: PetscErrorCode  PetscMemcmp(const void *str1,const void *str2,size_t len,PetscBool  *e)
 37: {
 38:   int r;

 41:   if (len > 0 && !str1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to compare at a null pointer");
 42:   if (len > 0 && !str2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to compare at a null pointer");
 43:   r = memcmp((char*)str1,(char*)str2,len);
 44:   if (!r) *e = PETSC_TRUE;
 45:   else    *e = PETSC_FALSE;
 46:   return(0);
 47: }

 51: /*@
 52:    PetscMemmove - Copies n bytes, beginning at location b, to the space
 53:    beginning at location a. Copying  between regions that overlap will
 54:    take place correctly.

 56:    Not Collective

 58:    Input Parameters:
 59: +  b - pointer to initial memory space
 60: -  n - length (in bytes) of space to copy

 62:    Output Parameter:
 63: .  a - pointer to copy space

 65:    Level: intermediate

 67:    Note:
 68:    This routine is analogous to memmove().

 70:    Since b can overlap with a, b cannot be declared as const

 72:    Concepts: memory^copying with overlap
 73:    Concepts: copying^memory with overlap

 75: .seealso: PetscMemcpy()
 76: @*/
 77: PetscErrorCode  PetscMemmove(void *a,void *b,size_t n)
 78: {
 80:   if (n > 0 && !a) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to copy to null pointer");
 81:   if (n > 0 && !b) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to copy from a null pointer");
 82: #if !defined(PETSC_HAVE_MEMMOVE)
 83:   if (a < b) {
 84:     if (a <= b - n) memcpy(a,b,n);
 85:     else {
 86:       memcpy(a,b,(int)(b - a));
 87:       PetscMemmove(b,b + (int)(b - a),n - (int)(b - a));
 88:     }
 89:   } else {
 90:     if (b <= a - n) memcpy(a,b,n);
 91:     else {
 92:       memcpy(b + n,b + (n - (int)(a - b)),(int)(a - b));
 93:       PetscMemmove(a,b,n - (int)(a - b));
 94:     }
 95:   }
 96: #else
 97:   memmove((char*)(a),(char*)(b),n);
 98: #endif
 99:   return(0);
100: }