xref: /petsc/src/sys/utils/memc.c (revision 8a2c3ff8b24df8ae55801480ce550b9f0897fcc9)
1e5c89e4eSSatish Balay 
2e5c89e4eSSatish Balay /*
3e5c89e4eSSatish Balay     We define the memory operations here. The reason we just do not use
4e5c89e4eSSatish Balay   the standard memory routines in the PETSc code is that on some machines
5e5c89e4eSSatish Balay   they are broken.
6e5c89e4eSSatish Balay 
7e5c89e4eSSatish Balay */
8c6db04a5SJed Brown #include <petscsys.h>        /*I  "petscsys.h"   I*/
9c6db04a5SJed Brown #include <petscbt.h>
10c6db04a5SJed Brown #include <../src/sys/utils/ftn-kernels/fcopy.h>
11*8a2c3ff8SSatish Balay #if defined(PETSC_HAVE_STRING_H)
12*8a2c3ff8SSatish Balay #include <string.h>
13*8a2c3ff8SSatish Balay #endif
14e5c89e4eSSatish Balay 
15e5c89e4eSSatish Balay #undef __FUNCT__
16e5c89e4eSSatish Balay #define __FUNCT__ "PetscMemcmp"
174610e317SSatish Balay /*@
18e5c89e4eSSatish Balay    PetscMemcmp - Compares two byte streams in memory.
19e5c89e4eSSatish Balay 
20e5c89e4eSSatish Balay    Not Collective
21e5c89e4eSSatish Balay 
22e5c89e4eSSatish Balay    Input Parameters:
23e5c89e4eSSatish Balay +  str1 - Pointer to the first byte stream
24e5c89e4eSSatish Balay .  str2 - Pointer to the second byte stream
25e5c89e4eSSatish Balay -  len  - The length of the byte stream
26e5c89e4eSSatish Balay          (both str1 and str2 are assumed to be of length len)
27e5c89e4eSSatish Balay 
28e5c89e4eSSatish Balay    Output Parameters:
29e5c89e4eSSatish Balay .   e - PETSC_TRUE if equal else PETSC_FALSE.
30e5c89e4eSSatish Balay 
31e5c89e4eSSatish Balay    Level: intermediate
32e5c89e4eSSatish Balay 
33e5c89e4eSSatish Balay    Note:
34e5c89e4eSSatish Balay    This routine is anologous to memcmp()
35e5c89e4eSSatish Balay @*/
367087cfbeSBarry Smith PetscErrorCode  PetscMemcmp(const void *str1,const void *str2,size_t len,PetscBool  *e)
37e5c89e4eSSatish Balay {
38e5c89e4eSSatish Balay   int r;
39e5c89e4eSSatish Balay 
40e5c89e4eSSatish Balay   PetscFunctionBegin;
41e32f2f54SBarry Smith   if (len > 0 && !str1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to compare at a null pointer");
42e32f2f54SBarry Smith   if (len > 0 && !str2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to compare at a null pointer");
43e5c89e4eSSatish Balay   r = memcmp((char*)str1,(char*)str2,len);
44e5c89e4eSSatish Balay   if (!r) *e = PETSC_TRUE;
45e5c89e4eSSatish Balay   else    *e = PETSC_FALSE;
46e5c89e4eSSatish Balay   PetscFunctionReturn(0);
47e5c89e4eSSatish Balay }
48e5c89e4eSSatish Balay 
49e5c89e4eSSatish Balay #undef __FUNCT__
50e5c89e4eSSatish Balay #define __FUNCT__ "PetscMemmove"
514610e317SSatish Balay /*@
52e5c89e4eSSatish Balay    PetscMemmove - Copies n bytes, beginning at location b, to the space
53e5c89e4eSSatish Balay    beginning at location a. Copying  between regions that overlap will
54e5c89e4eSSatish Balay    take place correctly.
55e5c89e4eSSatish Balay 
56e5c89e4eSSatish Balay    Not Collective
57e5c89e4eSSatish Balay 
58e5c89e4eSSatish Balay    Input Parameters:
59e5c89e4eSSatish Balay +  b - pointer to initial memory space
60e5c89e4eSSatish Balay -  n - length (in bytes) of space to copy
61e5c89e4eSSatish Balay 
62e5c89e4eSSatish Balay    Output Parameter:
63e5c89e4eSSatish Balay .  a - pointer to copy space
64e5c89e4eSSatish Balay 
65e5c89e4eSSatish Balay    Level: intermediate
66e5c89e4eSSatish Balay 
67e5c89e4eSSatish Balay    Note:
68e5c89e4eSSatish Balay    This routine is analogous to memmove().
69e5c89e4eSSatish Balay 
70300a7f5bSBarry Smith    Since b can overlap with a, b cannot be declared as const
71300a7f5bSBarry Smith 
72e5c89e4eSSatish Balay    Concepts: memory^copying with overlap
73e5c89e4eSSatish Balay    Concepts: copying^memory with overlap
74e5c89e4eSSatish Balay 
75e5c89e4eSSatish Balay .seealso: PetscMemcpy()
76e5c89e4eSSatish Balay @*/
777087cfbeSBarry Smith PetscErrorCode  PetscMemmove(void *a,void *b,size_t n)
78e5c89e4eSSatish Balay {
79e5c89e4eSSatish Balay   PetscFunctionBegin;
80e32f2f54SBarry Smith   if (n > 0 && !a) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to copy to null pointer");
81e32f2f54SBarry Smith   if (n > 0 && !b) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to copy from a null pointer");
82e5c89e4eSSatish Balay #if !defined(PETSC_HAVE_MEMMOVE)
83e5c89e4eSSatish Balay   if (a < b) {
84a297a907SKarl Rupp     if (a <= b - n) memcpy(a,b,n);
85a297a907SKarl Rupp     else {
86e5c89e4eSSatish Balay       memcpy(a,b,(int)(b - a));
87e5c89e4eSSatish Balay       PetscMemmove(b,b + (int)(b - a),n - (int)(b - a));
88e5c89e4eSSatish Balay     }
89e5c89e4eSSatish Balay   } else {
90a297a907SKarl Rupp     if (b <= a - n) memcpy(a,b,n);
91a297a907SKarl Rupp     else {
92e5c89e4eSSatish Balay       memcpy(b + n,b + (n - (int)(a - b)),(int)(a - b));
93e5c89e4eSSatish Balay       PetscMemmove(a,b,n - (int)(a - b));
94e5c89e4eSSatish Balay     }
95e5c89e4eSSatish Balay   }
96e5c89e4eSSatish Balay #else
97e5c89e4eSSatish Balay   memmove((char*)(a),(char*)(b),n);
98e5c89e4eSSatish Balay #endif
99e5c89e4eSSatish Balay   PetscFunctionReturn(0);
100e5c89e4eSSatish Balay }
101e5c89e4eSSatish Balay 
102e5c89e4eSSatish Balay 
103e5c89e4eSSatish Balay 
104e5c89e4eSSatish Balay 
105