convert line ends
[canaan.git] / prj / tech / libsrc / lg / tmpalloc.c
blob739904a4dd9dd7d57308549e176f73290394352d
1 /*
2 * $Source: x:/prj/tech/libsrc/lg/RCS/tmpalloc.c $
3 * $Revision: 1.6 $
4 * $Author: JAEMZ $
5 * $Date: 1997/06/02 14:51:16 $
7 * Routines for controlling temporary memory buffer.
9 * This file is part of the 2d library.
12 #include <dbg.h>
13 #include <memall.h>
14 #include <tmpalloc.h>
15 #include <_lg.h>
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
34 error. */
35 int temp_mem_init(MemStack *ms)
37 if (ms==NULL) {
38 /* allocate memstack struct and buffer dynamically. */
39 Spew(DSRC_LG_Tempmem,
40 ("TempMemInit: dynamically allocating stack of %d bytes\n",
41 TEMP_BUF_SIZE));
42 if ((ms=(MemStack *)Malloc(sizeof(MemStack)+TEMP_BUF_SIZE))==NULL) {
43 Warning(("TempMemInit: can't allocate dynamic buffer.\n"));
44 return -1;
46 stack_dynamic=TRUE;
47 ms->baseptr=(void *)(ms+1);
48 ms->sz=TEMP_BUF_SIZE;
49 MemStackInit(ms);
50 temp_mem_stack=ms; /* save pointer to temp memstack */
51 return 0;
52 } else {
53 /* use passed in memstack. */
54 temp_mem_stack=ms;
55 return 0;
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) {
64 Spew(DSRC_LG_Tempmem,
65 ("TempMemUninit: freeing dynamically allocated stack\n"));
66 Free(temp_mem_stack);
67 stack_dynamic=FALSE;
69 temp_mem_stack=NULL;
70 return 0;
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)
78 return NULL;
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;
94 #ifdef DBG_ON
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)
100 int r;
101 r=temp_mem_init(ms);
102 Spew(DSRC_LG_Tempmem,
103 ("TempMemInit: stack: %p rval: %d (file: %s line: %d)\n",
104 temp_mem_stack,r,file,line));
105 return r;
108 int temp_spew_mem_uninit(char *file,int line)
110 int r;
112 r=temp_mem_uninit();
113 Spew(DSRC_LG_Tempmem,
114 ("TempMemUninit rval: %d (file: %s line: %s)\n",
115 r,file,line));
116 return r;
119 void *temp_spew_malloc(long size,char *file,int line)
121 void *p;
122 p=temp_malloc(size);
123 Spew(DSRC_LG_Tempmem,
124 ("TempMalloc: p: 0x%x size: %d (file: %s line: %d)\n",
125 p,size,file,line));
126 return p;
129 void *temp_spew_realloc(void *ptr,long size,char *file,int line)
131 void *p;
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));
136 return p;
139 int temp_spew_free(void *ptr,char *file,int line)
141 int r;
142 r=temp_free(ptr);
143 Spew(DSRC_LG_Tempmem,
144 ("TempFree: p: 0x%x (file: %s line: %d)\n",
145 ptr,file,line));
146 return r;
148 #endif /* DBG_ON */