ide_alloc.c 9.16 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
/*
 * ide memory allocation routines
 *
 * All memory is allocated from static pools.
 *
 */

#include "ide.h"
#ident "gfx/common/ide/ide_alloc.c $Revision: 1.1.1.1 $"

#define NEWSYMC 4096	/* total number of symbol records */
#define NEWNODEC 16384	/* total number of node records */

#define NSTR1 2048	/* size of 16 character string pool */
#define NSTR2 1024	/* size of 32 character string pool */
#define NSTR3 512	/* size of 64 character string pool */
#define NSTR4 256	/* size of 128 character string pool */
#define NSTR5 128	/* size of 256 character string pool */

sym_t	*freesym = 0;	/* list of freed symbol records */
sym_t	newsym[NEWSYMC];/* symbol record pool */
int	newsymc = 0;	/* index into newsym */

node_t	*freenode = 0;	/* list of freed node records */
node_t	newnode[NEWNODEC];/* node record pool */
int	newnodec = 0;	/* index into newnode */

/*
 * The following arrays are used for string allocation.
 * Strings are allocated on a best fit of a fixed length
 * storage block. A null byte in the first position of an
 * allocation string indicates that the string is free.
 */
char	string1[NSTR1][16];
char	string2[NSTR2][32];
char	string3[NSTR3][64];
char	string4[NSTR4][128];
char	string5[NSTR5][256];

int	last1 = 0;	/* last string allocated from string1 */
int	last2 = 0;	/*  "     "        "      "   string2 */
int	last3 = 0;	/*  "     "        "      "   string3 */
int	last4 = 0;	/*  "     "        "      "   string4 */
int	last5 = 0;	/*  "     "        "      "   string5 */

int	nstr1 = 0;
int	nstr2 = 0;
int	nstr3 = 0;
int	nstr4 = 0;
int	nstr5 = 0;

int	nnodes = 0;
int	nsyms  = 0;

showmstats()
{
	printf("strings: 16 x %d, 32 x %d, 64 x %d, 128 x %d, 256 x %d\n",
		nstr1, nstr2, nstr3, nstr4, nstr5);

	printf("symbols: %d out of %d used.\n", nsyms, NEWSYMC);
	printf("nodes: %d out of %d used.\n", nnodes, NEWNODEC);
}

/*
 * descend a parse tree freeing each node
 *
 * The descent path must match the tree pattern created in ide_gram.y
 *
 */
destroytree(pnode)
node_t *pnode;
{
	if ( ! pnode )
		return;

	switch ( pnode->node_type ) {
		case OP_STMT:
			destroytree( pnode->node_right);
			destroytree( pnode->node_left);
			break;
		case OP_PRINT:
			destroyexpr(pnode->node_left);
			destroynode(pnode);
			break;
		case OP_IF:
			destroyif(pnode);
			break;
		case OP_WHILE:
			destroywhile(pnode);
			break;
		case OP_FOR:
			destroyfor(pnode);
			break;
		case OP_RETURN:
			destroynode(pnode);
			break;
		case OP_BREAK:
			destroynode(pnode);
			break;
		case OP_CONTINUE:
			destroynode(pnode);
			break;
		case OP_DO:
			destroywhile(pnode);
			break;
		case OP_SWITCH:
			destroyswitch(pnode);
			break;
		case OP_CMD:
			destroycmd(pnode);
			break;
		case OP_REPEAT:
			destroyrepeat(pnode);
			break;
		default:
			destroyexpr(pnode);
			break;
		}
}

destroyrepeat(pnode)
node_t *pnode;
{
	destroytree(pnode->node_right);
	destroysym(pnode->node_psym);
	destroynode(pnode);
}
destroycmd(pnode)
node_t *pnode;
{

	destroyargv(pnode->node_right);
	destroynode(pnode);
}

destroyargv(pnode)
node_t *pnode;
{
	if ( ! pnode )
		return;

	destroyargv(pnode->node_right);
	destroyexpr(pnode->node_left);
	destroynode(pnode);
}
destroyswitch(pnode)
node_t *pnode;
{

	destroysym(pnode->node_psym);
	destroycaselist(pnode->node_right);
	destroynode(pnode);

}

destroycaselist( pnode )
node_t *pnode;
{
	if ( ! pnode )
		return;

	if ( pnode->node_type == OP_CASELIST ) {
		destroycaselist( pnode->node_right );
		pnode = pnode->node_left;
		}
	
	if ( pnode->node_psym )
		destroysym(pnode->node_psym);
	
	destroytree( pnode->node_right );
}

destroyif(pnode)
node_t *pnode;
{
	destroyexpr(pnode->node_left);
	if ( pnode->node_right->node_type == OP_ELSE ) {
		destroytree(pnode->node_right->node_left);
		destroytree(pnode->node_right->node_right);
		}
	
	destroynode(pnode->node_right);
	destroynode(pnode);
}

destroyfor(pnode)
node_t *pnode;
{
	node_t *n1 = pnode->node_left->node_left;
	node_t *n2 = pnode->node_left->node_right;
	node_t *n3 = pnode->node_right->node_left;
	node_t *n4 = pnode->node_right->node_right;

	if (n1) destroyexpr(n1);
	if (n2) destroyexpr(n2);
	if (n3) destroyexpr(n3);
	destroytree(n4);

	destroynode(pnode->node_left);
	destroynode(pnode->node_right);
	destroynode(pnode);
}

destroywhile(pnode)
node_t *pnode;
{
	destroyexpr(pnode->node_left);
	destroytree(pnode->node_right);
	destroynode(pnode);
}

destroyexpr(pnode)
node_t *pnode;
{
	if ( ! pnode )
		return;

	switch(pnode->node_type) {
		case OP_SHL:	case OP_SHR:
		case OP_ADD:	case OP_SUB:
		case OP_MUL:	case OP_DIV:
		case OP_MOD:	case OP_LT:	case OP_LE:
		case OP_GT:	case OP_GE:	case OP_EQ:	case OP_NE:
		case OP_OR:	case OP_AND:	case OP_ANDAND:	case OP_OROR:
			destroyexpr(pnode->node_right);
			destroyexpr(pnode->node_left);
			break;
		case OP_ASSIGN:
		case OP_ADDASS: case OP_SUBASS:
		case OP_MULASS: case OP_DIVASS: case OP_MODASS:
		case OP_SHLASS: case OP_SHRASS:
		case OP_ANDASS: case OP_ORASS:
			destroyexpr(pnode->node_right);
			/* Assignments are only made to things in the
			   symbol table so
			   destroysym(pnode->node_psym); is not necessary */
			break;
		case OP_UMINUS:
		case OP_COM:
		case OP_NOT:
			destroyexpr(pnode->node_right);
			break;
		case OP_PRED:
		case OP_PREI:
		case OP_POSTD:
		case OP_POSTI:
			break;
		case OP_VAR:
			destroysym(pnode->node_psym);
			break;
		case OP_FCALL:
		case OP_ARGLIST:
			break;
		}

	destroynode(pnode);
}

/*
 * destroyed nodes are put at the head of the free list
 *
 */
destroynode(pnode)
node_t *pnode;
{
	nnodes--;
	pnode->node_right = freenode;
	freenode = pnode;
	return;
}

/*
 * destroyed symbols are put at the head of the free list
 *
 */
destroysym(psym)
sym_t *psym;
{
	/* symbols with names are permanent */
	if ( psym->sym_flags & SYM_INTABLE )
		return;

	nsyms--;

	if ( psym->sym_basetype == SYM_STR )
		destroystr(psym->sym_s);
	
	psym->sym_hlink = freesym;
	freesym = psym;

	return;
}

/*
 * free a string by setting its string table allocation byte
 * to null.
 */
destroystr(s)
char *s;
{
	if ( ! s )
		return;

	if ( *(s-1) == '1' )
		nstr1--;
	else
	if ( *(s-1) == '2' )
		nstr2--;
	else
	if ( *(s-1) == '3' )
		nstr3--;
	else
	if ( *(s-1) == '4' )
		nstr4--;
	else
	if ( *(s-1) == '5' )
		nstr5--;
	else
		ide_panic("Bad call to destroystr");

	*(s-1) = '\0';
}

/*
 * create a new symbol record by either taking one off of the free list
 * or getting another one out of the pool.
 *
 */
sym_t *
makesym()
{
	sym_t *tmp;

	nsyms++;


	if ( freesym ) {
		tmp = freesym;
		freesym = tmp->sym_hlink;
		tmp->sym_indx	= 0;
		tmp->sym_basetype = 0;
		tmp->sym_type	= 0;
		tmp->sym_i	= 0;
		tmp->sym_hlink	= 0;
		tmp->sym_flags  = 0;
		return tmp;
		}
	

	if ( newsymc <  NEWSYMC )
		return &newsym[newsymc++];
	else
		ide_panic("Out of symbols\n");
}

/*
 * same algorithm as above for nodes
 */
node_t *
makenode()
{
	node_t *tmp;

	nnodes++;

	if ( freenode ) {
		tmp = freenode;
		freenode = tmp->node_right;
		tmp->node_type  = 0;
		tmp->node_right = 0;
		tmp->node_left  = 0;
		return tmp;
		}
	
	if ( newnodec < NEWNODEC )
		return &newnode[newnodec++];
	else
		ide_panic("Out of nodes\n");
}
/*
 * allocate space for string
 *
 * A best fit out of 5 possible pools algorithm is used.  The
 * -1 position of the string stores a status byte, null indicates
 * that the string is free.
 *
 */
char *
talloc(len)
int len;
{
	int i;
	char *t;

	len += 1;
	if ( len <= 16 )
		for ( i = last1; i < last1+NSTR1; i++ )
			if ( string1[i % NSTR1][0] == '\0' ) {
				last1 = i % NSTR1;
				t = &string1[last1][1];
				string1[last1][0] = '1';
				nstr1++;
				return t;
				}

	if ( len <= 32 )
		for ( i = last2; i < last2+NSTR2; i++ )
			if ( string2[i % NSTR2][0] == '\0' ) {
				last2 = i % NSTR2;
				t = &string2[last2][1];
				string2[last2][0] = '2';
				nstr2++;
				return t;
				}

	if ( len <= 64 )
		for ( i = last3; i < last3+NSTR3; i++ )
			if ( string3[i % NSTR3][0] == '\0' ) {
				last3 = i % NSTR3;
				t = &string3[last3][1];
				string3[last3][0] = '3';
				nstr3++;
				return t;
				}

	if ( len <= 128 )
		for ( i = last4; i < last4+NSTR4; i++ )
			if ( string4[i % NSTR4][0] == '\0' ) {
				last4 = i % NSTR4;
				t = &string4[last4][1];
				string4[last4][0] = '4';
				nstr4++;
				return t;
				}

	if ( len <= NSTR5 )
		for ( i = last5; i < last5+NSTR5; i++ )
			if ( string5[i % NSTR5][0] == '\0' ) {
				last5 = i % NSTR5;
				t = &string5[last5][1];
				string5[last5][0] = '5';
				nstr5++;
				return t;
				}
	
	ide_panic("Out of string space");
}
/*
 * get space for a string and copy into it
 */
char *
makestr(s)
char *s;
{
	int len = strlen(s);
	char *t;

	t = talloc(len+1);

	strcpy(t, s);

	return t;
}
/*
 * get space for a pair of strings (to be concatenated together) and 
 * copy into it
 */
char *
makecatstring(s1, s2)
char *s1, *s2;
{
	int len = strlen(s1) + strlen(s2);
	char *t;

	t = talloc(len+1);

	strcpy(t, s1);
	strcat(t, s2);

	return t;
}
ide_panic(s)
char *s;
{
	printf("ide panic: %s\n",s);
	exit(1);
}
/*
 * make a symbol out of an integer
 *
 */
sym_t *
makeisym(i)
int i;
{
	sym_t *tmp = makesym();

	tmp->sym_basetype = SYM_INT;
	tmp->sym_type = SYM_CON;
	tmp->sym_i = i;

	return tmp;
}
/*
 * make a symbol out of a string
 *
 */
sym_t *
makessym(s)
char *s;
{
	sym_t *tmp = makesym();

	tmp->sym_basetype = SYM_STR;
	tmp->sym_type = SYM_CON;
	tmp->sym_s = s;

	return tmp;
}