home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / pascal / 4830 < prev    next >
Encoding:
Text File  |  1992-08-12  |  3.0 KB  |  95 lines

  1. Newsgroups: comp.lang.pascal
  2. Path: sparky!uunet!cis.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!po.CWRU.Edu!wct
  3. From: wct@po.CWRU.Edu (William C. Thompson)
  4. Subject: Re: Arrays > 64K
  5. Message-ID: <1992Aug13.003031.19643@usenet.ins.cwru.edu>
  6. Sender: news@usenet.ins.cwru.edu
  7. Nntp-Posting-Host: slc5.ins.cwru.edu
  8. Reply-To: wct@po.CWRU.Edu (William C. Thompson)
  9. Organization: Case Western Reserve University, Cleveland, OH (USA)
  10. References: <1992Aug3.102558.21254@newstand.syr.edu> <1992Aug02.170808.2053@crash>
  11. Date: Thu, 13 Aug 92 00:30:31 GMT
  12. Lines:       82
  13.  
  14.  
  15. Here is a file you can include with your source code.  You need to
  16. define a type before you {$I} it.
  17.  
  18. { To include this file in a program, you have to define what
  19.   HugeDataType is.  It follows that another restriction would
  20.   be you can only have one type of huge array. }
  21.  
  22. const
  23.   hugemaxitems=64000 div sizeof(hugedatatype);
  24.  
  25. type
  26.   hugedataptr=^hugedatatype;
  27.   partition=array[1..hugemaxitems] of hugedatatype;
  28.   partitionptr=^partition;
  29.   hugearray=record
  30.     p: array[1..10] of partitionptr;
  31.     partitionct: byte;
  32.     elements: longint;
  33.     end;
  34.  
  35. procedure allocate(var h: hugearray; elements: longint);
  36. var
  37.   i,j: byte;
  38.   lastsize: word;    { Size required for last partition }
  39. begin
  40.   h.elements:=elements;
  41.   if elements=0 then h.partitionct:=0
  42.   else begin
  43.     h.partitionct:=(h.elements-1) div hugemaxitems+1;
  44.     { First, allocate space for first large chunks }
  45.     for i:=1 to h.partitionct-1 do begin
  46.       if maxavail>64000 then getmem(h.p[i],64000)
  47.       else begin
  48.         { First, deallocate (undo) everything before this }
  49.         for j:=1 to i-1 do freemem(h.p[j],64000);
  50.         h.elements:=0;   { This signals the allocation was unsuccessful }
  51.         h.partitionct:=0;
  52.         exit   { Exit the procedure }
  53.         end;
  54.       end;
  55.     { Now, allocate space for last chunk }
  56.     lastsize:=elements*sizeof(hugedatatype)-(h.partitionct-1)*longint(64000);
  57.     if maxavail>lastsize then getmem(h.p[h.partitionct],lastsize)
  58.     else begin
  59.       { Now deallocate (undo) everything before this }
  60.       for j:=1 to h.partitionct-1 do freemem(h.p[j],64000);
  61.       h.elements:=0;
  62.       h.partitionct:=0;
  63.       exit
  64.       end;
  65.   end
  66. end;
  67.  
  68. procedure deallocate(var h: hugearray);
  69. var
  70.   i: byte;
  71.   lastsize: word;
  72. begin
  73.   if h.partitionct>0 then begin
  74.     { first, deallocate first partitionct-1 partitions }
  75.     for i:=1 to h.partitionct-1 do freemem(h.p[i],64000);
  76.     lastsize:=h.elements*sizeof(hugedatatype)-(h.partitionct-1)*longint(64000);
  77.     { now deallocate last partition }
  78.     freemem(h.p[h.partitionct],lastsize);
  79.     h.partitionct:=0;
  80.     h.elements:=0
  81.     end
  82. end;
  83.  
  84. function gethuge(var h:hugearray; element:longint):hugedataptr;
  85. { returns a pointer to the element in the huge array }
  86. begin
  87.   { (element-1) div hugemaxitems+1 is the partition to use, and
  88.     (element-1) mod hugemaxitems+1 is the element in that partition }
  89.   gethuge:=@h.p[(element-1) div hugemaxitems+1]^[(element-1) mod hugemaxitems+1]
  90. end;
  91.  
  92.  
  93. -- 
  94. "What do you mean Star Trek isn't real!?" - anonymous
  95.