Actual source code: memc.c

petsc-3.4.4 2014-03-13
  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>

 14: /*@
 15:    PetscMemcmp - Compares two byte streams in memory.

 17:    Not Collective

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

 25:    Output Parameters:
 26: .   e - PETSC_TRUE if equal else PETSC_FALSE.

 28:    Level: intermediate

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

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

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

 53:    Not Collective

 55:    Input Parameters:
 56: +  b - pointer to initial memory space
 57: -  n - length (in bytes) of space to copy

 59:    Output Parameter:
 60: .  a - pointer to copy space

 62:    Level: intermediate

 64:    Note:
 65:    This routine is analogous to memmove().

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

 69:    Concepts: memory^copying with overlap
 70:    Concepts: copying^memory with overlap

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