home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.pascal
- Path: sparky!uunet!cis.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!po.CWRU.Edu!wct
- From: wct@po.CWRU.Edu (William C. Thompson)
- Subject: Re: Arrays > 64K
- Message-ID: <1992Aug13.003031.19643@usenet.ins.cwru.edu>
- Sender: news@usenet.ins.cwru.edu
- Nntp-Posting-Host: slc5.ins.cwru.edu
- Reply-To: wct@po.CWRU.Edu (William C. Thompson)
- Organization: Case Western Reserve University, Cleveland, OH (USA)
- References: <1992Aug3.102558.21254@newstand.syr.edu> <1992Aug02.170808.2053@crash>
- Date: Thu, 13 Aug 92 00:30:31 GMT
- Lines: 82
-
-
- Here is a file you can include with your source code. You need to
- define a type before you {$I} it.
-
- { To include this file in a program, you have to define what
- HugeDataType is. It follows that another restriction would
- be you can only have one type of huge array. }
-
- const
- hugemaxitems=64000 div sizeof(hugedatatype);
-
- type
- hugedataptr=^hugedatatype;
- partition=array[1..hugemaxitems] of hugedatatype;
- partitionptr=^partition;
- hugearray=record
- p: array[1..10] of partitionptr;
- partitionct: byte;
- elements: longint;
- end;
-
- procedure allocate(var h: hugearray; elements: longint);
- var
- i,j: byte;
- lastsize: word; { Size required for last partition }
- begin
- h.elements:=elements;
- if elements=0 then h.partitionct:=0
- else begin
- h.partitionct:=(h.elements-1) div hugemaxitems+1;
- { First, allocate space for first large chunks }
- for i:=1 to h.partitionct-1 do begin
- if maxavail>64000 then getmem(h.p[i],64000)
- else begin
- { First, deallocate (undo) everything before this }
- for j:=1 to i-1 do freemem(h.p[j],64000);
- h.elements:=0; { This signals the allocation was unsuccessful }
- h.partitionct:=0;
- exit { Exit the procedure }
- end;
- end;
- { Now, allocate space for last chunk }
- lastsize:=elements*sizeof(hugedatatype)-(h.partitionct-1)*longint(64000);
- if maxavail>lastsize then getmem(h.p[h.partitionct],lastsize)
- else begin
- { Now deallocate (undo) everything before this }
- for j:=1 to h.partitionct-1 do freemem(h.p[j],64000);
- h.elements:=0;
- h.partitionct:=0;
- exit
- end;
- end
- end;
-
- procedure deallocate(var h: hugearray);
- var
- i: byte;
- lastsize: word;
- begin
- if h.partitionct>0 then begin
- { first, deallocate first partitionct-1 partitions }
- for i:=1 to h.partitionct-1 do freemem(h.p[i],64000);
- lastsize:=h.elements*sizeof(hugedatatype)-(h.partitionct-1)*longint(64000);
- { now deallocate last partition }
- freemem(h.p[h.partitionct],lastsize);
- h.partitionct:=0;
- h.elements:=0
- end
- end;
-
- function gethuge(var h:hugearray; element:longint):hugedataptr;
- { returns a pointer to the element in the huge array }
- begin
- { (element-1) div hugemaxitems+1 is the partition to use, and
- (element-1) mod hugemaxitems+1 is the element in that partition }
- gethuge:=@h.p[(element-1) div hugemaxitems+1]^[(element-1) mod hugemaxitems+1]
- end;
-
-
- --
- "What do you mean Star Trek isn't real!?" - anonymous
-