/*---------------------------------------------------------------------------*
  Project:  NitroSDK - OS - demos - heap-1
  File:     main.c

  Copyright 2003,2004 Nintendo.  All rights reserved.

  These coded instructions, statements, and computer programs contain
  proprietary information of Nintendo of America Inc. and/or Nintendo
  Company Ltd., and are protected by Federal copyright law.  They may
  not be disclosed to third parties or copied or duplicated in any form,
  in whole or in part, without the prior written consent of Nintendo.

  $Log: main.c,v $
  Revision 1.7  2004/09/28 12:22:10  yada
  not call OS_InitArena() because OS_Init() called

  Revision 1.6  2004/04/15 06:51:12  yada
  only arrange comment

  Revision 1.5  2004/03/08 08:31:58  yada
  fix top comment

  Revision 1.4  2004/02/12 11:41:07  yada
  Work to divide ARM9/ARM7

  Revision 1.3  2004/02/05 07:09:03  yasu
  change SDK prefix iris -> nitro

  Revision 1.2  2004/01/09 09:18:31  yada
  Changed display of ending part

  Revision 1.1  2004/01/07 06:39:50  yada
  First edition

  $NoKeywords: $
 *---------------------------------------------------------------------------*/
#include <nitro.h>

//----------------------------------------------------------------
#define   MAIN_HEAP_SIZE    0x2000

//================================================================================
//        Comprehensive sample of heap
//        Create heap for arena of main memory, and allocate/free region
/*---------------------------------------------------------------------------*
  Name:         NitroMain

  Description:  main

  Arguments:    None

  Returns:      None
 *---------------------------------------------------------------------------*/
void NitroMain( void )
{
    void*          heapStart;
    void*          nstart;
    OSHeapHandle   handle;
    void*          area[10];

    OS_Init();
    //OS_InitArena(); /*called in OS_Init()*/
    OS_Printf( "arena lo = %x\n", OS_GetMainArenaLo() );
    OS_Printf( "arena hi = %x\n", OS_GetMainArenaHi() );


    //---- Initialize memory allocation system for arena of MainRAM
    nstart = OS_InitAlloc( OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi(), 2 );
    OS_SetMainArenaLo( nstart );

    //---- Allocate region for heap from arena
    heapStart = OS_AllocFromMainArenaLo( MAIN_HEAP_SIZE, 32 );
    OS_Printf( "heapStart %x\n", heapStart );

    //---- Create Heap
    handle = OS_CreateHeap( OS_ARENA_MAIN, heapStart, (void*)( (u32)heapStart + MAIN_HEAP_SIZE ) );
    OS_Printf( "heap handle %d\n", handle );

    //---- Set Current Heap
    (void)OS_SetCurrentHeap( OS_ARENA_MAIN, handle );

    //---- Allocate region
    area[0] = OS_Alloc( 32 );
    area[1] = OS_Alloc( 32 );
    area[2] = OS_Alloc( 32 );
    area[3] = OS_Alloc( 64 );

    OS_Printf( "area[0] %x\n", area[0] );
    OS_Printf( "area[1] %x\n", area[1] );
    OS_Printf( "area[2] %x\n", area[2] );
    OS_Printf( "area[3] %x\n", area[3] );

    //---- Display heap information
    OS_DumpHeap( OS_ARENA_MAIN, handle );


    //---- Free region
    OS_Free( area[1] );
    OS_Free( area[3] );

    //---- Display heap information
    OS_DumpHeap( OS_ARENA_MAIN, handle );

    OS_Printf( "==== Finish sample.\n" );
    OS_Terminate();
}

/*====== End of main.c ======*/
