SQL Programming Manual for Pascal
Examples of Dynamic NonStop SQL Programs
HP NonStop SQL Programming Manual for Pascal—528614-001
C-14
Detailed Dynamic SQL Program
 197 0 {**********************************************************}
 198 0 {* ALLOC_SPACE : Allocate the required number of bytes, nb, on
           *}
 199 0 {* the current heap if there is sufficient space. *}
 200 0 {* Otherwise, allocate a heap of size HEAPSIZE.  *}
 201 0 {* Maintain a LIFO list of heaps anchored in pheap. *}
 202 0 {**********************************************************}
 203 0 function alloc_space(var pheap : P_HEAP_HDR; nb : longint) :
          extaddr;
 204 1 var
 205 1  newp,oldp : P_ANY;  { scratch pointer   }
 206 1 begin
 207 2  if (pheap = NIL) or  { heap has not been alloc'd  }
 208 3  (pheap^.off + nb > HEAPSIZE) then
       { space is insufficient  }
 209 3  begin    { allocate a new heap ----1  }
 210 4  newp.xa := MALLOC(HEAPSIZE);
 211 4  if (newp.i > 0) then  { successfully allocated ? }
 212 5  begin    { yes, it did ----2  }
 213 6  oldp.hp := pheap;  { load local pointer  }
 214 6  newp.hp^.nxt := oldp.xa; { 1st heap will become 2nd }
 215 6  pheap := newp.hp;  { new heap becomes 1st }
 216 6  pheap^.off := sizeof(HEAP_HDR);
       { set offset to free space }
 217 6  end    { yes, it did ----2  }
 218 5  else     { allocation failed  }
 219 5  begin    { report an error ----3  }
 220 6  G_errcb.erc := ERC_NOHEAPSPACE;
 221 6
 222 6  alloc_space := RETYPE(0, EXTADDR);
       { return an EXTADDR 0  }
 223 6      { if function fails  }
 224 6  return;
 225 6  end    { report an error ----3  }
 226 5  end     { allocate a new heap ----1 }
 227 3 else newp.hp := pheap;
 228 2 newp.i  := newp.i + pheap^.off; { set ptr to free area }
 229 2
 230 2 { allocate space and round result to even number of bytes, to }
 231 2 { accommodate values which must be word-aligned:   }
 232 2 pheap^.off := pheap^.off + nb + (nb mod 2);
 233 2 alloc_space := newp.xa;   { set return value  }
 234 2 end;      { of proc alloc_space }
 235 0
 236 0 {**********************************************************}
 237 0 {* DEALLOC_SPACE : Deallocate all of the storage on the heap *}
 238 0 {* pointed to by pheap.      *}
 239 0 {**********************************************************}
 240 0 procedure dealloc_space(var pheap : P_HEAP_HDR);
 241 1 var
 242 1  curp,nxtp : P_ANY;  { scratch pointer   }
 243 1 begin
 244 2  nxtp.hp := pheap;  { next heap to free  }
 245 2  while (nxtp.hp <> NIL) do { while got some heap space }
 246 3  begin
 247 4  curp := nxtp;  { set ptr to heap to free  }
 248 4  nxtp.xa := curp.hp^.nxt;{ remember ptr to neighbour }
 249 4  FREE(curp.xa);  { deallocate current heap }
 250 4  end;
 251 2  pheap := NIL;   { heap storage deallocated }
 252 2 end;     { of proc dealloc_space }
 253 0










