Changeset 1198 for trunk/src/kash/memalloc.c
- Timestamp:
- Oct 6, 2007, 11:19:19 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kash/memalloc.c
r809 r1198 59 59 60 60 pointer 61 ckmalloc( int nbytes)61 ckmalloc(size_t nbytes) 62 62 { 63 63 pointer p; … … 65 65 p = malloc(nbytes); 66 66 if (p == NULL) 67 error( "Out of space");67 error(NULL, "Out of space"); 68 68 return p; 69 69 } … … 75 75 76 76 pointer 77 ckrealloc(pointer p, int nbytes)77 ckrealloc(pointer p, size_t nbytes) 78 78 { 79 79 p = realloc(p, nbytes); 80 80 if (p == NULL) 81 error( "Out of space");81 error(NULL, "Out of space"); 82 82 return p; 83 83 } … … 108 108 */ 109 109 110 #define MINSIZE 504 /* minimum size of a block */111 112 struct stack_block {113 struct stack_block *prev;114 char space[MINSIZE];115 };116 117 struct stack_block stackbase;118 struct stack_block *stackp = &stackbase;119 struct stackmark *markp;120 char *stacknxt = stackbase.space;121 int stacknleft = MINSIZE;122 int sstrnleft;123 int herefd = -1;110 //#define MINSIZE 504 /* minimum size of a block */ 111 112 //struct stack_block { 113 // struct stack_block *prev; 114 // char space[MINSIZE]; 115 //}; 116 117 //struct stack_block stackbase; 118 //struct stack_block *stackp = &stackbase; 119 //struct stackmark *markp; 120 //char *stacknxt = stackbase.space; 121 //int stacknleft = MINSIZE; 122 //int sstrnleft; 123 //int herefd = -1; 124 124 125 125 pointer 126 stalloc( int nbytes)126 stalloc(shinstance *psh, size_t nbytes) 127 127 { 128 128 char *p; 129 129 130 130 nbytes = SHELL_ALIGN(nbytes); 131 if (nbytes > stacknleft) {132 int blocksize;131 if (nbytes > psh->stacknleft) { 132 size_t blocksize; 133 133 struct stack_block *sp; 134 134 … … 138 138 INTOFF; 139 139 sp = ckmalloc(sizeof(struct stack_block) - MINSIZE + blocksize); 140 sp->prev = stackp;141 stacknxt = sp->space;142 stacknleft =blocksize;143 stackp = sp;140 sp->prev = psh->stackp; 141 psh->stacknxt = sp->space; 142 psh->stacknleft = (int)blocksize; 143 psh->stackp = sp; 144 144 INTON; 145 145 } 146 p = stacknxt;147 stacknxt += nbytes;148 stacknleft -=nbytes;146 p = psh->stacknxt; 147 psh->stacknxt += nbytes; 148 psh->stacknleft -= (int)nbytes; 149 149 return p; 150 150 } … … 152 152 153 153 void 154 stunalloc( pointer p)154 stunalloc(shinstance *psh, pointer p) 155 155 { 156 156 if (p == NULL) { /*DEBUG */ 157 write(2, "stunalloc\n", 10);158 abort();159 } 160 stacknleft += stacknxt - (char *)p;161 stacknxt = p;162 } 163 164 165 166 void 167 setstackmark(s truct stackmark *mark)168 { 169 mark->stackp = stackp;170 mark->stacknxt = stacknxt;171 mark->stacknleft = stacknleft;172 mark->marknext = markp;173 markp = mark;174 } 175 176 177 void 178 popstackmark(s truct stackmark *mark)157 shfile_write(&psh->fdtab, 2, "stunalloc\n", 10); 158 sh_abort(psh); 159 } 160 psh->stacknleft += (int)(psh->stacknxt - (char *)p); 161 psh->stacknxt = p; 162 } 163 164 165 166 void 167 setstackmark(shinstance *psh, struct stackmark *mark) 168 { 169 mark->stackp = psh->stackp; 170 mark->stacknxt = psh->stacknxt; 171 mark->stacknleft = psh->stacknleft; 172 mark->marknext = psh->markp; 173 psh->markp = mark; 174 } 175 176 177 void 178 popstackmark(shinstance *psh, struct stackmark *mark) 179 179 { 180 180 struct stack_block *sp; 181 181 182 182 INTOFF; 183 markp = mark->marknext;184 while ( stackp != mark->stackp) {185 sp = stackp;186 stackp = sp->prev;183 psh->markp = mark->marknext; 184 while (psh->stackp != mark->stackp) { 185 sp = psh->stackp; 186 psh->stackp = sp->prev; 187 187 ckfree(sp); 188 188 } 189 stacknxt = mark->stacknxt;190 stacknleft = mark->stacknleft;189 psh->stacknxt = mark->stacknxt; 190 psh->stacknleft = mark->stacknleft; 191 191 INTON; 192 192 } … … 204 204 205 205 void 206 growstackblock( void)207 { 208 int newlen = SHELL_ALIGN( stacknleft * 2 + 100);209 210 if ( stacknxt == stackp->space && stackp != &stackbase) {206 growstackblock(shinstance *psh) 207 { 208 int newlen = SHELL_ALIGN(psh->stacknleft * 2 + 100); 209 210 if (psh->stacknxt == psh->stackp->space && psh->stackp != &psh->stackbase) { 211 211 struct stack_block *oldstackp; 212 212 struct stackmark *xmark; … … 214 214 215 215 INTOFF; 216 oldstackp = stackp;217 sp = stackp;218 stackp = sp->prev;216 oldstackp = psh->stackp; 217 sp = psh->stackp; 218 psh->stackp = sp->prev; 219 219 sp = ckrealloc((pointer)sp, 220 220 sizeof(struct stack_block) - MINSIZE + newlen); 221 sp->prev = stackp;222 stackp = sp;223 stacknxt = sp->space;224 stacknleft = newlen;221 sp->prev = psh->stackp; 222 psh->stackp = sp; 223 psh->stacknxt = sp->space; 224 psh->stacknleft = newlen; 225 225 226 226 /* … … 228 228 * must be relocated to point to the new block 229 229 */ 230 xmark = markp;230 xmark = psh->markp; 231 231 while (xmark != NULL && xmark->stackp == oldstackp) { 232 xmark->stackp = stackp;233 xmark->stacknxt = stacknxt;234 xmark->stacknleft = stacknleft;232 xmark->stackp = psh->stackp; 233 xmark->stacknxt = psh->stacknxt; 234 xmark->stacknleft = psh->stacknleft; 235 235 xmark = xmark->marknext; 236 236 } 237 237 INTON; 238 238 } else { 239 char *oldspace = stacknxt;240 int oldlen = stacknleft;241 char *p = stalloc( newlen);239 char *oldspace = psh->stacknxt; 240 int oldlen = psh->stacknleft; 241 char *p = stalloc(psh, newlen); 242 242 243 243 (void)memcpy(p, oldspace, oldlen); 244 stacknxt = p; /* free the space */245 stacknleft += newlen; /* we just allocated */246 } 247 } 248 249 void 250 grabstackblock( int len)244 psh->stacknxt = p; /* free the space */ 245 psh->stacknleft += newlen; /* we just allocated */ 246 } 247 } 248 249 void 250 grabstackblock(shinstance *psh, int len) 251 251 { 252 252 len = SHELL_ALIGN(len); 253 stacknxt += len;254 stacknleft -= len;253 psh->stacknxt += len; 254 psh->stacknleft -= len; 255 255 } 256 256 … … 260 260 * to be a register. The macro STARTSTACKSTR initializes things. Then 261 261 * the user uses the macro STPUTC to add characters to the string. In 262 * effect, STPUTC( c, p) is the same as *p++ = c except that the stack is262 * effect, STPUTC(psh, c, p) is the same as *p++ = c except that the stack is 263 263 * grown as necessary. When the user is done, she can just leave the 264 * string there and refer to it using stackblock( ). Or she can allocate264 * string there and refer to it using stackblock(psh). Or she can allocate 265 265 * the space for it using grabstackstr(). If it is necessary to allow 266 266 * someone else to use the stack temporarily and then continue to grow … … 274 274 275 275 char * 276 growstackstr( void)277 { 278 int len = stackblocksize( );279 if ( herefd >= 0 && len >= 1024) {280 xwrite( herefd, stackblock(), len);281 sstrnleft = len - 1;282 return stackblock( );283 } 284 growstackblock( );285 sstrnleft = stackblocksize() - len - 1;286 return stackblock( ) + len;276 growstackstr(shinstance *psh) 277 { 278 int len = stackblocksize(psh); 279 if (psh->herefd >= 0 && len >= 1024) { 280 xwrite(psh, psh->herefd, stackblock(psh), len); 281 psh->sstrnleft = len - 1; 282 return stackblock(psh); 283 } 284 growstackblock(psh); 285 psh->sstrnleft = stackblocksize(psh) - len - 1; 286 return stackblock(psh) + len; 287 287 } 288 288 … … 292 292 293 293 char * 294 makestrspace( void)295 { 296 int len = stackblocksize( ) -sstrnleft;297 growstackblock( );298 sstrnleft = stackblocksize() - len;299 return stackblock( ) + len;300 } 301 302 void 303 ungrabstackstr( char *s, char *p)304 { 305 stacknleft += stacknxt - s;306 stacknxt = s;307 sstrnleft = stacknleft - (p - s);308 309 } 294 makestrspace(shinstance *psh) 295 { 296 int len = stackblocksize(psh) - psh->sstrnleft; 297 growstackblock(psh); 298 psh->sstrnleft = stackblocksize(psh) - len; 299 return stackblock(psh) + len; 300 } 301 302 void 303 ungrabstackstr(shinstance *psh, char *s, char *p) 304 { 305 psh->stacknleft += (int)(psh->stacknxt - s); 306 psh->stacknxt = s; 307 psh->sstrnleft = (int)(psh->stacknleft - (p - s)); 308 309 }
Note:
See TracChangeset
for help on using the changeset viewer.