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