xref: /petsc/src/sys/utils/memc.c (revision e6d157a596aafecd620e87b08a69361f371b32d0)
1 
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.
6 
7 */
8 #include <petscsys.h>        /*I  "petscsys.h"   I*/
9 #include <petscbt.h>
10 #include <../src/sys/utils/ftn-kernels/fcopy.h>
11 
12 #undef __FUNCT__
13 #define __FUNCT__ "PetscMemcmp"
14 /*@
15    PetscMemcmp - Compares two byte streams in memory.
16 
17    Not Collective
18 
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)
24 
25    Output Parameters:
26 .   e - PETSC_TRUE if equal else PETSC_FALSE.
27 
28    Level: intermediate
29 
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;
36 
37   PetscFunctionBegin;
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   PetscFunctionReturn(0);
44 }
45 
46 #undef __FUNCT__
47 #define __FUNCT__ "PetscMemmove"
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.
52 
53    Not Collective
54 
55    Input Parameters:
56 +  b - pointer to initial memory space
57 -  n - length (in bytes) of space to copy
58 
59    Output Parameter:
60 .  a - pointer to copy space
61 
62    Level: intermediate
63 
64    Note:
65    This routine is analogous to memmove().
66 
67    Since b can overlap with a, b cannot be declared as const
68 
69    Concepts: memory^copying with overlap
70    Concepts: copying^memory with overlap
71 
72 .seealso: PetscMemcpy()
73 @*/
74 PetscErrorCode  PetscMemmove(void *a,void *b,size_t n)
75 {
76   PetscFunctionBegin;
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) {
82       memcpy(a,b,n);
83     } else {
84       memcpy(a,b,(int)(b - a));
85       PetscMemmove(b,b + (int)(b - a),n - (int)(b - a));
86     }
87   }  else {
88     if (b <= a - n) {
89       memcpy(a,b,n);
90     } else {
91       memcpy(b + n,b + (n - (int)(a - b)),(int)(a - b));
92       PetscMemmove(a,b,n - (int)(a - b));
93     }
94   }
95 #else
96   memmove((char*)(a),(char*)(b),n);
97 #endif
98   PetscFunctionReturn(0);
99 }
100 
101 
102 
103 
104