Table of Contents Previous page Next page Index

ModelSim

Model Technology Inc.


mti_Realloc()

Reallocates simulator-managed memory.

Syntax

memptr = mti_Realloc( origptr, size ) 

Returns

Name
Type
Description
memptr
void *
A pointer to the reallocated memory

Arguments

Name
Type
Description
origptr
void *
A pointer to the currently allocated memory
size
unsigned long
The size in bytes of the new memory to be allocated

Description

mti_Realloc() works like the C realloc() function on memory allocated by mti_Malloc(). If the specified size is larger than the size of memory already allocated to the origptr parameter, then new memory of the required size is allocated and initialized to zero, the entire content of the old memory is copied into the new memory, and a pointer to the new memory is returned. Otherwise, a pointer to the old memory is returned.

Any memory allocated by mti_Realloc() is guaranteed to be checkpointed and restored just like memory allocated by mti_Malloc(). Memory allocated by mti_Realloc() can be freed only by mti_Free().

mti_Realloc() automatically checks for a NULL pointer. In the case of an allocation error, mti_Realloc() issues the following error message and aborts the simulation:

****** Memory allocation failure. *****
Please check your system for available memory and swap space 

Related functions

mti_Free()

mti_Malloc()

Example

FLI code

#include <stdlib.h>
#include <stdio.h>
#include <mti.h>

static char * instance_info;

void saveCallback( void * param )
{
  char * inst_info = (char *)param;
  mti_PrintFormatted( "Saving instance info pointer to \"%s\"\n",
                     inst_info );
  mti_SaveBlock( &inst_info, sizeof(inst_info) );
}

void restoreCallback( void * param )
{
  mti_RestoreBlock( &instance_info );
  mti_PrintFormatted( "Restored instance info \"%s\"\n", instance_info );
}

void cleanupCallback( void * param )
{
  mti_PrintMessage( "Cleaning up...\n" );
  /*
   * NOTE:  Memory allocated by mti_Malloc() and mti_Realloc() will
   *        be freed by vsim.
   */
}

void initForeign(
  mtiRegionIdT       region,   /* The ID of the region in which this     */
                               /* foreign architecture is instantiated.  */
  char              *param,    /* The last part of the string in the     */
                               /* foreign attribute.                     */
  mtiInterfaceListT *generics, /* A list of generics for the foreign model.*/
  mtiInterfaceListT *ports     /* A list of ports for the foreign model.   */
)
{
  if ( mti_IsRestore() ) {
    mti_PrintMessage( "Restore in progress ...\n" );
  } else {
    instance_info = mti_Malloc( strlen(param) + 1 );
    strcpy( instance_info, param );
    if ( ! mti_IsFirstInit() ) {
      instance_info = mti_Realloc( instance_info, strlen(param) + 9 );
      sprintf( instance_info, "%s_restart", param );
    }
  }
  mti_AddSaveCB( saveCallback, instance_info );
  mti_AddRestoreCB( restoreCallback, instance_info );
  mti_AddQuitCB( cleanupCallback, instance_info );
  mti_AddRestartCB( cleanupCallback, instance_info );
} 

HDL code

entity for_model is
end for_model;

architecture a of for_model is
  attribute foreign of a : architecture is
    "initForeign for_model.sl; my_for_model";
begin
end a;

entity top is
end top;

architecture a of top is

  signal s1 : bit := '0';

  component for_model is
  end component;

  for all : for_model use entity work.for_model(a);

begin

  i1 : for_model;

  s1 <= not s1 after 5 ns;

end a; 

Simulation output

% vsim -c top
Reading .../modeltech/sunos5/../tcl/vsim/pref.tcl 

# 5.4b

# vsim -c top 
# Loading .../modeltech/sunos5/../std.standard
# Loading work.top(a)
# Loading work.for_model(a)
# Loading ./for_model.sl
VSIM 1> run 20
VSIM 2> checkpoint cpfile
# Saving instance info pointer to "my_for_model"
VSIM 3> run 30
VSIM 4> restore cpfile
# Loading checkpoint/restore data from file "cpfile"
# Checkpoint created Fri Jul  7 13:20:28 2000
# Restoring state at time 20 ns, iteration 1
# Restore in progress ...
# Restored instance info "my_for_model"
VSIM 5> run 40
VSIM 6> restart -f
# Cleaning up...
# Loading ./for_model.sl
VSIM 7> run 15
VSIM 8> checkpoint cpf2
# Saving instance info pointer to "my_for_model_restart"
VSIM 9> run 25
VSIM 10> restore cpf2
# Loading checkpoint/restore data from file "cpf2"
# Checkpoint created Fri Jul  7 13:20:52 2000
# Restoring state at time 15 ns, iteration 1
# Restore in progress ...
# Restored instance info "my_for_model_restart"
VSIM 11> run 35
VSIM 12> quit
# Cleaning up... 


Model Technology Inc.
Voice: (503) 641-1340
Fax: (503)526-5410
http://www.model.com
sales@model.com
TOC PREV NEXT INDEX

ModelSim