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 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
|
/*
* block - fixed, dynamic, fifo and circular memory blocks
*
* Copyright (C) 1999 Landon Curt Noll and Ernest Bowen
*
* Primary author: Landon Curt Noll
*
* Calc is open software; you can redistribute it and/or modify it under
* the terms of the version 2.1 of the GNU Lesser General Public License
* as published by the Free Software Foundation.
*
* Calc is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
* Public License for more details.
*
* A copy of version 2.1 of the GNU Lesser General Public License is
* distributed with calc under the filename COPYING-LGPL. You should have
* received a copy with calc; if not, write to Free Software Foundation, Inc.
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* @(#) $Revision: 29.3 $
* @(#) $Id: block.c,v 29.3 2006/05/01 19:16:57 chongo Exp $
* @(#) $Source: /usr/local/src/cmd/calc/RCS/block.c,v $
*
* Under source code control: 1997/02/27 00:29:40
* File existed as early as: 1997
*
* chongo <was here> /\oo/\ http://www.isthe.com/chongo/
* Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
*/
#include <stdio.h>
#include "value.h"
#include "zmath.h"
#include "config.h"
#include "block.h"
#include "nametype.h"
#include "string.h"
#include "calcerr.h"
#define NBLOCKCHUNK 16
static long nblockcount = 0;
static long maxnblockcount = 0;
static STRINGHEAD nblocknames;
static NBLOCK **nblocks;
/* forward declarations */
static void blkchk(BLOCK*);
/*
* blkalloc - allocate a block
*
* given:
* len - initial memory length of the block
* type - BLK_TYPE_XYZ
* chunk - allocation chunk size
*
* returns:
* pointer to a newly allocated BLOCK
*/
BLOCK *
blkalloc(int len, int chunk)
{
BLOCK *nblk; /* new block allocated */
/*
* firewall
*/
if (len < 0)
len = 0;
if (chunk <= 0)
chunk = BLK_CHUNKSIZE;
/*
* allocate BLOCK
*/
nblk = (BLOCK *)malloc(sizeof(BLOCK));
if (nblk == NULL) {
math_error("cannot allocate block");
/*NOTREACHED*/
}
/*
* initialize BLOCK
*/
nblk->blkchunk = chunk;
nblk->maxsize = ((len+chunk)/chunk)*chunk;
nblk->data = (USB8*)malloc(nblk->maxsize);
if (nblk->data == NULL) {
math_error("cannot allocate block data storage");
/*NOTREACHED*/
}
memset(nblk->data, 0, nblk->maxsize);
nblk->datalen = len;
/*
* return BLOCK
*/
if (conf->calc_debug & CALCDBG_BLOCK) {
blkchk(nblk);
}
return nblk;
}
/*
* blk_free - free a block
*
* NOTE: THIS IS NOT THE CALC blktrunc() BUILTIN FUNCTION!! This
* is what is called to free block storage.
*
* given:
* blk - the block to free
*/
void
blk_free(BLOCK *blk)
{
/* free if non-NULL */
if (blk != NULL) {
/* free data storage */
if (blk->data != NULL) {
free(blk->data);
}
/* free the block */
free(blk);
}
return;
}
/*
* blkchk - check the sanity of a block
*
* These checks should never fail if calc is working correctly. During
* debug time, we plan to call this function often. Once we are satisfied,
* we will normally call this code only in a few places.
*
* If "calc_debug" has the bit corresponding to CALCDBG_BLOCK set, this
* function is called during execution of the following builtins:
*
* alloc(), realloc(), free()
*
* given:
* blk - the BLOCK to check
*
* returns:
* if all is ok, otherwise math_error() is called and this
* function does not return
*/
static void
blkchk(BLOCK *blk)
{
/*
* firewall - general sanity check
*/
if ((conf->calc_debug & CALCDBG_BLOCK) == 0) {
/* do nothing when debugging is disabled */
return;
}
if (blk == NULL) {
math_error("internal: blk ptr is NULL");
/*NOTREACHED*/
}
/*
* pointers must not be NULL
*/
if (blk->data == NULL) {
math_error("internal: blk->data ptr is NULL");
/*NOTREACHED*/
}
/*
* check data lengths
*/
if (blk->datalen < 0) {
math_error("internal: blk->datalen < 0");
/*NOTREACHED*/
}
/*
* check the datalen and datalen2 values
*/
if (blk->datalen < 0) {
math_error("internal: blk->datalen < 0");
/*NOTREACHED*/
}
return;
}
/*
* blkrealloc - reallocate a block
*
* Reallocation of a block can change several aspects of a block.
*
* It can change the much data it holds or can hold.
*
* It can change the memory footprint (in terms of
* how much storage is malloced for current or future use).
*
* It can change the chunk size used to grow malloced size
* as the data size grows.
*
* Each of the len and chunksize may be kept the same.
*
* given:
* blk - old BLOCK to reallocate
* newlen - how much data the block holds
* newchunk - allocation chunk size (<0 ==> no change, 0 == default)
*/
BLOCK *
blkrealloc(BLOCK *blk, int newlen, int newchunk)
{
USB8 *nblk; /* realloced storage */
int newmax; /* new maximum stoage size */
/*
* firewall
*/
if (conf->calc_debug & CALCDBG_BLOCK) {
blkchk(blk);
}
/*
* process args
*/
/* newlen < 0 means do not change the length */
if (newlen < 0) {
newlen = blk->datalen;
}
/* newchunk <= 0 means do not change the chunk size */
if (newchunk < 0) {
newchunk = blk->blkchunk;
} else if (newchunk == 0) {
newchunk = BLK_CHUNKSIZE;
}
/*
* reallocate storage if we have a different allocation size
*/
newmax = ((newlen+newchunk)/newchunk)*newchunk;
if (newmax != blk->maxsize) {
/* reallocate new storage */
nblk = (USB8*)realloc(blk->data, newmax);
if (nblk == NULL) {
math_error("cannot reallocate block storage");
/*NOTREACHED*/
}
/* clear any new storage */
if (newmax > blk->maxsize) {
memset(nblk+blk->maxsize, 0, (newmax-blk->maxsize));
}
blk->maxsize = newmax;
/* restore the data pointers */
blk->data = nblk;
}
/*
* deal the case of a newlen == 0 early and return
*/
if (newlen == 0) {
/*
* setup the empty buffer
*
* We know that newtype is not circular since we force
* newlen to be at least 1 (because circular blocks
* always have at least one unused octet).
*/
if (blk->datalen < blk->maxsize) {
memset(blk->data, 0, blk->datalen);
} else {
memset(blk->data, 0, blk->maxsize);
}
blk->datalen = 0;
if (conf->calc_debug & CALCDBG_BLOCK) {
blkchk(blk);
}
return blk;
}
/*
* Set the data length
*
* We also know that the new block is not empty since we have
* already dealth with that case above.
*
* After this section of code, limit and datalen will be
* correct in terms of the new type.
*/
if (newlen > blk->datalen) {
/* there is new storage, clear it */
memset(blk->data + blk->datalen, 0, newlen-blk->datalen);
/* growing storage for blocks grows the data */
blk->datalen = newlen;
} else if (newlen <= blk->datalen) {
/* the block will be full */
blk->datalen = newlen;
}
/*
* return realloced type
*/
if (conf->calc_debug & CALCDBG_BLOCK) {
blkchk(blk);
}
return blk;
}
/*
* blktrunc - truncate a BLOCK down to a minimal fixed block
*
* NOTE: THIS IS NOT THE INTERNAL CALC FREE FUNCTION!! This
* is what blktrunc() builtin calls to reduce storage of a block
* down to an absolute minimum.
*
* This actually forms a zero length fixed block with a chunk of 1.
*
* given:
* blk - the BLOCK to shrink
*
* returns:
* pointer to a newly allocated BLOCK
*/
void
blktrunc(BLOCK *blk)
{
/*
* firewall
*/
if (conf->calc_debug & CALCDBG_BLOCK) {
blkchk(blk);
}
/*
* free the old storage
*/
free(blk->data);
/*
* setup as a zero length fixed block
*/
blk->blkchunk = 1;
blk->maxsize = 1;
blk->datalen = 0;
blk->data = (USB8*)malloc(1);
if (blk->data == NULL) {
math_error("cannot allocate truncated block storage");
/*NOTREACHED*/
}
blk->data[0] = (USB8)0;
if (conf->calc_debug & CALCDBG_BLOCK) {
blkchk(blk);
}
return;
}
/*
* blk_copy - copy a block
*
* given:
* blk - the block to copy
*
* returns:
* pointer to copy of blk
*/
BLOCK *
blk_copy(BLOCK *blk)
{
BLOCK *nblk; /* copy of blk */
/*
* malloc new block
*/
nblk = (BLOCK *)malloc(sizeof(BLOCK));
if (nblk == NULL) {
math_error("blk_copy: cannot malloc BLOCK");
/*NOTREACHED*/
}
/*
* duplicate most of the block
*/
*nblk = *blk;
/*
* duplicate block data
*/
nblk->data = (USB8 *)malloc(blk->maxsize);
if (nblk->data == NULL) {
math_error("blk_copy: cannot duplicate block data");
/*NOTREACHED*/
}
memcpy(nblk->data, blk->data, blk->maxsize);
return nblk;
}
/*
* blk_cmp - compare blocks
*
* given:
* a first BLOCK
* b second BLOCK
*
* returns:
* TRUE => BLOCKs are different
* FALSE => BLOCKs are the same
*/
int
blk_cmp(BLOCK *a, BLOCK *b)
{
/*
* firewall and quick check
*/
if (a == b) {
/* pointers to the same object */
return FALSE;
}
if (a == NULL || b == NULL) {
/* one pointer is NULL, so they differ */
return TRUE;
}
/*
* compare lengths
*/
if (a->datalen != b->datalen) {
/* different lengths are different */
return TRUE;
}
/*
* compare the section
*
* We have the same lengths and types, so compare the data sections.
*/
if (memcmp(a->data, b->data, a->datalen) != 0) {
/* different sections are different */
return TRUE;
}
/*
* the blocks are the same
*/
return FALSE;
}
/*
* Print chunksize, maxsize, datalen on line line and if datalen > 0,
* up to * 30 octets on the following line, with ... if datalen exceeds 30.
*/
/*ARGSUSED*/
void
blk_print(BLOCK *blk)
{
long i;
BOOL havetail;
USB8 *ptr;
/* XXX - should use the config parameters for better print control */
printf("chunksize = %d, maxsize = %d, datalen = %d\n\t",
(int)blk->blkchunk, (int)blk->maxsize, (int)blk->datalen);
i = blk->datalen;
havetail = (i > 30);
if (havetail)
i = 30;
ptr = blk->data;
while (i-- > 0)
printf("%02x", *ptr++);
if (havetail)
printf("...");
}
/*
* Routine to print id and name of a named block and details of its
* block component.
*/
void
nblock_print(NBLOCK *nblk)
{
BLOCK *blk;
/* XXX - use the config parameters for better print control */
blk = nblk->blk;
printf("block %d: %s\n\t", nblk->id, nblk->name);
if (blk->data == NULL) {
printf("chunksize = %d, maxsize = %d, datalen = %d\n\t",
(int)blk->blkchunk, (int)blk->maxsize, (int)blk->datalen);
printf("NULL");
} else {
blk_print(blk);
}
}
/*
* realloc a named block specified by its id. The new datalen and
* chunksize are specified by len >= 0 and chunk > 0. If len < 0
* or chunk <= 0, these values used are the current datalen and
* chunksize, so there is no point in calling this unless len >= 0
* and/or chunk > 0.
* No reallocation occurs if the new maxsize is equal to the old maxsize.
*/
NBLOCK *
reallocnblock(int id, int len, int chunk)
{
BLOCK *blk;
int newsize;
int oldsize;
USB8* newdata;
/* Fire wall */
if (id < 0 || id >= nblockcount) {
math_error("Bad id in call to reallocnblock");
/*NOTREACHED*/
}
blk = nblocks[id]->blk;
if (len < 0)
len = blk->datalen;
if (chunk < 0)
chunk = blk->blkchunk;
else if (chunk == 0)
chunk = BLK_CHUNKSIZE;
newsize = (1 + len/chunk) * chunk;
oldsize = blk->maxsize;
newdata = blk->data;
if (newdata == NULL) {
newdata = malloc(newsize);
if (newdata == NULL) {
math_error("Allocation failed");
/*NOTREACHED*/
}
} else if (newsize != oldsize) {
newdata = realloc(blk->data, newsize);
if (newdata == NULL) {
math_error("Reallocation failed");
/*NOTREACHED*/
}
}
memset(newdata + len, 0, newsize - len);
blk->maxsize = newsize;
blk->datalen = len;
blk->blkchunk = chunk;
blk->data = newdata;
return nblocks[id];
}
/*
* Create and return a new namedblock with specified name, len and
* chunksize.
*/
NBLOCK *
createnblock(char *name, int len, int chunk)
{
NBLOCK *res;
char *newname;
if (nblockcount >= maxnblockcount) {
if (maxnblockcount <= 0) {
maxnblockcount = NBLOCKCHUNK;
nblocks = (NBLOCK **)malloc(NBLOCKCHUNK *
sizeof(NBLOCK *));
if (nblocks == NULL) {
maxnblockcount = 0;
math_error("unable to malloc new named blocks");
/*NOTREACHED*/
}
} else {
maxnblockcount += NBLOCKCHUNK;
nblocks = (NBLOCK **)realloc(nblocks, maxnblockcount *
sizeof(NBLOCK *));
if (nblocks == NULL) {
maxnblockcount = 0;
math_error("cannot malloc more named blocks");
/*NOTREACHED*/
}
}
}
if (nblockcount == 0)
initstr(&nblocknames);
if (findstr(&nblocknames, name) >= 0) {
math_error("Named block already exists!!!");
/*NOTREACHED*/
}
newname = addstr(&nblocknames, name);
if (newname == NULL) {
math_error("Block name allocation failed");
/*NOTREACHED*/
}
res = (NBLOCK *) malloc(sizeof(NBLOCK));
if (res == NULL) {
math_error("Named block allocation failed");
/*NOTREACHED*/
}
nblocks[nblockcount] = res;
res->name = newname;
res->subtype = V_NOSUBTYPE;
res->id = nblockcount++;
res->blk = blkalloc(len, chunk);
return res;
}
/*
* find a named block
*/
int
findnblockid(char * name)
{
return findstr(&nblocknames, name);
}
/*
* free data block for named block with specified id
*/
int
removenblock(int id)
{
NBLOCK *nblk;
if (id < 0 || id >= nblockcount)
return E_BLKFREE3;
nblk = nblocks[id];
if (nblk->blk->data == NULL)
return 0;
if (nblk->subtype & V_NOREALLOC)
return E_BLKFREE5;
free(nblk->blk->data);
nblk->blk->data = NULL;
nblk->blk->maxsize = 0;
nblk->blk->datalen = 0;
return 0;
}
/*
* count number of current unfreed named blocks
*/
int
countnblocks(void)
{
int n;
int id;
for (n = 0, id = 0; id < nblockcount; id++) {
if (nblocks[id]->blk->data != NULL)
n++;
}
return n;
}
/*
* display id and name for each unfreed named block
*/
void
shownblocks(void)
{
int id;
if (countnblocks() == 0) {
printf("No unfreed named blocks\n\n");
return;
}
printf(" id name\n");
printf("---- -----\n");
for (id = 0; id < nblockcount; id++) {
if (nblocks[id]->blk->data != NULL)
printf("%3d %s\n", id, nblocks[id]->name);
}
printf("\n");
}
/*
* Return pointer to nblock with specified id, NULL if never created.
* The memory for the nblock found may have been freed.
*/
NBLOCK *
findnblock(int id)
{
if (id < 0 || id >= nblockcount)
return NULL;
return nblocks[id];
}
/*
* Create a new block with specified newlen and new chunksize and copy
* min(newlen, oldlen) octets to the new block. The old block is
* not changed.
*/
BLOCK *
copyrealloc(BLOCK *blk, int newlen, int newchunk)
{
BLOCK * newblk;
int oldlen;
oldlen = blk->datalen;
if (newlen < 0) /* retain length */
newlen = oldlen;
if (newchunk < 0) /* retain chunksize */
newchunk = blk->blkchunk;
else if (newchunk == 0) /* use default chunksize */
newchunk = BLK_CHUNKSIZE;
newblk = blkalloc(newlen, newchunk);
if (newlen < oldlen)
oldlen = newlen;
if (newlen > 0)
memcpy(newblk->data, blk->data, oldlen);
return newblk;
}
|