1 #include "bltInt.h"
2 
3 #ifndef linux
4 #ifdef HAVE_MALLOC_H
5 #include <malloc.h>
6 #endif /* HAVE_MALLOC_H */
7 #endif
8 
9 /*
10  * Blt_MallocProcPtr, Blt_FreeProcPtr --
11  *
12  *	These global variables allow you to override the default
13  *	memory allocation/deallocation routines, simply by setting the
14  *	pointers to your own C functions.  By default, we try to use
15  *	the same memory allocation scheme that Tcl is using: generally
16  *	that's Tcl_Alloc and Tcl_Free.
17  */
18 #ifdef WIN32
19 
20 #ifdef __GNUC__
21 extern char *Tcl_Alloc _ANSI_ARGS_((unsigned int size));
22 extern void Tcl_Free _ANSI_ARGS_((char * ptr));
23 extern char *Tcl_Realloc _ANSI_ARGS_((char *ptr, unsigned int size));
24 #endif /*__GNUC__*/
25 
26 Blt_MallocProc *Blt_MallocProcPtr = (Blt_MallocProc *)Tcl_Alloc;
27 Blt_FreeProc *Blt_FreeProcPtr = (Blt_FreeProc *)Tcl_Free;
28 Blt_ReallocProc *Blt_ReallocProcPtr = (Blt_ReallocProc *)Tcl_Realloc;
29 
30 #else
31 
32 /*
33  * Try to use the same memory allocator/deallocator that Tcl is
34  * using. Before 8.1 it used malloc/free.
35  */
36 
37 #if (TCL_VERSION_NUMBER >= _VERSION(8,1,0))
38 /*
39  * We're pointing to the private TclpAlloc/TclpFree instead of public
40  * Tcl_Alloc/Tcl_Free routines because they don't automatically cause
41  * a panic when not enough memory is available. There are cases (such
42  * as allocating a very large vector) where it's recoverable.
43  */
44 EXTERN Blt_MallocProc TclpAlloc;
45 EXTERN Blt_FreeProc TclpFree;
46 EXTERN Blt_ReallocProc TclpRealloc;
47 
48 Blt_MallocProc *Blt_MallocProcPtr = TclpAlloc;
49 Blt_FreeProc *Blt_FreeProcPtr = TclpFree;
50 Blt_ReallocProc *Blt_ReallocProcPtr = TclpRealloc;
51 #else
52 
53 Blt_MallocProc *Blt_MallocProcPtr = malloc;
54 Blt_FreeProc *Blt_FreeProcPtr = free;
55 Blt_ReallocProc *Blt_ReallocProcPtr = realloc;
56 
57 #endif /* >= 8.1.0 */
58 #endif /* WIN32 */
59 
60 void *
Blt_Calloc(nElems,sizeOfElem)61 Blt_Calloc(nElems, sizeOfElem)
62     unsigned int nElems;
63     size_t sizeOfElem;
64 {
65     char *ptr;
66     size_t size;
67 
68     size = nElems * sizeOfElem;
69     ptr = Blt_Malloc(size);
70     if (ptr != NULL) {
71 	memset(ptr, 0, size);
72     }
73     return ptr;
74 }
75 
76 /*
77  *----------------------------------------------------------------------
78  *
79  * Blt_Strdup --
80  *
81  *      Create a copy of the string from heap storage.
82  *
83  * Results:
84  *      Returns a pointer to the need string copy.
85  *
86  *----------------------------------------------------------------------
87  */
88 char *
Blt_Strdup(string)89 Blt_Strdup(string)
90     CONST char *string;
91 {
92     size_t size;
93     char *ptr;
94 
95     size = strlen(string) + 1;
96     ptr = Blt_Malloc(size * sizeof(char));
97     if (ptr != NULL) {
98 	strcpy(ptr, string);
99     }
100     return ptr;
101 }
102 
103