Sources/MemoryClass.cp

/*
    File:       MemoryClass.cp
 
    Contains:   TMemory is a simple object checks heap and stack values, as well as changes them.
                TMemoryClass.cp contains the TMemory member function definitions.
 
    Written by: Kent Sandvik    
 
    Copyright:  Copyright © 1993-1999 by Apple Computer, Inc., All Rights Reserved.
 
                You may incorporate this Apple sample source code into your program(s) without
                restriction. This Apple sample source code has been provided "AS IS" and the
                responsibility for its operation is yours. You are not permitted to redistribute
                this Apple sample source code as "Apple sample source code" after having made
                changes. If you're going to re-distribute the source, we require that you make
                it clear in the source that the code was descended from Apple sample source
                code, but that you've made changes.
 
    Change History (most recent first):
                8/18/1999   Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
#ifndef _MEMORYCLASS_
#include "MemoryClass.h"
#endif
 
 
// CONSTRUCTORS AND DESTRUCTORS
#pragma segment Memory
TMemory::TMemory(const long minHeap,
                 const long minStack)
// The one and only constructor, takes min values for stack and heap if wanted, but
// these are not needed.
{
    fMinHeap = minHeap;                         // minimum heap size
    fMinStack = minStack;                       // minimum stack size
}
 
 
#pragma segment Memory
TMemory::~TMemory()
// Default constructor -- unused for the time being.
{
}
 
 
// MAIN INTERFACE
#pragma segment Memory
long TMemory::GetStackSize()
// Get size of the current stack.
{
    return ::StackSpace();                      // return size of stack
}
 
 
#pragma segment Memory
Boolean TMemory::SetStackSize(const long stackValue)
// Set absolute size of current stack.
{
    long oldValue = ::StackSpace();
 
    // Set the new value
    SetApplLimit((Ptr)((long)GetApplLimit() - (stackValue - oldValue)));
 
    fError = MemError();
    VASSERT(fError == noErr, ("Problems with SetApplLimit = %d", fError));
 
    if (fError != noErr)
        return false;
    else
        return true;
}
 
 
#pragma segment Memory
Boolean TMemory::SetStackSizeFromResources()
// Get stack values from pre-defined resource (that should be part of the application).
{
    long newStackSize = 0;
 
    short num = ::CountResources(kStackResource);// find out how many we got
    if (num == 0)                               // none?
        goto FromResourceFalse;
    else
    {
        for (int i = 1; i <= num; ++i)
        {
            Handle temp = ::GetIndResource(kStackResource, i);
            fError = ResError();
            VASSERT(fError == noErr, ("Problems with GetIndResource = %d", fError));
            {
                const StackResource& stackValueH = **((StackValueHandle)temp);
                newStackSize += stackValueH.stackVal;
            }
            if (temp != NULL)
                ::ReleaseResource(temp);
        }
    }
    this->SetStackSize(newStackSize);           // set the new stack size
    return true;
 
FromResourceFalse:return false;
}
 
 
#pragma segment Memory
Boolean TMemory::IncreaseStackSize(const long stackValue)
// Increase the stack value from the current size.
{
    ::SetApplLimit((Ptr)((long)GetApplLimit() - stackValue));
 
    fError = MemError();
    VASSERT(fError == noErr, ("Problems with SetApplLimit = %d", fError));
 
    if (fError != noErr)
        return false;
    else
        return true;
}
 
 
#pragma segment Memory
Boolean TMemory::CheckStackSize()
// Check if we hit the low watermark of the stack size.
{
    long currentStack = ::StackSpace();
 
    if (currentStack < fMinStack)
        return false;
    else
        return true;
}
 
 
#pragma segment Memory
Boolean TMemory::CheckHeapSize()
// Check if we hit the low watermark of the heap size.
{
    long currentHeap = this->GetHeapSize();
 
    if (currentHeap < fMinHeap)
        return false;
    else
        return true;
}
 
 
#pragma segment Memory
long TMemory::GetHeapSize()
// Get size of current heap.
{
    return ((long)GetApplLimit() - (long)ApplicationZone());
}
 
 
// _________________________________________________________________________________________________________ //
 
/*  Change History (most recent last):
  No        Init.   Date        Comment
  1         khs     1/2/93      New file
  2         khs     1/3/93      Cleanup
*/