2 * $Source: x:/prj/tech/libsrc/lg/RCS/tmpalloc.c $
5 * $Date: 1997/06/02 14:51:16 $
7 * Routines for controlling temporary memory buffer.
9 * This file is part of the 2d library.
17 /* arbitrary size for buffer. used if a buffer isn't explicitly set. */
18 #define TEMP_BUF_SIZE 3*16384
20 /* memstack to use for temporary memory requests. */
21 static MemStack
*temp_mem_stack
=NULL
;
23 /* TRUE if buffer is allocated by temp_mem_init. */
24 static bool stack_dynamic
=FALSE
;
26 MemStack
*temp_mem_get_stack(void)
28 return temp_mem_stack
;
31 /* sets the memstack to be used by the temporary memory routines to ms.
32 if ms is NULL, it attempts to allocate a dynamic buffer of size given
33 by TEMP_BUF_SIZE. returns 0 if all is well, nonzero if there is an
35 int temp_mem_init(MemStack
*ms
)
38 /* allocate memstack struct and buffer dynamically. */
40 ("TempMemInit: dynamically allocating stack of %d bytes\n",
42 if ((ms
=(MemStack
*)Malloc(sizeof(MemStack
)+TEMP_BUF_SIZE
))==NULL
) {
43 Warning(("TempMemInit: can't allocate dynamic buffer.\n"));
47 ms
->baseptr
=(void *)(ms
+1);
50 temp_mem_stack
=ms
; /* save pointer to temp memstack */
53 /* use passed in memstack. */
59 /* sets the memstack used by the temporary memory routines to NULL.
60 if the buffer was allocated dynamically, it's freed. */
61 int temp_mem_uninit(void)
63 if (stack_dynamic
==TRUE
) {
65 ("TempMemUninit: freeing dynamically allocated stack\n"));
73 /* allocate a temporary buffer of size n from temp_mem_stack. */
74 void *temp_malloc(long n
)
76 if (temp_mem_stack
==NULL
)
77 if (temp_mem_init(NULL
)!=0)
79 return MemStackAlloc(temp_mem_stack
,n
);
82 /* resize temporary buffer pointed to by p to be new size n. */
83 void *temp_realloc(void *p
,long n
)
85 return MemStackRealloc(temp_mem_stack
,p
,n
);
88 /* free temporary buffer pointed to by p. */
89 int temp_free(void *p
)
91 return MemStackFree(temp_mem_stack
,p
)==FALSE
;
95 /* the spewing versions of the temporary memory routines print out
96 additional information about the call to the real routine, including
97 the file name and line number where the call was made. */
98 int temp_spew_mem_init(MemStack
*ms
,char *file
,int line
)
102 Spew(DSRC_LG_Tempmem
,
103 ("TempMemInit: stack: %p rval: %d (file: %s line: %d)\n",
104 temp_mem_stack
,r
,file
,line
));
108 int temp_spew_mem_uninit(char *file
,int line
)
113 Spew(DSRC_LG_Tempmem
,
114 ("TempMemUninit rval: %d (file: %s line: %s)\n",
119 void *temp_spew_malloc(long size
,char *file
,int line
)
123 Spew(DSRC_LG_Tempmem
,
124 ("TempMalloc: p: 0x%x size: %d (file: %s line: %d)\n",
129 void *temp_spew_realloc(void *ptr
,long size
,char *file
,int line
)
132 p
=temp_realloc(ptr
,size
);
133 Spew(DSRC_LG_Tempmem
,
134 ("TempRealloc: p: 0x%x pold: 0x%x size: %d (file: %s line: %d)\n",
135 p
,ptr
,size
,file
,line
));
139 int temp_spew_free(void *ptr
,char *file
,int line
)
143 Spew(DSRC_LG_Tempmem
,
144 ("TempFree: p: 0x%x (file: %s line: %d)\n",