📄 testmain.c
字号:
/*
______________________________________________________________________________________
Designed by:
Niraj Kedar,
Systems Engineer,
L&T Infotech Ltd,
Bangalore, India.
e-mail: nirajkedar@yahoo.com
Date: July, 2001.
______________________________________________________________________________________
*/
#include "recycle.h"
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// enumerated return type
typedef enum RETURN_TYPE
{
// equating the 'success' enum with the value of MALLOC_SUCCESS
SUCCESS = SET_SUCCESS_VAL,
FAIL, // failure condition
INVALID, // some other conditions
}RETURN_TYPE;
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// a sample link-list data structure.
typedef struct node
{
int data;
struct node* pNext;
}node;
typedef struct handle
{
node* pHead;
}handle;
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// test functions.
RETURN_TYPE Trial1();
RETURN_TYPE Trial2();
RETURN_TYPE Trial3();
RETURN_TYPE Trial4();
// prints the final values of all the variables.
void PrintAll();
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// global pointers
char* pCh = 0;
int* pInt = 0;
// global hadle object of the sample link-list.
handle hdl;
// macro for declaring the global Recycle stack handle object.
// protect this with some mutual exclusion mechanism in multithreaded environments.
DECL_STACK_HANDLE;
main()
{
// return type variable
RETURN_TYPE ret;
// use the macro for initializing Recycle stack handle
MEMSET_STACK_HANDLE;
// call the function Trial1()
ret = Trial1();
PrintAll();
}
RETURN_TYPE Trial1()
{
// return type variable
RETURN_TYPE ret;
// set the function scope by using this macro.
SET_FN_SCOPE(Trial1);
// allocate memory for a character array. Type of memory - persistant
if((ret = Allocate(6, &pCh, GET_FN_SCOPE, MALLOC_SUCCESS)) != MALLOC_SUCCESS)
{
printf("memory allocation failed...\n");
RETURN(ret);
}
// copy some string
strcpy(pCh, "Niraj");
// trial print
printf("\nThe value of pCh in Trial1() is %s\n", pCh);
// call Trial2()
ret = Trial2();
// use this macro in place of normal return statements
RETURN(ret);
// RETURN(MALLOC_FAIL); // for test
}
RETURN_TYPE Trial2()
{
RETURN_TYPE ret;
SET_FN_SCOPE(Trial2);
int iTemp = 1024;
// allocate memory for a character array. Type of memory - out of scope
if((ret = Allocate(sizeof(int), &pInt, GET_FN_SCOPE, OUT_OF_SCOPE)) != MALLOC_SUCCESS)
{
printf("memory allocation failed...\n");
RETURN(ret);
}
// copy value
memcpy(pInt, &iTemp, sizeof(int));
printf("\nThe value in pInt in Trial2() is %d\n", *pInt);
//call Trial3()
ret = Trial3();
RETURN(ret);
// RETURN(MALLOC_SUCCESS); // for trial
}
RETURN_TYPE Trial3()
{
RETURN_TYPE ret;
SET_FN_SCOPE(Trial3);
node* pTemp = 0;
node* pNew = 0;
node* pHead = 0;
short iCnt = 0;
// allocate memory for a linklist. Type of memory - persistant
while(iCnt++ < 10)
{
if((ret = Allocate(sizeof(node), &pNew, GET_FN_SCOPE, MALLOC_SUCCESS)) != MALLOC_SUCCESS)
{
printf("memory allocation failed...\n");
RETURN(ret);
}
pNew->data = iCnt;
pNew->pNext = 0;
if(pHead == 0)
pTemp = pHead = pNew;
else
{
pTemp->pNext = pNew;
pTemp = pTemp->pNext;
}
}
hdl.pHead = pHead;
ret = Trial4();
RETURN(ret);
// RETURN(MALLOC_FAIL); for trial
}
RETURN_TYPE Trial4()
{
RETURN_TYPE ret;
SET_FN_SCOPE(Trial4);
RETURN(MALLOC_SUCCESS);
}
void PrintAll()
{
node* pIter = 0;
// print all final values
printf("Final value of char array is %s\n\n", pCh);
printf("Final Value of pInt is %d\n\n", *pInt);
printf("printing Final linklist...\n\n");
for(pIter = hdl.pHead; pIter != 0; pIter = pIter->pNext)
{
printf("%d\n", pIter->data);
}
printf("\n\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -