• MacTech Network:
  • Tech Support
  • |
  • MacForge.net
  • |
  • Apple News
  • |
  • Register Domains
  • |
  • SSL Certificates
  • |
  • iPod Deals
  • |
  • Mac Deals
  • |
  • Mac Book Shelf

MAC TECH

  • Home
  • Magazine
    • About MacTech in Print
    • Issue Table of Contents
    • Subscribe
    • Risk Free Sample
    • Back Issues
    • MacTech DVD
  • Archives
    • MacTech Print Archives
    • MacMod
    • MacTutor
    • FrameWorks
    • develop
  • Forums
  • News
    • MacTech News
    • MacTech Blog
    • MacTech Reviews and KoolTools
    • Whitepapers, Screencasts, Videos and Books
    • News Scanner
    • Rumors Scanner
    • Documentation Scanner
    • Submit News or PR
    • MacTech News List
  • Store
  • Apple Expo
    • by Category
    • by Company
    • by Product
  • Job Board
  • Editorial
    • Submit News or PR
    • Writer's Kit
    • Editorial Staff
    • Editorial Calendar
  • Advertising
    • Benefits of MacTech
    • Mechanicals and Submission
    • Dates and Deadlines
    • Submit Apple Expo Entry
  • User
    • Register for Ongoing Raffles
    • Register new user
    • Edit User Settings
    • Logout
  • Contact
    • Customer Service
    • Webmaster Feedback
    • Submit News or PR
    • Suggest an article
  • Connect Tools
    • MacTech Live Podcast
    • RSS Feeds
    • Twitter

ADVERTISEMENT
Volume Number:3
Issue Number:9
Column Tag:Mac Hack Forum

Taking TML Around the 32K Barrier

By Darryl Lavoto, TML Systems, Inc., Contributing Editor

It has been a while since I have had a chance to write something for my favorate Magazine! MacTutor! A lot of Technical Support Questions I get at TML Systems are about the 32k data limitation that exists on most compilers. I have written a work-around that will allow most people to get around this problem.

Most Compilers for the Macintosh have a 32k limitation on the sum of all global variables. This is because global variables are stored in a area of memory WORD indexed off of A5. This is a significant limitation if your program needs large arrays of data. There is a workaround, however, to allow any size of array to be created. The only size limitation for our array is available memory.

The alternitive way to create large arrays is to call NewHandle to get a handle to a block of memory large enough to hold the array. Accessing the individual elements of the new array is done by calculating offsets into the block. The Array unit below includes routines for Creating and Disposing arrays, and for accessing the array elements.

The unit could be improved by adding support for multi-dimentional arrays, dynamic sizing of the array, or even variable length data sizes. The speed of the routines could be improved substantially by re-coding the unit in 680x0 assembler. Note that these routines assume that the lower bound of the array is always 0. Also, error checking needs to be added to the create array routine in case there is not enough memory to create the block.

{#################################################}
{#  File: Arrays.Pas      #}
{#  Author: Darryl Lovato.#}
{#  Description: #}
{# This module provides a set of routines to       #}
{#  create & use large arrays      #}
{#################################################}

unit Arrays;

interface

uses MacIntf;

function  CreateNewArray(elemSize, upperBound : Longint) : Handle;
procedure DisposeArray(ary : Handle);
procedure SetElement(ary : Handle; elemNum : Longint; elemPtr : Ptr);
function  GetElement(ary : Handle; elemNum : Longint) : Ptr;

implementation

type
   ArrayHdl = ^ArrayPtr;
   ArrayPtr = ^ArrayRec;
   ArrayRec = record
       hiBound : integer;
       cellSize : Longint;
       dataStuff : integer;
   end;

{#################################################}
{#        #}
{#  function CreateNewArray(elemSize, lowerBound,  #} 
{#      upperBound : Longint) : Handle;#}
{# #}
{#################################################}

function CreateNewArray(elemSize, upperBound : Longint) : Handle;
  var
    tempHdl : ArrayHdl;
  begin
    tempHdl := ArrayHdl(NewHandle(Ord4(SizeOf(ArrayRec)) - 2
 + ((upperbound + 1) * elemSize)));
    with tempHdl^^ do
      begin
        hiBound := upperBound;
        cellSize := elemSize;
      end;
    CreateNewArray := Handle(tempHdl);
  end;

{#################################################}
{#        #}
{#  procedure DisposeArray(ary : Handle);          #} 
{#      #}
{# #}
{#################################################}

procedure DisposeArray(ary : Handle);
  begin
    DisposHandle(ary);
  end;

{#################################################}
{#        #}
{#        #}
{# procedure SetElement(ary : Handle; elemNum :    #}
{# Longint; elemPtr : Ptr); #} 
{#      #}
{# #}
{#################################################}

procedure SetElement(ary : Handle; elemNum : Longint; elemPtr : Ptr);
  type
    fakeary = packed array[1..10000] of signedbyte;
    fakeptr = ^fakeary;
  var
    cellAddr : fakePtr;
    i : integer;
  begin
    with ArrayHdl(ary)^^ do
      if (elemNum > 0) and (elemNum < hibound) then
        begin
          cellAddr := fakePtr(Ord4(@dataStuff) + (elemNum * cellSize));
          for i := 0 to cellSize - 1 do
            cellAddr^[i] := fakePtr(elemPtr)^[i];
        end;
  end;

{#################################################}
{#        #}
{#        #}
{# procedure GetElement(ary : Handle; elemNum :    #}
{# Longint) : Ptr);  #} 
{#      #}
{# #}
{#################################################}

function GetElement(ary : Handle; elemNum : Longint) : Ptr;
  begin
    with ArrayHdl(ary)^^ do
      if (elemNum > 0) and (elemNum < hibound) then
        GetElement := Pointer(Ord4(@dataStuff) + (elemNum * cellSize));
  end;
end.

{#################################################}
{#        #}
{# This program tests the Array Package#}
{# #} 
{# #}
{#################################################}

program AryTest(input, output);

uses Macintf, Arrays;
type
  BigPtr = ^BigRec;
  BigRec = record
             r : real;
             r2 : real;
             other : array[1..20] of Longint;
           end;
var
  myArray : handle;
  i : longint;
  Big : BigRec;
  x : integer;
  StackMark : integer;
begin
  SetApplLimit(Pointer(Ord4(@StackMark) - Ord4(16000))); {16k stack}
  MaxApplZone;

  myArray := CreateNewArray(SizeOf(BigRec), 2000);

  writeln(‘Total Size of Record = ‘,SizeOf(BigRec));
  writeln(‘Total Size of the array = ‘,GetHandleSize(myArray));
  writeln;
  writeln(‘press mouse to start’);
  repeat until button;

  for i := 0 to 2000 do
    begin
      Big.r := i / 1.0;
      SetElement(myArray, i, @Big);
      writeln(BigPtr(GetElement(myArray, i))^.r:7:2);
    end;

  DisposeArray(myArray);
end.
 
MacTech Only Search:
Community Search:

 
 
 

 
 
 
 
 
  • SPREAD THE WORD:
  • Slashdot
  • Digg
  • Del.icio.us
  • Reddit
  • Newsvine
  • Generate a short URL for this page:



MacTech Magazine. www.mactech.com
Toll Free 877-MACTECH, Outside US/Canada: 805-494-9797
MacTech is a registered trademark of Xplain Corporation. Xplain, "The journal of Apple technology", Apple Expo, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, Apple Expo, MacTech Central, MacTech Domains, MacNews, MacForge, and the MacTutorMan are trademarks or service marks of Xplain Corporation. Sprocket is a registered trademark of eSprocket Corporation. Other trademarks and copyrights appearing in this printing or software remain the property of their respective holders.
All contents are Copyright 1984-2010 by Xplain Corporation. All rights reserved. Theme designed by Icreon.
 
Nov. 20: Take Control of Syncing Data in Sow Leopard' released
Nov. 19: Cocktail 4.5 (Leopard Edition) released
Nov. 19: macProVideo offers new Cubase tutorials
Nov. 18: S Stardom anounces Safe Capsule, a companion piece for Apple's
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live