/*----------------------------------------------------------------------------
** Name: byadrtbl.h
**
** Title: By-address Sorted Table
**
** Purpose:
**  Class and methods for managing the by-address sorted table.  Each base
**  structure points to one by-address sorted table.
**
** Status: PRELIMINARY | CODED
**
** $Log:   S:/tbird/mt2_186/symbol/byadrtbl.h_v  $
** 
**    Rev 1.2   22 Jul 1997 10:10:16   Judy
** 
**    Rev 1.1   26 Feb 1997 11:42:24   Judy
** 
**    Rev 1.1   14 Jan 1997 15:30:34   Judy
** No change.
** 
**    Rev 1.0   14 Jun 1996 16:41:54   Judy
** Initial revision.
** 
**    Rev 1.10   03 Aug 1994 12:08:32   brucea
** Added parameter to ::SearchForAddr
** 
**    Rev 1.9   14 Jul 1994 01:49:28   brucea
** Removed EXPORT from CreateAllAddrRanges
** 
**    Rev 1.8   20 Jun 1994 10:03:08   nghia
** Added CreateAddressRange and CreateAllAddrRanges to ByAddressTable class.
** 
**    Rev 1.7   25 Jul 1992 16:35:20   brucea
** Added: output parameter tableSize to ByAddressTableGetNext
** 
**    Rev 1.6   10 Jul 1992 18:34:22   brucea
** Added: ByAddressTableGetNext
** 
**    Rev 1.5   24 Jun 1992 18:08:34   brucea
** Added: #ifdef __cplusplus around the C++ class definitions
** Added: EXPORT to SortAllByAddressTables
** 
**    Rev 1.4   29 Nov 1991 19:48:04   brucea
** changed LOGICAL_ADDR_TYPE to OFFSET_ADDR_TYPE
** 
**    Rev 1.3   01 Nov 1991 10:51:46   brucea
** Added SYMBOL_INFO_TYPE
** Modified ByAddressTable::GetOffset
** Added #ifdef __cplusplus
** Took AddressCompare out of class method
** Added SortAllByAddressTables
** 
**    Rev 1.2   11 Oct 1991 15:01:26   brucea
** Removed #include _SYMMGR_
** Changed ByAddressTable call to public:
** 
**    Rev 1.0   13 Sep 1991 09:57:50   brucea
** Initial revision.
** 
** $Header:   S:/tbird/mt2_186/symbol/byadrtbl.h_v   1.2   22 Jul 1997 10:10:16   Judy  $
**
** Copyright (C) 1991 Microtek International.  All rights reserved.
**
**--------------------------------------------------------------------------*/

#define _BYADRTBL_

#ifndef _MEMPOOL_
#include "mempool.h"
#endif

#ifndef _SYMMGR_
#include "symmgr.h"
#endif

                       /****************************
                        *                          *
                        *        DEFINITIONS       *
                        *                          *
                        ****************************/

typedef struct {
   TABLE_OFFSET symbolOffset;
   U32          delta;      // requested addr minus start addr of matching sym
} SYMBOL_INFO_TYPE;

                       /**********************************
                        *                                *
                        *   MEMBER FUNCTION PROTOTYPES   *
                        *                                *
                        **********************************/

#ifdef __cplusplus

class ByAddressTable : public MemPool {

public:
//---------------------------------------------------------------------------
// ByAddressTable()  constructor
//
// Purpose:
//    Initialize by-address table variables for memory management.
//---------------------------------------------------------------------------
   ByAddressTable();    // constructor


//-----------------------------------------------------------------------
// GetOffset
//
// Purpose: returns the offset to the symbol stored in the by-address table
//-----------------------------------------------------------------------
TABLE_OFFSET ByAddressTable::GetOffset(U32 index) {
   return *(HP_TABLE_OFFSET)GetHugeDataPtr(index * sizeof(TABLE_OFFSET));
   } 


//-----------------------------------------------------------------------
// GetSymbolCount
// 
// Purpose: returns the number of symbol offsets placed into table
//-----------------------------------------------------------------------
   U32 GetSymbolCount(VOID) { return (GetAvailMemOffset()
                              / sizeof(TABLE_OFFSET)); };


//-----------------------------------------------------------------------
// SearchForAddr
//
// Purpose:
//    Searches for a symbol which has a range that contains the requested
//    address and has the smallest end address that still contains addr.
//-----------------------------------------------------------------------
   RETCODE
   ByAddressTable::SearchForAddr(OFFSET_ADDR_TYPE requestedAddr,
				 U16              segment,
                                 BOOLEAN          NoPublics,
                                 SYMBOL_INFO_TYPE& symbolInfo);


//-----------------------------------------------------------------------
// SortTable
//
// Purpose: Sorts the offsets to symbols by the start address of each symbol
//
// Input parameter: NONE
//
// Return value: NONE
//
// Errors:
//    returns ER_BYADDRTABLE_EMPTY if no items in the table; this should
//    never occur since the call to SortTable should never be made if the
//    the dirty flag is not set - i.e. no symbols are put into table.
//------------------------------------------------------------------------
   RETCODE SortTable(VOID);

//-----------------------------------------------------------------------
// CreateAddrRanges
//
// Purpose: Create address range of the specified label type.
//
// Input parameter: label type
//
// Return value: NONE
//
// Errors:
//    returns ER_BYADDRTABLE_EMPTY if no items in the table; this should
//    never occur since the call to SortTable should never be made if the
//    the dirty flag is not set - i.e. no symbols are put into table.
//------------------------------------------------------------------------
   RETCODE CreateAddrRanges(SYM_TYPE_TYPE labelType);

   
};  // end of class ByAddressTable

#endif


#ifdef __cplusplus
extern "C" {
#endif

//------------------------------------------------------------------------
// AddressCompare
//
// Purpose:
//    Provides the compare function for the QSort sort
//------------------------------------------------------------------------
int AddressCompare(const VOID _FAR * first,
                   const VOID _FAR * second);



//------------------------------------------------------------------------
// SortAllByAddressTables
//
// Purpose:
//    Sorts all by-address tables with addrTableDirty TRUE
//------------------------------------------------------------------------
RETCODE EXPORT
SortAllByAddressTables(VOID);


//-----------------------------------------------------------------------
// ByAddressTableGetNext
//
// Purpose:
//   Iterator to walk the bases and return the name and symbol count
//
// Input parameters:
//    startLoop: TRUE ==> starts the interation
//              FALSE ==> must be set after first access
//
// Output parameters:
//    loopPtr: call by reference; must be returned when startLoop FALSE
//    baseName: string of base name
//    entryCount: reference var returning number of symbols in this base
//    tableSize: allocated size of by-address table
//-----------------------------------------------------------------------
RETCODE
ByAddressTableGetNext(BOOLEAN       startLoop,
                      TABLE_OFFSET& loopPtr,
                      LPSTR         baseName,
                      U32&          entryCount,
                      U32&          tableSize);

//------------------------------------------------------------------------
// CreateAllAddrRanges
//
// Purpose:
//    Create address range for all public labels.
//
//------------------------------------------------------------------------
RETCODE CreateAllAddrRanges(SYM_TYPE_TYPE type);

#ifdef __cplusplus
}
#endif
/******************************** E O F *************************************/