Debian Codegraph @

generic/bltAlloc.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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 * Using private is dangerous.
45 * Tcl changes mem functions for debug which will conflict then with BLT
46 * stick to public API
47 */
•48 //EXTERN Blt_MallocProc TclpAlloc;
49 //EXTERN Blt_FreeProc TclpFree;
50 //EXTERN Blt_ReallocProc TclpRealloc;
51
52 //Blt_MallocProc *Blt_MallocProcPtr = TclpAlloc;
53 //Blt_FreeProc *Blt_FreeProcPtr = TclpFree;
54 //Blt_ReallocProc *Blt_ReallocProcPtr = TclpRealloc;
55
56 Blt_MallocProc *Blt_MallocProcPtr = (Blt_MallocProc *)Tcl_Alloc;
57 Blt_FreeProc *Blt_FreeProcPtr = (Blt_FreeProc *)Tcl_Free;
58 Blt_ReallocProc *Blt_ReallocProcPtr = (Blt_ReallocProc *)Tcl_Realloc;
59
60 #else
61
62 Blt_MallocProc *Blt_MallocProcPtr = malloc;
63 Blt_FreeProc *Blt_FreeProcPtr = free;
64 Blt_ReallocProc *Blt_ReallocProcPtr = realloc;
65
66 #endif /* >= 8.1.0 */
67 #endif /* WIN32 */
68
69 void *
70 Blt_Calloc(nElems, sizeOfElem)
71 unsigned int nElems;
72 size_t sizeOfElem;
73 {
74 char *ptr;
75 size_t size;
76
77 size = nElems * sizeOfElem;
78 ptr = Blt_Malloc(size);
79 if (ptr != NULL) {
80 memset(ptr, 0, size);
81 }
82 return ptr;
83 }
84
85 /*
86 *----------------------------------------------------------------------
87 *
88 * Blt_Strdup --
89 *
90 * Create a copy of the string from heap storage.
91 *
92 * Results:
93 * Returns a pointer to the need string copy.
94 *
95 *----------------------------------------------------------------------
96 */
97 char *
98 Blt_Strdup(string)
99 CONST char *string;
100 {
101 size_t size;
102 char *ptr;
103
104 size = strlen(string) + 1;
105 ptr = Blt_Malloc(size * sizeof(char));
106 if (ptr != NULL) {
107 strcpy(ptr, string);
108 }
109 return ptr;
110 }
111