xp_comm_proj/dlgtools/read/ram.c
/*********************************************************************

  get more memory from a list

*********************************************************************/
get more memory from a list
get more memory from a list
char *ram_more(int block_number, size_t more_bytes )
{
  div_t mydiv;
  size_t new_bytes;
  int factor;
  char *return_val;
  Ram_Block_Data *this_block;
  if(block_number < 1 ||
     block_number > MAX_RAM_BLOCKS-1)
    {
      error_message[0] = '\0';
      sprintf( error_message+strlen(error_message),
              "Block %i\nblock_number out of range\n",
               block_number);
      sprintf( error_message+strlen(error_message),
              "Allowable minimum = 1  Allowable maximum = %i ",
               MAX_RAM_BLOCKS-1);
      ALF(error_message);
      P_MESS( "ram_more", error_message  );
      return(NULL);
    }
  if( more_bytes < 1 )
    {
      error_message[0] = '\0';
      sprintf( error_message+strlen(error_message),
              "Block = %i\nrequested # bytes = %i out of range",
               block_number, more_bytes);
      ALF(error_message);
      P_MESS( "ram_more", error_message  );
      return(NULL);
    }
  if( ram_block_entries[block_number] == NULL  )
    {
      error_message[0] = '\0';
      sprintf( error_message+strlen(error_message),
              "\nrequested block = %i has NULL head (not init'ed)",
               block_number);
      ALF(error_message);
      P_MESS( "ram_more", error_message  );
      return(NULL);
    }
  this_block = ram_block_entries[ block_number ];
  if( more_bytes <= this_block->left )
    {
      this_block->used += more_bytes;
      this_block->left -= more_bytes;
      return_val = this_block->tail;
      this_block->tail += more_bytes;
      if( this_block->left == 0 )
        {
          this_block->tail = NULL;
        }
      if( ram_debug_enabled == 1 ) ram_dump( "ram_more" );
      return( return_val );
    }
  this_block = ram_block_entries[ block_number ];
  if( this_block->used == 0 ) this_block->used = 1;
  this_block->head
    = (char *) realloc( this_block->head, this_block->used );
  if( this_block->head == NULL )
    {
      error_message[0] = '\0';
      sprintf( error_message+strlen(error_message),
	      "Block %i\nrealloc failure\nexisting bytes = %i  requested bytes = %i",
	      block_number, this_block->size, this_block->used );
      sprintf( error_message+strlen(error_message),
	      "\n\nERR: %s", strerror(errno) );
      ALF(error_message);
      P_MESS( "ram_more" , error_message );
      return(NULL);
    }
  this_block = ram_block_entries[ block_number ];
  this_block->size = this_block->used;
  this_block->left = 0;
  this_block->tail = NULL;
  mydiv = div( more_bytes, ram_block_sizes[ block_number ] );
  if( mydiv.rem == 0 )
    {
      new_bytes =  mydiv.quot    * ram_block_sizes[ block_number ];
    }
  else
    {
      new_bytes = (mydiv.quot+1) * ram_block_sizes[ block_number ];
    }
  this_block = ram_add_block( block_number, new_bytes );
  if( this_block == NULL )
    {
      if( ram_debug_enabled == 1 ) ram_dump( "ram_more" );
      return( NULL );
    }
  this_block->next = ram_block_entries[ block_number ];
  ram_block_entries[ block_number ] = this_block;
  this_block = ram_block_entries[ block_number ];
  this_block->used = more_bytes;
  this_block->left = this_block->size - more_bytes;
  return_val = this_block->tail;
  this_block->tail = this_block->head + more_bytes;
  if( this_block->left == 0 )
    {
      this_block->tail = NULL;
    }
  return_val = this_block->head;
  if( ram_debug_enabled == 1 ) ram_dump( "ram_more" );
  return( return_val );
}
char * ram_more ( int block_number , size_t more_bytes ) { div_t mydiv ; size_t new_bytes ; int factor ; char * return_val ; Ram_Block_Data * this_block ; if ( block_number < 1 || block_number > MAX_RAM_BLOCKS - 1 ) { error_message [ 0 ] = ' \0 ' ; sprintf ( error_message + strlen ( error_message ) , " \n \n " , block_number ) ; sprintf ( error_message + strlen ( error_message ) , " " , MAX_RAM_BLOCKS - 1 ) ; ALF ( error_message ) ; P_MESS ( " " , error_message ) ; return ( NULL ) ; } if ( more_bytes < 1 ) { error_message [ 0 ] = ' \0 ' ; sprintf ( error_message + strlen ( error_message ) , " \n " , block_number , more_bytes ) ; ALF ( error_message ) ; P_MESS ( " " , error_message ) ; return ( NULL ) ; } if ( ram_block_entries [ block_number ] == NULL ) { error_message [ 0 ] = ' \0 ' ; sprintf ( error_message + strlen ( error_message ) , " \n " , block_number ) ; ALF ( error_message ) ; P_MESS ( " " , error_message ) ; return ( NULL ) ; } this_block = ram_block_entries [ block_number ] ; if ( more_bytes <= this_block -> left ) { this_block -> used += more_bytes ; this_block -> left -= more_bytes ; return_val = this_block -> tail ; this_block -> tail += more_bytes ; if ( this_block -> left == 0 ) { this_block -> tail = NULL ; } if ( ram_debug_enabled == 1 ) ram_dump ( " " ) ; return ( return_val ) ; } this_block = ram_block_entries [ block_number ] ; if ( this_block -> used == 0 ) this_block -> used = 1 ; this_block -> head = ( char * ) realloc ( this_block -> head , this_block -> used ) ; if ( this_block -> head == NULL ) { error_message [ 0 ] = ' \0 ' ; sprintf ( error_message + strlen ( error_message ) , " \n \n " , block_number , this_block -> size , this_block -> used ) ; sprintf ( error_message + strlen ( error_message ) , " \n \n " , strerror ( errno ) ) ; ALF ( error_message ) ; P_MESS ( " " , error_message ) ; return ( NULL ) ; } this_block = ram_block_entries [ block_number ] ; this_block -> size = this_block -> used ; this_block -> left = 0 ; this_block -> tail = NULL ; mydiv = div ( more_bytes , ram_block_sizes [ block_number ] ) ; if ( mydiv . rem == 0 ) { new_bytes = mydiv . quot * ram_block_sizes [ block_number ] ; } else { new_bytes = ( mydiv . quot + 1 ) * ram_block_sizes [ block_number ] ; } this_block = ram_add_block ( block_number , new_bytes ) ; if ( this_block == NULL ) { if ( ram_debug_enabled == 1 ) ram_dump ( " " ) ; return ( NULL ) ; } this_block -> next = ram_block_entries [ block_number ] ; ram_block_entries [ block_number ] = this_block ; this_block = ram_block_entries [ block_number ] ; this_block -> used = more_bytes ; this_block -> left = this_block -> size - more_bytes ; return_val = this_block -> tail ; this_block -> tail = this_block -> head + more_bytes ; if ( this_block -> left == 0 ) { this_block -> tail = NULL ; } return_val = this_block -> head ; if ( ram_debug_enabled == 1 ) ram_dump ( " " ) ; return ( return_val ) ; }
get more memory from a list
get more memory from a list
/* make sure block number in bounds */ /* make sure the size requested makes sense */ /* make sure this block has been init'ed properly */ /* no more memory needed */ /* is the block full */ /* debug printing */ /* first truncate the existing block */ /* watch for zero length blocks - remove if possible */ /* don't remove the head if there is only one link */ /* adjust the information in this block */ /* now create a new block */ /* allow for exact block fills */ /* create the new block */ /* debug printing */ /* put it at the head of the list and return*/ /* adjust for memory being returned */ /* debug printing */