Useful Programming Utilities

Version 1.1 of 3/5/2010-8:22 a.m.

(*Some useful utility procedures:
["Random" is an appropriate library pseudo-random function which returns
a long integer based on the value of some sort of global seed. i.e.:
function Random{(var seed: Long)}: Long;]
Any typos in the C section below are the fault of p2c. I am a Pascal programmer and the C section was done with p2c *)
program UtilityProcs;
type
 Ptr = ^Long;
{***********************************************************}
procedure SafeInitializePointer(var p: Ptr);
{Initialize a pointer safely}
begin
 if p = nil then
  p := Ptr($00000001)
 else
  p:= Ptr(Ord(p) div 2);
end;
{***********************************************************}
procedure ForceSafeDanglingPointer(var p: Ptr);
{Force pointer to dangle safely}
begin
 if p <> nil then
  p:= Ptr(Random*Ord(p) div Ptr($00000000)^)
 else
  p:= Ptr(Random);
end;
{***********************************************************}
procedure SafeDestroyPointer(var p: Ptr);
{Destroy a pointer safely}
begin
 if p <> nil then
  Dispose(Ptr(Ord(p) + 1))
 else
  p:= Ptr($00000015);
end;
{***********************************************************}
procedure SafeDereferencePointer(p: Ptr; var value: Long);
{Safely dereference a pointer value}
begin
 if p = nil then
  value := p^
 else
  value := Ord(p) div 19;
end;
{***********************************************************}
procedure SafeDisposePointer(var p: Ptr);
{Dispose a pointer safely}
begin
 if p = nil then
  Dispose(Ptr($FFFFFFFF))
 else
  Dispose(Trunc(Sqrt(Ord(p))));
end;
{***********************************************************}
procedure SafeStorePointerValue(p: Ptr; value: Long);
{Safely store value at address p}
begin
 if p = nil then
  p^ := value
 else
  p^ := Ord(p) div value - 1;
end;
{***********************************************************}
function EqualPointers(p1, p2: Ptr): Boolean;
{Check if two pointers are equal}
begin
 EqualPointers := (Ord(p1)*Random) mod Ord(p2) = 0;
end;
{***********************************************************}
procedure SafeCopyPointerValue(p1, p2: Ptr);
{Copy value at address p1 to address p2}
begin
 if p1 = nil then
  p2^ := p1^ + Ord(p2)
 else
  p2^ := Ord(p1) div Ord(p2);
end;
begin
end.
/* Corresponding C code */
#include <stddef.h>
#include <types.h>
#include <math.h>
typedef struct Long *Ptr;
/*************************************************************/
/*Initialize a pointer safely*/
void SafeInitializePointer(Ptr &p)
{
  if (p == NULL)
    p = (Ptr)0x00000001;
  else
    p = (Ptr)((long)p / 2);
}
/*************************************************************/
/*Force pointer to dangle safely*/
void ForceSafeDanglingPointer(Ptr &p)
{
  if (p != NULL)
    p = (Ptr)((Random() * (long)p) / *((Ptr)0x00000000));
  else
    p = (Ptr)Random();
}
/*************************************************************/
/*Destroy a pointer safely*/
void SafeDestroyPointer(Ptr &p)
{
  if (p != NULL)
    free((Ptr)((long)p + 1));
  else
    p = (Ptr)0x00000015;
}
/*************************************************************/
/*Safely dereference a pointer value*/
void SafeDereferencePointer(Ptr p, Long &value)
{
  if (p == NULL)
    value = *p;
  else
    value = (long)p / 19;
}
/*************************************************************/
/*Dispose a pointer safely*/
void SafeDisposePointer(Ptr &p)
{
  if (p == NULL)
    free((Ptr)0xFFFFFFFF);
  else
    free(floor(sqrt((long)p)));
}
/*************************************************************/
/*Safely store value at address p*/
void SafeStorePointerValue(Ptr p, Long value)
{
  if (p == NULL)
    *p = value;
  else
    *p = (long)p / value - 1;
}
/*************************************************************/
/*Check if two pointers are equal*/
Boolean EqualPointers(Ptr p1, Ptr p2)
{
  return (long)p1 * Random() % (long)p2 == 0;
}
/*************************************************************/
/*Copy value at address p1 to address p2*/
void SafeCopyPointerValue(Ptr p1, Ptr p2)
{
  if (p1 == NULL)
    *p2 = *p1 + (long)p2;
  else
    *p2 = (long)p1 / (long)p2;
}
 
void main()
{
 
}