Yggdrasil Seeds : Software Works : Language Select : Denullpo, the pointer checker : Denullpo

Denullpo


How to Minimum Use

Include denullpo.h in each C/C++ source, but it needs settings, otherwise all macros in denullpo.h become invalid.
All setting is written in "#define" before including denullpo.h.
DENULLPO_TRAP
If this is defined, error detection callback becomes effective.
DENULLPO_SKIP
If this is defined, Denullpo detects errors.
If neither DENULLPO_TRAP nor DENULLPO_SKIP is defined, Denullpo does not detect errors.
DENULLPO_4CC
It is a value to indicate that an instance is valid.
If this is not defined, it is defined at a constant value automatically.
At the spot that should check a pointer, give it in a pointer in denullpo().
When a pointer is NULL, Denullpo processes a set error.
Sample Code
/*=== evade access to an NULL pointer with denullpo() ===*/
#include <stdio.h>
/*=== Denullpo needs setting before including denullpo.h ===*/
/*=== You can remove handling of Denullpo when you change this line into comment. ===*/
#define DENULLPO_SKIP
#include "denullpo.h"

int main(int argc,char *argv[]){
void* p=NULL;
int err=0;

p=malloc(128);
/*=== divides processing by a check result of a pointer ===*/
if(denullpo(p)){
/*=== processing when it is abnormal ===*/
fprintf(stderr,"this is nullpo\n");
err=1;
}
else{
/*=== processing when it is normal ===*/
// :
// :
// :
free(p);
p=NULL;
}
return err;
}


It examines the validity of instance

Denullpo can examine the validity of instance by adding a member for a magic code to a class and a structure body.
Class and structure of a check target define a writing point of a magic code in DENULLPO_INITCHECKER.
When created it in instance, it indicates that valid instance by calling denullpo_setinit().
And when destroy it in instance, it indicates that invalid instance by calling denullpo_setpurge().
At the spot that should check a pointer, give it in a pointer in denullpo2().
When a pointer is illegal, Denullpo processes a set error.
Sample Code
/*=== resist a method call of illicit instance ===*/
#include <stdio.h>
#define DENULLPO_SKIP
#include "denullpo.h"

class FOO{
public:
/*=== define a variable to fill in a magic cord in a class ===*/
DENULLPO_INITCHECKER;

FOO(){
/*=== fill in a magic cord indicating that it initialized ===*/
denullpo_setinit(this);
}

~FOO(){
/*=== fill in a magic cord indicating that it terminated ===*/
denullpo_setpurge(this);
}

void exec(){
/*=== treat it as an error if this method is called from except between denullpo_setinit() and denullpo_setpurge() ===*/
if(denullpo2())return;
// :
// :
// :
}
};


Error detection callback

denullpo() and denullpo2() usually return a result immediately when they detected an error.
When you call denullpo_settrap() and set callback, callback is called when Denullpo detected an error, and the result is reflected.
DENULLPO_TRAP must be defined before including a denullpo.h to validate callback.
Sample Code
/*=== implement error handling of a illicit pointer ===*/
#include <stdio.h>
/*=== setting to validate callback ===*/
#define DENULLPO_TRAP
#include "denullpo.h"

int err=0;

/*=== processing when Denullpo detected an error ===*/
static unsigned __cdecl hook(void *p,eDenullpoReason reason){
fprintf(stderr,"nullpo is hooked\n");
err=1;

/*=== returning value just becomes returning value of denullpo() ===*/
return 1;
}

int main(int argc,char *argv[]){
void* p=NULL;

/*=== setting of callback ===*/
denullpo_settrap(hook);

p=malloc(128);
/*=== If a pointer is NULL, Denullpo execute set callback. ===*/
if(!denullpo(p)){
/*=== processing when it is normal ===*/
// :
// :
// :
free(p);
p=NULL;
}
return err;
}


Details

Definitions

eDenullpoReason
Pointer error info from hDenullpoTrap callback.
DENULLPO_NULL
Examined pointer is NULL.
DENULLPO_INVALID
Examined instance is invalid.

Callback Types

unsigned (*hDenullpoTrap)(void* p, eDenullpoReason reason)
Callback when error is detected.
When an error was detected, a function set in denullpo_settrap() is called.
Arguments
p
pointer of an examination target
reason
basis of an error
Returning Value
0
This error factor was solved.
1
This error factor is still left.

Functions

hDenullpoTrap denullpo_gettrap()
get an error detection callback.
Returning Value
the setting value
hDenullpoTrap denullpo_settrap(hDenullpoTrap trap)
set an error detection callback.
Arguments
trap
a function to treat as callback
Returning Value
the last setting value
Notes
This function is not thread safe.
unsigned denullpo_exectrap(void* p, eDenullpoReason reason)
call the error detection callback.
Arguments
p
pointer of an examination target
reason
basis of an error
Returning Value
0
This error factor was solved.
1
This error factor is still left.
unsigned denullpo_getinit(any* p)
get a value of instance checker
Arguments
p
target instance
Returning Value
value of instance checker
void denullpo_setinit(any* p)
notify instance checker that instance is initialized
Arguments
p
target instance
Notes
You must call this macro for each instance to become a target of denullpo2().
void denullpo_setpurge(any* p)
notify instance checker that instance is terminated
Arguments
p
target instance
Notes
You must call this macro for each instance to become a target of denullpo2().
unsigned denullpo_chkinit(any* p)
examine whether instance is initialized
Arguments
p
target instance
Returning Value
0
not abnormality
1
abnormality
Notes
You do not usually call this macro directly. You call denullpo2() so that Denullpo examines instance.
int denullpo(any* p)
examine whether a pointer is NULL
Arguments
p
target pointer
Returning Value
0
NULL was not detected
1
NULL was detected
Notes
The callback function is called in case of the error detection if set by a callback function in denullpo_settrap().
int denullpo2(any* p)
examine whether an instance is invalid
Arguments
p
target instance
Returning Value
0
not abnormality
1
abnormality
Notes
The callback function is called in case of the error detection if set by a callback function in denullpo_settrap().
You have to call denullpo_setinit() and denullpo_setpurge() at an appropriate position so that this function is handled normally.

Future Plan


DSHs PHPlib v0.1.0 (C)2006-9 Denullpo Smasher Hammerson