void freeBuf( void *oldBuffer, const char *file, const size_t line ) { while( top != NULL ) { BUFQUEUE *save = top; KWBoolean done = (KWBoolean) ( top->userBuffer == oldBuffer ? KWTrue : KWFalse ); if ( top->signature != SIGNATURE ) { printmsg(0, "freeBuf: Invalid buffer queue at %p", top ); bugout( file, line ); } if ( top->userBuffer == oldBuffer ) done = KWTrue; #ifdef UDEBUG printmsg(done ? 8 : 2,"%s(%d): freeBuf: %slicit " "free of %p (%d,%d) for %u bytes", file, line, done ? "ex" : "im", top, top->entry, entries, top->length ); entries--; #endif /*--------------------------------------------------------------------*/ /* Pop the current entry off the stack and free it */ /*--------------------------------------------------------------------*/ top = top->previous; free( save ); if ( done ) return; } /* while( top != NULL ) */ /*--------------------------------------------------------------------*/ /* If we didn't find the block to free, we have a serious */ /* internal error -- report it and exit. */ /*--------------------------------------------------------------------*/ printmsg( 0, "freeBuf: Unable to locate requested memory block at %p", oldBuffer ); bugout( file, line ); } /* freeBuf */
void safefree( void *input , const char UUFAR *file, size_t line) { STR_QUEUE *current = anchor; int buffers = 0; while( current != NULL ) { buffers ++; if (( input > (void *) current ) && (input < (void *) (current->pool + pool_size ) )) { printmsg(0,"Attempt to free string \"%s\" allocated via newstr() in pool %d", input , buffers ); bugout( file, line); } current = current->next_link; } #undef free free(input); } /* safefree */
void checkptr( const char UUFAR *file, const size_t line) { printmsg(0,"Storage allocation failure; possible cause: " " memory shortage."); bugout( file, line); } /* checkptr */
void tty_putc(uint8_t minor, unsigned char c) { if (minor == 1) { bugout(c); vtoutput(&c, 1); } else if (minor == 2) dart0d = c; else dart1d = c; }
void *makeBuf( const size_t length, const char *file, const size_t line) { BUFQUEUE *current; if ( length == 0 ) { printmsg(0,"makeBuf: Invalid request for zero byte buffer"); bugout( file, line ); } current = malloc( length + sizeof (BUFQUEUE) ); /*--------------------------------------------------------------------*/ /* Verify our buffer was properly allocated */ /*--------------------------------------------------------------------*/ if ( !current ) checkptr( file, line); /*--------------------------------------------------------------------*/ /* Chain new buffer to top of queue */ /*--------------------------------------------------------------------*/ #ifdef UDEBUG entries++; current->length = length; current->entry = entries; #endif current->signature = SIGNATURE; current->userBuffer = (void *) (sizeof (BUFQUEUE) + (char *) current); current->previous = top; top = current; return current->userBuffer; } /* makeBuf */
char *strpool( const char *input , const char UUFAR *file, size_t line) { unsigned len; int best_fit = SHRT_MAX; char *result; STR_QUEUE *current = anchor; STR_QUEUE *last = anchor; STR_QUEUE *save = NULL; #ifdef __DEBUG_ALLOC__ _heap_check(); #endif if ( input == NULL ) { printmsg(0,"strpool: NULL pointer passed to newstr()"); bugout( file, line ); /* Become Info Highway Roadkill */ } len = strlen( input ); /*--------------------------------------------------------------------*/ /* Handle over length strings */ /*--------------------------------------------------------------------*/ if ( len > UCHAR_MAX ) { result = strdup( input ); if ( !result) checkptr( file, line); return result; } /*--------------------------------------------------------------------*/ /* Perform best fit search */ /*--------------------------------------------------------------------*/ while(current != NULL ) { int available; /*--------------------------------------------------------------------*/ /* Scan current buffer for the string */ /*--------------------------------------------------------------------*/ if ( ! bflag[ F_SPEEDOVERMEMORY ] ) { char *target = current->pool; char *bufend = target + current->used; while( target < bufend ) { int target_len = (unsigned char) *target++; int diff = target_len - (int) len; if ((diff >= 0 ) && equal( target + diff, input)) { #ifdef UDEBUG duplicates ++; saved += (long) len + 2; #endif return target+diff; } target += target_len + 1; /* Step to start of next string */ } /* while( offset < current->used ) */ } /* if */ /*--------------------------------------------------------------------*/ /* No string in this buffer, look for best fit in case we need */ /* to allocate the string from scratch */ /*--------------------------------------------------------------------*/ available = (int) (pool_size - current->used); if (( available < (int) best_fit) && (available > (int) (len+1) )) { best_fit = available; save = current; } else last = current; /* Save last buffer in case we have to chain new buffer in */ current = current->next_link; } /* while */ /*--------------------------------------------------------------------*/ /* We have no matching string, we have to insert the new string */ /* into our pool */ /*--------------------------------------------------------------------*/ if ( save == NULL ) /* We find a buffer? */ { /* No --> Allocate a new one */ pools ++; save = malloc( sizeof *save ); if ( !save) checkptr( file, line); if ( anchor == NULL ) { #ifdef UDEBUG atexit( dump_pool ); #endif anchor = save; } else last->next_link = save; save->used = 0; save->next_link = NULL; } /*--------------------------------------------------------------------*/ /* Save the string, update memory available in current pool, */ /* and return to the caller with the new string */ /*--------------------------------------------------------------------*/ result = save->pool + save->used; *result = (char) ((unsigned char) len); strcpy( ++result, input ); save->used += len + 2; #ifdef UDEBUG strings ++; used += (long) len + 2; #endif return result; } /* strpool */