Actual source code: mal.c
1: /*$Id: mal.c,v 1.57 2001/08/07 03:02:00 balay Exp $*/
2: /*
3: Code that allows a user to dictate what malloc() PETSc uses.
4: */
5: #include petsc.h
6: #if defined(PETSC_HAVE_STDLIB_H)
7: #include <stdlib.h>
8: #endif
9: #if defined(PETSC_HAVE_MALLOC_H)
10: #include <malloc.h>
11: #endif
12: #include "petscfix.h"
15: /*
16: We want to make sure that all mallocs of double or complex numbers are complex aligned.
17: 1) on systems with memalign() we call that routine to get an aligned memory location
18: 2) on systems without memalign() we
19: - allocate one sizeof(PetscScalar) extra space
20: - we shift the pointer up slightly if needed to get PetscScalar aligned
21: - if shifted we store at ptr[-1] the amount of shift (plus a cookie)
22: */
23: #define SHIFT_COOKIE 456123
25: /* need to use 16 and 8 below instead of sizeof() cause #if cannot handle sizeof() */
26: #if !defined(PETSC_MEMALIGN)
27: # if defined(PETSC_USE_COMPLEX)
28: # define PETSC_MEMALIGN 16
29: # else
30: # define PETSC_MEMALIGN 8
31: # endif
32: #endif
34: #undef __FUNCT__
36: int PetscMallocAlign(size_t mem,int line,char *func,char *file,char *dir,void** result)
37: {
38: if (mem == 0) SETERRQ(PETSC_ERR_MEM_MALLOC_0,"Cannot malloc size zero");
39: #if defined(PETSC_HAVE_DOUBLE_ALIGN_MALLOC) && (PETSC_MEMALIGN == 8)
40: *result = malloc(mem);
41: #elif defined(PETSC_HAVE_MEMALIGN)
42: *result = memalign(PETSC_MEMALIGN,mem);
43: #else
44: {
45: int *ptr,shift;
46: /*
47: malloc space for two extra chunks and shift ptr 1 + enough to get it PetscScalar aligned
48: */
49: ptr = (int*)malloc(mem + 2*PETSC_MEMALIGN);
50: if (ptr) {
51: shift = (int)(((unsigned long) ptr) % PETSC_MEMALIGN);
52: shift = (2*PETSC_MEMALIGN - shift)/sizeof(int);
53: ptr += shift;
54: ptr[-1] = shift + SHIFT_COOKIE ;
55: *result = (void*)ptr;
56: } else {
57: *result = 0;
58: }
59: }
60: #endif
61: if (!*result) SETERRQ1(PETSC_ERR_MEM,"Memory requested %lu",(long)mem);
62: return 0;
63: }
65: #undef __FUNCT__
67: int PetscFreeAlign(void *ptr,int line,char *func,char *file,char *dir)
68: {
69: int 0;
71: #if (!(defined(PETSC_HAVE_DOUBLE_ALIGN_MALLOC) && (PETSC_MEMALIGN == 8)) && !defined(PETSC_HAVE_MEMALIGN))
72: int shift;
73: /*
74: Previous int tells us how many ints the pointer has been shifted from
75: the original address provided by the system malloc().
76: */
77: shift = ((int *)ptr)[-1] - SHIFT_COOKIE;
78: if (shift > PETSC_MEMALIGN-1) return PetscError(line,func,file,dir,1,1,"Likely memory corruption in heap");
79: ptr = (void*)(((int*)ptr) - shift);
80: #endif
82: #if defined(PETSC_HAVE_FREE_RETURN_INT)
83: free(ptr);
84: if (ierr) {
85: return PetscError(line,func,file,dir,1,1,"System free returned error %dn",ierr);
86: }
87: #else
88: free(ptr);
89: #endif
90: return ierr;
91: }
93: /*
94: We never use the system free directly because on many machines it
95: does not return an error code.
96: */
97: #undef __FUNCT__
99: int PetscFreeDefault(void *ptr,int line,char *func,char *file,char *dir)
100: {
101: #if defined(PETSC_HAVE_FREE_RETURN_INT)
102: int free(ptr);
103: if (ierr) {
104: return PetscError(line,func,file,dir,1,1,"System free returned error %dn",ierr);
105: }
106: #else
107: free(ptr);
108: #endif
109: return 0;
110: }
112: int (*PetscTrMalloc)(size_t,int,char*,char*,char*,void**) = PetscMallocAlign;
113: int (*PetscTrFree)(void *,int,char*,char *,char*) = PetscFreeAlign;
115: PetscTruth petscsetmallocvisited = PETSC_FALSE;
117: #undef __FUNCT__
119: /*@C
120: PetscSetMalloc - Sets the routines used to do mallocs and frees.
121: This routine MUST be called before PetscInitialize() and may be
122: called only once.
124: Not Collective
126: Input Parameters:
127: + malloc - the malloc routine
128: - free - the free routine
130: Level: developer
132: Concepts: malloc
133: Concepts: memory^allocation
135: @*/
136: int PetscSetMalloc(int (*imalloc)(size_t,int,char*,char*,char*,void**),
137: int (*ifree)(void*,int,char*,char*,char*))
138: {
140: if (petscsetmallocvisited && (imalloc != PetscTrMalloc || ifree != PetscTrFree)) SETERRQ(PETSC_ERR_SUP,"cannot call multiple times");
141: PetscTrMalloc = imalloc;
142: PetscTrFree = ifree;
143: petscsetmallocvisited = PETSC_TRUE;
144: return(0);
145: }
147: #undef __FUNCT__
149: /*@C
150: PetscClearMalloc - Resets the routines used to do mallocs and frees to the
151: defaults.
153: Not Collective
155: Level: developer
157: Notes:
158: In general one should never run a PETSc program with different malloc() and
159: free() settings for different parts; this is because one NEVER wants to
160: free() an address that was malloced by a different memory management system
162: @*/
163: int PetscClearMalloc(void)
164: {
166: PetscTrMalloc = PetscMallocAlign;
167: PetscTrFree = PetscFreeAlign;
168: petscsetmallocvisited = PETSC_FALSE;
169: return(0);
170: }