xref: /petsc/src/sys/utils/memc.c (revision fe998a80077c9ee0917a39496df43fc256e1b478)
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 #if defined(PETSC_HAVE_STRING_H)
12 #include <string.h>
13 #endif
14 
15 #undef __FUNCT__
16 #define __FUNCT__ "PetscMemcmp"
17 /*@
18    PetscMemcmp - Compares two byte streams in memory.
19 
20    Not Collective
21 
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)
27 
28    Output Parameters:
29 .   e - PETSC_TRUE if equal else PETSC_FALSE.
30 
31    Level: intermediate
32 
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;
39 
40   PetscFunctionBegin;
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   PetscFunctionReturn(0);
47 }
48 
49 #undef __FUNCT__
50 #define __FUNCT__ "PetscMemmove"
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.
55 
56    Not Collective
57 
58    Input Parameters:
59 +  b - pointer to initial memory space
60 -  n - length (in bytes) of space to copy
61 
62    Output Parameter:
63 .  a - pointer to copy space
64 
65    Level: intermediate
66 
67    Note:
68    This routine is analogous to memmove().
69 
70    Since b can overlap with a, b cannot be declared as const
71 
72    Concepts: memory^copying with overlap
73    Concepts: copying^memory with overlap
74 
75 .seealso: PetscMemcpy()
76 @*/
77 PetscErrorCode  PetscMemmove(void *a,void *b,size_t n)
78 {
79   PetscFunctionBegin;
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   PetscFunctionReturn(0);
100 }
101 
102 
103 
104 
105